C++實踐分?jǐn)?shù)類中運算符重載的方法參考
【項目-分?jǐn)?shù)類中的運算符重載】
(1)實現(xiàn)分?jǐn)?shù)類中的運算符重載,在分?jǐn)?shù)類中可以完成分?jǐn)?shù)的加減乘除(運算后再化簡)、比較(6種關(guān)系)的運算。
class CFraction { private: int nume; // 分子 int deno; // 分母 public: //構(gòu)造函數(shù)及運算符重載的函數(shù)聲明 }; //重載函數(shù)的實現(xiàn)及用于測試的main()函數(shù)
(2)在(1)的基礎(chǔ)上,實現(xiàn)分?jǐn)?shù)類中的對象和整型數(shù)的四則運算。分?jǐn)?shù)類中的對象可以和整型數(shù)進(jìn)行四則運算,且運算符合交換律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同樣,可以完成i+a, 45+a, a*27, 5/a等各種運算。
(3)定義分?jǐn)?shù)的一目運算+和-,分別代表分?jǐn)?shù)取正和求反,將“按位取反運算符”~重載為分?jǐn)?shù)的求倒數(shù)運算。
(4)定義分?jǐn)?shù)類中<<和>>運算符重載,實現(xiàn)分?jǐn)?shù)的輸入輸出,改造原程序中對運算結(jié)果顯示方式,使程序讀起來更自然。
【參考解答】
#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 istream &operator>>(istream &in,CFraction &x); friend ostream &operator<<(ostream &out,CFraction x); CFraction operator+(const CFraction &c); //兩個分?jǐn)?shù)相加,結(jié)果要化簡 CFraction operator-(const CFraction &c); //兩個分?jǐn)?shù)相減,結(jié)果要化簡 CFraction operator*(const CFraction &c); //兩個分?jǐn)?shù)相乘,結(jié)果要化簡 CFraction operator/(const CFraction &c); //兩個分?jǐn)?shù)相除,結(jié)果要化簡 CFraction operator+(); //取正一目運算 CFraction operator-(); //取反一目運算 CFraction operator~(); //取倒數(shù)一目運算 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); }; // 分?jǐn)?shù)化簡 void CFraction::simplify() { int m,n,r; n=fabs(deno); m=fabs(nume); while(r=m%n) // 求m,n的最大公約數(shù) { m=n; n=r; } deno/=n; // 化簡 nume/=n; if (deno<0) // 將分母轉(zhuǎn)化為正數(shù) { deno=-deno; nume=-nume; } } // 重載輸入運算符>> istream &operator>>(istream &in,CFraction &x) { char ch; while(1) { cin>>x.nume>>ch>>x.deno; if (x.deno==0) cerr<<"分母為0, 請重新輸入\n"; else if(ch!='/') cerr<<"格式錯誤(形如m/n)! 請重新輸入\n"; else break; } return cin; } // 重載輸出運算符<< ostream &operator<<(ostream &out,CFraction x) { cout<<x.nume<<'/'<<x.deno; return cout; } // 分?jǐn)?shù)相加 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; } // 分?jǐn)?shù)相減 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; } // 分?jǐn)?shù)相乘 CFraction CFraction:: operator*(const CFraction &c) { CFraction t; t.nume=nume*c.nume; t.deno=deno*c.deno; t.simplify(); return t; } // 分?jǐn)?shù)相除 CFraction CFraction:: operator/(const CFraction &c) { CFraction t; if (!c.nume) return *this; //除法無效(除數(shù)為)時,這種情況需要考慮,但這種處理仍不算合理 t.nume=nume*c.deno; t.deno=deno*c.nume; t.simplify(); return t; } // 分?jǐn)?shù)取正號 CFraction CFraction:: operator+() { return *this; } // 分?jǐn)?shù)取負(fù)號 CFraction CFraction:: operator-() { CFraction x; x.nume=-nume; x.deno=deno; return x; } // 分?jǐn)?shù)取倒數(shù) CFraction CFraction:: operator~() { CFraction x; x.nume=deno; x.deno=nume; //未對原分子為0的情況進(jìn)行處理 if(x.deno<0) //保證負(fù)分?jǐn)?shù)的負(fù)號在分子上 { x.deno=-x.deno; x.nume=-x.nume; } return x; } // 分?jǐn)?shù)比較大小 bool CFraction::operator>(const CFraction &c) { int this_nume,c_nume,common_deno; this_nume=nume*c.deno; // 計算分?jǐn)?shù)通分后的分子,同分母為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; } // 分?jǐn)?shù)比較大小 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; } // 分?jǐn)?shù)比較大小 bool CFraction::operator==(const CFraction &c) { if (*this!=c) return false; return true; } // 分?jǐn)?shù)比較大小 bool CFraction::operator!=(const CFraction &c) { if (*this>c || *this<c) return true; return false; } // 分?jǐn)?shù)比較大小 bool CFraction::operator>=(const CFraction &c) { if (*this<c) return false; return true; } // 分?jǐn)?shù)比較大小 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<<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的倒數(shù): "<<~x<<endl; cout<<x; if (x>y) cout<<"大于"; if (x<y) cout<<"小于"; if (x==y) cout<<"等于"; cout<<y<<endl; return 0; }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
![C++中delete和delete[]的區(qū)別詳細(xì)介紹](http://img.jbzj.com/images/xgimg/bcimg2.png)
C++中delete和delete[]的區(qū)別詳細(xì)介紹

C++ opencv實現(xiàn)的把藍(lán)底照片轉(zhuǎn)化為白底照片功能完整示例

基于Matlab實現(xiàn)離散系統(tǒng)分岔圖的繪制