Defining class of rational numbers

I am trying to write a program that sets up a class for rational numbers. When i type in 15/-32 the output is -16/31 which is wrong.when i type 1/3+1/3 it gives me 1/3 which is wrong. when i type 3/4 / 1/2 it gives me 3/4 and when i type in 2/3*1/3 it gives me 2/3.which are all wrong so my proplem is it doesnt give me the right answers. How can I fix it? for the rest of the question pls read my second postthank you


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
using namespace std;

class rationalNumbers
{
public:
	friend rationalNumbers operator +(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend rationalNumbers operator -(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend rationalNumbers operator -(const rationalNumbers& FRACTION1);
	friend rationalNumbers operator *(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend rationalNumbers operator /(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend bool operator ==(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend bool operator <(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend bool operator <=(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend bool operator >(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend bool operator >=(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2);
	friend istream& operator >>(istream& ins, rationalNumbers& fraction);
	friend ostream& operator <<(ostream& outs, const rationalNumbers& FRACTION);
	friend rationalNumbers normalize(rationalNumbers& FRACTION);
	rationalNumbers(int top, int bottom);
	rationalNumbers(int wholeNumber);
	rationalNumbers();
private:
	int numerator;
	int denominator;

};
// Simplifies fractions
int digit_to_int(char c);
int gcd(int x, int y);


//Rational Number test
int main()
{
	rationalNumbers test;
	cout << "what is the fraction?";
	cin >> test;
	cout << test;
}
//Numerator-Denominator
rationalNumbers::rationalNumbers(int top, int bottom)
{
	numerator = top;
	denominator = bottom;
}
//Whole number definition
rationalNumbers::rationalNumbers(int wholeNumber)
{
	numerator = wholeNumber;
	denominator = 1;
}
//Numerator 0
rationalNumbers::rationalNumbers()
{
	numerator = 0;
	denominator = 1;
}

/*****************************************************************************
This is the funtion for normalizing the fracton.
*****************************************************************************/
 rationalNumbers normalize(rationalNumbers& FRACTION)
 {
	 rationalNumbers temp;
	 int commonDivisor;
	 if (FRACTION.denominator < 0)
	 {
		 temp.numerator = ~FRACTION.numerator;
		 temp.denominator = ~FRACTION.denominator;
	 }
	 else 
	 {
		temp.numerator = FRACTION.numerator;
		temp.denominator = FRACTION.denominator;
	 }
	commonDivisor = gcd(temp.numerator, temp.denominator);
	temp.numerator = (temp.numerator/commonDivisor);
	temp.denominator = (temp.denominator/commonDivisor);
	return temp;
 }

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

/*****************************************************************************
This is the funtion for overloading the + operator for class rationalNumbers
*****************************************************************************/
	rationalNumbers operator +(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		rationalNumbers temp1;
		rationalNumbers temp2;
		rationalNumbers result;
		if (FRACTION1.denominator == FRACTION2.denominator)
		{
			result.numerator = FRACTION1.numerator + FRACTION2.numerator;
			result.denominator = FRACTION1.denominator;
		}
		else
		{
			temp1.numerator = FRACTION1.numerator * FRACTION2.denominator;
			temp1.denominator = FRACTION1.denominator * FRACTION2.denominator;
			temp2.numerator = FRACTION2.numerator * FRACTION1.denominator;
			temp2.denominator = FRACTION2.denominator * FRACTION1.denominator;
			result.numerator = temp1.numerator + temp2.numerator;
			result.denominator = temp1.denominator;
		}
		return result;
	}

/*****************************************************************************
This is the funtion for overloading the - operator for class rationalNumbers
*****************************************************************************/
	rationalNumbers operator -(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		rationalNumbers temp1;
		rationalNumbers temp2;
		rationalNumbers result;
		if (FRACTION1.denominator == FRACTION2.denominator)
		{
			if (FRACTION1.numerator > FRACTION2.numerator)
			{
				result.numerator = FRACTION1.numerator - FRACTION2.numerator;
				result.denominator = FRACTION1.denominator;
			}
			else
			{
				result.numerator = FRACTION2.numerator - FRACTION1.numerator;
				result.denominator = FRACTION1.denominator;
			}
		}
		else
		{
			temp1.numerator = FRACTION1.numerator * FRACTION2.denominator;
			temp1.denominator = FRACTION1.denominator * FRACTION2.denominator;
			temp2.numerator = FRACTION2.numerator * FRACTION1.denominator;
			temp2.denominator = FRACTION2.denominator * FRACTION1.denominator;
			if (temp1.numerator > temp2.numerator)
			{
				result.numerator = temp1.numerator - temp2.numerator;
				result.denominator = temp1.denominator;
			}
			else
			{
				result.numerator = temp1.numerator - temp2.numerator;
				result.denominator = temp1.denominator;
			}
		}
		return result;
	}
	
	

/*****************************************************************************
This is the funtion for overloading the - operator for class rationalNumbers 
with a single object
*****************************************************************************/
	rationalNumbers operator -(const rationalNumbers& FRACTION)
	{
		rationalNumbers temp;
		temp.numerator = -FRACTION.numerator;
		temp.denominator = FRACTION.denominator;
		return temp;
	}

/*****************************************************************************
This is the funtion for overloading the * operator for class rationalNumbers
*****************************************************************************/
	rationalNumbers operator *(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		rationalNumbers temp;
		temp.numerator = FRACTION1.numerator * FRACTION2.numerator;
		temp.denominator = FRACTION1.denominator * FRACTION2.denominator;
		return temp;
	}


/*****************************************************************************
This is the funtion for overloading the / operator for class rationalNumbers
*****************************************************************************/
	rationalNumbers operator /(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		rationalNumbers temp;
		temp.numerator = FRACTION1.numerator * FRACTION2.denominator;
		temp.denominator = FRACTION2.denominator * FRACTION2.numerator;
		return temp;
	}

/*****************************************************************************
This is the funtion for overloading the == operator for class rationalNumbers
*****************************************************************************/
	bool operator ==(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		bool temp;	
		if ((FRACTION1.numerator * FRACTION2.denominator) == (FRACTION1.denominator * FRACTION2.numerator))
		{
			temp = true;
		}
		else
		{
			temp = false;
		}
		return temp;
	}

/*****************************************************************************
This is the funtion for overloading the < operator for class rationalNumbers
*****************************************************************************/
	bool operator < (const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		bool temp;	
		if ((FRACTION1.numerator * FRACTION2.denominator) < (FRACTION1.denominator * FRACTION2.numerator))
		{
			temp = true;
		}
		else
		{
			temp = false;
		}
		return temp;
	}
/*****************************************************************************
This is the funtion for overloading the <= operator for class rationalNumbers
*****************************************************************************/
	bool operator <=(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		bool temp;	
		if ((FRACTION1.numerator * FRACTION2.denominator) <= (FRACTION1.denominator * FRACTION2.numerator))
		{
			temp = true;
		}
		else
		{
			temp = false;
		}
		return temp;
	}

/*****************************************************************************
This is the funtion for overloading the > operator for class rationalNumbers
*****************************************************************************/
	bool operator >(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		bool temp;	
		if ((FRACTION1.numerator * FRACTION2.denominator) > (FRACTION1.denominator * FRACTION2.numerator))
		{
			temp = true;
		}
		else
		{
			temp = false;
		}
		return temp;
	}

/*****************************************************************************
This is the funtion for overloading the >= operator for class rationalNumbers
*****************************************************************************/
	bool operator >=(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
	{
		bool temp;	
		if ((FRACTION1.numerator * FRACTION2.denominator) >= (FRACTION1.denominator * FRACTION2.numerator))
		{
			temp = true;
		}
		else
		{
			temp = false;
		}
		return temp;
	}
/*****************************************************************************
This is the funtion for overloading the << operator for class rationalNumbers
*****************************************************************************/
	ostream& operator <<(ostream& outs, const rationalNumbers& FRACTION)
	{
		int TopNumber;
		int BottomNumber;
		TopNumber = FRACTION.numerator;
		BottomNumber = FRACTION.denominator;
		outs << TopNumber << '/' << BottomNumber;
		return outs;
	}

	/******************************************************************
	This function is for overloading the >> operator
	******************************************************************/

	istream& operator >>(istream& ins, rationalNumbers& fraction)
	{
		int TopNumber;
		int BottomNumber;
		char one_char;
		
		ins >> TopNumber;
		ins >> one_char;
		if (one_char == '/')
		{
			ins >> BottomNumber;
		}
		else
		{
			BottomNumber = 1;
		}
		fraction.numerator = TopNumber;
		fraction.denominator = BottomNumber;
		fraction = normalize(fraction);
		return ins;
	}

	/**********************************************************************
	changes input char into an integer
	**********************************************************************/
	int digit_to_int(char c)
	{
		return (static_cast<int>(c) - static_cast<int>('0'));
	}

	/**********************************************************************
	finds and returns greatest common divisor
	**********************************************************************/
	int gcd(int x, int y)
	{
		int g;

		 /* Work with absolute values (positive integers) */
		 if (x < 0)
		 {
			 x = -x;
		 }
		 if (y < 0) 
		 {
			 y = -y;
		 }
		 if ((x+y) == 0) 
		 {
			 return 0; /* Error, both parameters zero */
		 }
		 g = y;
		 /* Iterate until x = 0 */
		 while(x > 0)
		 {
		  g = x;
		  x = y % x;
		  y = g;
		 }

		 return g;
	}

That's a lot of reading, and I probably missed something.

Do you have a method of (during calculations) determining which operations are executed first?

-Albatross
No, do you mean something like a test program?
Okay, cancel what I just said.

It looks like your program just spits out the first two integers and the first operator you feed it. It sounds like you need a while loop that will look ahead two integers and two operators to determine what to do. Okay, cancel that. This is not an easy problem.
-Albatross
Last edited on
Like this?

1
2
3
4
5
while(const rationalNumbers& FRACTION1, const rationalNumbers& FRACTION2)
{
int test;
cout<<test;
}
This is no easy problem, as I said above. Really. If you want to parse expressions, you will need a LOT more code.

If your program will only accept rational numbers in fraction form, then it's easy to explain. Store the expression to a block memory outside of your class (you'll need two classes), preferably a vector that will store integers, and have a function that will convert your operators to integers to represent those operations.

Use <ctype.h> to determine when to move to a new space in the vector.

Your program will read the first block and the third block, and set the numerator and denominator (in your first class) to those, respectively. Then, it will read the fifth block and seventh block, and store that in the second class's numerator and denominator. Then, perform the computation between them according to what is stored in the fourth block. Delete the first seven blocks of the vector, and then use push_back() to write (in this order) the denominator of the result of your computation, a division integer, and the numerator of the result of your computation.

Proceed until you get an error that the fourth block doesn't exist, at which point you're done.

Or just use Flex and Bison.

-Albatross
Last edited on
can you guide me?
because havent learned push_back(0
Last edited on
Okay... I could help a bit. I will try to write as little code as possible. I will also explain how to do this using the C set of functions for reading and writing.

I'm assuming your operators are correct, that a class containing 3/2 and a class containing 2/3 when multiplied using the operator will set the first class to 6/6.

Get an extra rationalNumbers class in main(), called test2 (example). Then, you will need deque<int> data; (not vector). Also, you will need a string called input. Store the output from your cin in this.

---------------

You will be storing your data in this deque. Make a function that uses a while loop, named int read(deque<int> &inp). Inside, you will need a boolean value set to zero. You will also need three integers, and two strings. From there, initialize the second string to anything, so long as it's not a newline.
while (string2 != "\n")
Now, inside, you will check your string character by character. For this, you will need four strings and four integers.

Read each character until you get something that isn't an integer and isn't a whitespace, then copy everything up to that point into your first string, convert it to and integer, and store it in your first integer. Continue reading until you find something that is an integer, then copy what you read into the second string, convert it to your integral representation of your operator, store it in the second integer. Repeat, except using the third and fourth strings and ints.

Then, use push_back() for your deque to insert your integers into your deque. Repeat this and the above paragraph until you reach the end of the string you're reading from.

---------------

From there, in main(), read the first block and the third block, and set the numerator and denominator (in your first class) to those, respectively. Then, read the fifth block and seventh block, and store that in the second class's numerator and denominator. Then, perform the computation between them according to what is stored in the fourth block (I recommend using a chain if if-else statements).

Delete the first seven blocks of the deque using pop_front(), and then use push_front() to write (in this order) the denominator of the result of your computation, a division integer, and the numerator of the result of your computation.

http://cplusplus.com/reference/stl/deque/

Keep going until your deque has exactly three elements.

-Albatross
Last edited on
ok thank you so much i fixed it i have one more question how can i fix the division?
Your division operator should multiply numerator 1 * denominator 2 and denominator 1 * numerator 2, assuming that's what's in block 4 of your deque.

-Albatross

EDIT: Number of days in a year posts, and counting.
Last edited on
is this right?
1
2
3
4
rationalNumbers temp;
		temp.numerator = FRACTION1.numerator * FRACTION2.denominator;
		temp.denominator = FRACTION2.denominator * FRACTION2.numerator;
		return temp;
Check line 3, if should be FRACTION1.denominator.

-Albatross
ok thank you. The interface is the classes part.
what is the implementation and the application/driver file in this example?
I do not understand your question. The implementation of what? And what application file?

-Albatross
How can I create an interface file, implementation file and an application/ driver file from this program
Do you know how to use your compiler for two files? One file?

As for the interface file, cut out (but don't delete) all your declarations from the file you have (that is, everything between the keyword class (there should be only one in your file), and the semi-colon).Paste them into a separate file that ends with .h. Then, use your compiler to compile both of them at the same time. Assuming your compiler is sane, you should get an executable from that.

The header file, as we call them, is anything but mandatory. That file will compile just fine on its own, assuming you don't modify it from what works.

-Albatross
Last edited on
Topic archived. No new replies allowed.