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

[cpp]  view plain copy
  1. #include <iostream.h>          
  2. class CTime          
  3. {          
  4. private:          
  5.     unsigned short int hour;    // 时          
  6.     unsigned short int minute;  // 分          
  7.     unsigned short int second;  // 秒          
  8. public:          
  9.     CTime(int h=0,int m=0,int s=0);  
  10.     //比较二目运算符的重载          
  11.     bool operator > (CTime &t);          
  12.     bool operator < (CTime &t);          
  13.     bool operator >= (CTime &t);          
  14.     bool operator <= (CTime &t);          
  15.     bool operator == (CTime &t);          
  16.     bool operator != (CTime &t);          
  17.     //二目运算符的重载          
  18.     CTime operator+(CTime &c);  //返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15          
  19.     CTime operator-(CTime &c);//对照+理解          
  20.     CTime operator+(int s);//返回s秒后的时间          
  21.     CTime operator-(int s);//返回s秒前的时间          
  22.     //一目运算符的重载          
  23.     CTime operator++(int);//后置++,下一秒          
  24.     CTime operator++();//前置++,下一秒          
  25.     CTime operator--(int);//后置--,前一秒          
  26.     CTime operator--();//前置--,前一秒          
  27.     //赋值运算符的重载               
  28.     CTime operator+=(CTime &c);          
  29.     CTime operator-=(CTime &c);          
  30.     CTime operator+=(int s);//返回s秒后的时间          
  31.     CTime operator-=(int s);//返回s秒前的时间     
  32.     //重载流插入运算符“<<”    
  33.     friend ostream& operator << (ostream&,CTime&);    
  34.     //重载流提取运算符“>>”    
  35.     friend istream& operator >> (istream&,CTime&);    
  36. };    
  37. CTime::CTime(int h,int m,int s):hour(h),minute(m),second(s){}          
  38.   
  39. //下面实现所有的运算符重载代码  
  40. //比较二目运算符的重载             
  41. ostream &operator << (ostream &output,CTime &c)    
  42. {     
  43.     output<<c.hour<<":"<<c.minute <<":"<<c.second <<endl;     
  44.     return output;    
  45. }  
  46.        
  47. istream &operator >> (istream &input,CTime &c)    
  48. {     
  49.     input>>c.hour>>c.minute>>c.second;    
  50.     return input;    
  51. }    
  52.   
  53. bool CTime::operator > (CTime &t)          
  54. {           
  55.     if (hour>t.hour) return true;      
  56.     if (hour<t.hour) return false;      
  57.     if (minute>t.minute) return true;      
  58.     if (minute<t.minute) return false;      
  59.     if (second>t.second) return true;      
  60.     return false;      
  61. }  
  62.           
  63. bool CTime::operator < (CTime &t)          
  64. {          
  65.     if(hour<t.hour)          
  66.         return true;          
  67.     else if(hour>t.hour)          
  68.         return false;          
  69.     else if(minute<t.minute )          
  70.         return true;          
  71.     else if(minute>t.minute )          
  72.         return false;          
  73.     else if(second<t.second )          
  74.         return true;          
  75.     else if(second>t.second )          
  76.         return false;          
  77.     return false;          
  78. }  
  79.           
  80. bool CTime::operator >= (CTime &t)          
  81. {          
  82.     CTime t1;          
  83.     t1.hour =hour;          
  84.     t1.minute =minute ;          
  85.     t1.second =second ;          
  86.     if (t1<t)          
  87.         return false;          
  88.     return true;          
  89. }  
  90.           
  91. bool CTime::operator <= (CTime &t)          
  92. {          
  93.     CTime t1;          
  94.     t1.hour =hour;          
  95.     t1.minute =minute ;          
  96.     t1.second =second ;          
  97.     if (t1>t)          
  98.         return false;          
  99.     return true;          
  100. }  
  101.           
  102. bool CTime::operator == (CTime &t)          
  103. {          
  104.     CTime t1;          
  105.     t1.hour =hour;          
  106.     t1.minute =minute ;          
  107.     t1.second =second ;          
  108.     if (t1>t)          
  109.         return false;          
  110.     if (t1<t)          
  111.         return false;          
  112.     return true;          
  113. }          
  114.   
  115. bool CTime::operator != (CTime &t)          
  116. {          
  117.     CTime t1;          
  118.     t1.hour =hour;          
  119.     t1.minute =minute ;          
  120.     t1.second =second ;          
  121.     if(t1==t)          
  122.         return false;          
  123.     return true;          
  124. }          
  125. //二目运算符的重载          
  126. CTime CTime::operator+(CTime &c)//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15          
  127. {          
  128.     CTime c2;        
  129.     int time;        
  130.     time=(hour*3600+minute*60+second)+(c.hour*3600+c.minute*60+c.second);        
  131.     c2.hour=time/3600;        
  132.     time=time%3600;        
  133.     c2.minute=time/60;        
  134.     time=time%60;        
  135.     c2.second=time;        
  136.     return c2;        
  137. }  
  138.           
  139. CTime CTime::operator-(CTime &c)//对照+理解          
  140. {          
  141.     CTime c2;         
  142.     int time;          
  143.     time=(hour*3600+minute*60+second)-(c.hour*3600+c.minute*60+c.second);          
  144.     c2.hour=time/3600;          
  145.     time=time%3600;          
  146.     c2.minute=time/60;          
  147.     time=time%60;          
  148.     c2.second=time;          
  149.     return c2;          
  150. }  
  151.           
  152. CTime CTime::operator+(int s)//返回s秒后的时间          
  153. {          
  154.     int time=hour*3600+minute*60+second+s;        
  155.     hour=time/3600;        
  156.     time=time%3600;        
  157.     minute=time/60;        
  158.     time=time%60;        
  159.     second=time;          
  160.     return (*this);      
  161. }  
  162.           
  163. CTime CTime::operator-(int s)//返回s秒前的时间          
  164. {          
  165.     int time=hour*3600+minute*60+second;          
  166.     time=time-s;          
  167.     hour=time/3600;          
  168.     time=time%3600;          
  169.     minute=time/60;        
  170.     time=time%60;         
  171.     second=time;          
  172.     return (*this);  
  173. }   
  174. //一目运算符的重载          
  175. CTime CTime::operator++(int)//后置++,下一秒          
  176. {          
  177.     CTime temp(*this);          
  178.     second++;          
  179.     if(second>=60)          
  180.     {           
  181.         second-=60;          
  182.         ++minute;          
  183.     }          
  184.     return temp;          
  185. }  
  186.           
  187. CTime CTime::operator++()//前置++,下一秒          
  188. {          
  189.     if(++second>=60)          
  190.     {          
  191.         second-=60;          
  192.         ++minute;          
  193.     }          
  194.       
  195.     return *this;          
  196. }  
  197.           
  198. CTime CTime::operator--(int)//后置--,前一秒          
  199. {          
  200.     CTime temp(*this);          
  201.     second--;          
  202.     if(second<0)          
  203.     {           
  204.         second=59;          
  205.         --minute;          
  206.     }          
  207.     return temp;          
  208. }  
  209.           
  210. CTime CTime::operator--()//前置--,前一秒          
  211. {          
  212.     if(--second<0)          
  213.     {          
  214.         second=59;          
  215.         --minute;          
  216.     }          
  217.     return *this;          
  218. }          
  219. //赋值运算符的重载               
  220. CTime CTime::operator+=(CTime &c)          
  221. {          
  222.     CTime t1,t;          
  223.     t1.hour =hour;          
  224.     t1.minute =minute ;          
  225.     t1.second =second ;          
  226.     t=t1+c;          
  227.     hour=t.hour ;          
  228.     minute=t.minute ;          
  229.     second=t.second ;          
  230.     return t;          
  231. }  
  232.           
  233. CTime CTime::operator-=(CTime &c)          
  234. {          
  235.     *this=*this-c;      
  236.     return *this;          
  237.       
  238. }  
  239.           
  240. CTime CTime::operator+=(int s)//返回s秒后的时间          
  241. {          
  242.     CTime t1,t;          
  243.     t1.hour =hour;          
  244.     t1.minute =minute ;          
  245.     t1.second =second ;          
  246.     t=t1+s;          
  247.     hour=t.hour ;          
  248.     minute=t.minute ;          
  249.     second=t.second ;          
  250.     return t;          
  251. }  
  252.           
  253. CTime CTime::operator-=(int s)//返回s秒前的时间          
  254. {          
  255.     *this=*this-s;      
  256.     return *this;      
  257. }  
  258.           
  259. void main()          
  260. {          
  261.     CTime t1(10,15,22),t2(8,24,37),t;          
  262.     int n;     
  263.     cout<<"请输入t1的时间:(以aa bb cc的格式输入)";    
  264.     cin>>t1;    
  265.     cout<<"请输入t2的时间:(以aa bb cc的格式输入)";    
  266.     cin>>t2;    
  267.     cout<<"t1为:";          
  268.     cout<<t1;          
  269.     cout<<"t2为:";          
  270.     cout<<t2;          
  271.     cout<<"比较两个时间大小:\n";          
  272.     if (t1>t2) cout<<"t1>t2"<<endl;          
  273.     if (t1<t2) cout<<"t1<t2"<<endl;          
  274.     if (t1==t2) cout<<"t1=t2"<<endl;           
  275.     if (t1!=t2) cout<<"t1≠t2"<<endl;          
  276.     if (t1>=t2) cout<<"t1≥t2"<<endl;          
  277.     if (t1<=t2) cout<<"t1≤t2"<<endl;          
  278.     cout<<endl;          
  279.     cout<<"t1+t2的时间为:";          
  280.     t=t1+t2;          
  281.     cout<<t;          
  282.     cout<<"t2-t1的时间为:";          
  283.     t=t2-t1;          
  284.     cout<<t;          
  285.     cout<<endl;          
  286.     cout<<"请输入您想增加的秒数:";          
  287.     cin>>n;          
  288.     t=t1+n;          
  289.     cout<<"增加"<<n<<"秒后的时间为:";          
  290.     cout<<t;          
  291.     t=t1-n;          
  292.     cout<<"增加"<<n<<"秒前的时间为:";          
  293.     cout<<t;          
  294.     cout<<endl;          
  295.     cout<<"对t1后置++,结果为:";          
  296.     t=t1++;          
  297.     cout<<t;          
  298.     cout<<"运算结束后,t1的值为:";          
  299.     cout<<t1;          
  300.     cout<<endl;          
  301.     cout<<"对t1前置++,结果为:";          
  302.     t=++t1;          
  303.     cout<<t;          
  304.     cout<<"运算结束后,t1的值为:";          
  305.     cout<<t1;          
  306.     cout<<endl;          
  307.     cout<<"对t1后置--,结果为:";          
  308.     t=t1--;          
  309.     cout<<t;          
  310.     cout<<"运算结束后,t1的值为:";          
  311.     cout<<t1;          
  312.     cout<<endl;          
  313.     cout<<"对t1前置--,结果为:";          
  314.     t=--t1;          
  315.     cout<<t;          
  316.     cout<<"运算结束后,t1的值为:";          
  317.     cout<<t1;          
  318.     cout<<endl;          
  319.     cout<<"运算t1+=t2的结果为:";          
  320.     t1+=t2;          
  321.     cout<<t1;          
  322.     cout<<endl;          
  323.     cout<<"此时t1的值为:";          
  324.     cout<<t1;          
  325.     cout<<"此时t2的值为:";          
  326.     cout<<t2;          
  327.     cout<<endl;          
  328.     cout<<"运算t1-=t2的结果为:";          
  329.     t1-=t2;          
  330.     cout<<t1;          
  331.     cout<<"此时t1的值为:";          
  332.     cout<<t1;          
  333.     cout<<"此时t2的值为:";          
  334.     cout<<t2;          
  335.     cout<<endl;          
  336.     cout<<"请输入您想增加的秒数:";          
  337.     cin>>n;          
  338.     t1+=n;          
  339.     cout<<"t1增加"<<n<<"秒后的时间为:";          
  340.     cout<<t1;          
  341.     cout<<"t2增加"<<n<<"秒前的时间为:";          
  342.     t2-=n;          
  343.     cout<<t2;          
  344.     cout<<endl;              
  345. }
  346.  
  • 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、付费专栏及课程。

余额充值