/*************************************************/ // R. A. Hillyard // rational06.cpp // November 2001 // // Complete Rational class /*************************************************/ #include<iostream> using namespace std; class Rational { public : friend Rational operator +(const Rational& f1, const Rational& f2); friend Rational operator -(const Rational& f1, const Rational& f2); friend Rational operator *(const Rational& f1, const Rational& f2); friend Rational operator /(const Rational& f1, const Rational& f2); friend bool operator ==(const Rational& f1, const Rational& f2); friend bool operator !=(const Rational& f1, const Rational& f2); friend bool operator <=(const Rational &f1, const Rational& f2); friend bool operator >=(const Rational &f1, const Rational& f2); friend bool operator <(const Rational &f1, const Rational& f2); friend bool operator >(const Rational &f1, const Rational& f2); friend istream& operator >>(istream& ins, Rational& f1); friend ostream& operator <<(ostream& output, const Rational& f1); friend int gcd(int x, int y); //constructors Rational(); Rational(int top); Rational(int top, int bottom); //accessor functions void output() const; int getTop() const; int getBottom() const; //mutator functions void setTop(int t); void setBottom(int b); private: int top; int bottom; }; /********************************************************/ /********************************************************/ Rational operator +(const Rational& r, const Rational& s) { int factor; int t1 = r.top * s.bottom + s.top * r.bottom; int b1 = r.bottom * s.bottom; factor = gcd(t1,b1); Rational temp(t1/factor,b1/factor); return temp; } /********************************************************/ Rational operator -(const Rational &r, const Rational &s) { int factor; int t1 = r.top * s.bottom - s.top * r.bottom; int b1 = r.bottom * s.bottom; factor = gcd(t1,b1); Rational temp(t1/factor,b1/factor); return temp; } /********************************************************/ Rational operator *(const Rational &r, const Rational &s) { int factor; int t1 = r.top * s.top; int b1 = r.bottom * s.bottom; factor = gcd(t1,b1); Rational temp(t1/factor,b1/factor); return temp; } /********************************************************/ Rational operator /(const Rational &r, const Rational &s) { int factor; int t1 = r.top * s.bottom; int b1 = r.bottom * s.top; factor = gcd(t1,b1); Rational temp(t1/factor,b1/factor); return temp; } /********************************************************/ bool operator ==(const Rational& f1, const Rational& f2) { return((f1.top * f2.bottom) == (f1.bottom * f2.top )); } /********************************************************/ bool operator !=(const Rational& f1, const Rational& f2) { return !(f1==f2); } /********************************************************/ bool operator <=(const Rational &f1, const Rational& f2) { return((f1.top * f2.bottom) <= (f1.bottom * f2.top )); } /********************************************************/ bool operator >=(const Rational &f1, const Rational& f2) { return((f1.top * f2.bottom) >= (f1.bottom * f2.top )); } /********************************************************/ bool operator <(const Rational &f1, const Rational& f2) { return((f1.top * f2.bottom) < (f1.bottom * f2.top )); } /********************************************************/ bool operator >(const Rational &f1, const Rational& f2) { return((f1.top * f2.bottom) > (f1.bottom * f2.top )); } /********************************************************/ ostream& operator <<(ostream& output, const Rational& f1) { output << f1.top << "/" << f1.bottom; return output; } /********************************************************/ istream& operator >>(istream& ins, Rational& f1) { cout << "Enter top and bottom > "; ins >> f1.top >> f1.bottom; return ins; } /********************************************************/ int gcd(int x, int y) { int c; //store remainder while( (c = x%y) != 0 ) { x = y; y = c; }//end while return y; }//end of gcd /********************************************************/ Rational::Rational() { top = 0; bottom = 1; } /********************************************************/ Rational::Rational(int t) { top = t; bottom = 1; } /********************************************************/ Rational::Rational(int t, int b) { top = t; bottom = b; } /********************************************************/ int Rational::getTop() const { return top; } /********************************************************/ int Rational::getBottom() const { return bottom; } /********************************************************/ void Rational::output() const { cout << top << "/" << bottom; } /********************************************************/ void Rational::setTop(int t) { top = t; } /********************************************************/ void Rational::setBottom(int b) { bottom = b; } /********************************************************/ /********************************************************/ int main( void ) { char choice; Rational f1; Rational f2; Rational f3; Rational sentinal(-99, -99); while(f1 != sentinal) { cout << endl; cout << "A to add fractions\n"; cout << "S to subtract fractions\n"; cout << "D to divide fractions\n"; cout << "M to multiply fractions\n"; cout << "C to compare fractions\n"; cout << "Any other char to quit\n"; cin >> choice; if(choice == 'A') { cout << "Enter fractions to add:\n"; cin >> f1 >> f2; cout << f1 << " + " << f2 << " = " << f1+f2 << endl; } else if(choice == 'S') { cout << "Enter fractions to subtract:\n"; cin >> f1 >> f2; cout << f1 << " - " << f2 << " = " << f1-f2 << endl; } else if(choice == 'D') { cout << "Enter fractions to divide:\n"; cin >> f1 >> f2; cout << f1 << " / " << f2 << " = " << f1/f2 << endl; } else if(choice == 'M') { cout << "Enter fractions to multiply:\n"; cin >> f1 >> f2; cout << f1 << " * " << f2 << " = " << f1*f2 << endl; } else if(choice == 'C') { cout << "Enter fractions to compare:\n"; cin >> f1 >> f2; if(f1 == f2) cout << f1 << " and " << f2 << " are equal\n"; if(f1 != f2) cout << f1 << " and " << f2 << " are not equal\n"; if(f1 >= f2) cout << f1 << " is greater or equal to " << f2 << endl; if(f1 > f2) cout << f1 << " is greater than " << f2 << endl; if(f1 <= f2) cout << f1 << " is less or equal to " << f2 << endl; if(f1 < f2) cout << f1 << " is less than " << f2 << endl; } else f1 = Rational(-99, -99); } return 0; } /********************************************************/ /*******************Program Output********************** A to add fractions A to add fractions S to subtract fractions D to divide fractions M to multiply fractions C to compare fractions Any other char to quit A Enter fractions to add: Enter top and bottom > 3 4 Enter top and bottom > 3 8 3/4 + 3/8 = 9/8 A to add fractions S to subtract fractions D to divide fractions M to multiply fractions C to compare fractions Any other char to quit S Enter fractions to subtract: Enter top and bottom > 3 4 Enter top and bottom > 3 8 3/4 - 3/8 = 3/8 A to add fractions S to subtract fractions D to divide fractions M to multiply fractions C to compare fractions Any other char to quit D Enter fractions to divide: Enter top and bottom > 3 4 Enter top and bottom > 3 8 3/4 / 3/8 = 2/1 A to add fractions S to subtract fractions D to divide fractions M to multiply fractions C to compare fractions Any other char to quit A to add fractions S to subtract fractions D to divide fractions M to multiply fractions C to compare fractions Any other char to quit C Enter fractions to compare: Enter top and bottom > 7 8 14 16 Enter top and bottom > 7/8 and 14/16 are equal 7/8 is greater or equal to 14/16 7/8 is less or equal to 14/16 A to add fractions S to subtract fractions D to divide fractions M to multiply fractions C to compare fractions Any other char to quit g */