2012年 上半年 第九周 C++程序设计 (二十七)
2012-04-21 22:48
453 查看
#include <iostream> 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 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); }; // 分数化简 void CFraction::simplify() { for ( int i = nume; i > 0; i-- ) { if ( nume % i == 0) if ( deno % i == 0 ) { nume /= i; deno /= i; break; } } } ostream& operator<<(ostream &output, CFraction &t) { output << t.nume << "/" << t.deno << endl; return output; } istream& operator>>(istream &input, CFraction &t) { char a,b,c; input >> a >> t.nume >> b >> t.deno >> c; while(1) { if ( a == '(' && b== ',' && c == ')' ) break; else cout << " 输入错误,请重新输入" << endl; } return input; } CFraction CFraction::operator+(const CFraction &c) { CFraction t; t.deno = c.deno * deno; t.nume = deno * c.nume + c.deno * nume; t.simplify(); return t; } CFraction CFraction::operator-(const CFraction &c) { CFraction t; t.deno = c.deno * deno; t.nume = c.deno * nume - deno * c.nume; t.simplify(); return t; } CFraction CFraction::operator*(const CFraction &c) { CFraction t; t.deno = c.deno * deno; t.nume = nume * c.nume; t.simplify(); return t; } CFraction CFraction::operator/(const CFraction &c) { CFraction t; t.deno = c.nume * deno; t.nume = nume * c.deno; t.simplify(); return t; } CFraction CFraction::operator+() { CFraction t; t.deno = deno; t.nume = nume; t.simplify(); return t; } CFraction CFraction::operator-() { CFraction t; t.deno = deno; t.nume = -nume; t.simplify(); return t; } bool CFraction::operator>(const CFraction &c) { int t1_nume,t2_nume; t1_nume = deno * c.nume ; t2_nume = c.deno * nume ; if(t1_nume < t2_nume) return true; else return false; } bool CFraction::operator<(const CFraction &c) { int t1_nume ,t2_nume ; t1_nume = deno * c.nume ; t2_nume = c.deno * nume ; if(t1_nume > t2_nume) return true; else return false; } bool CFraction::operator==(const CFraction &c) { int t1_nume ,t2_nume ; t1_nume = deno * c.nume ; t2_nume = c.deno * nume ; if(t1_nume == t2_nume) return true; else return false; } bool CFraction::operator>=(const CFraction &c) { int t1_nume ,t2_nume ; t1_nume = deno * c.nume ; t2_nume = c.deno * nume ; if(t1_nume < t2_nume ) return true; else return false; } bool CFraction::operator<=(const CFraction &c) { int t1_nume ,t2_nume ; t1_nume = deno * c.nume ; t2_nume = c.deno * nume ; if( t1_nume >= t2_nume) return true; else return false; } int main() { CFraction c1(1,2),c2,t; cout <<"please input c2 (X,X)"<< endl; cin >> c2; t = c1 + c2; cout<<"c1 + c2 = "<<t; t = c1 - c2; cout<<"c1 - c2 = "<<t; t = c1 * c2; cout<<"c1 * c2 = "<<t; t = c1 / c2; cout<<"c1 / c2 = "<<t; cout << "取反" << endl; t = c1; cout<<"t = "<<t; t = -c1; cout<<"-t = "<<t; cout << "比较大小" << endl; if(c1 > c2) cout<< " c1 > c2 " << endl; if(c1 < c2) cout<<" c1 < c2 " << endl; if(c1 == c2) cout<<" c1 == c2 " << endl; if(c1 >= c2) cout<<" c1 >= c2 " << endl; if(c1 <= c2) cout<<" c1 <= c2 " << endl; system("pause"); return 0; }
相关文章推荐
- 2012年 上半年 第九周 C++程序设计 (二十八)
- 2012年 上半年 第九周 C++程序设计 (二十九)
- 2012年 上半年 第九周 C++程序设计 (二十六)
- 2012年 上半年 第九周 C++程序设计 (三十)
- 2012年 上半年 第十三周 C++程序设计 (三十七)
- 2012年 上半年 第十三周 C++程序设计 (四十)
- 2012年 上半年 第三周 C++程序设计 (四)
- 2012年 上半年 第三周 C++程序设计 (七)
- 2012年 上半年 第十四周 C++程序设计 (四十一)
- 2012年 上半年 第五周 C++程序设计 (十一)
- 2012年 上半年 第七周 C++程序设计 (二十二)
- 2012年 上半年 第三周 C++程序设计 (五)
- 2012年 上半年 第三周 C++程序设计 (六)
- 2012年 上半年 第十四周 C++程序设计 (四十二)
- 2012年 上半年 第五周 C++程序设计 (十二)
- 2012年 上半年 第十三周 C++程序设计 (三十八)
- 2012年 上半年 第五周 C++程序设计 (十三)
- 2012年 上半年 第六周 C++程序设计 (十九)
- 2012年 上半年 第五周 C++程序设计 (十四)
- 2012年 上半年 第六周 C++程序设计 (十五)