Time类的运算符重载

来源:互联网 发布:刘国梁离任真相 知乎 编辑:程序博客网 时间:2024/05/14 03:01
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4. class CTime  
  5. {  
  6. private:  
  7.     unsigned short int hour;    // 时  
  8.     unsigned short int minute;  // 分  
  9.     unsigned short int second;  // 秒  
  10. public:  
  11.     CTime(int h=0,int m=0,int s=0);  
  12.     void setTime(int h,int m,int s);  
  13.     void display();  
  14.     //重载输入输出运算符  
  15.     friend istream &operator>>(istream &in,CTime &t);  
  16.     friend ostream &operator<<(ostream &out,CTime t);  
  17.     //二目的比较运算符重载  
  18.     bool operator > (CTime &t);  
  19.     bool operator < (CTime &t);  
  20.     bool operator >= (CTime &t);  
  21.     bool operator <= (CTime &t);  
  22.     bool operator == (CTime &t);  
  23.     bool operator != (CTime &t);  
  24.   
  25.     //二目的加减运算符的重载  
  26.     //返回t规定的时、分、秒后的时间  
  27.     //例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15  
  28.     CTime operator+(CTime &t);  
  29.     CTime operator-(CTime &t);//对照+理解  
  30.     CTime operator+(int s);//返回s秒后的时间  
  31.     CTime operator-(int s);//返回s秒前的时间  
  32.     //二目赋值运算符的重载  
  33.     CTime &operator+=(CTime &c);  
  34.     CTime &operator-=(CTime &c);  
  35.     CTime &operator+=(int s);//返回s秒后的时间  
  36.     CTime &operator-=(int s);//返回s秒前的时间  
  37.     //一目运算符的重载  
  38.     CTime operator++(int);//后置++,下一秒  
  39.     CTime &operator++();//前置++,下一秒  
  40.     CTime operator--( int);//后置--,前一秒  
  41.     CTime &operator--();//前置--,前一秒  
  42. };  
  43. //构造函数  
  44. CTime::CTime(int h,int m,int s)  
  45. {  
  46.         hour=h;  
  47.         minute=m;  
  48.         second=s;  
  49. }  
  50. //设置时间  
  51. void CTime::setTime(int h,int m,int s)  
  52. {  
  53.      hour=h;  
  54.         minute=m;  
  55.         second=s;  
  56. }  
  57.   //二目的比较运算符重载  
  58. bool CTime::operator > (CTime &t)  
  59. {  
  60.     if(hour>t.hour)return true;  
  61.     if(hour<t.hour)return false;  
  62.     if(minute>t.minute)return true;  
  63.     if(minute<t.minute)return false;  
  64.     if(second>t.second)return true;  
  65.     return false;  
  66. }  
  67. bool CTime::operator < (CTime &t)  
  68. {  
  69.     if (hour<t.hour) return true;  
  70.     if (hour>t.hour) return false;  
  71.     if (minute<t.minute) return true;  
  72.     if (minute>t.minute) return false;  
  73.     if (second<t.second) return true;  
  74.     return false;  
  75. }  
  76. bool CTime::operator == (CTime &t)  
  77. {  
  78.     if(*this<t||*this>t)return false;  
  79.     return true;  
  80. }  
  81. bool CTime::operator >= (CTime &t)  
  82. {  
  83.     if(*this<t)return false;  
  84.     return true;  
  85. }  
  86.  bool CTime::operator <= (CTime &t)  
  87.  {  
  88.      if(*this>t)return false;  
  89.     return true;  
  90.  }  
  91.  bool CTime::operator != (CTime &t)  
  92.  {  
  93.      if(*this==t)return false;  
  94.      return true;  
  95.  }  
  96.  //二目的加减运算符的重载  
  97. CTime CTime::operator+(CTime &t)  
  98. {  
  99.     int h,m,s;  
  100.     h=hour+t.hour;  
  101.     m=minute+t.minute;  
  102.     s=second+t.second;  
  103.     if(s>59)  
  104.     {  
  105.         s-=60;  
  106.         m++;  
  107.     }  
  108.     if(m>59)  
  109.     {  
  110.         m-=60;  
  111.         h++;  
  112.     }  
  113.     if(h>23)  
  114.         h-=24;  
  115.     CTime t0(h,m,s);  
  116.     return t0;  
  117. }  
  118. CTime CTime::operator-(CTime &t)//对照+理解  
  119. {  
  120.     int h,m,s;  
  121.     h=hour-t.hour;  
  122.     m=minute-t.minute;  
  123.     s=second-t.second;  
  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)  
  135.         h+=24;  
  136.     CTime t0(h,m,s);  
  137.     return t0;  
  138. }  
  139. CTime CTime::operator+(int s)//返回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. CTime CTime::operator-(int s)//返回s秒前的时间  
  148. {  
  149.     int ss=s%60;  
  150.     int mm=(s/60)%60;  
  151.     int hh=s/3600;  
  152.     CTime t0(hh,mm,ss);  
  153.     return *this-t0;  
  154. }  
  155. CTime CTime::operator++(int )//后置++,下一秒  
  156. {  
  157.     CTime t;  
  158.     t=*this;  
  159.     *this=*this+1;  
  160.     return t;  
  161. }  
  162. CTime &CTime::operator++()//前置++,下一秒  
  163. {  
  164.     *this=*this+1;  
  165.     return *this;  
  166. }  
  167. CTime CTime::operator--( int)//后置--,前一秒  
  168. {  
  169.     CTime t;  
  170.     t=*this;  
  171.     *this=*this-1;  
  172.     return t;  
  173. }  
  174. CTime &CTime::operator--()//前置--,前一秒  
  175. {  
  176.     *this=*this-1;  
  177.     return *this;  
  178. }  
  179. CTime &CTime::operator+=(CTime &c)  
  180. {  
  181.     *this=*this+c;  
  182.     return *this;  
  183. }  
  184. CTime &CTime::operator-=(CTime &c)  
  185. {  
  186.     *this=*this-c;  
  187.     return *this;  
  188. }  
  189. CTime &CTime::operator+=(int s)//返回s秒后的时间  
  190. {  
  191.     *this=*this+s;  
  192.     return *this;  
  193.   
  194. }  
  195. CTime &CTime::operator-=(int s)//返回s秒前的时间  
  196. {  
  197.     *this=*this-s;  
  198.     return *this;  
  199. }  
  200.   
  201. istream &operator>>(istream &in,CTime &t)  
  202. {  
  203.     char ch1,ch2;  
  204.     while(1)  
  205.     {  
  206.         cout<<"请输入时间(hh:mm:ss)"<<endl;  
  207.         cin>>t.hour>>ch1>>t.minute>>ch2>>t.second;  
  208.         if(ch1==':'&&ch2==':')  
  209.             if(t.hour>-1&&t.hour<24&&t.minute>1&&t.minute<60&&t.second>-1&&t.second<60) break;  
  210.         cerr<<"时间格式输入错误!"<<endl;  
  211.   
  212.     }  
  213.     return cin;  
  214. }  
  215. ostream &operator<<(ostream &out,CTime t)  
  216. {  
  217.     cout<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;  
  218.     return out;  
  219. }  
  220. int main()  
  221. {  
  222.     CTime t1,t2,t;  
  223.   
  224.     cout<<"t1为:";  
  225.     cin>>t1;  
  226.     cout<<"t2为:";  
  227.     cin>>t2;  
  228.     cout<<"下面比较两个时间大小:\n";  
  229.     if (t1>t2) cout<<"t1>t2"<<endl;  
  230.     if (t1<t2) cout<<"t1<t2"<<endl;  
  231.     if (t1==t2) cout<<"t1=t2"<<endl;  
  232.     if (t1!=t2) cout<<"t1≠t2"<<endl;  
  233.     if (t1>=t2) cout<<"t1≥t2"<<endl;  
  234.     if (t1<=t2) cout<<"t1≤t2"<<endl;  
  235.     cout<<endl;  
  236.     cout<<"t1= "<<t1<<endl;  
  237.     cout<<"t2= "<<t2<<endl;  
  238.   
  239.     cout<<"t=t1++"<<endl;  
  240.     t=t1++;  
  241.     cout<<"t= "<<t<<"    t1= "<<t1<<endl;  
  242.   
  243.     cout<<"t=++t1"<<endl;  
  244.     t=++t1;  
  245.     cout<<"t= "<<t<<"    t1= "<<t1<<endl;  
  246.   
  247.     cout<<"t1+t2= "<<t1+t2<<endl;  
  248.     cout<<"t1-t2= "<<t1-t2<<endl;  
  249.     cout<<"t1+2000= "<<t1+2000<<endl;  
  250.     cout<<"t1-5000= "<<t1-5000<<endl;  
  251.     return 0;  
  252. }  
0 0
原创粉丝点击