### 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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252`` ``````//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 #include #include #include #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:
 ``10111213`` ``````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 `return`ing 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.

 ``12`` ``````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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319`` ``````//project1driver.cpp #include #include #include #include #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:
 ``12`` ``````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:
 ``1234`` ``````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:
 ``123456789`` ``````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:
 ``1234567891011121314`` ``````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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115`` ``````//project1driver.cpp #include #include #include #include #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?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208`` ``````//project1drivertest.cpp #include #include #include #include #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
 ``12345678910111213`` ``````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.

 ``12345678910111213141516171819202122232425262728`` ``````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?

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?

 ``12345`` ``````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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232`` ``````//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 #include #include #include #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 #include #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) {

}

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:
 ``12345678`` ``````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:
 ``1234`` ``````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 `ostream`and `istream`. I did everything exactly as we learned in class and I am still not doing it correctly. What am I doing wrong?

 ``1234567891011121314`` ``````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:
 ``123`` ``````Rational r1, r2; cin >> r1 >> r2; cout << r1 << " + " << r2 << " = " << r3 << endl;``````

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167`` ``````#include #include #include #include #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<<"/"<>(istream & in, Rational & rat) { cin >> rat.numerator >> rat.denominator; return in; }``````
 ``123`` ``````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:
 ``12`` ``````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.