第九周实验报告(3)

来源:互联网 发布:噩梦会成真吗 知乎 编辑:程序博客网 时间:2024/05/22 16:49
  1. #include <iostream>      
  2. using namespace std;    
  3. class CFraction    
  4. {    
  5. private:    
  6.     int nume;  // 分子     
  7.     int deno;  // 分母     
  8. public:    
  9.     CFraction(int nu=0,int de=1):nume(nu),deno(de){}    
  10.     void simplify();    
  11.     
  12.     //输入输出的重载      
  13.     friend istream &operator>>(istream &in,CFraction &x);    
  14.     friend ostream &operator<<(ostream &out,CFraction x);    
  15.         
  16.     CFraction operator+(const CFraction &c);  //两个分数相加,结果要化简     
  17.     CFraction operator-(const CFraction &c);  //两个分数相减,结果要化简     
  18.     CFraction operator*(const CFraction &c);  //两个分数相乘,结果要化简     
  19.     CFraction operator/(const CFraction &c);  //两个分数相除,结果要化简     
  20.     CFraction operator+();  //取正一目运算      
  21.     CFraction operator-();  //取反一目运算     
  22.     bool operator>(const CFraction &c);    
  23.     bool operator<(const CFraction &c);    
  24.     bool operator==(const CFraction &c);    
  25.     bool operator!=(const CFraction &c);    
  26.     bool operator>=(const CFraction &c);    
  27.     bool operator<=(const CFraction &c);    
  28. };    
  29.     
  30. // 分数化简      
  31. void CFraction::simplify()    
  32. {    
  33.     int m,n,r;    
  34.     m=abs(deno);    
  35.     n=abs(nume);    
  36.     while(r=m%n)  // 求m,n的最大公约数     
  37.     {    
  38.         m=n;    
  39.         n=r;    
  40.     }    
  41.     deno/=n;     // 化简      
  42.     nume/=n;    
  43.     if (deno<0)  // 将分母转化为正数     
  44.     {    
  45.         deno=-deno;    
  46.         nume=-nume;    
  47.     }    
  48. }    
  49.     
  50. // 重载输入运算符>>      
  51. istream &operator>>(istream &in,CFraction &x)    
  52. {    
  53.     char ch;    
  54.     while(1)    
  55.     {    
  56.         cin>>x.nume>>ch>>x.deno;    
  57.         if (x.deno==0)     
  58.             cerr<<"分母为! 请重新输入\n";    
  59.         else if(ch!='/')    
  60.             cerr<<"格式错误(形如m/n)! 请重新输入\n";    
  61.         else    
  62.             break;    
  63.     }    
  64.     return cin;    
  65. }    
  66.     
  67. // 重载输出运算符<<      
  68. ostream &operator<<(ostream &out,CFraction x)    
  69. {    
  70.     cout<<x.nume<<'/'<<x.deno;    
  71.     return cout;    
  72. }    
  73.     
  74. // 分数相加      
  75. CFraction CFraction::operator+(const CFraction &c)    
  76. {    
  77.     CFraction t;    
  78.     t.nume=nume*c.deno+c.nume*deno;    
  79.     t.deno=deno*c.deno;    
  80.     t.simplify();    
  81.     return t;    
  82. }    
  83.     
  84. // 分数相减      
  85. CFraction CFraction:: operator-(const CFraction &c)    
  86. {    
  87.     CFraction t;    
  88.     t.nume=nume*c.deno-c.nume*deno;    
  89.     t.deno=deno*c.deno;    
  90.     t.simplify();    
  91.     return t;    
  92. }    
  93.     
  94. // 分数相乘      
  95. CFraction CFraction:: operator*(const CFraction &c)    
  96. {    
  97.     CFraction t;    
  98.     t.nume=nume*c.nume;    
  99.     t.deno=deno*c.deno;    
  100.     t.simplify();    
  101.     return t;    
  102. }    
  103.     
  104. // 分数相除      
  105. CFraction CFraction:: operator/(const CFraction &c)    
  106. {    
  107.     CFraction t;    
  108.     if (!c.nume) return *this;   //除法无效(除数为)时,这种情况需要考虑,但这种处理仍不算合理     
  109.     t.nume=nume*c.deno;    
  110.     t.deno=deno*c.nume;    
  111.     t.simplify();    
  112.     return t;    
  113. }    
  114.     
  115. // 分数取正号      
  116. CFraction CFraction:: operator+()    
  117. {    
  118.     return *this;    
  119. }    
  120.     
  121. // 分数取负号      
  122. CFraction CFraction:: operator-()    
  123. {    
  124.     CFraction x;    
  125.     x.nume=-nume;    
  126.     x.deno=deno;    
  127.     return x;         
  128. }    
  129.     
  130. // 分数比较大小      
  131. bool CFraction::operator>(const CFraction &c)    
  132. {    
  133.     int this_nume,c_nume,common_deno;    
  134.     this_nume=nume*c.deno;        // 计算分数通分后的分子,同分母为deno*c.deno     
  135.     c_nume=c.nume*deno;     
  136.     common_deno=deno*c.deno;    
  137.     if ((this_nume-c_nume)*common_deno>0) return true;    
  138.     return false;    
  139. }    
  140.     
  141. // 分数比较大小      
  142. bool CFraction::operator<(const CFraction &c)    
  143. {    
  144.     int this_nume,c_nume,common_deno;    
  145.     this_nume=nume*c.deno;          
  146.     c_nume=c.nume*deno;    
  147.     common_deno=deno*c.deno;    
  148.     if ((this_nume-c_nume)*common_deno<0) return true;     
  149.     return false;    
  150. }    
  151.     
  152. // 分数比较大小      
  153. bool CFraction::operator==(const CFraction &c)    
  154. {    
  155.     if (*this!=c) return false;    
  156.     return true;    
  157. }    
  158.     
  159. // 分数比较大小      
  160. bool CFraction::operator!=(const CFraction &c)    
  161. {    
  162.     if (*this>c || *this<c) return true;    
  163.     return false;    
  164. }    
  165.     
  166. // 分数比较大小      
  167. bool CFraction::operator>=(const CFraction &c)    
  168. {    
  169.     if (*this<c) return false;    
  170.     return true;    
  171. }    
  172.     
  173. // 分数比较大小      
  174. bool CFraction::operator<=(const CFraction &c)    
  175. {    
  176.     if (*this>c) return false;    
  177.     return true;    
  178. }    
  179.     
  180. int main()    
  181. {    
  182.     CFraction x,y,s;    
  183.     cout<<"输入x: ";    
  184.     cin>>x;    
  185.     cout<<"输入y: ";    
  186.     cin>>y;    
  187.     s=+x+y;    
  188.     cout<<"+x+y="<<s<<endl;    
  189.     cout<<"x-y="<<s<<endl;    
  190.     s=x*y;    
  191.     cout<<"x*y="<<s<<endl;    
  192.     s=x/y;    
  193.     cout<<"x/y="<<s<<endl;    
  194.     s=-x+y;    
  195.     cout<<"-x+y="<<s<<endl;    
  196.         
  197.     cout<<x;    
  198.     if (x>y) cout<<"大于";    
  199.     if (x<y) cout<<"小于";    
  200.     if (x==y) cout<<"等于";    
  201.     cout<<y<<endl;    
  202.     system("pause");    
  203.     return 0;    
  204. }   
原创粉丝点击