第九周实验报告(2)

  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);    
  11.     void setTime(int h,int m,int s);    
  12.     
  13.     //输入输出运算的重载      
  14.     friend istream &operator>>(istream &in,CTime &t);    
  15.     friend ostream &operator<<(ostream &out,CTime t);     
  16.     //比较运算符(二目)的重载      
  17.     bool operator > (CTime &t);    
  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.     //二目运算符的重载      
  24.     CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15      
  25.     CTime operator-(CTime &c);//对照+理解      
  26.     CTime operator+(int s);//返回s秒后的时间      
  27.     CTime operator-(int s);//返回s秒前的时间      
  28.     //一目运算符的重载      
  29.     CTime operator++(int);//后置++,下一秒      
  30.     CTime operator++();//前置++,下一秒      
  31.     CTime operator--(int);//后置--,前一秒      
  32.     CTime operator--();//前置--,前一秒      
  33.     //赋值运算符的重载        
  34.     CTime operator+=(CTime &c);    
  35.     CTime operator-=(CTime &c);    
  36.     CTime operator+=(int s);//返回s秒后的时间      
  37.     CTime operator-=(int s);//返回s秒前的时间      
  38. };    
  39.     
  40. //构造函数      
  41. CTime::CTime(int h,int m,int s)    
  42. {    
  43.     hour=h;    
  44.     minute=m;    
  45.     second=s;    
  46. }    
  47. // 设置时间      
  48. void CTime::setTime(int h,int m,int s)    
  49. {    
  50.     hour=h;    
  51.     minute=m;    
  52.     second=s;    
  53. }    
  54.     
  55. // 重载输入运算符>>      
  56. istream &operator>>(istream &in,CTime &t)    
  57. {    
  58.     char ch1,ch2;    
  59.     while(1)    
  60.     {    
  61.         cout<<"请输入时间(hh:mm:ss) ";    
  62.         cin>>t.hour>>ch1>>t.minute>>ch2>>t.second;    
  63.         if (ch1==':' && ch2==':')    
  64.             if (t.hour>-1 && t.hour<24 && t.minute>-1 && t.minute<60 && t.second>-1 && t.second<60) break;    
  65.         cerr<<"时间格式不正确! 请重新输入\n";    
  66.     }    
  67.     return cin;    
  68. }    
  69.     
  70. // 重载输出运算符<<      
  71. ostream &operator<<(ostream &out,CTime t)    
  72. {    
  73.     out<<t.hour<<':'<<t.minute<<':'<<t.second;    
  74.     return out;    
  75. }    
  76.     
  77.     
  78. //比较运算符的重载      
  79. bool CTime::operator > (CTime &t) // 判断时间t1>t2      
  80. {    
  81.     if (hour>t.hour) return true;    
  82.     if (hour<t.hour) return false;    
  83.     if (minute>t.minute) return true;    
  84.     if (minute<t.minute) return false;    
  85.     if (second>t.second) return true;    
  86.     return false;    
  87. }    
  88.     
  89. bool CTime::operator < (CTime &t)// 判断时间t1<t2      
  90. {    
  91.     if (hour<t.hour) return true;    
  92.     if (hour>t.hour) return false;    
  93.     if (minute<t.minute) return true;    
  94.     if (minute>t.minute) return false;    
  95.     if (second<t.second) return true;    
  96.     return false;    
  97. }    
  98.     
  99. bool CTime::operator == (CTime &t)// 判断时间t1==t2      
  100. {    
  101.     if (*this<t || *this>t) return false;    
  102.     return true;    
  103. }    
  104.     
  105. bool CTime::operator != (CTime &t) // 判断时间t1!=t2      
  106. {    
  107.     if (*this==t) return false;    
  108.     return true;    
  109. }    
  110.     
  111. bool CTime::operator >= (CTime &t)// 判断时间t1>=t2      
  112. {    
  113.     if (*this<t) return false;    
  114.     return true;    
  115. }    
  116.     
  117. bool CTime::operator <= (CTime &t) // 判断时间t1<=t2      
  118. {    
  119.     if (*this>t) return false;    
  120.     return true;    
  121. }    
  122.     
  123. //二目运算符的重载      
  124. // 计算时间之和, 返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15      
  125. CTime CTime::operator + (CTime &t)    
  126. {    
  127.     
  128.     int h,m,s;    
  129.     s=second+t.second;    
  130.     m=minute+t.minute;    
  131.     h=hour+t.hour;    
  132.     if (s>59)    
  133.     {    
  134.         s-=60;    
  135.         m++;    
  136.     }    
  137.     if (m>59)    
  138.     {    
  139.         m-=60;    
  140.         h++;    
  141.     }    
  142.     while (h>23) h-=24;    
  143.     CTime t0(h,m,s);    
  144.     return t0;    
  145. }    
  146.     
  147. //返回s秒后的时间      
  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.     
  157. // 计算时间之差      
  158. CTime CTime::operator - (CTime &t)    
  159. {    
  160.     int h,m,s;    
  161.     s=second-t.second;    
  162.     m=minute-t.minute;    
  163.     h=hour-t.hour;    
  164.     if (s<0)    
  165.     {    
  166.         s+=60;    
  167.         m--;    
  168.     }    
  169.     if (m<0)    
  170.     {    
  171.         m+=60;    
  172.         h--;    
  173.     }    
  174.     while (h<0) h+=24;    
  175.     CTime t0(h,m,s);    
  176.     return t0;    
  177. }    
  178.     
  179. //返回s秒前的时间      
  180. CTime CTime::operator-(int s)    
  181. {    
  182.     int ss=s%60;    
  183.     int mm=(s/60)%60;    
  184.     int hh=s/3600;    
  185.     CTime t0(hh,mm,ss);    
  186.     return *this-t0;    
  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.     return *this;    
  214. }    
  215.     
  216. //赋值运算符的重载        
  217. CTime CTime::operator+=(CTime &c)    
  218. {    
  219.     *this=*this+c;    
  220.     return *this;    
  221. }    
  222. CTime CTime::operator-=(CTime &c)    
  223. {    
  224.     *this=*this-c;    
  225.     return *this;    
  226. }    
  227. CTime CTime::operator+=(int s)//返回s秒后的时间      
  228. {    
  229.     *this=*this+s;    
  230.     return *this;    
  231. }    
  232. CTime CTime::operator-=(int s)//返回s秒前的时间      
  233. {    
  234.     *this=*this-s;    
  235.     return *this;    
  236. }    
  237.     
  238. int main()    
  239. {    
  240.     CTime t1,t2,t;    
  241.     
  242.     cout<<"t1为:";    
  243.     cin>>t1;    
  244.     cout<<"t2为:";    
  245.     cin>>t2;    
  246.     cout<<"下面比较两个时间大小:\n";    
  247.     if (t1>t2) cout<<"t1>t2"<<endl;    
  248.     if (t1<t2) cout<<"t1<t2"<<endl;    
  249.     if (t1==t2) cout<<"t1=t2"<<endl;     
  250.     if (t1!=t2) cout<<"t1≠t2"<<endl;    
  251.     if (t1>=t2) cout<<"t1≥t2"<<endl;    
  252.     if (t1<=t2) cout<<"t1≤t2"<<endl;    
  253.     cout<<endl;    
  254.     cout<<"t1= "<<t1<<endl;    
  255.     cout<<"t2= "<<t2<<endl;    
  256.     
  257.     cout<<"t=t1++"<<endl;    
  258.     t=t1++;    
  259.     cout<<"t= "<<t<<"    t1= "<<t1<<endl;    
  260.     
  261.     cout<<"t=++t1"<<endl;    
  262.     t=++t1;    
  263.     cout<<"t= "<<t<<"    t1= "<<t1<<endl;    
  264.     
  265.     cout<<"t1+t2= "<<t1+t2<<endl;    
  266.     cout<<"t1-t2= "<<t1-t2<<endl;    
  267.     cout<<"t1+2000= "<<t1+2000<<endl;    
  268.     cout<<"t1-5000= "<<t1-5000<<endl;    
  269.     
  270.     system("pause");    
  271.     return 0;    
  272. }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值