Time类中的运算符重载

来源:互联网 发布:vscode 修改字体大小 编辑:程序博客网 时间:2024/05/21 21:49
#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.    //二目的比较运算符重载   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.    bool operator != (CTime &t);  32.    //二目的加减运算符的重载   33.    //返回t规定的时、分、秒后的时间   34.    //例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15   35.    CTime operator+(CTime &t);  36.    CTime operator-(CTime &t);//对照+理解   37.    CTime operator+(int s);//返回s秒后的时间   38.    CTime operator-(int s);//返回s秒前的时间   39.    //二目赋值运算符的重载   40.    CTime operator+=(CTime &c);  41.    CTime operator-=(CTime &c);  42.    CTime operator+=(int s);//返回s秒后的时间   43.    CTime operator-=(int s);//返回s秒前的时间   44.};  45.CTime::CTime(int h,int m,int s)  46.{  47.    hour=h;  48.    minute=m;  49.    second=s;  50.}  51.bool CTime::operator > (CTime &t)  52.{  53.    if(hour>t.hour)  54.        return true;  55.    if(hour==t.hour)  56.    {  57.       if(minute>t.minute)  58.          return true;  59.          if(minute==t.minute)  60.          {  61.              if(second>t.second)  62.                return true;  63.          }  64.    }  65.    else  66.        return false;  67.}  68.bool CTime::operator < (CTime &t)  69.{  70.    if(hour<t.hour)  71.        return true;  72.    if(hour==t.hour)  73.    {  74.       if(minute<t.minute)  75.          return true;  76.          if(minute==t.minute)  77.          {  78.              if(second<t.second)  79.                return true;  80.          }  81.    }  82.    else  83.        return false;  84.}  85.bool CTime::operator >= (CTime &t)  86.{  87.    return !(*this<t);  88.}  89.bool CTime::operator <= (CTime &t)  90.{  91.    return !(*this>t);  92.}  93.bool CTime::operator == (CTime &t)  94.{  95.    if(hour==t.hour&&minute==t.minute&&second==t.second)  96.        return true;  97.    else  98.        return false;  99.}  100.bool CTime::operator != (CTime &t)  101.{  102.    if(hour!=t.hour||minute!=t.minute||second!=t.second)  103.        return true;  104.    else  105.        return false;  106.}  107.CTime CTime::operator+(CTime &t)  108.{  109.    CTime tt;  110.    tt.hour=hour+t.hour;  111.    tt.minute=minute+t.minute;  112.    tt.second=second+t.second;  113.    if(tt.second>59)  114.    {  115.        tt.second-=60;  116.        tt.minute++;  117.    }  118.    if(tt.minute>59)  119.    {  120.        tt.minute-=60;  121.        tt.hour++;  122.    }  123.    if(tt.hour>23)  124.        tt.hour-=24;  125.    return tt;  126.}  127.CTime CTime::operator-(CTime &t)  128.{  129.    CTime tt;  130.    tt.hour=hour-t.hour;  131.    tt.minute=minute-t.minute;  132.    tt.second=second-t.second;  133.    if(tt.second<0)  134.    {  135.        tt.second+=60;  136.        tt.minute--;  137.    }  138.    if(tt.minute<0)  139.    {  140.        tt.minute+=60;  141.        tt.hour--;  142.    }  143.    if(tt.hour<0)  144.        tt.hour+=24;  145.    return tt;  146.}  147.CTime CTime::operator+(int s)  148.{  149.    CTime tt;  150.    tt.second=second+s;  151.    if(tt.second>59)  152.    {  153.        tt.minute=minute+tt.second/60;  154.        tt.second=tt.second%60;  155.    }  156.    if(tt.minute>59)  157.    {  158.        tt.hour=hour+tt.hour/60;  159.        tt.minute=tt.minute%60;  160.    }  161.    if(tt.hour>23)  162.        tt.hour=0;  163.    return tt;  164.}  165.CTime CTime::operator-(int s)//聪明的做法!!   166.{  167.    CTime tt;  168.    tt.second=s%60;;  169.    tt.minute=(s/60)%60;  170.    tt.hour=s/3600;  171.    return *this-tt;  172.}  173.CTime CTime::operator+=(CTime &c)  174.{  175.    *this=*this+c;  176.    return *this;  177.}  178.CTime CTime::operator-=(CTime &c)  179.{  180.    *this=*this-c;  181.    return *this;  182.}  183.CTime CTime::operator+=(int s)  184.{  185.     *this=*this+s;  186.     return *this;  187.}  188.CTime CTime::operator-=(int s)  189.{  190.     *this=*this-s;  191.     return *this;  192.}  193.void CTime::setTime(int h,int m,int s)  194.{  195.    hour=h;  196.    minute=m;  197.    second=s;  198.}  199.void CTime::display()  200.{  201.    cout<<hour<<":"<<minute<<":"<<second<<endl;  202.}  203.int main()  204.{  205.    CTime t1(12,54,59),t2(15,54,43),t;  206.    cout<<"t1:";  207.    t1.display();  208.    cout<<"t2:";  209.    t2.display();  210.    t=t1+t2;  211.    cout<<"t1+t2=";  212.    t.display();  213.    t=t1-t2;  214.    t=t1+150;  215.    t=t2-280;  216.    t1+=t2;  217.    t2-=t1;  218.    t1+=340;  219.    t2-=360;  220.    if(t1>t2)  221.        cout<<"t1>t2"<<endl;  222.    if(t1<t2)  223.        cout<<"t1<t2"<<endl;  224.    if(t1==t2)  225.        cout<<"t1=t2"<<endl;  226.    if(t1!=t2)  227.        cout<<"t1≠t2"<<endl;  228.    if(t1>=t2)  229.        cout<<"t1≥t2"<<endl;  230.    if(t1<=t2)  231.        cout<<"t1小于等于t2"<<endl;  232.    return 0;  233.}  /** Copyright (c) 2015, 烟台大学计算机学院* All rights reserved.* 文件名称:Project2.cpp* 作    者:李楠* 完成日期:2015年4月23日* 版 本 号:v1.0** 问题描述: 实现Time类中的运算符重载。* 输入描述:* 程序输出:*/#include <iostream>using namespace std;class CTime{private:    unsigned short int hour;    // 时    unsigned short int minute;  // 分    unsigned short int second;  // 秒public:    CTime(int h=0,int m=0,int s=0);    void setTime(int h,int m,int s);    void display();    //二目的比较运算符重载    bool operator > (CTime &t);    bool operator < (CTime &t);    bool operator >= (CTime &t);    bool operator <= (CTime &t);    bool operator == (CTime &t);    bool operator != (CTime &t);    //二目的加减运算符的重载    //返回t规定的时、分、秒后的时间    //例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15    CTime operator+(CTime &t);    CTime operator-(CTime &t);//对照+理解    CTime operator+(int s);//返回s秒后的时间    CTime operator-(int s);//返回s秒前的时间    //二目赋值运算符的重载    CTime operator+=(CTime &c);    CTime operator-=(CTime &c);    CTime operator+=(int s);//返回s秒后的时间    CTime operator-=(int s);//返回s秒前的时间};CTime::CTime(int h,int m,int s){    hour=h;    minute=m;    second=s;}bool CTime::operator > (CTime &t){    if(hour>t.hour)        return true;    if(hour==t.hour)    {       if(minute>t.minute)          return true;          if(minute==t.minute)          {              if(second>t.second)                return true;          }    }    else        return false;}bool CTime::operator < (CTime &t){    if(hour<t.hour)        return true;    if(hour==t.hour)    {       if(minute<t.minute)          return true;          if(minute==t.minute)          {              if(second<t.second)                return true;          }    }    else        return false;}bool CTime::operator >= (CTime &t){    return !(*this<t);}bool CTime::operator <= (CTime &t){    return !(*this>t);}bool CTime::operator == (CTime &t){    if(hour==t.hour&&minute==t.minute&&second==t.second)        return true;    else        return false;}bool CTime::operator != (CTime &t){    if(hour!=t.hour||minute!=t.minute||second!=t.second)        return true;    else        return false;}CTime CTime::operator+(CTime &t){    CTime tt;    tt.hour=hour+t.hour;    tt.minute=minute+t.minute;    tt.second=second+t.second;    if(tt.second>59)    {        tt.second-=60;        tt.minute++;    }    if(tt.minute>59)    {        tt.minute-=60;        tt.hour++;    }    if(tt.hour>23)        tt.hour-=24;    return tt;}CTime CTime::operator-(CTime &t){    CTime tt;    tt.hour=hour-t.hour;    tt.minute=minute-t.minute;    tt.second=second-t.second;    if(tt.second<0)    {        tt.second+=60;        tt.minute--;    }    if(tt.minute<0)    {        tt.minute+=60;        tt.hour--;    }    if(tt.hour<0)        tt.hour+=24;    return tt;}CTime CTime::operator+(int s){    CTime tt;    tt.second=second+s;    if(tt.second>59)    {        tt.minute=minute+tt.second/60;        tt.second=tt.second%60;    }    if(tt.minute>59)    {        tt.hour=hour+tt.hour/60;        tt.minute=tt.minute%60;    }    if(tt.hour>23)        tt.hour=0;    return tt;}CTime CTime::operator-(int s)//聪明的做法!!{    CTime tt;    tt.second=s%60;;    tt.minute=(s/60)%60;    tt.hour=s/3600;    return *this-tt;}CTime CTime::operator+=(CTime &c){    *this=*this+c;    return *this;}CTime CTime::operator-=(CTime &c){    *this=*this-c;    return *this;}CTime CTime::operator+=(int s){     *this=*this+s;     return *this;}CTime CTime::operator-=(int s){     *this=*this-s;     return *this;}void CTime::setTime(int h,int m,int s){    hour=h;    minute=m;    second=s;}void CTime::display(){    cout<<hour<<":"<<minute<<":"<<second<<endl;}int main(){    CTime t1(12,54,59),t2(15,54,43),t;    cout<<"t1:";    t1.display();    cout<<"t2:";    t2.display();    t=t1+t2;    cout<<"t1+t2=";    t.display();    t=t1-t2;    t=t1+150;    t=t2-280;    t1+=t2;    t2-=t1;    t1+=340;    t2-=360;    if(t1>t2)        cout<<"t1>t2"<<endl;    if(t1<t2)        cout<<"t1<t2"<<endl;    if(t1==t2)        cout<<"t1=t2"<<endl;    if(t1!=t2)        cout<<"t1≠t2"<<endl;    if(t1>=t2)        cout<<"t1≥t2"<<endl;    if(t1<=t2)        cout<<"t1小于等于t2"<<endl;    return 0;}

输出结果:


0 0