第5周-任务2-分数类(拓展1)

来源:互联网 发布:淘宝信誉值在哪里看 编辑:程序博客网 时间:2024/05/21 17:46

题目】C++中提供了多种基本的数据类型。实际上,这些远不能满足我们的需求,如复数(第10章的例子大多是处理虚数的),再如分数。本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,二则也为第10章做运算符重载等积累些感性认识。

任务要求:完成下面类的设计,并在main()函数中自行定义对象,调用各成员函数,完成基本的测试。

[cpp] view plaincopyprint?
  1. classCFraction  
  2. {  
  3. private:  
  4.          int nume;  // 分子  
  5.          int deno;  // 分母  
  6.  public:  
  7.          CFraction(int nu=0,int de=1);   //构造函数,初始化用  
  8.          void Set(int nu=0,int de=1);    //置值,改变值时用  
  9.          void input();                                  //按照"nu/de"的格式,如"5/2"的形式输入  
  10.          void Simplify();                    //化简(使分子分母没有公因子)  
  11.          void amplify(int n);                     //放大n倍,如2/3放大5倍为10/3  
  12.          void output(int style=0);           //输出:以8/6为例,style为0时,输出8/6;  
  13.                                                                  //style为1时,输出4/3;  
  14.                                                                  //style为2时,输出1(1/3),表示一又三分之一;  
  15.                                                                  //不给出参数和非1、2,认为是方式0  
  16. };  

【解答过程】请看视频,解答结果可以看拓展1的内容,其中仅有的不同是output参数类型不同。



【拓展1(选做)】:上面output(int style=0)中的输出方式style的类型最适合使用自定义的枚举类(见第7章)了,试着改造一下。

本任务直接讲解引入自定义的枚举类型表示输出方式,体会枚举类型的用处。

需要做的工作有两个方面:

(1)定义枚举类型

[cpp] view plaincopyprint?
  1. enum OutStyle{original, simplified, mixed, approximate};//此处比原题目中多了一种方式:输出近似值  
(2)output类成员函数的声明中,形式参数使用自定义的枚举类型OutStyle

[cpp] view plaincopyprint?
  1. //输出分数:以8/6为例   
  2. //style为original时,原样输出:8/6;   
  3. //style为simplified时,输出化简的形式:4/3;  
  4. //style为mixed时,用带分数形式输出:1(1/3),表示一又三分之一;   
  5. //style为approximate时,输出用实数表示的似近值:1.33333  
  6. //不给出参数时,默认方式为original   
  7. void output(OutStyle style=original);     

【参考解答】

[cpp] view plaincopyprint?
  1. <PRE class=cpp name="code">#include<iostream>  
  2. #include<Cmath>   
  3. using namespace std;  
  4. enum OutStyle{original, simplified, mixed, approximate};  
  5. int gcd(int m, int n);  
  6. class CFraction  
  7. {  
  8. private:  
  9.     int nume;  // 分子  
  10.     int deno;  // 分母  
  11. public:  
  12.     CFraction(int nu=0,int de=1);   //构造函数,初始化用  
  13.     void set(int nu=0,int de=1);    //置值,改变值时用  
  14.     void input();               //按照"nu/de"的格式,如"5/2"的形式输入  
  15.     void simplify();            //化简(使分子分母没有公因子)  
  16.     void amplify(int n);            //放大n倍,如/3放大倍为/3  
  17.     void output(OutStyle style=original);     
  18. };  
  19.   
  20. CFraction::CFraction(int nu,int de) //构造函数,初始化用  
  21. {  
  22.     if (de!=0)  
  23.     {  
  24.         nume=nu;  
  25.         deno=de;  
  26.     }  
  27.     else  
  28.     {  
  29.         cerr<<"初始化中发生错误,程序退出\n";  
  30.         system("pause");  
  31.         exit(0);  
  32.     }  
  33. }  
  34.   
  35. void CFraction::set(int nu,int de)    //置值,改变值时用  
  36. {  
  37.     if (de!=0) //如果不合适,改变值操作无效  
  38.     {  
  39.         nume=nu;  
  40.         deno=de;  
  41.     }  
  42. }  
  43.   
  44. void CFraction::input()             //按照"nu/de"的格式,如"5/2"的形式输入  
  45. {  
  46.     int nu,de;  
  47.     char c;  
  48.     while(1)  
  49.     {  
  50.         cout<<"输入分数(m/n): ";  
  51.         cin>>nu>>c>>de;  
  52.         if(c!='/')  
  53.             cout<<"输入格式错误!\n ";  
  54.         else if (de==0)  
  55.             cout<<"分母不能为零!\n ";  
  56.         else  
  57.             break;  
  58.     }     
  59.     nume=nu;  
  60.     deno=de;  
  61. }  
  62.   
  63. // 分数化简,使分子分母没有公因子   
  64. void CFraction::simplify()  
  65. {  
  66.     int n=gcd(deno, nume);  
  67.     deno/=n;     // 化简   
  68.     nume/=n;  
  69. }  
  70.   
  71. // 求m,n的最大公约数   
  72. int gcd(int m, int n)  
  73. {  
  74.     int r;  
  75.     if (m<n){r=m;m=n;n=r;}  
  76.     while(r=m%n)  // 求m,n的最大公约数  
  77.     {  
  78.         m=n;  
  79.         n=r;  
  80.     }  
  81.     return n;  
  82. }  
  83.   
  84. void CFraction::amplify(int n)  //放大n倍,如/3放大倍为/3  
  85. {  
  86.     nume*=n;  
  87. }  
  88.   
  89. //输出分数:以8/6为例   
  90. //style为original时,原样输出:8/6;   
  91. //style为simplified时,输出化简的形式:8/3;  
  92. //style为mixed时,用带分数形式输出:1(1/3),表示一又三分之一;   
  93. //style为approximate时,输出用实数表示的似近值:1.33333  
  94. //不给出参数时,默认方式为original   
  95. void CFraction::output(OutStyle style)  
  96. {  
  97.     int n,nu,de;  
  98.     switch(style)  
  99.     {  
  100.     case original:  
  101.         cout<<"原样:" <<nume<<'/'<<deno<<endl;   
  102.         break;  
  103.     case simplified:  
  104.         n=gcd(deno, nume);  
  105.         cout<<"化简形式: "<<nume/n<<'/'<<deno/n<<endl;     //输出化简形式,并不是要化简  
  106.         break;  
  107.     case mixed:  
  108.         n=gcd(deno, nume);  
  109.         nu=nume/n;   
  110.         de=deno/n;  
  111.         cout<<"带分数形式:" <<nu/de<<'('<<nu%de<<'/'<<de<<')'<<endl;   
  112.         break;  
  113.     case approximate:  
  114.         cout<<"近似值:" <<nume/double(deno)<<endl;   
  115.         break;  
  116.     default:  
  117.         cout<<"默认原样:" <<nume<<'/'<<deno<<endl;      
  118.     }  
  119. }  
  120.   
  121. int main()  
  122. {  
  123.     CFraction c1,c2(8,6);  
  124.   
  125.     cout<<"关于c1: "<<endl;  
  126.     c1.output(original);  
  127.   
  128.     cout<<"改变c1: "<<endl;  
  129.     c1.set(2,7);  
  130.     c1.output();  
  131.   
  132.     cout<<"输入c1: "<<endl;  
  133.     c1.input();  
  134.     c1.output(original);  
  135.   
  136.     cout<<"关于c2: "<<endl;  
  137.     c2.output(original);  
  138.     c2.output(simplified);   
  139.     c2.output(mixed);   
  140.     c2.output(approximate);  
  141.   
  142.     cout<<"将c2化简: "<<endl;  
  143.     c2.simplify();  
  144.     c2.output(original);  
  145.   
  146.     cout<<"将c2放大倍: "<<endl;  
  147.     c2.amplify(5);  
  148.     c2.output(original);  
  149.     c2.output(mixed);   
  150.   
  151.     system("pause");  
  152.     return 0;  
  153. }  
  154. </PRE>  
  155. <PRE></PRE>  
  156. <P></P>  
  157. <PRE></PRE>  
  158. <P></P>  
  159. <P><STRONG>任务2拓展2(思考):</STRONG>这个思考题吊一下大家的胃口:设定义了两个分数类的对象,如CFraction c1, c2。如果定义了int i,我们能用cin>>i;在键盘上输入i的值,是否期望用cin>>c1>>c2;输入分数呢?同理,用cout<<c1<<c2;进行输出,可以吗?进一步,用c1+c2得到新的一个分数,用c1/c2实现两个分数的除法,以及其他加、减、比较、求倒数等也是理所当然的。实际上,要自定义分数类,这些直观的基本运算应该要实现,这叫运算符的重载。本任务中用amplify()给出了“放大”运算的一种实现方案,更多内容值得期待地。</P>  
  160. <DIV style="TOP: 105px"><SPAN style="FONT-FAMILY: monospace"><SPAN style="WHITE-SPACE: pre"><BR>  
  161. </SPAN></SPAN></DIV>  
  162. <DIV><SPAN style="FONT-FAMILY: monospace; WHITE-SPACE: pre; BACKGROUND-COLOR: rgb(240,240,240)"><BR>  
  163. </SPAN></DIV>  
  164. <PRE></PRE>