第十一周项目——Time类中的运算符重载

来源:互联网 发布:电脑cpu评测软件 编辑:程序博客网 时间:2024/05/28 11:51
  1. /* 
  2. *Copyright (c) 2015,烟台大学计算机学院 
  3. *All rights reserved. 
  4. *文件名称:text.cpp 
  5. *作者:李德彪
  6. *完成日期:2015年5月16日 
  7. *版本号:v1.0 
  8. * 
  9. *问题描述:  实现time类的运算符重载 
  10. *输入描述: 无 
  11. *程序输出: 求time类运算符重载后的结果 
  12. */  
  13. #include <iostream>  
  14. using namespace std;  
  15. class CTime  
  16. {  
  17. private :  
  18.     unsigned short int hour;  
  19.     unsigned short int minute;  
  20.     unsigned short int second;  
  21. public :  
  22.     CTime (int h=0,int m=0,int s=0);  
  23.     void setTime(int h,int m,int s);  
  24.     void display();  
  25.     bool operator >(CTime &t);  
  26.     bool operator <(CTime &t);  
  27.     bool operator >=(CTime &t);  
  28.     bool operator <=(CTime &t);  
  29.     bool operator ==(CTime &t);  
  30.     bool operator !=(CTime &t);  
  31.     CTime operator +(CTime &t);  
  32.     CTime operator -(CTime &t);  
  33.     CTime operator +(int s);  
  34.     CTime operator -(int s);  
  35.     CTime &operator +=(CTime &c);  
  36.     CTime &operator -=(CTime &c);  
  37.     CTime &operator +=(int s);  
  38.     CTime &operator -=(int s);  
  39.     CTime operator ++(int );  
  40.     CTime operator ++();  
  41.     CTime operator --(int );  
  42.     CTime operator --();  
  43.   
  44. };  
  45. CTime::CTime (int h,int m,int s):hour(h),minute(m),second(s)  
  46. {  
  47.   
  48. }  
  49.     void CTime::setTime(int h,int m,int s)  
  50.     {  
  51.         minute=m;  
  52.         hour=h;  
  53.         second=s;  
  54.     }  
  55.     void CTime::display()  
  56.     {  
  57.         cout<<hour<<":"<<minute<<":"<<second<<endl;  
  58.     }  
  59.     bool CTime::operator >(CTime &t)  
  60.     {  
  61.         if(hour>t.hour)return true;  
  62.         if(hour<t.hour)return false;  
  63.         if(minute>t.minute)return true;  
  64.         if(minute<t.minute)return false;  
  65.         if(second>t.second)return true;  
  66.         if(second<t.second)return false;  
  67.     }  
  68.     bool CTime::operator <(CTime &t)  
  69.     {  
  70.         if(hour>t.hour)return false;  
  71.         if(hour<t.hour)return true;  
  72.         if(minute>t.minute)return false;  
  73.         if(minute<t.minute)return true;  
  74.         if(second>t.second)return false;  
  75.         if(second<t.second)return true;  
  76.     }  
  77.     bool CTime::operator >=(CTime &t)  
  78.     {  
  79.         if(*this<t)return false;  
  80.         return true;  
  81.     }  
  82.     bool CTime::operator <=(CTime &t)  
  83.     {  
  84.         if(*this>t)return false;  
  85.         return true;  
  86.     }  
  87.     bool CTime::operator ==(CTime &t)  
  88.     {  
  89.         if(*this<t||*this>t)return false;  
  90.         return true;  
  91.     }  
  92.     bool CTime::operator !=(CTime &t)  
  93.     {  
  94.         if(*this>t||*this<t)return true;  
  95.         return false;  
  96.     }  
  97.     //当两个不规范的时间相加时可以用此方法  
  98.     CTime CTime::operator +(CTime &t)  
  99.     {  
  100.         int h,m,s;  
  101.         h=hour+t.hour;  
  102.         m=minute+t.minute;  
  103.         s=second+t.second;  
  104.         if(s>=60){  
  105.             s=s%60;  
  106.             m=m+s/60;  
  107.         }  
  108.         if(m>=60){  
  109.             m=m%60;  
  110.             h=h+m/60;  
  111.             }  
  112.         if(h>=24)  
  113.             h=h%24;  
  114.         CTime tt(h,m,s);  
  115.         return tt;  
  116.     }  
  117.     //当两个规范的时间相减时可以用此方法  
  118.     CTime CTime::operator -(CTime &t)  
  119.     {  
  120.         int h,m,s;  
  121.         s=second-t.second;  
  122.         m=minute-t.minute;  
  123.         h=hour-t.hour;  
  124.         if (s<0)  
  125.         {  
  126.             s+=60;  
  127.             m--;  
  128.         }  
  129.         if (m<0)  
  130.         {  
  131.             m+=60;  
  132.             h--;  
  133.         }  
  134.         if (h<0) h+=24;  
  135.         CTime tt(h,m,s);  
  136.         return tt;  
  137.   
  138.     }  
  139.     CTime CTime::operator +(int s)  
  140.     {  
  141.         int ss=s%60;  
  142.         int mm=(s/60)%60;  
  143.         int hh=s/3600;  
  144.         CTime t0(hh,mm,ss);  
  145.         return *this+t0;  
  146.   
  147.     }  
  148.     CTime CTime::operator -(int s)  
  149.     {  
  150.         int ss=s%60;  
  151.         int mm=(s/60)%60;  
  152.         int hh=s/3600;  
  153.         CTime t0(hh,mm,ss);  
  154.         return *this-t0;  
  155.     }  
  156.     CTime &CTime::operator +=(CTime &c)  
  157.     {  
  158.         *this=*this+c;  
  159.         return *this;  
  160.   
  161.     }  
  162.     CTime &CTime::operator -=(CTime &c)  
  163.     {  
  164.         *this=*this-c;  
  165.         return *this;  
  166.   
  167.     }  
  168.     CTime &CTime::operator +=(int s)  
  169.     {  
  170.         *this=*this+s;  
  171.         return *this;  
  172.     }  
  173.     CTime &CTime::operator -=(int s)  
  174.     {  
  175.         *this=*this-s;  
  176.         return *this;  
  177.     }  
  178.     CTime CTime::operator ++(int )  
  179.     {  
  180.         CTime t=*this;  
  181.         *this=*this+1;  
  182.         return t;  
  183.     }  
  184.     CTime CTime::operator ++()  
  185.     {  
  186.         *this=*this+1;  
  187.         return *this;  
  188.     }  
  189.     CTime CTime::operator --(int )  
  190.     {  
  191.         CTime t=*this;  
  192.         *this=*this-1;  
  193.         return t;  
  194.     }  
  195.     CTime CTime::operator --()  
  196.     {  
  197.         *this=*this-1;  
  198.         return *this;  
  199.     }  
  200. int main()  
  201. {  
  202. CTime t1(12,22,31),t2(19,20,55),t;  
  203.     cout<<"t1为:";  
  204.     t1.display();  
  205.     cout<<"t2为:";  
  206.     t2.display();  
  207.     if (t1>t2) cout<<"t1>t2"<<endl;  
  208.     if (t1<t2) cout<<"t1<t2"<<endl;  
  209.     if (t1==t2) cout<<"t1=t2"<<endl;  
  210.     if (t1!=t2) cout<<"t1≠t2"<<endl;  
  211.     if (t1>=t2) cout<<"t1≥t2"<<endl;  
  212.     if (t1<=t2) cout<<"t1≤t2"<<endl;  
  213.     t=t1+t2;  
  214.     t=t1-t2;  
  215.     t=t1+3000;  
  216.     t=t1-7000;  
  217.     t1+=t2;  
  218.     t1-=t2;  
  219.     t1+=3000;  
  220.     t1-=7000;  
  221.     return 0;  
  222. }  
0 0
原创粉丝点击