bool Rational::operator==(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (ratPassed.num == num) && (ratPassed.den == den);
}
bool Rational::operator!=(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (ratPassed.num != num) && (ratPassed.den != den);
}
bool Rational::operator>(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (num * ratPassed.den) > (ratPassed.num * den);
}
bool Rational::operator<(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (num * ratPassed.den) < (ratPassed.num * den);
}
bool Rational::operator>=(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (num * ratPassed.den) >= (ratPassed.num * den);
}
bool Rational::operator<=(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (num * ratPassed.den) <= (ratPassed.num * den);
}
#include
#include
using namespace std;
class Rational
{
private:
int num;
int den;
public:
// Accessor and mutator functions
void setNumerator(int);
void setDenominator(int);
int getNumerator();
int getDenominator();
// Greatest common divisor, needed to simplify fractions
int gcd();
// Simplifies fractions
void simplify();
// Arithmetic operators
Rational operator+(Rational);
Rational operator-(Rational);
Rational operator*(Rational);
Rational operator/(Rational);
// Comparison operators
bool operator==(Rational);
bool operator!=(Rational);
bool operator>(Rational);
bool operator<(Rational);
bool operator>=(Rational);
bool operator<=(Rational);
// Unary operator
Rational operator-();
};
int main()
{
// Preparing the numerator of the first rational number
int ratNum1 = 0;
cout << "Enter the denominator of the first rational number: ";
cin >> ratNum1;
// Preparing the denominator of the first rational number
int ratDen1 = 0;
cout << "Enter the numerator of the first rational number: ";
cin >> ratDen1;
// Create our first rational number
Rational rat1;
rat1.setNumerator(ratNum1);
rat1.setDenominator(ratDen1);
// Preparing the numerator of the second rational number
int ratNum2 = 0;
cout << "Enter the denominator of the second rational number: ";
cin >> ratNum2;
// Preparing the denominator of the second rational number
int ratDen2 = 0;
cout << "Enter the numerator of the second rational number: ";
cin >> ratDen2;
// Create our second rational number
Rational rat2;
rat2.setNumerator(ratNum2);
rat2.setDenominator(ratDen2);
// Sample arithmetic operation
Rational rat3 = rat1 + rat2;
cout << rat1.getNumerator() << "/" << rat1.getDenominator() << " + ";
cout << rat2.getNumerator() << "/" << rat2.getDenominator();
cout << " = " << rat3.getNumerator() << "/" << rat3.getDenominator() << endl;
// Sample simplification
cout << "Simplified " << rat3.getNumerator() << "/" << rat3.getDenominator() << " is ";
rat3.simplify();
cout << rat3.getNumerator() << "/" << rat3.getDenominator() << endl;
// Sample comparison operation
if(rat1 > rat2)
{
cout << rat1.getNumerator() << "/" << rat1.getDenominator() << " is bigger than " << rat2.getNumerator() << "/" << rat2.getDenominator() << endl;
}
else
{
cout << rat1.getNumerator() << "/" << rat1.getDenominator() << " is NOT bigger than " << rat2.getNumerator() << "/" << rat2.getDenominator() << endl;
}
system("PAUSE");
return 0;
}
void Rational::setNumerator(int n)
{
num = n;
}
void Rational::setDenominator(int n)
{
den = n;
}
int Rational::getNumerator()
{
return num;
}
int Rational::getDenominator()
{
return den;
}
// Greatest common divisor
int Rational::gcd()
{
int a = num;
int b = den;
int tmp;
// While b is not 0
while (b)
{
tmp = b;
b = a % b;
a = tmp;
}
return a;
}
// Simplifies the fraction
void Rational::simplify()
{
// Get the greatest common divisor
int gcdNum = gcd();
// If there is a common divisor, we don't want to divide by 0
if(gcdNum != 0)
{
// Set the new numerator
num = num / gcdNum;
// Set the new denominator
den = den / gcdNum;
}
}
Rational Rational::operator+(Rational ratPassed)
{
// Adding fractions
Rational ratResult;
ratResult.num = num * ratPassed.den + den * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational::operator-(Rational ratPassed)
{
// Subtracting fractions
Rational ratResult;
ratResult.num = num * ratPassed.den - den * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational::operator*(Rational ratPassed)
{
// Multiplying fractions
Rational ratResult;
ratResult.num = num * ratPassed.num;
ratResult.den = den * ratPassed.den;
return ratResult;
}
Rational Rational::operator/(Rational ratPassed)
{
// Dividing fractions
Rational ratResult;
ratResult.num = num * ratPassed.den;
ratResult.den = den * ratPassed.num;
return ratResult;
}
bool Rational::operator==(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (ratPassed.num == num) && (ratPassed.den == den);
}
bool Rational::operator!=(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (ratPassed.num != num) && (ratPassed.den != den);
}
bool Rational::operator>(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (num * ratPassed.den) > (ratPassed.num * den);
}
bool Rational::operator<(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (num * ratPassed.den) < (ratPassed.num * den);
}
bool Rational::operator>=(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (num * ratPassed.den) >= (ratPassed.num * den);
}
bool Rational::operator<=(Rational ratPassed)
{
// Simplify the first rational number
simplify();
// Simplify the second (passed) rational number
ratPassed.simplify();
return (num * ratPassed.den) <= (ratPassed.num * den);
}
Rational Rational::operator-()
{
// Unary operator multiplies the denominator by -1
Rational ratResult;
ratResult.den = den * -1;
return ratResult;
}