On the basis of operator overloading in fractional classes
(1) Define the fraction of the first operation + and-, respectively, to represent the fractional positive and negation, the "bitwise negation operator" ~ Overloading the calculation of the inverse of the fractional.
(2) define the << and >> operator overloads in the fractional class, realize the input and output of the fractions, and transform the display mode of the operation results in the original program, making the program read more naturally.
<p>/* * Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * file name: test.cpp * for & nbsp By: Lenkidong * Completion Date: May 8, 2015 * version:v1.0*/</p><p> #include <iostream> #include <Cmath> using namespace std;class cfraction{private: int nume; //molecule int deno; / /denominator public: cfraction (int nu=0,int de=1): Nume (NU), Deno (DE) {} void simplify (); </p><p> //input/output overloads friend IStream &operator>> (IStream &in,cfraction &x); friend Ostream &operator<< (ostream &out,cfraction x);< /p><p> cfraction operator+ (const cfraction &c); //Two fractions added, results to simplify CFraction operator-(const cfraction &c); //two fractional subtraction, resulting in a reduction cfraction operator* (const CFraction &c); //two fractions multiplied, resulting in simplification cfractionoperator/(const cfraction &c); //Two fractions divide, result to simplify cfraction operator+ (); //Take positive one-mesh operation cfraction operator-(); //Take the inverse one mesh operation cfraction operator~ (); //Take the countdown operation 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);}; </p><p>//Fractional reduction void cfraction::simplify () { int m,n,r; n=fabs (Deno); m=fabs (Nume); while (r=m%n) //M,n Greatest common divisor m=n; n=r; } deno/=n; //simplification nume/=n; if (deno<0) //convert denominator to positive { deno=-deno; nume=-nume; }}</p><p>/ /Overloaded input operator >>istream &operator>> (IStream &in,cfraction &x) { Char ch; while (1) { cin>>x.nume>>ch >>x.deno; if (x.deno==0) cerr<< "Denominator is 0, re-enter \ n"; else if (ch!= '/') cerr<< "format error (shaped like m/n)! Please re-enter \ n "; else break; } return cin;} </p><p>//overloaded output operator <<ostream &operator<< (ostream &out,cfraction x) { cout<<x.nume<< '/' <<x.deno; return cout;} </p><p>//Fractional addition cfraction cfraction::operator+ (const cfraction &c) { CFraction t; t.nume=nume*c.deno+c.nume*deno; t.deno=deno*c.deno; T.simplify (); return t;} </p><p>//Fractional Subtraction cfraction cfraction:: operator-(const cfraction &c) { CFraction t; t.nume=nume*c.deno-c.nume*deno; t.deno=deno*c.deno; T.simplify (); return t;} </p><p>//Fractional multiplication cfraction cfraction:: operator* (const cfraction &c) { CFraction t; t.nume=nume*c.nume; t.deno=deno*c.deno; t.simplify (); return t;} </p><p>//fractions Divide cfraction cfraction:: operator/(const cfraction &c) { cfraction t; if (!c.nume) return *this; //division is invalid (divisor is), this situation needs to be considered, but this processing is still not reasonable t.nume=nume*c.deno; t.deno=deno*c.nume; t.simplify (); return t;} </p><p>//score take plus cfraction cfraction:: operator+ () { return *this;} </p><p>//fractional take minus cfraction CFraction:: operator-() { cfraction x; x.nume=-nume; x.deno=deno; return x;} </p><p>//score Countdown cfraction cfraction:: operator~ () { cfraction x; x.nume=deno; x.deno=nume; //does not handle the case of the original molecule 0 if (x.deno<0) //guaranteed negative fraction of the minus sign on the molecule { x.deno=-x.deno; x.nume=-x.nume; } return x;} </p><p>//fraction comparison size bool Cfraction::operator> (const cfraction &c) { int this_nume,c_nume,common_deno; this_nume=nume*c.deno; //Calculates the numerator after fractional-pass with the same denominator as deno*c.deno c_nume=c.nume*deno; common_deno=deno*c.deno; if (this_nume-c_nume) *common_ deno>0) return true; return false;} </p><p>//fraction Comparison size 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;} </p><p>//fraction Comparison size bool cfraction::operator== (const cfraction &c) { if (*this!=c) return false; return true;} </p><p>//fraction Comparison size bool cfraction::operator!= (const cfraction &c) { if (*this>c | | *this<c) return true; return false;} </p><p>//fraction Comparison size bool cfraction::operator>= (const cfraction &c) { if (*this<c) return false; return true;} </p><p>//fraction Comparison size bool cfraction::operator<= (const cfraction &c) { if (*this>c) return false; return true;} </p><p>int Main () { cfraction x,y,s; cout<< "input x:"; cin>>x; cout<< "Input y:"; cin>>y; s=+x+y; cout<< "+x+y=" <<s<<endl; s=x-y; cout<< "x-y=" <<s<<endl; s=x*y; cout<< "x*y=" < <s<<endl; s=x/y; cout<< "x/y=" <<s<<endl; cout<< "-x=" <<-X≪<endl; cout<< "+x=" <<+x<<endl; cout<< "x countdown:" < <~x<<endl;</p><p> cout<<x; if (x>y) cout<< "Greater than"; if (x<y) cout<< "less than"; if (x==y) cout<< "equals"; cout<<y<<endl; return 0;} </p>
Operation Result:
Learning experience:
Good study Day Day up
Nineth Week item three-operator overloading in fractional classes (cont.)