/** Copyright (c) 2013, School of Computer Science, Yantai University * All rights reserved. * file name: text. cpp * Author: Hu Ying * Completion Date: July 15, May 1, 2013 * version: v1.0 ** input Description: none * Problem description: objects in the score class can perform four arithmetic operations with integer values * program output: output score calculation result * Problem Analysis: * Algorithm Design: omitted */# include <iostream> # include <cmath> using namespace std; class CFraction {public: CFraction (int nu = 0, int de = 1): nume (nu), deno (de) {} void simplify (); void display (); friend CFraction operator + (const CFraction & c1, const CFr Action & c2); friend CFraction operator-(const CFraction & c1, const CFraction & c2); friend CFraction operator * (const CFraction & c1, const CFraction & c2 ); friend CFraction operator/(const CFraction & c1, const CFraction & c2); friend CFraction operator + (const CFraction & c, int I); friend CFraction operator + (int I, const CFraction & c); friend CFraction operator-(const CFraction & c, int I); friend CFraction operat Or-(int I, const CFraction & c); friend CFraction operator * (const CFraction & c, int I); friend CFraction operator * (int I, const CFraction & c ); friend CFraction operator/(const CFraction & c, int I); friend CFraction operator/(int I, const CFraction & c); CFraction operator + (); CFraction operator -(); friend bool operator = (const CFraction & c1, const CFraction & c2); friend bool operator! = (Const CFraction & c1, const CFraction & c2); friend bool operator >=( const CFraction & c1, const CFraction & c2 ); friend bool operator <= (const CFraction & c1, const CFraction & c2); friend bool operator> (const CFraction & c1, const CFraction & c2 ); friend bool operator <(const CFraction & c1, const CFraction & c2); friend bool operator = (int I, const CFraction & c ); friend bool operator = (const CFraction & c, int I); frie Nd bool operator! = (Int I, const CFraction & c); friend bool operator! = (Const CFraction & c, int I); friend bool operator >=( int I, const CFraction & c); friend bool operator >=( const CFraction & c, int I); friend bool operator <= (int I, const CFraction & c); friend bool operator <= (const CFraction & c, int I ); friend bool operator> (int I, const CFraction & c); friend bool operator> (const CFraction & c, int I); friend bool operator <(int I, const CFraction & c); friend bool operator <(con St CFraction & c, int I); private: int nume; // int deno; // denominator}; void CFraction: simplify () {int m, n, r; m = fabs (deno); n = fabs (nume); while (r = m % n) {m = n; n = r ;} deno/= n; nume/= n; if (deno <0) {deno =-deno; nume =-nume ;}} void CFraction: display () {cout <nume <"/" <deno <endl;} CFraction operator + (const CFraction & c1, const CFraction & c2) {CFraction t; t. nume = c1.nume * c2.deno + c2.nume * c1.deno; t. deno = c1.deno * c2. Deno; t. simplify (); return t;} CFraction operator-(const CFraction & c1, const CFraction & c2) {CFraction t; t. nume = c1.nume * c2.deno-c2.nume * c1.deno; t. deno = c1.deno * c2.deno; t. simplify (); return t;} CFraction operator * (const CFraction & c1, const CFraction & c2) {CFraction t; t. nume = c1.nume * c2.nume; t. deno = c1.deno * c2.deno; t. simplify (); return t;} CFraction operator/(const CFraction & c1, const CFraction & c2) {C Fraction t; t. nume = c1.nume * c2.deno; t. deno = c1.deno * c2.nume; t. simplify (); return t;} CFraction operator + (const CFraction & c, int I) {CFraction c1 (c. nume + I * c. deno, c. deno); c1.simplify (); return c1;} CFraction operator + (int I, const CFraction & c) {CFraction c1 (c. nume + I * c. deno, c. deno); c1.simplify (); return c1;} CFraction operator-(const CFraction & c, int I) {CFraction c1 (c. nume-I * c. deno, c. deno); c1.simplify (); Return c1;} CFraction operator-(int I, const CFraction & c) {CFraction c1 (I * c. deno-c.nume, c. deno); c1.simplify (); return c1;} CFraction operator * (const CFraction & c, int I) {CFraction c1 (c. nume * I, c. deno); c1.simplify (); return c1;} CFraction operator * (int I, const CFraction & c) {CFraction c1 (I * c. nume, c. deno); c1.simplify (); return c1;} CFraction operator/(const CFraction & c, int I) {CFraction c1 (c. nume/I, c. Deno); c1.simplify (); return c1;} CFraction operator/(int I, const CFraction & c) {CFraction c1 (I * c. deno, c. nume); c1.simplify (); return c1;} CFraction: operator + () {return * this;} CFraction: operator-() {CFraction c; c. nume =-nume; c. deno =-deno; return c;} bool operator = (const CFraction & c1, const CFraction & c2) {if (c1! = C2) return false; return true;} bool operator! = (Const CFraction & c1, const CFraction & c2) {if (c1> c2 | c1 <c2) return true; return false ;} bool operator >=( const CFraction & c1, const CFraction & c2) {if (c1 <c2) return false; return true;} bool operator <= (const CFraction & c1, const CFraction & c2) {if (c1> c2) return false; return true;} bool operator> (const CFraction & c1, const CFraction & c2) {int c1_nume, c2_nume, common_deno; c1_nume = c1.nume * c2.deno; c2_nume = c2.n Ume * c1.deno; common_deno = c1.deno * c2.deno; if (c1_nume-c2_nume) * common_deno> 0) return true; return false;} bool operator <(const CFraction & c1, const CFraction & c2) {int c1_nume, c2_nume, numeric; c1_nume = c1.nume * c2.deno; c2_nume = c2.nume * c1.deno; Numeric = c1.deno * c2.deno; if (c1_nume-c2_nume) * common_deno <0) return true; return false;} bool operator = (int I, const CFraction & c) {return (I * c. deno) = c. nu Me;} bool operator = (const CFraction & c, int I) {return c. nume = (I * c. deno);} bool operator! = (Int I, const CFraction & c) {return (I * c. deno )! = C. nume;} bool operator! = (Const CFraction & c, int I) {return c. nume! = (I * c. deno);} bool operator >=( int I, const CFraction & c) {return! (I <c);} bool operator >=( const CFraction & c, int I) {return! (C <I);} bool operator <= (int I, const CFraction & c) {return! (I> c);} bool operator <= (const CFraction & c, int I) {return! (C> I);} bool operator> (int I, const CFraction & c) {if (c. deno> 0) return (I * c. deno)> c. nume; else return (I * c. deno) <c. nume;} bool operator> (const CFraction & c, int I) {if (c. deno> 0) return c. nume> (I * c. deno); else return c. nume <(I * c. deno);} bool operator <(int I, const CFraction & c) {if (c. deno> 0) return (I * c. deno) <c. nume; else return (I * c. deno)> c. nume;} bool operator <(const CFraction & c, int I) {if (c. deno> 0) return c. nume <(I * c. deno); else return c. nume> (I * c. deno);} int main () {CFraction x (1, 3), y (-5, 10), s; cout <"score: * = 1/3 y =-5/10 "<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; CFraction c1 (5, 3), c2; // we recommend that you complete the test in the debugging environment, c2 = c1 + 5; c2 = 5 + c1; c2 = C1-5; c2 = 5-c1; c2 = c1 * 5; c2 = 5 * c1; c2 = c1/5; c2 = 5/c1; bool B; B = (c1> 2); B = (2> c1); return 0 ;}
Running result: