Hey I've been trying to compare two rationals and see which one is bigger, but I can't figure out why it's not working. When I put in two rationals like 6/7 and 1/2 it says the second rational is bigger than the first. Everything else works as intend however.
#include <iostream>
usingnamespace std;
class Rational
{
public:
Rational();
Rational(int d, int n);
void ReadRational();
void display();
Rational operator +(Rational r)
{
Rational temp, temp2;
if (denom == r.denom)//making sure if both denominators are the same they stay the same
{
temp.num = num + r.num;
temp.denom = denom = r.denom;
temp.denom = r.denom = denom;
}
else
{
temp.denom = denom * r.denom;
temp.num = num * r.denom;
temp2.num = r.num * denom;//needed another temperoary container otherwise this equation would have overwritten the one above
temp.num += temp2.num;
}
return temp;
return temp2;
}
Rational operator -(Rational r)
{
Rational temp, temp2;
if (denom == r.denom)//making sure if both denominators are the same they stay the same
{
temp.num = num - r.num;
temp.denom = denom = r.denom;
temp.denom = r.denom = denom;
}
else
{
temp.denom = denom * r.denom;
temp.num = num * r.denom;
temp2.num = r.num * denom;
temp.num -= temp2.num;
}
return temp;
return temp2;
}
Rational operator *(Rational r)
{
Rational temp;
temp.num = num * r.num;
temp.denom = denom * r.denom;
return temp;
}
Rational operator /(Rational r)//multiplying by the resipricle
{
Rational temp;
temp.num = num * r.denom;
temp.denom = denom * r.num;
return temp;
}
booloperator == (Rational r)//this is bool since we are comparing two things and returning it as true or false
{
if (num == r.num && denom == r.denom)
{
returntrue;
}
returnfalse;
}
booloperator >(Rational r)//problem here
{
Rational temp, temp2;
temp.num = num / denom;
temp.num = r.num / r.denom;
if (temp.num > temp2.num)
{
returntrue;
}
returnfalse;
}
private:
int num = 0;
int denom = 1;
};
Rational::Rational()
{
}
Rational::Rational(int d, int n)
{
num = 0;
denom = 1;
}
int main()
{
int d, n;
Rational r1, r2;//r in this case will be short for rational
r1.ReadRational();
cout << "First rational is ";
r1.display();//since num and denom are private I call upon this since it is part of the class
r2.ReadRational();
cout << "Second rational is ";
r2.display();
Rational Sum = r1 + r2;
cout << "The sum is: ";
Sum.display();
Rational Sub = r1 - r2;
cout << "The subtraction is: ";
Sub.display();
Rational multi = r1 * r2;
cout << "Multiplication is: ";
multi.display();
Rational div = r1 / r2;
cout << "Division is: ";
div.display();
if (r1 == r2)
{
cout << "Both rationals are the same" << endl;
}
else
{
cout << "Both rationals are different" << endl;
}
if (r1 > r2)//problem here
{
cout << "The first rational is bigger than the second" << endl;
}
else
{
cout << "The second rational is bigger than the first " << endl;
}
}
void Rational::display()
{
cout << num << "/" << denom << endl;
}
void Rational::ReadRational()
{
cout << "Enter in the numerator: ";
cin >> num;
cout << endl;
cout << "Enter in the denominator (do not put zero in): ";
cin >> denom;
if (denom == 0)
{
cout << "Invalid input!" << endl;
exit(1);
}
}
Also, you can't return two things from a function, your second return statements are unreachable code your in +/- operators. Turn on compiler warnings (-Wall in g++). I think you're confusing yourself by having multiple (perhaps redundant) "temp" variables. Try to avoid that.
Also also, I'm not sure if you ever call this constructor, but read this out loud to yourself:
1 2 3 4 5 6
Rational::Rational(int d, int n)
{
num = 0;
denom = 1;
}
Read it physically out loud to yourself. What does the constructor take in as parameters, what does the constructor do in its body?
Sorry for some of those trivial errors. I must of controlled Z too many times when I was working on this problem. As for the returning values problem I still get the right outcome for everything with the exception of comparing the two rationals still. Is that why it doesn't work? Because of the returning of more than one value?
Show us what you have changed the comparison to, @Deadweight77.
Assuming you have corrected the defining of the same thing twice (and the not defining of the other) then you will almost certainly end up suffering from integer division problems.
Avoid them by noting that (if b and d are positive):
a/b > c/d if and only if ad > bc.
If you are allowing negatives in the denominator (I don't recommend it) then you will have to consider the various sign cases.
Basic rules of numbers (as in my previous post). For positive b and d:
a/b > c/d if and only if ad > bc.
This will avoid problems with integer division.
I would recommend that, eventually, you should divide numerator and denominator by gcd (greatest common divisor). e.g. 10/8 becomes 5/4. That way you will keep numbers as small as possible and lessen the chances of overflow in some operations.