01./* 02 .* Program Copyright and version description section 03. * copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved. 05. * file name: cfraction. CPP 06. * Author: Zhao guanzhe 07. * Completion Date: April 8, May 7, 2013. * version: V1.0 09. * input Description: 10. * Problem description: 11. */# 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) {}// constructor void simplify (); friend ostream & operator <(ostream &, Cfraction &); // declare the operator ">" to overload friend istream & operator> (istream &, cfraction &); // declare the operator "<" to overload the cfraction operator + (const cfraction & C); // Add the two scores, and the result is reduced to cfraction operator-(const cfraction & C ); // subtract two scores. The result is reduced by cfraction operator * (const cfraction & C). // The result is reduced by cfraction operator/(const cfraction & C ); // divide two scores, and the result is reduced by cfraction operator + (); // calculate cfraction operator-() as the first object; // obtain 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) ;}; void cfraction: simplify () {int m, n, R; M = ABS (deno); // obtain the absolute value of the denominator n = ABS (nume ); // take the absolute value of the numerator while (r = m % N) // evaluate m, the maximum common divisor 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 ;}} ostream & operator <(ostream & output, cfraction & C) {output <C. nume <"/" <C. deno <Endl; return output ;} Istream & operator> (istream & input, cfraction & C) {char ch; while (1) {input> C. nume> CH> C. deno; If (C. deno = 0) cerr <"the denominator cannot be 0. Enter"; else if (Ch! = '/') Cerr <"incorrect input format. Please enter"; else break;} return input;} cfraction: Operator + (const cfraction & C) again) {cfraction t; T. nume = nume * C. deno + C. nume * deno; T. deno = deno * C. deno; T. simplify (); Return t;} cfraction: Operator-(const cfraction & C) {cfraction t; T. nume = nume * C. deno-c.nume x deno; T. deno = deno * C. deno; T. simplify (); Return t;} cfraction: Operator * (const cfraction & C) {cfractio N t; T. nume = 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; T. nume = nume * C. deno; T. deno = deno * C. nume; T. simplify (); Return t;} cfraction: Operator + () {return * This;} cfraction: Operator-() {cfraction X; X. nume =-nume; X. deno =-deno; return X;} 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. deno c_nume = C. nume * deno; common_deno = deno * C. deno; I F (this_nume> c_nume & common_deno> 0 | this_nume <c_nume & common_deno <0) return true; return false;} bool cfraction: Operator <(const cfraction & C) {int this_nume, c_nume, common_deno; this_nume = nume * C. deno; c_nume = C. nume * deno; common_deno = deno * C. deno; If (this_nume-c_nume) * common_deno <0) return true; return false;} bool cfraction: Operator = (const cfraction & C) {If (* This! = C) Return false; return true;} bool cfraction: Operator! = (Const cfraction & C) {If (* This> C | * THIS <c) Return true; return false;} bool cfraction :: operator >=( const cfraction & C) {If (* THIS <c) Return false; return true;} bool cfraction: Operator <= (const cfraction & C) {If (* This> C) return false; return true;} int main () {cfraction X, Y, s; cout <"Enter the score X :"; cin> X; cout <"Enter the score Y:"; CIN> Y; S = x + y; cout <"x + y =" <s; S = x-y; cout <"x-y =" <s; S = x * Y; cout <"x * Y =" <s; S = x/y; cout <"x/y =" <s; S =-x + y; cout <"-x + y =" <s; cout <X; If (x> Y) cout <"greater than"; if (x <Y) cout <"less than"; if (x = y) cout <"equal to"; cout <Y <Endl; return 0 ;}
Running result: