473,804 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;

};
ParsingStructur eType 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::operato r-() 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 1475

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::varian t<int, RationalNumber;

class plus_visitor : public boost::static_v isitor<Number>
{
public:

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

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

Number operator()(Rati onal 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_vi sitor(), 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;

};
ParsingStructur eType 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::operato r-() 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
2041
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 web application using PHP, to which I am relatively new. I have seen some sites use the model whereby a single index page is created that handles authentication and receives option parameters telling the index what to load in the body of the...
36
6413
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 something I'll need in this case is some experience-based set of rules about how to use python in this context. For example... is defining readonly attributes in classes worth the hassle ? Does duck-typing scale well in complex
2
3576
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 fairly thorough literature search, but i haven't found anything fitting this criteria. In general there seems to be a huge amount concerning the C++ language as such and more "narrow" design issues, e.g. along the lines of Meyers Effective-
10
1925
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 singleton class has amongst other stuff the following line of code in it: Private m_DL_DBPath As String = System.Configuration.ConfigurationSettings.AppSettings("DMDatabasePath") As you guess, it must read a string containing the path. Based on this...
6
4260
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 while other users in this shared database might be using other, unrelated reports. Now, in Access2k, it appears that whenever you do not have exclusive access, you cannot make any design changes to reports. It seems that the presence of any...
5
1849
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 idea is to have a Class that stores elements of Class2 in different ways (arrays in stack memory, arrays in heap memory, using the std::vector and so on). I would like the user to customize the creation of a class with Class2 and StoragePolicy like...
0
2513
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 don't work nearly as well as they should, even for analysts and power users. The reason they haven't reached the masses is because most of the tools are so difficult to use and reveal so little
0
3699
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 database calls (using a separate DB handler class). "combo1" contains a list of countries and is fairly static, it can be added to but no other external events cause a change in its population. "combo2" however is populated with a list of options...
2
2906
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 many posts in this group explaining the pitfalls of using the lookup feature in tables. Best practice appears to be, keep the lookup in the forms using combo box or list box."
5
1741
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 members Y is the location of a file to be read. The original design assumes that this class will be instantiated and each instance will happily mange its own members. (ie One file location per instance...no thread-safety). Now another class A...
0
9572
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.