# Include <iostream> # include <Cmath> using namespace std; class CFraction {private: int nume; // int deno; // denominator public: CFraction (int nu = 0, int de = 1): nume (nu), deno (de) {} void simplify (); void display (); CFraction operator + (const CFraction & c ); // Add two scores. The result is reduced by CFraction operator-(const CFraction & c); // The result is reduced by CFraction operator * (const CFraction & c ); // multiply two scores, and the result must be simplified to CFraction operator/(const CFraction & c ); // Divide two scores, and the result is reduced to CFraction operator + (); // calculate CFraction operator-() as the first object -(); // return bool operator> (const CFraction & c); bool operator <(const CFraction & c); bool operator = (const CFraction & c); bool operator! = (Const CFraction & c); bool operator >=( const CFraction & c); bool operator <= (const CFraction & c) ;}; // score reduction void CFraction :: simplify () {int m, n, r; m = fabs (deno); n = fabs (nume ); cout <"=" <m <":" <n; while (r = m % n) // evaluate m, maximum common approx. Of n {m = n; n = r;} deno/= n; // simplify nume/= n; if (deno <0) // convert the denominator to a positive number {deno =-deno; nume =-nume ;}/// display the score void CFraction: display () {cout <"(" <nume <"/" <deno <")" <endl ;}// Add the CFraction: operat Or + (const CFraction & c) {CFraction t; t. nume = nume * c. deno + c. nume * deno; t. deno = deno * c. deno; t. simplify (); return t;} // score subtraction CFraction: operator-(const CFraction & c) {CFraction t; t. nume = nume * c. deno-c.nume * deno; if (t. nume = 0) {cout <"result is 0"; cout <endl; t. nume = 0; t. deno = 0;} else {t. deno = deno * c. deno; t. simplify ();} return t;} // score multiplication CFraction: operator * (const CFraction & c) {CFraction t; t. num E = nume * c. nume; t. deno = deno * c. deno; t. simplify (); return t;} // score division CFraction: operator/(const CFraction & c) {CFraction t; if (! C. nume) return * this; // division is invalid. this case needs to be considered, but this processing is still not reasonable. nume = nume * c. deno; t. deno = deno * c. nume; t. simplify (); return t;} // obtain the positive CFraction: operator + () {return * this;} // obtain the negative CFraction: operator -() {CFraction x; x. nume =-nume; x. deno =-deno; return x;} // score comparison bool CFraction: operator> (const CFraction & c) {int this_nume, c_nume, common_deno; this_nume = nume * c. deno; // the numerator after the score is calculated. The denominator is deno * c. denoc_num E = c. nume * deno; common_deno = deno * c. deno; // if (this_nume> c_nume) return true; unable to cope with common_deno <0 // a more concise way of writing the following statement if (this_nume-c_nume) * common_deno> 0) return true; if (this_nume> c_nume & common_deno> 0) | (this_nume <c_nume & common_deno <0) return true; // compare the numerator after the pass-through to return false;} // compare the score bool CFraction: operator <(const CFraction & c) {int this_nume, c_nume, common_deno; this_nume = nume * c. deno; c_nume = c. nume * d Eno; common_deno = deno * c. deno; if (this_nume-c_nume) * common_deno <0) return true; return false;} // score compare size bool CFraction: operator = (const CFraction & c) {if (* this! = C) return false; return true;} // score comparison bool CFraction: operator! = (Const CFraction & c) {if (* this> c | * this <c) return true; return false ;}// score comparison bool CFraction :: operator >=( const CFraction & c) {if (* this <c) return false; return true;} // compare the score bool CFraction :: operator <= (const CFraction & c) {if (* this> c) return false; return true;} int main () {CFraction x (2, 6), y (3, 9 ), s; cout <"score x = 2/6 y = 3/9" <endl; s = + x + y; cout <"+ x + y ="; s. display (); s = x-y; cout <"x-y ="; s. display (); s = x * y; cout <"x * y ="; s. display (); s = x/y; cout <"x/y ="; s. display (); s =-x + y; cout <"-x + y ="; s. display (); x. display (); if (x> y) cout <"greater than" <endl; if (x <y) cout <"less than" <endl; if (x = y) cout <"=" <endl; y. display (); cout <endl; return 0 ;}
Running result: