01./* 02.* 程式的著作權和版本聲明部分 03.* Copyright (c)2013, 煙台大學電腦學院學生 04.* All rightsreserved. 05.* 檔案名稱: CFraction.cpp 06.* 作 者:趙冠哲 07.* 完成日期:2013年5月7日 08.* 版本號碼: v1.0 09.* 輸入描述: 10.* 問題描述: 11.*/ #include <iostream>#include<cmath>using namespace std;class CFraction{private: int nume; // 分子 int deno; // 分母public: CFraction(int nu=0,int de=1):nume(nu),deno(de){}//建構函式 void simplify(); friend ostream& operator << (ostream&,CFraction&);//聲明運算子">>"的重載friend istream& operator >> (istream&,CFraction&);//聲明運算子"<<"的重載 CFraction operator+(const CFraction &c); //兩個分數相加,結果要化簡 CFraction operator-(const CFraction &c); //兩個分數相減,結果要化簡 CFraction operator*(const CFraction &c); //兩個分數相乘,結果要化簡 CFraction operator/(const CFraction &c); //兩個分數相除,結果要化簡 CFraction operator+(); //取正一目運算 CFraction operator-(); //取反一目運算 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); //取分母的絕對值 n=abs(nume); //取分子的絕對值 while(r=m%n) // 求m,n的最大公約數 { m=n; n=r; } deno/=n; // 化簡 nume/=n; if (deno<0) // 將分母轉化為正數 { 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<<"分母不能為0,請重新輸入"; else if(ch!='/') cerr<<"輸入的格式錯誤, 請重新輸入"; else break; }return input;}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;}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;}CFraction CFraction:: operator*(const CFraction &c){ CFraction t; t.nume=nume*c.nume; t.deno=deno*c.deno; t.simplify(); return t;}// 分數相除CFraction 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 CFraction:: operator+(){ return *this;}CFraction 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; // 計算分數通分後的分子,同分母為deno*c.deno c_nume=c.nume*deno; common_deno=deno*c.deno; if (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<<"請輸入分數x:"; cin>>x;cout<<"請輸入分數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<<"大於"; if (x<y) cout<<"小於"; if (x==y) cout<<"等於"; cout<<y<<endl; return 0;}
運行結果: