#include #include using namespace std; void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int gcd(int a, int b){ assert (a || b); if ( a < 0 ) a = -a; if ( b < 0 ) b = -b; if ( a < b ) swap(a,b); // Force a >= b. if ( b == 0 ) return a; int r = a%b; if (r == 0) return b; else return gcd(b, r); } struct Fraction{ int n, d; Fraction(int, int); // Constructor. Fraction():n(0),d(1){} // Default Constructor. istream& get(istream&); // Input. ostream& put(ostream&); // Output. }; Fraction::Fraction(int n0, int d0){ assert (d0); if (d0 < 0) { // For definiteness, always store d0 = -d0; // the denominator as positive. n0 = -n0; } int g = gcd(n0,d0); n = n0/g; d = d0/g; } istream& Fraction::get(istream& is){ // Input. int n0,d0; // temporaries. is >> n0; char ch; if (is){ is >> ch; assert(ch == '/'); } if (is) is >> d0; // Enforce format. if (is) { int g = gcd(n0,d0); n = n0/g; d = d0/g; } return is; } ostream& Fraction::put(ostream& os){ // Output. os << n << '/' << d; return os; } Fraction operator+(const Fraction& f, const Fraction& g ){ if ( f.n == 0 ) return g; else if ( g.n == 0 ) return f; else return Fraction( f.n*g.d + f.d*g.n, f.d*g.d ); } Fraction operator-(const Fraction& f){ return Fraction(-f.n, f.d); } Fraction operator-(const Fraction& f, const Fraction& g ){ if ( f.n == 0 ) return -g; else if ( g.n == 0 ) return f; else return Fraction( f.n*g.d - f.d*g.n, f.d*g.d ); } Fraction operator*(const Fraction& f, const Fraction& g ){ return Fraction( f.n*g.n, f.d*g.d ); } Fraction operator/(const Fraction& f, const Fraction& g ){ return Fraction( f.n*g.d, f.d*g.n ); } bool operator==( const Fraction& f, const Fraction& g ){ return (f.n==g.n) && (f.d==g.d); } bool operator!=( const Fraction& f, const Fraction& g ){ return !(f==g); } bool operator<=( const Fraction& f, const Fraction& g ){ return (f.n*g.d <= f.d*g.n); } bool operator>=( const Fraction& f, const Fraction& g ){ return (f.n*g.d >= f.d*g.n); } bool operator>( const Fraction& f, const Fraction& g ){ return (f.n*g.d > f.d*g.n); } bool operator<( const Fraction& f, const Fraction& g ){ return (f.n*g.d < f.d*g.n); } int main(){ // Test driver. Fraction f, g, h; cout << "Enter two fractions, separated by spaces only: "; f.get(cin); g.get(cin); cout << "f = "; f.put(cout); cout << " g = "; g.put(cout); cout << endl; cout << "f + g = "; h = f+g; h.put(cout); cout << endl; cout << "f - g = "; h = f-g; h.put(cout); cout << endl; cout << "f * g = "; h = f*g; h.put(cout); cout << endl; cout << "f / g = "; h = f/g; h.put(cout); cout << endl << endl; if ( f == g ) cout << " f == g.\n"; else if ( f < g ) cout << " f < g.\n"; else if ( f > g ) cout << " f > g.\n"; else cout << "Error in comparing f and g.\n"; return 0; } /* Sample I/O. Enter two fractions, separated by spaces only: 96/15 24/18 f = 32/5 g = 4/3 f + g = 116/15 f - g = 76/15 f * g = 128/15 f / g = 24/5 f > g. Enter two fractions, separated by spaces only: 2/3 7/8 f = 2/3 g = 7/8 f + g = 37/24 f - g = -5/24 f * g = 7/12 f / g = 16/21 f < g. Enter two fractions, separated by spaces only: 6/9 18/27 f = 2/3 g = 2/3 f + g = 4/3 f - g = 0/1 f * g = 4/9 f / g = 1/1 f == g. */