定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

来源:互联网 发布:c语言中使用thumb指令 编辑:程序博客网 时间:2024/04/28 03:25
  1. /* 
  2. *Copyright (c) 2016,烟台大学计算机学院 
  3. *All rights reserved. 
  4. *文件名称:main.cpp 
  5. *作    者:李德彪
  6. *完成时间:2016年6月15日 
  7. *版 本 号:v1.0 
  8. * 
  9. *问题描述:定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。 
  10. *输入描述:无。 
  11. *程序输出:分数的各种形式。 
  12. */  
  13. #include<iostream>  
  14. using namespace std;  
  15. class CFraction  
  16. {  
  17. private:  
  18.     int nume;//fenzi  
  19.     int deno;//fenmu  
  20. public:  
  21.     CFraction(int a=0,int b=0);  
  22.       
  23.     void show();  
  24.     friend istream &operator>>(istream &in,CFraction &x);  
  25.     friend ostream &operator<<(ostream &out,CFraction x);  
  26.     CFraction operator+(double c);  
  27.     CFraction operator-(double c);  
  28.     CFraction operator*(double c);  
  29.     CFraction operator/(double c);  
  30.     bool  operator>(CFraction &c);  
  31.     bool  operator<(CFraction &c);  
  32.     bool operator==(CFraction &c);  
  33.     bool  operator>=(CFraction &c);  
  34.     bool  operator<=(CFraction &c);  
  35.     bool  operator!=(CFraction &c);  
  36.     CFraction operator+();  
  37.     CFraction operator-();  
  38.     CFraction operator~();  
  39. };  
  40. istream &operator>>(istream &in,CFraction &x)  
  41. {  
  42.     char c;  
  43.     for(;;)  
  44.     {  
  45.         cin>>x.nume>>c>>x.deno;  
  46.         if (x.deno==0)  
  47.             cerr<<"分母为0, 请重新输入\n";  
  48.         else if(c!='/')  
  49.             cerr<<"格式错误(形如m/n)! 请重新输入\n";  
  50.         else  
  51.             break;  
  52.     }  
  53.     return cin;  
  54. }  
  55. ostream &operator<<(ostream &out,CFraction x)  
  56. {  
  57.     cout<<x.nume<<'/'<<x.deno;  
  58.     return cout;  
  59. }  
  60. CFraction CFraction::operator~()  
  61. {  
  62.     int a=nume,b=deno,t;  
  63.     t=a;  
  64.     a=b;  
  65.     b=t;  
  66.     CFraction c(a,b);  
  67.     return c;  
  68. }  
  69. CFraction CFraction::operator+()  
  70. {  
  71.     return *this;  
  72. }  
  73. CFraction CFraction::operator-()  
  74. {  
  75.     int mu,zi;  
  76.     mu=deno;  
  77.     zi=-nume;  
  78.     CFraction t(zi,mu);  
  79.     return t;  
  80. }  
  81. bool  CFraction::operator>(CFraction &c)  
  82. {  
  83.     int mu,zi1,zi2;  
  84.     mu=deno*c.deno;  
  85.     zi1=nume*c.deno;  
  86.     zi2=c.nume*deno;  
  87.     if(zi1>zi2)  
  88.         return true;  
  89.     else   
  90.         return false;  
  91.   
  92.           
  93. }  
  94. bool CFraction::operator<(CFraction &c)  
  95. {  
  96.     int mu,zi1,zi2;  
  97.     mu=deno*c.deno;  
  98.     zi1=nume*c.deno;  
  99.     zi2=c.nume*deno;  
  100.     if(zi1< zi2)  
  101.         return true;  
  102.     else   
  103.         return false;  
  104.   
  105.           
  106. }  
  107. bool CFraction::operator==(CFraction &c)  
  108. {  
  109.     int mu,zi1,zi2;  
  110.     mu=deno*c.deno;  
  111.     zi1=nume*c.deno;  
  112.     zi2=c.nume*deno;  
  113.     if(zi1==zi2)  
  114.         return true;  
  115.     else   
  116.         return false;  
  117.   
  118.           
  119. }  
  120. bool  CFraction::operator<=(CFraction &c)  
  121. {  
  122.     int mu,zi1,zi2;  
  123.     mu=deno*c.deno;  
  124.     zi1=nume*c.deno;  
  125.     zi2=c.nume*deno;  
  126.     if(zi1<=zi2)  
  127.         return true;  
  128.     else   
  129.         return false;  
  130.   
  131.           
  132. }  
  133. bool  CFraction::operator>=(CFraction &c)  
  134. {  
  135.     int mu,zi1,zi2;  
  136.     mu=deno*c.deno;  
  137.     zi1=nume*c.deno;  
  138.     zi2=c.nume*deno;  
  139.     if(zi1>=zi2)  
  140.         return true;  
  141.     else   
  142.         return false;         
  143. }  
  144. bool  CFraction::operator!=(CFraction &c)  
  145. {  
  146.     int mu,zi1,zi2;  
  147.     mu=deno*c.deno;  
  148.     zi1=nume*c.deno;  
  149.     zi2=c.nume*deno;  
  150.     if(zi1!=zi2)  
  151.         return true;  
  152.     else   
  153.         return false;         
  154. }  
  155. CFraction::CFraction(int a,int b)  
  156. {  
  157.     nume=a;  
  158.     deno=b;  
  159. }  
  160. CFraction CFraction::operator+(double c)  
  161. {  
  162.     int zi;  
  163.       
  164.     zi=nume+c*deno;  
  165.     CFraction t(zi,deno);  
  166.     return t;  
  167. }  
  168. CFraction CFraction::operator-(double c)  
  169. {  
  170.         int zi;  
  171.       
  172.     zi=nume-c*deno;  
  173.     CFraction t(zi,deno);  
  174.     return t;  
  175. }  
  176. CFraction CFraction::operator*(double c)  
  177. {  
  178.         int zi;  
  179.       
  180.     zi=nume*c;  
  181.     CFraction t(zi,deno);  
  182.     return t;  
  183. }  
  184. CFraction CFraction::operator/(double c)  
  185. {  
  186.         int mu;  
  187.       
  188.     mu=deno*c;  
  189.     CFraction t(nume,mu);  
  190.     return t;  
  191. }  
  192. void CFraction::show()  
  193. {int t,m,r,n;  
  194.     m=deno;  
  195.     n=nume;  
  196.     if(deno<nume)  
  197.     {  
  198.         t=m;  
  199.         m=n;  
  200.         n=t;  
  201.     }  
  202.       
  203.     while(r=m%n)    
  204.     {  
  205.         m=n;  
  206.         n=r;  
  207.     }  
  208.     deno=deno/n;  
  209.     nume=nume/n;  
  210.     if(deno==1)  
  211.         cout<<nume<<endl;  
  212.     else  
  213.     cout<<nume<<"/"<<deno<<endl;  
  214.   
  215. }  
  216. int main()  
  217. {  
  218.     CFraction  c1(1,2),c2(2,3),c3;  
  219.     c3=c1+2;  
  220.     c3.show();  
  221.     c3=c1-2;  
  222.     c3.show();  
  223.     c3=c1*2;  
  224.     c3.show();  
  225.     c3=c1/2;  
  226.     c3.show();  
  227.     if(c1>c2)  
  228.         cout<<"c1>c2"<<endl;  
  229.     if(c1<c2)  
  230.         cout<<"c1<c2"<<endl;  
  231.     if(c1==c2)  
  232.         cout<<"c1==c2"<<endl;  
  233.     if(c1>=c2)  
  234.         cout<<"c1>=c2"<<endl;  
  235.     if(c1<=c2)  
  236.         cout<<"c1<=c2"<<endl;  
  237.     if(c1!=c2)  
  238.         cout<<"c1!=c2"<<endl;  
  239.   
  240.   
  241.     cout<<"-c1="<<-c1<<endl;  
  242.     cout<<"+c1="<<+c1<<endl;  
  243.     cout<<"c1的倒数: "<<~c1<<endl;  
  244.   
  245.   
  246. return 0;  
  247. }  
0 0
原创粉丝点击