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

来源:互联网 发布:腾讯全民wifi网络异常 编辑:程序博客网 时间:2024/06/05 03:34
[cpp] view plaincopy
  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.     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. CTime::CTime(int h,int m,int s)    
  37. {    
  38.     hour=h;    
  39.     minute=m;    
  40.     second=s;    
  41. }    
  42. void CTime::setTime(int h,int m,int s)    
  43. {    
  44.     hour=h;    
  45.     minute=m;    
  46.     second=s;    
  47. }    
  48.     
  49. void CTime::display()    
  50. {    
  51.     cout<<hour<<":"<<minute<<":"<<second<<endl;      
  52. }    
  53. //下面实现所有的运算符重载代码。    
  54. //为简化编程,请注意通过调用已有函数,利用好各函数之间的关系    
  55. //比较运算符(二目)的重载    
  56. bool CTime::operator > (CTime &t)    
  57. {    
  58.     if(hour>t.hour)    
  59.         return true;    
  60.     else if(hour<t.hour)    
  61.        return false;    
  62.     else if(minute>t.minute )    
  63.         return true;    
  64.     else if(minute<t.minute )    
  65.         return false;    
  66.     else if(second>t.second )    
  67.         return true;    
  68.     else if(second<t.second )    
  69.         return false;    
  70.     return false;    
  71. }    
  72. bool CTime::operator < (CTime &t)    
  73. {    
  74.     if(hour<t.hour)    
  75.         return true;    
  76.     else if(hour>t.hour)    
  77.         return false;    
  78.     else if(minute<t.minute )    
  79.         return true;    
  80.     else if(minute>t.minute )    
  81.         return false;    
  82.     else if(second<t.second )    
  83.         return true;    
  84.     else if(second>t.second )    
  85.         return false;    
  86.     return false;    
  87. }    
  88. bool CTime::operator >= (CTime &t)    
  89. {    
  90.     CTime t1;    
  91.     t1.hour =hour;    
  92.     t1.minute =minute ;    
  93.     t1.second =second ;    
  94.     if (t1<t)    
  95.         return false;    
  96.     return true;    
  97. }    
  98. bool CTime::operator <= (CTime &t)    
  99. {    
  100.     CTime t1;    
  101.     t1.hour =hour;    
  102.     t1.minute =minute ;    
  103.     t1.second =second ;    
  104.     if (t1>t)    
  105.         return false;    
  106.     return true;    
  107. }    
  108. bool CTime::operator == (CTime &t)    
  109. {    
  110.     CTime t1;    
  111.     t1.hour =hour;    
  112.     t1.minute =minute ;    
  113.     t1.second =second ;    
  114.     if (t1>t)    
  115.         return false;    
  116.     if (t1<t)    
  117.         return false;    
  118.     return true;    
  119. }    
  120.     
  121. bool CTime::operator != (CTime &t)    
  122. {    
  123.    CTime t1;    
  124.    t1.hour =hour;    
  125.     t1.minute =minute ;    
  126.     t1.second =second ;    
  127.     if(t1==t)    
  128.         return false;    
  129.     return true;    
  130. }    
  131. //二目运算符的重载    
  132. CTime CTime::operator+(CTime &c)    
  133. {    
  134.     CTime t;    
  135.     t.hour =hour+c.hour ;    
  136.     t.minute =minute+c.minute ;    
  137.    t.second =second +c.second ;    
  138.     if (t.second >59)            
  139.     {      
  140.        t.minute=(t.minute +t.second/60);    //增加sec/60分钟     
  141.         if (t.minute>59)          
  142.         {      
  143.             t.hour=(t.hour+t.minute/60);      
  144.             if (t.hour>23)      
  145.                 t.hour=(t.hour %24);        
  146.             t.minute=(t.minute %60);      
  147.         }    
  148.         t.second=(t.second %60);           //秒数应该是sec%=60      
  149.     }      
  150.     if (t.minute>59)          
  151.     {      
  152.         t.hour=(t.hour +t.minute/60);      
  153.         if (t.hour>23)      
  154.             t.hour=(t.hour %24);     
  155.         t.minute=(t.minute %60);      
  156.     }    
  157.     if (t.hour>23)      
  158.         t.hour=(t.hour %24);            
  159.        return t;    
  160. }    
  161. CTime CTime::operator-(CTime &c)   
  162. {    
  163.     CTime t;    
  164.         unsigned short int k,h;    
  165.     t.hour =hour;    
  166.     t.minute =minute ;    
  167.     t.second =second ;    
  168.     if(second<c.second )    
  169.     {    
  170.         k=(-(second-c.second) )%60;    
  171.         h=(-(second-c.second ))/60;    
  172.         t.second =(60-k);            
  173.         t.minute =(minute-h-1);                 
  174.         if(t.minute<0)    
  175.         {    
  176.             t.minute =(-t.minute );    
  177.             k=t.minute %60;    
  178.             h=t.minute /60;    
  179.             t.hour=t.hour-h-1;     
  180.             t.minute =(60-k);    
  181.             if(t.hour<0)    
  182.             {    
  183.                 t.hour =(-t.hour );    
  184.                 t.hour =(t.hour %24);    
  185.             }    
  186.         }    
  187.     }    
  188.     else     
  189.     {    
  190.         t.second =second-c.second ;    
  191.    }    
  192.     if(t.minute<c.minute )    
  193.     {    
  194.         h=(-(t.minute -c.minute ))%60;    
  195.         k=(-(t.minute -c.minute ))/60;    
  196.         t.minute =(60-k);    
  197.         t.hour=(t.hour-1-h);                     
  198.         if(t.hour<0)    
  199.        {    
  200.             t.hour =(-t.hour );    
  201.             t.hour =(t.hour %24);    
  202.         }    
  203.     }    
  204.    else    
  205.    {    
  206.         t.minute =(t.minute-c.minute );    
  207.     }    
  208.    if(t.hour<c.hour )    
  209.     {    
  210.         t.hour =(-(t.hour -c.hour ))%24;    
  211.     }    
  212.     else    
  213.     {    
  214.         t.hour=(t.hour -c.hour );    
  215.     }    
  216.         
  217.        return t;    
  218. }    
  219. CTime CTime::operator+(int s)     //返回s秒后的时间    
  220. {    
  221.     CTime t;    
  222.         
  223.     t.hour =hour;    
  224.     t.minute =minute ;    
  225.     t.second =second ;    
  226.     t.second =(t.second +s);    
  227.     if (t.second >59)            
  228.    {      
  229.         t.minute+=t.second/60;    //增加sec/60分钟     
  230.         if (t.minute>59)          
  231.         {      
  232.             t.hour+=t.minute/60;      
  233.             if (t.hour>23)      
  234.                 t.hour%=24;        
  235.             t.minute%=60;      
  236.         }    
  237.         t.second%=60;            //秒数应该是sec%=60      
  238.     }      
  239.    else if (t.minute>59)          
  240.     {      
  241.         t.hour+=t.minute/60;      
  242.         if (t.hour>23)      
  243.             t.hour%=24;     
  244.         t.minute%=60;      
  245.    }    
  246.     else  if (t.hour>23)      
  247.         t.hour%=24;             
  248.        return t;    
  249. }    
  250. CTime CTime::operator-(int s)//返回s秒前的时间    
  251. {    
  252.     CTime t;    
  253.     unsigned short int k,h;    
  254.     t.hour =hour;    
  255.     t.minute =minute ;    
  256.     t.second =second ;    
  257.     if(t.second <s)    
  258.     {    
  259.         k=(-(second-s) )%60;    
  260.         h=(-(second-s ))/60;    
  261.         t.second =(60-k);            
  262.         t.minute =(minute-h-1);                 
  263.         if(t.minute<0)    
  264.         {    
  265.             t.minute =(-t.minute );    
  266.             k=t.minute %60;    
  267.             h=t.minute /60;    
  268.             t.hour=t.hour-h-1;     
  269.             t.minute =(60-k);    
  270.             if(t.hour<0)    
  271.             {    
  272.                 t.hour =(-t.hour );    
  273.                 t.hour =(t.hour %24);    
  274.             }    
  275.         }    
  276.     }    
  277.     else     
  278.     {    
  279.         t.second =second-s;    
  280.     }    
  281.     return t;    
  282.        
  283. }    
  284. //一目运算符的重载    
  285. CTime CTime::operator++(int)    
  286. {    
  287.     CTime temp(*this);    
  288.     second++;    
  289.     if(second>=60)    
  290.     {     
  291.         second-=60;    
  292.         ++minute;    
  293.     }    
  294.     return temp;    
  295. }    
  296. CTime CTime::operator++()  
  297. {    
  298.     if(++second>=60)    
  299.     {    
  300.         second-=60;    
  301.         ++minute;    
  302.     }    
  303.         
  304.     return *this;    
  305. }    
  306. CTime CTime::operator--(int)    
  307. {    
  308.     CTime temp(*this);    
  309.     second--;    
  310.     if(second<0)    
  311.     {     
  312.         second=59;    
  313.         --minute;    
  314.     }    
  315.     return temp;    
  316. }    
  317. CTime CTime::operator--()    
  318. {    
  319.     if(--second<0)    
  320.     {    
  321.         second=59;    
  322.         --minute;    
  323.     }    
  324.     return *this;    
  325. }    
  326. //赋值运算符的重载         
  327. CTime CTime::operator+=(CTime &c)    
  328. {    
  329.     CTime t1,t;    
  330.     t1.hour =hour;    
  331.     t1.minute =minute ;    
  332.     t1.second =second ;    
  333.     t=t1+c;    
  334.     hour=t.hour ;    
  335.     minute=t.minute ;    
  336.     second=t.second ;    
  337.     return t;    
  338. }    
  339. CTime CTime::operator-=(CTime &c)    
  340. {    
  341.    CTime t1,t;    
  342.     t1.hour =hour;    
  343.     t1.minute =minute ;    
  344.     t1.second =second ;    
  345.     t=t1-c;    
  346.     hour=t.hour ;    
  347.     minute=t.minute ;    
  348.     second=t.second ;    
  349.     return t;    
  350.         
  351. }    
  352. CTime CTime::operator+=(int s)//返回s秒后的时间    
  353. {    
  354.     CTime t1,t;    
  355.     t1.hour =hour;    
  356.     t1.minute =minute ;    
  357.     t1.second =second ;    
  358.     t=t1+s;    
  359.     hour=t.hour ;    
  360.     minute=t.minute ;    
  361.    second=t.second ;    
  362.    return t;    
  363. }    
  364. CTime CTime::operator-=(int s)//返回s秒前的时间    
  365. {    
  366.     CTime t1,t;    
  367.     t1.hour =hour;    
  368.     t1.minute =minute ;    
  369.     t1.second =second ;    
  370.     t=t1-s;    
  371.     hour=t.hour ;    
  372.     minute=t.minute ;    
  373.     second=t.second ;    
  374.     return t;    
  375. }    
  376. void main()    
  377. {    
  378.     CTime t1(8,20,25),t2(11,20,50),t;    
  379.     int n;    
  380.     cout<<"t1为:";    
  381.    t1.display();    
  382.     cout<<"t2为:";    
  383.     t2.display();    
  384.     cout<<"下面比较两个时间大小:\n";    
  385.     if (t1>t2) cout<<"t1>t2"<<endl;    
  386.     if (t1<t2) cout<<"t1<t2"<<endl;    
  387.     if (t1==t2) cout<<"t1=t2"<<endl;     
  388.     if (t1!=t2) cout<<"t1≠t2"<<endl;    
  389.     if (t1>=t2) cout<<"t1≥t2"<<endl;    
  390.     if (t1<=t2) cout<<"t1≤t2"<<endl;    
  391.     cout<<endl;    
  392.     cout<<"t1+t2的时间为:";    
  393.     t=t1+t2;    
  394.     t.display();    
  395.     cout<<"t1+t2的时间为:";    
  396.     t=t1-t2;    
  397.     t.display();    
  398.     cout<<endl;    
  399.     cout<<"请输入您想增加的秒数:";    
  400.     cin>>n;    
  401.    t=t1+n;    
  402.     cout<<"增加"<<n<<"秒后的时间为:";    
  403.     t.display ();    
  404.     t=t1-n;    
  405.     cout<<"增加"<<n<<"秒前的时间为:";    
  406.     t.display ();    
  407.     cout<<endl;    
  408.     cout<<"对t1后置++,结果为:";    
  409.     t=t1++;    
  410.     t.display ();    
  411.     cout<<"运算结束后,t1的值为:";    
  412.     t1.display ();    
  413.     cout<<endl;    
  414.     cout<<"对t1前置++,结果为:";    
  415.     t=++t1;    
  416.     t.display ();    
  417.     cout<<"运算结束后,t1的值为:";    
  418.     t1.display ();    
  419.     cout<<endl;    
  420.     cout<<"对t1后置--,结果为:";    
  421.     t=t1--;    
  422.     t.display ();    
  423.     cout<<"运算结束后,t1的值为:";    
  424.     t1.display ();    
  425.     cout<<endl;    
  426.     cout<<"对t1前置--,结果为:";    
  427.     t=--t1;    
  428.     t.display ();    
  429.     cout<<"运算结束后,t1的值为:";    
  430.     t1.display ();    
  431.     cout<<endl;    
  432.     cout<<"运算t1+=t2的结果为:";    
  433.     t1+=t2;    
  434.     t1.display ();    
  435.     cout<<endl;    
  436.     cout<<"此时t1的值为:";    
  437.     t1.display ();    
  438.     cout<<"此时t2的值为:";    
  439.     t2.display ();    
  440.     cout<<endl;    
  441.     cout<<"运算t1-=t2的结果为:";    
  442.    t1-=t2;    
  443.     t1.display ();    
  444.     cout<<"此时t1的值为:";    
  445.     t1.display ();    
  446.     cout<<"此时t2的值为:";    
  447.     t2.display ();    
  448.     cout<<endl;    
  449.     cout<<"请输入您想增加的秒数:";    
  450.     cin>>n;    
  451.     t1+=n;    
  452.     cout<<"t1增加"<<n<<"秒后的时间为:";    
  453.     t1.display ();    
  454.     cout<<"t2增加"<<n<<"秒前的时间为:";    
  455.     t2-=n;    
  456.     t2.display ();    
  457.     cout<<endl;    
  458.     system("PAUSE");    
  459. }    

原创粉丝点击