第八周任务二

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值