第九周C++实验报告(三)

[cpp]  view plain copy
  1. #include <iostream>    
  2. using namespace std;   
  3. int gcd(int m, int n);        
  4. class CFraction      
  5. {      
  6. private:      
  7.     int nume;  // 分子      
  8.     int deno;  // 分母      
  9. public:      
  10.     //构造函数及运算符重载的函数声明      
  11.     CFraction(int nu=0,int de=1);   //构造函数,初始化用        
  12.     void Simplify();                //化简(使分子分母没有公因子)          
  13.     void output();                  //输出:以8/6为例,style为0时,输出8/6;        
  14.     bool operator > (CFraction &t);        
  15.     bool operator < (CFraction &t);        
  16.     bool operator >= (CFraction &t);        
  17.     bool operator <= (CFraction &t);        
  18.     bool operator == (CFraction &t);        
  19.     bool operator != (CFraction &t);        
  20.     CFraction operator+(CFraction &c);         
  21.     CFraction operator-(CFraction &c);      
  22.     CFraction operator*(CFraction &c);        
  23.     CFraction operator/(CFraction &c);      
  24.     CFraction operator-();      
  25.     friend ostream &operator << (ostream &,CFraction &);    
  26.     friend istream &operator >> (istream &,CFraction &);  
  27. };  
  28.   
  29. istream &operator >> (istream &input,CFraction &c)    
  30. {     
  31.     input>>c.nume>>c.deno;    
  32.     return input;    
  33. }  
  34.   
  35. ostream &operator << (ostream &output,CFraction &c)    
  36. {    
  37.     output<<c.nume<<'/'<<c.deno<<endl;     
  38.     return output;    
  39. }  
  40.   
  41. CFraction::CFraction(int nu,int de)   //构造函数,初始化用         
  42. {      
  43.     if (de!=0)        
  44.     {        
  45.         nume=nu;        
  46.         deno=de;        
  47.     }        
  48.     else        
  49.     {        
  50.         cerr<<"初始化中发生错误,程序退出\n";            
  51.         exit(0);        
  52.     }        
  53. }      
  54. void CFraction::Simplify()         //化简(使分子分母没有公因子)       
  55. {       
  56.     int n;      
  57.     if(nume<0)        
  58.     {        
  59.         n=gcd(-nume,deno);        
  60.     }        
  61.     else        
  62.     {        
  63.         n=gcd(nume,deno);        
  64.     }  
  65.     nume=nume/n;          
  66.     deno=deno/n;      
  67. }      
  68. // 求m,n的最大公约数        
  69. int gcd(int m, int n)        
  70. {  
  71.     int r;        
  72.     if (m<n){r=m;m=n;n=r;}        
  73.     while(r=m%n)                 // 求m,n的最大公约数        
  74.     {        
  75.         m=n;        
  76.         n=r;        
  77.     }        
  78.     return n;        
  79. }        
  80.   
  81. bool CFraction::operator > (CFraction &t)      
  82. {      
  83.     CFraction c2,c3;      
  84.     c2.nume=nume*t.deno ;      
  85.     c3.nume=t.nume*deno;      
  86.     if(c2.nume>c3.nume )      
  87.         return true;      
  88.     else      
  89.         return false;      
  90. }  
  91.   
  92. bool CFraction::operator < (CFraction &t)       
  93. {      
  94.     CFraction c2,c3;      
  95.     c2.nume=nume*t.deno ;      
  96.     c3.nume=t.nume*deno;      
  97.     if(c2.nume<c3.nume )      
  98.         return true;      
  99.     else      
  100.         return false;      
  101. }  
  102.   
  103. bool CFraction::operator >= (CFraction &t)      
  104. {        
  105.     CFraction c1;        
  106.     c1.nume=nume;      
  107.     c1.deno=deno;      
  108.     if (c1<t)        
  109.         return false;        
  110.     return true;        
  111. }        
  112.   
  113. bool CFraction::operator <= (CFraction &t)      
  114. {        
  115.     CFraction c1;        
  116.     c1.nume=nume;      
  117.     c1.deno=deno;      
  118.     if(c1>t)        
  119.         return false;        
  120.     return true;        
  121. }        
  122. bool CFraction::operator == (CFraction &t)       
  123. {      
  124.     CFraction c1;        
  125.     c1.nume=nume;      
  126.     c1.deno=deno;      
  127.     if (c1<t)        
  128.         return false;        
  129.     if (c1>t)        
  130.         return false;        
  131.     return false;      
  132. }      
  133.   
  134. bool CFraction::operator != (CFraction &t)      
  135. {      
  136.     CFraction c1;        
  137.     c1.nume=nume;      
  138.     c1.deno=deno;      
  139.     if (c1==t)        
  140.         return false;      
  141.     return true;      
  142. }  
  143.   
  144. CFraction CFraction::operator+(CFraction &c)      
  145. {      
  146.     CFraction c2,c3,c4;      
  147.     c2.nume=nume*c.deno ;      
  148.     c3.nume=c.nume *deno;      
  149.     c2.deno=deno*c.deno ;      
  150.     c3.deno=c.deno *deno;      
  151.     c4.nume=c2.nume +c3.nume ;      
  152.     c4.deno=c2.deno ;      
  153.     c4.Simplify ();      
  154.     return c4;      
  155. }  
  156.   
  157. CFraction CFraction::operator-(CFraction &c)      
  158. {      
  159.     CFraction c2,c3,c4;      
  160.     c2.nume=nume*c.deno ;      
  161.     c3.nume=c.nume*deno;      
  162.     c2.deno=deno*c.deno ;      
  163.     c3.deno=c.deno*deno;      
  164.     c4.nume=c2.nume-c3.nume ;      
  165.     c4.deno=c2.deno ;      
  166.     c4.Simplify ();      
  167.     return c4;      
  168. }  
  169.   
  170. CFraction CFraction::operator*(CFraction &c)       
  171. {      
  172.     CFraction c2,c3,c4;      
  173.     c2.nume=nume*c.nume  ;      
  174.     c2.deno=deno*c.deno ;      
  175.     c2.Simplify();      
  176.     return c2;      
  177. }  
  178.   
  179. CFraction CFraction::operator/(CFraction &c)      
  180. {      
  181.     CFraction c2,c3;      
  182.     c2.nume=c.deno ;      
  183.     c2.deno=c.nume ;      
  184.     c3.nume=nume*c2.nume ;      
  185.     c3.deno=deno*c2.deno ;      
  186.     c3.Simplify ();      
  187.     return c3;      
  188. }  
  189.   
  190. CFraction CFraction::operator-()      
  191. {      
  192.     CFraction c2;      
  193.     c2.nume=nume;      
  194.     c2.deno=deno;      
  195.     c2.Simplify ();      
  196.     if(c2.nume<0 || c2.deno<0)      
  197.     {      
  198.         if(c2.nume <0)      
  199.         {      
  200.             c2.nume=-nume;      
  201.         }      
  202.         else      
  203.         {      
  204.             c2.deno=-deno;      
  205.         }      
  206.     }      
  207.     else      
  208.     {      
  209.         c2.nume=-nume;      
  210.         c2.deno=deno;      
  211.     }      
  212.     return c2;      
  213. }      
  214.   
  215. //用于测试的main()函数      
  216. void main()        
  217. {        
  218.     CFraction c1,c2,c;    
  219.     cout<<"请您输入一个分数c1:(以a b的形式输入)";    
  220.     cin>>c1;    
  221.     cout<<"请您输入一个分数c1:(以a b的形式输入)";    
  222.     cin>>c2;    
  223.     cout<<"c1为:";        
  224.     cout<<c1;        
  225.     cout<<"c2为:";        
  226.     cout<<c2;        
  227.     cout<<"下面比较两个时间大小:\n";        
  228.     if (c1>c2) cout<<"c1>c2"<<endl;        
  229.     if (c1<c2) cout<<"c1<c2"<<endl;        
  230.     if (c1==c2) cout<<"c1=c2"<<endl;         
  231.     if (c1!=c2) cout<<"c1≠c2"<<endl;        
  232.     if (c1>=c2) cout<<"c1≥c2"<<endl;        
  233.     if (c1<=c2) cout<<"c1≤c2"<<endl;        
  234.     cout<<endl;        
  235.     cout<<"c1+c2的数值为:";        
  236.     c=c1+c2;        
  237.     cout<<c;      
  238.     cout<<endl;      
  239.     cout<<"c1-c2的数值为:";        
  240.     c=c1-c2;        
  241.     cout<<c;        
  242.     cout<<endl;        
  243.     cout<<"c1*c2的数值为:";        
  244.     c=c1*c2;        
  245.     cout<<c;      
  246.     cout<<endl;      
  247.     cout<<"c1/c2的数值为:";        
  248.     c=c1/c2;        
  249.     cout<<c;       
  250.     cout<<endl;      
  251.     cout<<"对c1取反的结果为:";      
  252.     c=-c1;      
  253.     cout<<c;      
  254.     cout<<endl;         
  255. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了方便,把代码放在Word里面了,每次上机实验的题目代码都在。 第一次: 对如下多项式编写类定义: + + +…+ 其中,n为多项式的次数。完成如下功能: (1) 可存储任意大的多项式(提示:可用动态数组实现)。 (2) 定义构造函数、析构函数、拷贝构造函数。 (3) 包含一个static成员存储定义的多项式的数量。 (4) 定义一个成员函数输出多项式。(可参照-x^4-6x^3+5格式输出) (5) 定义一个成员函数计算多项式的值。 (6) 写main函数测试类的功能。 (7) 采用多文件实现。 考虑:哪些成员函数可以声明为const. 第二次: (8) 重载“+”运算符,实现两个多项式相加。 (9) 重载“-”运算符,实现两个多项式相减。 (10) 重载“*”运算符,实现两个多项式相乘。 (11) 重载“=”运算符,实现两个多项式的赋值运算。 考虑:把其中某个运算符重载为友元函数。 第次: C++的一般编译器都定义和封装了字符串功能,请模仿定义string类的实现,可以实现并支持如下功能: (1)string s = “吉林大学”; (2)string t = s; (3)string m; m = t; (4)m.legnth() 函数测量字符串的长度 (5)m.cat(string const &)连接字符串 第四次: 我公司仪器生产企业目前生产摄像机行车记录仪两种产品,分别销售用户摄像机包含像、图像质量设定、编码算法等属性。 将摄像机增加相应芯片(具有操作菜单、自动拍、车速传感器、源代码等功能)后,形成一个行车记录仪。 要求: 设计摄像机类,并请根据下列不同的功能要求,采用不同的继承方式,设计行车记录仪类,并添加测试代码,体验不同继承方式下的成员访问属性。(类设计时可根据需要自行添加数据成员和其他成员函数。) (1) 行车记录仪的芯片可以使用摄像机像、图像质量设定功能。 行车记录仪用户可以操作行车记录仪的操作菜单和摄像机像功能。 (2)行车记录仪的芯片可以使用摄像机的拍、图像质量设定功能。 行车记录仪用户仅仅可以操作行车记录仪的操作菜单。 (3) 行车记录仪的芯片可以使用摄像机的拍、图像质量设定功能。 行车记录仪用户仅仅可以操作行车记录仪的操作菜单 同时其他公司购买行车记录仪,因该公司也用于销售,不得泄露其全部内容 课后: (1)采用组合方式设计行车记录仪类,增加相应测试代码,体验继承和组合的关系。 (2)分别为继承和组合方式下为各类添加构造函数、析构函数,增加相应测试代码,体验对象的初始化和构造顺序。 (3)将摄像机类和行车记录仪类功能相近的函数(如拍、编码等功能函数)设为同名函数,增加相应测试代码,体验同名函数覆盖。 (4)为我公司建立一个多态的产品类层次结构,使用抽象类,测试时,创建一个基类指针的容器,通过基类指针调用虚函数,体验多态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值