第8周项目2 Time类中的运算符重载

来源:互联网 发布:国家公务员考试 知乎 编辑:程序博客网 时间:2024/06/07 06:03
/**Copyright (c)2015,烟台大学计算机与控制工程学院*All rights reserved.*文件名称:score.cpp*作    者:惠睿*完成日期:2015年4月30日*版 本 号:v1.0**问题描述:Time类中的运算符重载。*程序输入:无输入。*程序输出:输出调用函数后的值。*/#include<iostream>#include<cmath>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();    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);    CTime operator+(CTime &t);    CTime operator-(CTime &t);    CTime operator+(int s);    CTime operator-(int s);    CTime operator+=(CTime &c);    CTime operator-=(CTime &c);    CTime operator+=(int s);    CTime operator-=(int s);};CTime::CTime(int h,int m,int s){    hour=h;    minute=m;    second=s;}void CTime::setTime(){    char a,b;    cout<<"格式(h:m:s):";    cin>>hour>>a>>minute>>b>>second;    if(hour>24 || minute>59 || second>59 || a!=':' || b!=':')        cout<<"格式错误,请重新输入!"<<endl;}void CTime::display(){    cout<<hour<<":"<<minute<<":"<<second<<endl;}bool CTime::operator > (CTime &t){    if (hour>t.hour) return true;    if (hour<t.hour) return false;    if (minute>t.minute) return true;    if (minute<t.minute) return false;    if (second>t.second) return true;    return false;}bool CTime::operator < (CTime &t){    if (hour<t.hour) return true;    if (hour>t.hour) return false;    if (minute<t.minute) return true;    if (minute>t.minute) return false;    if (second<t.second) return true;    return false;}bool CTime::operator >= (CTime &t){    if(*this<t) return false;    return true;}bool CTime::operator <= (CTime &t){    if(*this>t) return false;    return true;}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 time;    int h,m,s;    h=hour+t.hour;    m=minute+t.minute;    s=second+t.second;    if(s>59)    {        s-=60;        m++;        if(m>59)        {            m-=60;            h++;        }        if(h>23)            h-=24;    }    if(m>59)    {        m-=60;        h++;        if(h>23)            h-=24;    }    if(h>23)        h-=24;    time.hour=h;    time.minute=m;    time.second=s;    return time;}CTime CTime::operator-(CTime &t){    CTime time;    int h,m,s;    h=hour-t.hour;    m=minute-t.minute;    s=second-t.second;    if(s<0)    {        s+=60;        m--;        if(m<0)        {            m+=60;            h--;        }        if(h<0)            h+=24;    }    if(m<0)    {        m+=60;        h--;        if(h<0)            h+=24;    }    if(h<0)        h+=24;    time.hour=h;    time.minute=m;    time.second=s;    return time;}CTime CTime::operator+(int s){    int h,m,sec;    sec=s%60;    m=(s/60)%60;    h=s/3600;    CTime time(h,m,sec);    return *this+time;}CTime CTime::operator-(int s){    int h,m,sec;    sec=s%60;    m=(s/60)%60;    h=s/3600;    CTime time(h,m,sec);    return *this-time;}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){    int h,m,sec;    sec=s%60;    m=(s/60)%60;    h=s/3600;    CTime time(h,m,sec);    *this=*this+time;    return *this;}CTime CTime::operator-=(int s){    int h,m,sec;    sec=s%60;    m=(s/60)%60;    h=s/3600;    CTime time(h,m,sec);    *this=*this-time;    return *this;}int main(){    CTime t1(8,20,25),t2(11,20,50),t;    cout<<"t1为:";    t1.display();    cout<<"t2为:";    t2.display();    cout<<"下面比较两个时间的大小:"<<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;    if (t1!=t2) cout<<"t1≠t2"<<endl;    cout<<endl;    cout<<endl<<"下面对两个时间进行计算:"<<endl<<"t1+t2:  ";    t=t1+t2;    t.display();    cout<<"t1-t2:  ";    t=t1-t2;    t.display();    cout<<"t1+2000s:  ";    t=t1+2000;    t.display();    cout<<"t1-5000s:  ";    t=t1-5000;    t.display();    cout<<"t1+=t2:  ";    t1+=t2;    t1.display();    cout<<"t1-=t2:  ";    t1-=t2;    t1.display();    cout<<"t1+=2000s:  ";    t1+=2000;    t1.display();    cout<<"t1-=5000s:  ";    t1-=5000;    t1.display();    return 0;}

 

运行结果:

 

0 0
原创粉丝点击