Rational Numbers Homework Help

For my project, we need to ask the user for two non-negative numbers and it will display what the rational number is. And then, it will prompt the user to ask for a operation to use for this new rational number, ex. adding, subtracting, etc... My problem is that we need to make it inverted, we have to convert a mixed fraction into a rational number, reduce the fraction into simplest form, see if the fraction is less than or equal to another fraction and same for greater than or equal to another fraction. My problem is that I cannot seem to get the code to run properly and that whenever I try to make something it fails. What am i doing wrong?

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
//project1header.h
#ifndef project1header_h
#define project1header_h

class Rational {
public:
	Rational();
	Rational(int n = 0, int d = 1);
	int adding(const Rational& a);  
	int subtracting(const Rational& s); 
	int multiplying(const Rational& m); 
	int dividing(const Rational& b);    
	int invert(const Rational& i);
	int mixedfraction(const Rational& mf);
	int lessthan(const Rational& lt);
	int lessthanorequalto(const Rational& ltoet);
	int greaterthan(const Rational& gt);
	int greaterthanorequalto(const Rational& gtoet);
	int equalto(const Rational& et);
	int getNum();
	int getDem();
	int setDem(const Rational& d);
	int setNum(const Rational& n);
	void printRational();
	void printFloat();
private:
	int numerator;
	int denominator;
	int numerator2;
	int denominator2;
	int fraction1;
	int fraction2;
	int fraction;
	void reduce();
};
#endif
//project1driver.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

#include "project1header.h"

using namespace std;


	int Rational::getNum() {
		cout << "Please put a non-negative number: ";
		cin >> numerator;
		cout << "Please put another non-negative number: ";
		cin >> numerator2;
	}

	int Rational::getDem() {
		cout << "Please put a non-negative number: ";
		cin >> denominator;
		cout << "Please put another non-negative number: ";
		cin >> denominator2;
	}

	Rational::Rational(int n, int d) {
		numerator = d < 0 ? -n : n;
		denominator = d < 0 ? -d : n;
		reduce();
	}
	Rational Rational::adding(const Rational& a) {
		/*Rational r;

		r.numerator = a.numerator * denominator + a.denominator * numerator;
		r.numerator = a.denominator * denominator;
		r.reduce();

		return r; */

		cout << a.numerator << "/" << a.denominator;
		cin >> fraction1;
		cout << a.numerator2 << "/" << a.denominator2;
		cin >> fraction2;
		fraction = fraction1 + fraction2;
		reduce();

		return fraction;
	}
	Rational Rational::subtracting(const Rational& s) {
		Rational r;

		r.numerator = s.numerator * denominator - s.denominator * numerator;
		r.numerator = s.denominator * denominator;
		r.reduce();

		return r;
	}
	Rational Rational::multiplying(const Rational& m) {
		Rational r;

		r.numerator = m.numerator * numerator;
		r.numerator = m.denominator * denominator;
		if (denominator != 0)
			r.reduce();

		return r;
	}
	Rational Rational::dividing(const Rational& b) {
		Rational r;

		r.numerator = b.denominator * numerator;
		r.numerator = b.numerator * denominator;
		if (denominator != 0)
			r.reduce();

		return r;

	}

	Rational Rational::invert(const Rational& i) {

		int temp;
		denominator = temp;
		denominator = numerator;
		numerator = temp;
	}

	Rational Rational::mixedfraction(const Rational& mf) {
		if (numerator / 2) {
			cout << numerator / 2;
			cin >> numerator;
		} if (numerator / 3) {
			cout << numerator / 3;
			cin >> numerator;
		} if (numerator / 4) {
			cout << numerator / 4;
			cin >> numerator;
		} if (numerator / 5) {
			cout << numerator / 5;
			cin >> numerator;
		} if (numerator / 6) {
			cout << numerator / 6;
			cin >> numerator;
		} if (numerator / 7) {
			cout << numerator / 7;
			cin >> numerator;
		} if (numerator / 8) {
			cout << numerator / 8;
			cin >> numerator;
		} if (numerator / 9) {
			cout << numerator / 9;
			cin >> numerator;
		}
	}

	Rational Rational::lessthan(const Rational& lt) {
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		cin >> fraction2;
		if (fraction1 < fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	Rational Rational::lessthanorequalto(const Rational& ltoet) {
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		cin >> fraction2;
		if (fraction1 <= fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	Rational Rational::greaterthan(const Rational& gt)
	{
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		cin >> fraction2;
		if (fraction1 > fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	Rational Rational::greaterthanorequalto(const Rational& gtoet)
	{
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		if (fraction1 >= fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	Rational Rational::equalto(const Rational& et)
	{
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		cin >> fraction2;
		if (fraction1 == fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	void Rational::printRational() {
		if (denominator = 0)
			cout << "Error" << endl;
		else {
			cout << numerator << "/" << denominator << endl;
		}
	}

	void Rational::printFloat() {

		if (denominator = 0)
			cout << "Error" << endl;
		else
			cout << numerator / denominator << "." << numerator % denominator << endl;

	}

	void Rational::reduce() {
		int n = numerator < 0 ? -numerator : numerator;
		int d = denominator;
		int largest = n > d ? n : d;

		int gcd = 0;

		for (int i = largest; i >= 2; i--)
			if (numerator % i == 0 && denominator % i == 0) {
				gcd = i;
				break;
			}
		if (gcd != 0) {
			numerator /= gcd;
			denominator /= gcd;
		}
	}
Last edited on
The very first thing you should do is make it compile.

Crank up the compiler warnings and start working on them one by one. For example, the very first error I got was:

project1driver.cpp:13:1: warning: control reaches end of non-void function [-Wreturn-type]

Opening up the file I can see:
10
11
12
13
int Rational::getNum() {
	cout << "Please put a mumber that is non-negative: ";
	cin >> numerator;
}

This function appears to want to return an int, but you are not returning anything.

Hint: you have two private variables: numerator and denominator. Your class also declares “accessors”, which purpose is to get or set the value of those variables. NOT user input.

1
2
int Rational::getNum() { return numerator; }
void Rational::setNum( int n ) { numerator = n; }

It’s as simple as that.


error: call to constructor of 'Rational' is ambiguous

You have a default constructor:
    Rational();

AND a constructor with default arguments:
    Rational(int n = 0, int d = 1);

This is a bit confusing. Which of these constructors should the compiler call when you say:
    Rational r;
?

Hint: you didn’t even bother to write the implementation of the constructor you don’t need.


error: return type of out-of-line definition of 'Rational::adding' differs from that in the declaration

What it says on the box. In project1header.h you say:
    int adding(const Rational& a);

But in project1driver.cpp you say:
    Rational Rational::adding(const Rational& a)

Which is it? Does it return an int or does it return a Rational? You’ve got to pick one.


You have a similar issue with lessthan(), etc. They are declared as existing in project1header.h, but do not exist in project1driver.cpp.


Most of what remains are mathematical errors or carelessness. For example, look at what you are doing on the fourth line of adding().


That should get you started.
Hope this helps.
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//project1driver.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

#include "project1header.h"

using namespace std;

	int Rational::getNum() {
		cout << "Please put a non-negative number: ";
		cin >> numerator;
		cout << "Please put another non-negative number: ";
		cin >> numerator2;

		return numerator, numerator2;
	}

	int Rational::getDem() {
		cout << "Please put a non-negative number: ";
		cin >> denominator;
		cout << "Please put another non-negative number: ";
		cin >> denominator2;

		return denominator, denominator2;
	}

	Rational::Rational(int n, int d) {
		numerator = d < 0 ? -n : n;
		denominator = d < 0 ? -d : n;
		reduce();
	}
	int Rational::adding(const Rational& a) {
		/*Rational r;

		r.numerator = a.numerator * denominator + a.denominator * numerator;
		r.numerator = a.denominator * denominator;
		r.reduce();

		return r; */

		cout << a.numerator << "/" << a.denominator;
		cin >> fraction1;
		cout << a.numerator2 << "/" << a.denominator2;
		cin >> fraction2;
		fraction = fraction1 + fraction2;
		reduce();

		return fraction;
	}
	int Rational::subtracting(const Rational& s) {
		/*Rational r;

		r.numerator = s.numerator * denominator - s.denominator * numerator;
		r.numerator = s.denominator * denominator;
		r.reduce();

		return r;*/

		cout << s.numerator << "/" << s.denominator;
		cin >> fraction1;
		cout << s.numerator2 << "/" << s.denominator2;
		cin >> fraction2;
		fraction = fraction1 - fraction2;
		reduce();

		return fraction;
	}
	int Rational::multiplying(const Rational& m) {

		/*r.numerator = m.numerator * numerator;
		r.numerator = m.denominator * denominator;
		if (denominator != 0)
			r.reduce();

		return r;*/

		if (denominator != 0)
			reduce();
		cout << m.numerator << "/" << m.denominator;
		cin >> fraction1;
		cout << m.numerator2 << "/" << m.denominator2;
		cin >> fraction2;
		cout << fraction1 * fraction2;
		cin >> fraction;

		return fraction;
	}
	int Rational::dividing(const Rational& b) {

		/*r.numerator = b.denominator * numerator;
		r.numerator = b.numerator * denominator;
		if (denominator != 0)
			r.reduce();

		return r;*/

		if (denominator != 0)
			reduce();
		cout << b.numerator << "/" << b.denominator;
		cin >> fraction1;
		cout << b.numerator2 << "/" << b.denominator2;
		cin >> fraction2;
		cout << fraction1 / fraction2;
		cin >> fraction;

		return fraction;
	}

	int Rational::invert(const Rational& i) {

		denominator = temp;
		denominator = numerator;
		numerator = temp;

		cout << numerator << "/" << denominator;
		cin >> fraction;

		return fraction;
	}

	int Rational::mixedfraction(const Rational& mf) {
		if (numerator / 2) {
			cout << numerator / 2;
			cin >> numerator;
			cout << numerator << "/" << denominator;
			cin >> fraction;

			return fraction;
		} if (numerator / 3) {
			cout << numerator / 3;
			cin >> numerator;
			cout << numerator << "/" << denominator;
			cin >> fraction;

			return fraction;
		} if (numerator / 4) {
			cout << numerator / 4;
			cin >> numerator;
			cout << numerator << "/" << denominator;
			cin >> fraction;

			return fraction;
		} if (numerator / 5) {
			cout << numerator / 5;
			cin >> numerator;
			cout << numerator << "/" << denominator;
			cin >> fraction;

			return fraction;
		} if (numerator / 6) {
			cout << numerator / 6;
			cin >> numerator;
			cout << numerator << "/" << denominator;
			cin >> fraction;

			return fraction;
		} if (numerator / 7) {
			cout << numerator / 7;
			cin >> numerator;
			cout << numerator << "/" << denominator;
			cin >> fraction;

			return fraction;
		} if (numerator / 8) {
			cout << numerator / 8;
			cin >> numerator;
			cout << numerator << "/" << denominator;
			cin >> fraction;

			return fraction;
		} if (numerator / 9) {
			cout << numerator / 9;
			cin >> numerator;
			cout << numerator << "/" << denominator;
			cin >> fraction;
			return fraction;
		}
		return fraction;
	}

	int Rational::lessthan(const Rational& lt) {
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		cin >> fraction2;
		if (fraction1 < fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	int Rational::lessthanorequalto(const Rational& ltoet) {
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		cin >> fraction2;
		if (fraction1 <= fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	int Rational::greaterthan(const Rational& gt)
	{
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		cin >> fraction2;
		if (fraction1 > fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	int Rational::greaterthanorequalto(const Rational& gtoet)
	{
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		if (fraction1 >= fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	int Rational::equalto(const Rational& et)
	{
		cout << numerator << "/" << denominator;
		cin >> fraction1;
		cout << numerator2 << "/" << denominator2;
		cin >> fraction2;
		if (fraction1 == fraction2) {
			return true;
		}
		else {
			return false;
		}
	}

	void Rational::printRational() {
		if (denominator = 0)
			cout << "Error" << endl;
		else {
			cout << numerator << "/" << denominator << endl;
		}
	}

	void Rational::printFloat() {

		if (denominator = 0)
			cout << "Error" << endl;
		else
			cout << numerator / denominator << "." << numerator % denominator << endl;

	}

	void Rational::reduce() {
		int n = numerator < 0 ? -numerator : numerator;
		int d = denominator;
		int largest = n > d ? n : d;

		int gcd = 0;

		for (int i = largest; i >= 2; i--)
			if (numerator % i == 0 && denominator % i == 0) {
				gcd = i;
				break;
			}
		if (gcd != 0) {
			numerator /= gcd;
			denominator /= gcd;
		}
	}
//project1header.h
#ifndef project1header_h
#define project1header_h

class Rational {
public:
	Rational();
	Rational(int n = 0, int d = 1);
	int adding(const Rational& a);  
	int subtracting(const Rational& s); 
	int multiplying(const Rational& m); 
	int dividing(const Rational& b);    
	int invert(const Rational& i);
	int mixedfraction(const Rational& mf);
	int lessthan(const Rational& lt);
	int lessthanorequalto(const Rational& ltoet);
	int greaterthan(const Rational& gt);
	int greaterthanorequalto(const Rational& gtoet);
	int equalto(const Rational& et);
	int getNum();
	int getDem();
	int setDem(const Rational& d);
	int setNum(const Rational& n);
	void printRational();
	void printFloat();
private:
	int numerator;
	int denominator;
	int numerator2;
	int denominator2;
	int fraction1;
	int fraction2;
	int fraction;
	int temp;
	void reduce();
};


Here is my new code and everything is correct but when I put int main(){} it gives me so many errors. What am I doing wrong
What you are currently doing wrong is ignoring the advice given you.

If you are going to make the effort to ask for help, you ought to at least read and try to follow the answer.

’Cause I’m not interested in doing it for you.
There are several things wrong with the code. Things could also be organized a little better.

For starters, a Rational should only have two data members: numerator and denominator. Any other values that you need should be local variables inside the methods.

getNum() and getDem() should just return the values. They shouldn't prompt the user for them. The same goes for the other methods: they should not prompt for the values, they should just do the operation. You may need to write a separate function (outside the class) to prompt for a numerator and denominator and return a Rational:
1
2
int getNum() { return numerator; }
int getDem() { return denominator; }


You shouldn't let the user set the numerator or denominator separately. I think there should just be a set() method that takes both:
1
2
3
4
void set(int n, int d) {
    numerator=n;
    denominator=d;
}


Your constructor has some of the code to reduce. Why? Just set the values and reduce the rational:

Adding() (why not just call it add()?) should return a Rational. It should NOT print the value. If the code that calls this wants to print the value, then it can call printRational. The code that you have commented out is good except for setting the denominator:
1
2
3
4
5
6
7
8
9
Rational Rational::add(const Rational& a)
{
    Rational r;

    r.numerator = a.numerator * denominator + a.denominator * numerator;
    r.denominator = a.denominator * denominator;
    r.reduce();
    return r;
}


lessthan() isn't right. Compare rationals the same way you'd compare them by hand: put them into a form with a common denominator and then compare the numerators.

You should implement lessthan and equalto. Then implement the other comparisons as calls to these two methods.

Lookup a function to compute the GCD() of two numbers. There are more efficient ways than what you have. Also, I think reduce() should handle the case of a negative denominator. In other words, you should be able to pass any whacky old fraction to reduce() and it should clean it up so the denominator is positive and the numerator & denominator are in lowest form:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Rational::reduce()
{
    // Make the denominator positive
    if (denominator < 0) {
        denominator = -denominator;
        numerator = - numerator;
    }

    int v = gcd(numerator, denominator);
    if (v) {
        numerator /= gcd;
        denominator /= gcd;
    }
}


Putting this altogether. Here is a program that creates one rational (1/2), prompts for a second one, adds them together and prints the result. I pasted everything into one file for my own convenience.

Try writing subtract() and multiply() using this as a starting point.
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
//project1driver.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

#ifndef project1header_h
#define project1header_h

class Rational {
public:
    Rational(int n = 0, int d = 1);
    Rational add(const Rational& other);  
    Rational subtract(const Rational& s); 
    Rational multiply(const Rational& m); 
    Rational divid(const Rational& b);    
    Rational invert(const Rational& i);
    Rational mixedfraction(const Rational& mf);
    int lessthan(const Rational& lt);
    int lessthanorequalto(const Rational& ltoet);
    int greaterthan(const Rational& gt);
    int greaterthanorequalto(const Rational& gtoet);
    int equalto(const Rational& et);
    int getNum() {return numerator; }
    int getDem() {return denominator; }
    void set(int n, int d) {
	numerator=n;
	denominator=d;
    }
    void printRational();
    void printFloat();
private:
	int numerator;
	int denominator;
	void reduce();
};
#endif

// #include "project1header.h"

using namespace std;


unsigned long gcd(unsigned long x, unsigned long y)
{
    if (x < y) return gcd(y,x);

    // x >= y
    while (y) {
	unsigned long tmp = y;
	y = x%y;
	x = tmp;
    }
    return x;
}

void Rational::printRational()
{
    cout << numerator << " / " << denominator;
}

Rational::Rational(int n, int d) {
    numerator = n;
    denominator = d;
    reduce();
}

Rational Rational::add(const Rational& a)
{
    Rational r;

    r.numerator = a.numerator * denominator + a.denominator * numerator;
    r.denominator = a.denominator * denominator;
    r.reduce();
    return r;
}


void Rational::reduce()
{
    // Make the denominator positive
    if (denominator < 0) {
	denominator = -denominator;
	numerator = - numerator;
    }

    int v = gcd(abs(numerator), denominator);
    if (v) {
	numerator /= v;
	denominator /= v;
    }
}

Rational getRational()
{
    int n, d;
    cout << "Enter a numerator: ";
    cin >> n;
    cout << "Enter a denominator: ";
    cin >> d;
    return Rational(n,d);
}


int main()
{
    Rational a(1,2);
    Rational b = getRational();

    Rational c = a.add(b);
    b.printRational();
    cout << " plus 1/2 is ";
    c.printRational();
    cout << '\n';
}


dhayden, I made another file for what you put in and I made the code for subtracting and multiplying. But when I run the code, whenever it multiplies or divides, it gives me a different answer than I should be expecting. What you wrote worked and it was going good until I had to make the different scenarios for each individual case. What is wrong with my code that is making me have this error?
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
//project1drivertest.cpp

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

#include "project1headertest.h"

using namespace std;


unsigned long gcd(unsigned long x, unsigned long y)
{
	if (x < y) return gcd(y, x);

	// x >= y
	while (y) {
		unsigned long tmp = y;
		y = x % y;
		x = tmp;
	}
	return x;
}

void Rational::printRational()
{
	cout << numerator << " / " << denominator;
}

Rational::Rational(int n, int d) {
	numerator = n;
	denominator = d;
	reduce();
}

Rational Rational::add(const Rational& a)
{
	Rational r;

	r.numerator = a.numerator * denominator + a.denominator * numerator;
	r.denominator = a.denominator * denominator;
	r.reduce();
	return r;
}

Rational Rational::subtract(const Rational& s)
{

	Rational r;

	r.numerator = s.numerator * denominator - s.denominator * numerator;
	r.denominator = s.denominator * denominator;
	r.reduce();
	return r;
}

Rational Rational::multiply(const Rational& m)
{

	Rational r;

	r.numerator = m.numerator * denominator * m.denominator * numerator;
	r.denominator = m.denominator * denominator;
	if (denominator != 0)
		r.reduce();
	return r;
}

Rational Rational::divid(const Rational& b)
{

	Rational r;

	r.numerator = b.numerator * denominator / b.denominator * numerator;
	r.numerator = b.denominator * denominator;
	if (denominator != 0)
		r.reduce();
	return r;
}

Rational Rational::invert(const Rational& i)
{
	
	Rational r;
	int temp;

	temp = r.numerator;
	r.numerator = r.denominator;
	r.denominator = temp;
	r.reduce();
	return r;
}

Rational Rational::mixedfraction(const Rational& mf)
{

	Rational r;
	int newfrac;

	if (numerator > denominator) {
		cout << r.numerator / r.denominator;
		/*cin >> newfrac;*/
		r.reduce();
		return r;
	}
}

void Rational::reduce()
{
	// Make the denominator positive
	if (denominator < 0) {
		denominator = -denominator;
		numerator = -numerator;
	}

	int v = gcd(abs(numerator), denominator);
	if (v) {
		numerator /= v;
		denominator /= v;
	}
}

Rational getRational()
{
	int n, d;
	cout << "Enter a numerator: ";
	cin >> n;
	cout << "Enter a denominator: ";
	cin >> d;
	return Rational(n, d);
}


int main()
{
	Rational a(1, 2);
	Rational b = getRational();

	Rational c = a.add(b);
	b.printRational();
	cout << " plus 1/2 is ";
	c.printRational();
	cout << '\n';

	Rational h(1, 2);
	Rational j = getRational();

	Rational k = h.subtract(j);
	j.printRational();
	cout << " minus 1/2 is ";
	k.printRational();
	cout << '\n';

	Rational l(1, 2);
	Rational p = getRational();

	Rational o = l.multiply(p);
	l.printRational();
	cout << " times 1/2 is ";
	o.printRational();
	cout << '\n';

	Rational q(1, 2);
	Rational r = getRational();

	Rational t = q.divid(r);
	q.printRational();
	cout << " divided by 1/2 is ";
	t.printRational();
	cout << '\n';

	system("pause");
}

//project1headertest.h

#ifndef project1headertest_h
#define project1headertest_h

class Rational {
public:
	Rational(int n = 0, int d = 1);
	Rational add(const Rational& other);
	Rational subtract(const Rational& s);
	Rational multiply(const Rational& m);
	Rational divid(const Rational& b);
	Rational invert(const Rational& i);
	Rational mixedfraction(const Rational& mf);
	int lessthan(const Rational& lt);
	int lessthanorequalto(const Rational& ltoet);
	int greaterthan(const Rational& gt);
	int greaterthanorequalto(const Rational& gtoet);
	int equalto(const Rational& et);
	int getNum() { return numerator; }
	int getDen() { return denominator; }
	void set(int n, int d) {
		numerator = n;
		denominator = d;
	}
	void printRational();
	void printFloat();
private:
	int numerator;
	int denominator;
	void reduce();
};
#endif 
Last edited on
repeated code --- consider putting the denom = 0 checks in reduce() instead of forcing the user to check this. you can just say if( d == 0) return; right off in reduce().

also I can't make sense of your multiply.
should be
(this.num * m.num ) / (this.den * m.den) reduced.
eg
1/2 * 2/3 = 1*2/2*3 = 2/6 reduces to 1/3 (double check, 1/2 and 2/x cancels the 2s leaving 1/x)

what is this doing?!
r.numerator = m.numerator * denominator * m.denominator * numerator;
that gives you 2*2*1*2 = 8
and 6 for the den with your code, 8/6 is nothing like 1/3
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
Rational Rational::mixedfraction(const Rational& mf)
{

	Rational r;
	int newfrac;

	if (numerator > denominator) {
		cout << r.numerator / r.denominator;
		/*cin >> newfrac;*/
		r.reduce();
		return r;
	}
}

Not all logic paths here return a value. You are only returning a value if numerator > denominator. Otherwise, it's undefined behavior.

Also, your multiply function -- Doesn't that just simplify to (m.numerator * numerator) over 1? It doesn't look right.
Last edited on
For my mixed fraction else statement should I tell the code to reduce if possible or keep the same denominator and numerator? Also for the multiply function it is not working when I give any numbers, when I give any numbers it is giving me the same answer of 1/4. What am I doing wrong.

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
Rational Rational::multiply(const Rational& m)
{

	Rational r;

	r.numerator = m.numerator * denominator / m.denominator * numerator;
	r.denominator = m.denominator * denominator;
	if (denominator == 0)
		r.reduce();
	return r;
}

Rational Rational::mixedfraction(const Rational& mf)
{

	Rational r;
	int newfrac;

	if (numerator > denominator) {
		cout << r.numerator / r.denominator;
		/*cin >> newfrac;*/
		r.reduce();
		return r;
	}
	else {
		
	}
}
I would reduce mixed numbers. 3 + 5/10 is 3+1/2 still. or 100/30 is 10/3 is 3 + 1/3 : either 10/3 or 3 and 1/3 is better than 100/30 or 3 and 10/30 right?

your multiply is wrong.
we told you already. how do you multiply fractions? do it on a sheet of paper. If you think you need to work with huge values, the algorithm is reduce both sides, then multiply, then reduce again. If you are not worried about that, then just multiply and reduce once.

your numerator assignment makes no sense at all to me, nor the denominator. multiply of fractions is (n*n2)/(d*d2). Right? 2/4*2/4 is 4/16 is 1/4 (1/2 * 1/2 is 1/4, right?)
you have denoms in your numerator assignment and a so on... its just not correct.
invert() looks wrong to me. Based on the signature, it appears that you're trying to invert the parameter i and return the inverted fraction. But you never actually do anything with i.

Also, why pass the parameter at all? Why not just invert *this?

1
2
3
4
5
Rational Rational::invert()
{
    Rational result(denominator, numerator);
    return result;
}
Alright, so now I am trying to make the statements for the less than, greater than, less than or equal to, greater than or equal to, and equal to statements but whatever I do, it does not work at all. What Am I doing wrong?

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
//project1drivertest.h
#ifndef project1headertest_h
#define project1headertest_h


class Rational{
	friend ostream operator<<(ostream& os, const Rational& p);
	friend istream operator>>(istream in, Rational rat);
	friend bool operator<(const Rational& p, const Rational& q);
	public:
		Rational(int n = 0, int d = 1);
		Rational operator+(const Rational& other);
		Rational operator-(const Rational& s);
		Rational operator*(const Rational& m);
		Rational operator/(const Rational& b);
		Rational invert(const Rational& i);
		Rational mixedfraction(const Rational& mf);
		int operator<(const Rational& lt);
		int operator<=(const Rational& ltoet);
		int operator>(const Rational& gt);
		int operator>=(const Rational& gtoet);
		int operator==(const Rational& et);
		int getNum();
		int getDen();
		int setNum(int &n);
		int setDen(int &d);
		void set(int n, int d) {
			numerator = n;
			denominator = d;
		}
		void printRational();
		void printFloat();
		int x;
		int y;
	private:
		int numerator;
		int denominator;
		void reduce();
};
#endif

//project1drivertest.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

#include "project1headertest.h"

using namespace std;


int Rational::setNum(int& n) {
	numerator = n;
}

int Rational::getNum() {
	return numerator;
}

int Rational::setDen(int& d) {
	denominator = d;
}

int Rational::getDen() {
	return denominator;
}

unsigned long gcd(unsigned long x, unsigned long y)
{
	if (x < y) return gcd(y, x);

	// x >= y
	while (y) {
		unsigned long tmp = y;
		y = x % y;
		x = tmp;
	}
	return x;
}

void Rational::printRational()
{
	cout << numerator << " / " << denominator;
}

Rational::Rational(int n, int d) {
	numerator = n;
	denominator = d;
	reduce();
}

Rational Rational::operator+(const Rational& a)
{
	Rational r;

	r.numerator = a.numerator * this->denominator + a.denominator * this->numerator;
	r.denominator = a.denominator * this->denominator;
	r.reduce();
	return r;
}

Rational Rational::operator-(const Rational& s)
{

	Rational r;

	r.numerator = s.numerator * this->denominator - s.denominator * this->numerator;
	r.denominator = s.denominator * this->denominator;
	r.reduce();
	return r;
}

Rational Rational::operator*(const Rational& m)
{

	Rational r;

	r.numerator = m.numerator * this->numerator;
	r.denominator = m.denominator * this->denominator;
	r.reduce();
	return r;
}

Rational Rational::operator/(const Rational& b)
{

	Rational r;

	r.numerator = b.numerator * this->numerator;
	r.numerator = b.denominator * this->denominator;
	r.reduce();
	return r;
}

Rational Rational::invert(const Rational& i)
{
	Rational result(denominator, numerator);
	return result;
}

Rational Rational::mixedfraction(const Rational& mf)
{
	int front;
	Rational r;
	int newfrac;
	if (numerator < denominator) {
		return mf;
	}
	else (numerator > denominator); {
		front = mf.numerator / mf.denominator;
		r.numerator = mf.numerator % mf.denominator;
		r.denominator = mf.denominator;
		r.reduce();
		return r;
	}
}

int Rational::operator<(const Rational& lt) {

	Rational r;
}

int Rational::operator<=(const Rational& ltoet) {

	Rational r;
}

void Rational::reduce()
{
	int v = gcd(abs(numerator), abs(denominator));
	if (denominator == 0) {
		cout << "#div0";
	}
	if (v) {
		numerator /= v;
		denominator /= v;
	}
}

ostream operator<<(ostream& os, const Rational& p) {
	os << p.x << "/" << p.y << endl;
	return os;
}

istream operator>>(istream in, Rational rat) {

}

bool operator<(const Rational& p, const Rational& q) {
	int newnum;
	int newnum2;

	cout << "Please Give a Rational Number";
	cin >> newnum;
	cout << "Please Give another Rational Number";
	cin >> newnum2;

	if (newnum < newnum2) {
		return true;
	}
	else {
		return false;
	}
}
//project1main.cpp
#include <iostream>
#include <cmath>

#include "project1headertest.h"

using namespace std;

int main()
{
	Rational number, number2;
	char newvar;

	cout << "Enter An Expression";
	cin >> number;

	cin >> newvar;
	switch (newvar) {
	case '+':
		cin >> number2;
		number + number2;
	case '-':
		cin >> number2;
		number + number2;
	}
	system("pause");
}
Last edited on
there isn't any code in them?
just defines a local variable and does nothing?

you can probably 'cheat' here...
check denoms for zero and error out if found, else
return ( (double)num/(double)den (which operator) (double)(input.num)/(double)(input.den)) ;

eg if you want to know if 1/2 is < 1/3, compare if 0.5 < 0.33333 and return the result of the DOUBLE built in comparison of the values as your answer (it will be correct). you may have to be more careful with equality checks, but you can see if you need that or not.


int Rational::operator<(const Rational& lt) {

Rational r; ///wheres your code?
}

int Rational::operator<=(const Rational& ltoet) {

Rational r;
}
Last edited on
Another option for an exact answer, if it doesn't overflow:

Create a cmp function that works like strcmp: just subtract the numbers and return the numerator:
1
2
3
4
5
6
7
8
private:
// Compare two rationals, Return an int that is less than, equal to, or greater than 0
// depending on whether *this is less than, equal to, or greater than rhs
int cmp(const Rational &rhs) const // rhs is "right hand side"
{
    Rational r(*this - rhs);
    return r.numerator;
};


Now the operators are trivial:
1
2
3
4
int operator < (const Rational &rhs) const   { return cmp(rhs) < 0; }
int operator == (const Rational &rhs) const   { return cmp(rhs) == 0; }
int operator > (const Rational &rhs) const   { return cmp(rhs) > 0; }
// etc. 

Alright, so I have all the cases set and the different codes for each case but I am having problems with my ostreamand istream. I did everything exactly as we learned in class and I am still not doing it correctly. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ostream operator<<(ostream& os, const Rational& p) {

	os << p.numerator << "/" << p.denominator << endl;
	return os;
}

istream operator>>(istream in, Rational rat) {

	in << rat.numerator << "/" << rat.denominator << endl;
	return in;
}

friend ostream& operator<<(ostream& os, const Rational& p);
friend istream& operator>>(istream in, Rational rat);
The definitions of the operators (lines 1-11) need to be outside the Rational class definition.
The friend declarations need to be inside the class definition.

Line 1: You should return a reference, not an actual ostream:
ostream & operator<<(ostream& os, const Rational& p) {
Line 7: Same comment.
Line 9: You extract from istreams with >>, not <<.

Don't read or write the endl inside these operators. The caller might want to read or write more stuff on the line with the rational. For example:
1
2
3
Rational r1, r2;
cin >> r1 >> r2;
cout << r1 << " + " << r2 << " = " << r3 << endl;

My code is not running. What am I doing wrong?

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
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

#include "project1headertest.h"

using namespace std;


void Rational::setNum(int& n) {
	numerator = n;
}

int Rational::getNum() const {
	return numerator;
}

void Rational::setDen(int& d) {
	denominator = d;
}

int Rational::getDen() const {
	return denominator;
}


void Rational::printRational()
{
	cout << numerator << " / " << denominator;
}

Rational::Rational(int n, int d) {
	numerator = n;
	denominator = d;
}

Rational Rational::operator+(const Rational& a)
{
	Rational r;

	r.numerator = a.getNum() * this->denominator + a.getDen() * this->numerator;
	r.denominator = a.getDen() * this->denominator;
	r.reduce(r);
	return r;
}

Rational Rational::operator-(const Rational& s)
{

	Rational r;

	r.numerator = s.getNum() * this->denominator - s.getDen() * this->numerator;
	r.denominator = s.getDen() * this->denominator;
	r.reduce(r);
	return r;
}

Rational Rational::operator*(const Rational& m)
{

	Rational r;

	r.numerator = m.getNum() * this->numerator;
	r.denominator = m.getDen() * this->denominator;
	r.reduce(r);
	return r;
}

Rational Rational::operator/(const Rational& b)
{

	Rational r;

	r.numerator = b.getNum() * this->numerator;
	r.denominator = b.getDen() * this->denominator;
	r.reduce(r);
	return r;
}

Rational Rational::invert(const Rational& igloo)
{
	Rational temp;

	temp.setNum(igloo.getDen());
	temp.setDen(igloo.getNum());
	

	return temp;
}

Rational Rational::mixedfraction(const Rational& mf)
{
	int front;
	Rational r;

	if (getNum() < getDen()) {
		cout << getNum() << "/" << getDen();
	}
	else (numerator > denominator); {
		front = mf.getNum() / mf.getDen();
		r.numerator = mf.getNum() % mf.getDen();
		r.denominator = mf.getDen();
		r.reduce(r);
		if (numerator == 0) {
			cout << front;
		}
		else {
			cout << front << numerator << "/" << denominator;
		}
	}
	return r;
}

bool Rational::operator<(const Rational& first) {

	return (*this < first);
	
}

bool Rational::operator<=(const Rational& ltoet) {
	return (*this <= ltoet);
}

bool Rational::operator>(const Rational& gt) {
	return (*this > gt);
}

bool Rational::operator>=(const Rational& gt) {
	
	return (*this >= gt);
}

bool Rational::operator==(const Rational& et) {

	return (*this == et);
}

Rational Rational::reduce( Rational & vup)
{
	Rational tup;
	int v = (abs(vup.getNum()), abs(vup.getDen()));

	if (vup.getDen() == '0')
	{
		cout << "#div0";
	}
	else 
	{
		tup.setDen = vup.getNum() / v;
		tup.setNum = vup.getDen() / v;
		cout << tup.getNum() << " " << tup.getDen();
	}
	return tup;
}

ostream& operator<<(ostream & os, Rational& p) {

	cout << p.numerator<<"/"<<p.denominator;
	return os;
}

istream & operator>>(istream & in, Rational & rat) {

	cin >> rat.numerator >> rat.denominator;
	return in;
}
1
2
3
bool Rational::operator<(const Rational& first) {
	return (*this < first);
}

Operator< just calls operator<. So your program will fail with infinite recursion.
Line 92: Can you explain in words what mixedFraction() is supposed to do? Once you decide in words, add the words as a comment before the method. Then write code that does what the words say. Right now it prints some stuff (sometimes) and returns something, but that might be a default-initialized Rational or maybe the fractional part of mf. And what's the relationship between this and mf? You use members of both in some places.

Line 139: why does reduce() take a parameter? Why not reduce this?
Line 142: What are you trying to do here?What you have is the same as:
1
2
abs(vup.getNum());
int v = abs(vup.getDen());


Line 144: This compares vup.getDen() with the ascii value of character '0'. So it's the same as if (vup.getDen() == 48) I think you want to compare to zero, so it should be if (vup.getDen() == 0)

Line 152: why does reduce() print anything?
Line 159: you should print to os, not cout.
Line 165: similar problem as above.
Topic archived. No new replies allowed.