第八週專案三 分數類中的運算子過載 2
阿新 • • 發佈:2019-02-07
/* *Copyright (c)2014,煙臺大學計算機與控制工程學院 *All rights reserved. *檔名稱:test.cpp *作 者:徐洪祥 *完成日期:2015年5月9日 *版 本 號:v1.0 * * 問題描述:實現分數類中的物件和整型數的四則運算,且運算符合交換律。 * 輸入描述: * 程式輸出: */ #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(); void display(); friend CFraction operator+(const CFraction &c1,const CFraction &c2); friend CFraction operator+(int c1,const CFraction &c2); friend CFraction operator+(const CFraction &c1,int c2); friend CFraction operator-(const CFraction &c1,const CFraction &c2); friend CFraction operator-(int c1,const CFraction &c2); friend CFraction operator-(const CFraction &c1,int c2); friend CFraction operator*(const CFraction &c1,const CFraction &c2); friend CFraction operator*(int c1,const CFraction &c2); friend CFraction operator*(const CFraction &c1,int c2); friend CFraction operator/(const CFraction &c1,const CFraction &c2); friend CFraction operator/(int c1,const CFraction &c2); friend CFraction operator/(const CFraction &c1,int c2); CFraction operator+(); CFraction operator-(); friend bool operator >(const CFraction &c1,const CFraction &c2); friend bool operator >(int c1,const CFraction &c2); friend bool operator >(const CFraction &c1,int c2); friend bool operator <(const CFraction &c1,const CFraction &c2); friend bool operator <(int c1,const CFraction &c2); friend bool operator <(const CFraction &c1,int c2); friend bool operator==(const CFraction &c1,const CFraction &c2); friend bool operator==(int c1,const CFraction &c2); friend bool operator==(const CFraction &c1,int c2); friend bool operator>=(const CFraction &c1,const CFraction &c2); friend bool operator>=(int c1,const CFraction &c2); friend bool operator>=(const CFraction &c1,int c2); friend bool operator<=(const CFraction &c1,const CFraction &c2); friend bool operator<=(int c1,const CFraction &c2); friend bool operator<=(const CFraction &c1,int c2); friend bool operator!=(const CFraction &c1,const CFraction &c2); friend bool operator!=(int c1,const CFraction &c2); friend bool operator!=(const CFraction &c1,int c2); }; 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+c1.deno*c2.nume; t.deno=c1.deno*c2.deno; t.simplify(); return t; } CFraction operator+(int c1,const CFraction &c2) { CFraction c(c1*c2.deno+c2.nume,c2.deno); c.simplify(); return c; } CFraction operator+(const CFraction &c1,int c2) { CFraction c(c2*c1.deno+c1.nume,c1.deno); c.simplify(); return c; } CFraction operator-(const CFraction &c1,const CFraction &c2) { CFraction t; t.nume=c1.nume*c2.deno-c1.deno*c2.nume; t.deno=c1.deno*c2.deno; t.simplify(); return t; } CFraction operator-(int c1,const CFraction &c2) { CFraction c(c1*c2.deno-c2.nume,c2.deno); c.simplify(); return c; } CFraction operator-(const CFraction &c1,int c2) { CFraction c(c1.nume-c2*c1.deno,c1.deno); c.simplify(); return c; } 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*(int c1,const CFraction &c2) { CFraction c(c1*c2.nume,c2.deno); c.simplify(); return c; } CFraction operator*(const CFraction &c1,int c2) { CFraction c(c2*c1.nume,c1.deno); c.simplify(); return c; } CFraction operator/(const CFraction &c1,const CFraction &c2) { CFraction t; t.nume=c1.nume*c2.deno; t.deno=c1.deno*c2.nume; t.simplify(); return t; } CFraction operator/(int c1,const CFraction &c2) { CFraction c(c1*c2.deno,c2.nume); c.simplify(); return c; } CFraction operator/(const CFraction &c1,int c2) { CFraction c(c1.nume,c1.deno*c2); c.simplify(); return c; } CFraction CFraction:: operator+() { return *this; } CFraction CFraction:: operator-() { CFraction c; c.nume=-nume; c.deno=-deno; return c; } bool operator>(const CFraction &c1, const CFraction &c2) { int c1_nume,c2_nume,common_deno; c1_nume=c1.nume*c2.deno; c2_nume=c1.deno*c2.nume; common_deno=c1.deno*c2.deno; if((c1_nume-c2_nume)*common_deno>0) return true; else return false; } bool operator>(const CFraction &c1, int c2) { if(c1.deno>0) return c1.nume>(c2*c1.deno); else return c1.nume<(c2*c1.deno); } bool operator>(int c1, const CFraction &c2) { if(c2.deno>0) return (c1*c2.deno)>c2.nume; else return (c1*c2.deno)<c2.nume; } bool operator<(const CFraction &c1, const CFraction &c2) { int c1_nume,c2_nume,common_deno; c1_nume=c1.nume*c2.deno; c2_nume=c2.nume*c1.deno; common_deno=c1.deno*c2.deno; if ((c1_nume-c2_nume)*common_deno<0) return true; else return false; } bool operator<(const CFraction &c1, int c2) { if(c1.deno>0) return c1.nume<(c2*c1.deno); else return c1.nume>(c2*c1.deno); } bool operator<(int c1, const CFraction &c2) { if(c2.deno>0) return (c1*c2.deno)<c2.nume; else return (c1*c2.deno)>c2.nume; } bool operator==(const CFraction &c1,const CFraction &c2) { if(c1-c2!=0) return false; else return true; } bool operator==(const CFraction &c1, int c2) { return c1.nume==(c2*c1.deno); } bool operator==(int c1, const CFraction &c2) { return c2.nume==(c1*c2.deno); } bool operator>=(const CFraction &c1,const CFraction &c2) { if(c1<c2) return false; else return true; } bool operator>=(const CFraction &c1, int c2) { return !(c1<c2); } bool operator>=(int c1, const CFraction &c2) { return !(c1<c2); } bool operator<=(const CFraction &c1,const CFraction &c2) { if(c1>c2) return false; else return true; } bool operator<=(const CFraction &c1, int c2) { return !(c1>c2); } bool operator<=(int c1, const CFraction &c2) { return !(c1>c2); } bool operator!=(const CFraction &c1,const CFraction &c2) { if(c1>c2||c1<c2) return true; else return false; } bool operator!=(const CFraction &c1, int c2) { return c1.nume!=(c2*c1.deno); } bool operator!=(int c1, const CFraction &c2) { return c2.nume!=(c1*c2.deno); } int main() { CFraction x(2,5),y(-2,3),z; cout<<"x=2/5,y=-2/3"<<endl; z=x+y; cout<<"x+y="; z.display(); z=x-y; cout<<"x-y="; z.display(); z=x*y; cout<<"x*y="; z.display(); z=x/y; cout<<"x/y="; z.display(); if(x>y) cout<<"x>y"<<endl; if(x<y) cout<<"x<y"<<endl; if(x==y) cout<<"x=y"<<endl; cout<<endl; CFraction c1(5,3),c2; cout<<"c1=5/3"<<endl; c2=c1+5; cout<<"c1+5="; c2.display(); c2=5+c1; cout<<"5+c1="; c2.display(); c2=c1-5; cout<<"c1-5="; c2.display(); c2=5-c1; cout<<"5-c1="; c2.display(); c2=c1*5; cout<<"c1*5="; c2.display(); c2=5*c1; cout<<"5*c1="; c2.display(); c2=c1/5; cout<<"c1/5="; c2.display(); c2=5/c1; cout<<"5/c1="; c2.display(); return 0; }
總結:注意友員函式跟成員函式的區別,取正一目運算、取反一目運算還沒搞懂有啥作用,剛開始做漏洞百出,還有好多地方只是參考老師的答案,沒有真正弄懂,友員函式比成員函式多出好多行程式碼啊。