473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Design issue... Question on best way to do it in C++.

Hi all,

Guess I wish to do some parsing for a calculator which might include
rational numbers.
So I can have integers (sequence of digits possibly started by -) and
rationals (two integers separated by '/').
I did a NumberWrapper which makes the use of a number, be it an integer
or rational transparent to the user.
Number(1, 2); represents 1/2
Number(2); represents 1

First issue, I have something like this in number (or would like to):
class Number {
....

private:
enum numberType {INTEGER, RATIONAL};
numberType type;
union {
int integer;
Rational rational; // Class defined by me...
};
};

Problem is Rational has non-trivial constructors so it just can't be
there. One way it to remove the union and just have an int and a
rational, never both are used at the same time (that wastes space but
saves me a lot of trouble).
Another way is to have a void* and then based on type I just cast it to
int or to rational as I need, obviously with caution, deleting and
freeing what I need. Well, this seems a lot of trouble!
Another is to just represent an integer as a rational with denominator
1 and forget the int or wrapper at all and use rationals all the way.

Another issue has to do with how to structure things around the
parsing. Bison uses unions for communication with flex so I have a
Number * in the union and whenever flex finds a rational or number,
allocates a new number and bison get's it so I can use it in parsing.
struct parsingState {
union {

Number *num;

};
ParsingStructureType type;
};
Problem is when I need to do some computations...
Guess I have Number *num; in the union and I want to update it to
become -num. I have defined operator-() in Number so I do:
parsingState ps;

....

ps->num = - ps->num;

It seems to be that this will leak right? (-ps->num) will allocate
another Number because operator-() is
Number Number::operator-() const;

and I'll lose my previous allocated number.
Any ideas on how I can do this or organize my structures?

Regards,

Paulo Matos

Nov 21 '06 #1
2 1452

Paulo Matos wrote:
Hi all,

Guess I wish to do some parsing for a calculator which might include
rational numbers.
So I can have integers (sequence of digits possibly started by -) and
rationals (two integers separated by '/').
I did a NumberWrapper which makes the use of a number, be it an integer
or rational transparent to the user.
Number(1, 2); represents 1/2
Number(2); represents 1

First issue, I have something like this in number (or would like to):
class Number {
...

private:
enum numberType {INTEGER, RATIONAL};
numberType type;
union {
int integer;
Rational rational; // Class defined by me...
};
};

Problem is Rational has non-trivial constructors so it just can't be
there. One way it to remove the union and just have an int and a
rational, never both are used at the same time (that wastes space but
saves me a lot of trouble).
Another way is to have a void* and then based on type I just cast it to
int or to rational as I need, obviously with caution, deleting and
freeing what I need. Well, this seems a lot of trouble!
Another is to just represent an integer as a rational with denominator
1 and forget the int or wrapper at all and use rationals all the way.
The safest way I found for this kind of a
problem is to use boost::variant

to start you up

typedef ::boost::variant<int, RationalNumber;

class plus_visitor : public boost::static_visitor<Number>
{
public:

Number operator()(int i, int j) const
{
return Number(i+j);
}

Number operator()(Rational const & i, Rational const & j) const
{
//suppose you have + op for Rational
return Number(i+j);
}

Number operator()(Rational const & i, int j) const
{
// suppose your rational has a constructor
// like Rational(int nominator, int denominator
return Number(Rational(i.nominator+j*i.denominator, i.denominator));
}

Number operator()(int i, Rational const & j) const
{
return Number(Rational(j.nominator+i*j.denominator, j.denominator));
}
};

Number n = 1;
Number r = Rational(3, 4);
Number t = ::boost::apply_visitor(plus_visitor(), n, r);
t is now 7/4

boost has a very nice documentation so look it up.
>
Another issue has to do with how to structure things around the
parsing. Bison uses unions for communication with flex so I have a
Number * in the union and whenever flex finds a rational or number,
allocates a new number and bison get's it so I can use it in parsing.
struct parsingState {
union {

Number *num;

};
ParsingStructureType type;
};
Problem is when I need to do some computations...
Guess I have Number *num; in the union and I want to update it to
become -num. I have defined operator-() in Number so I do:
parsingState ps;

...

ps->num = - ps->num;

It seems to be that this will leak right? (-ps->num) will allocate
another Number because operator-() is
Number Number::operator-() const;

and I'll lose my previous allocated number.
Any ideas on how I can do this or organize my structures?
Sorry I don't use nether Bison nor flex, can't help you there

Nov 21 '06 #2
I would simply store all numbers as rationals. Every integer N is also
special case of rational N/1.

Nov 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Mike Sutton | last post by:
I had first tried a less active PHP group, but let me try here. I searched for this, but didn't find anything that appeared related. Possibly I didn't know what to search for. I am designing a...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
2
by: Kymert persson | last post by:
Hi. I was wondering if there are any more C++ books along the lines of "Large scale C++ software design" by Lakos, J. I.e. concerning larger design issues in close relation to C++. I have made a...
10
by: eMKa | last post by:
Hi Code guru's I have created a user control which has access, and thus makes use of a shared singleton class like: Dim MyAppSettings As DLAppSettings = DLAppSettings.GetAppSettings This...
6
by: Mike Wiseley | last post by:
We recently converted our department wide shared Access97 database to Access2K. We used to be able to open various reports in design mode and make changes to the design (or create new reports) even...
5
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
0
by: Paul Hadfield | last post by:
I'm looking for thoughts on the "correct" design for this problem (DotNet 2.0 - in winforms, but that's not so important). I've got two combo boxes (combo1 and combo2), both are populating via...
2
by: King Ron | last post by:
Ola all. In responding to a recent post requesting help with a search issue, I recommended using a combo box lookup in the table design. "paii, Ron" (no relation) posted this reply: " There are...
5
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.