2-9-2 Time类中的运算符重载(续)

问题及代码:

#include <iostream>

using namespace std;
class CTime
{
private:
    unsigned short int hour;    // 时
    unsigned short int minute;  // 分
    unsigned short int second;  // 秒
    unsigned short int SEC;

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 operator++(int);
    CTime operator++();
    CTime operator--(int);
    CTime operator--();
    friend istream &operator>>(istream &,CTime &);
    friend ostream &operator<<(ostream &,CTime &);

};
CTime::CTime(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
    SEC=hour*3600+minute*60+second;
}
void CTime::setTime(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
    SEC=hour*3600+minute*60+second;
}
void CTime::display()
{
    cout<<hour<<':'<<minute<<':'<<second<<endl;
}
bool CTime::operator>(CTime &t)
{
    return SEC>t.SEC;
}
bool CTime::operator<(CTime &t)
{
    return SEC<t.SEC;
}
bool CTime::operator<=(CTime &t)
{
    return !(SEC>t.SEC);
}
bool CTime::operator>=(CTime &t)
{
    return !(SEC<t.SEC);
}
bool CTime::operator==(CTime &t)
{
    return SEC==t.SEC;
}
bool CTime::operator!=(CTime &t)
{
    return !(SEC==t.SEC);
}
CTime CTime::operator+(CTime &t)
{
    int h,m,s;
    s=second+t.second;
    m=minute+t.minute;
    h=hour+t.hour;
    if(s>=60)
    {
        ++m;
        s-=60;
    }
    if(m>=60)
    {
        ++h;
        m-=60;
    }
    if(h>=24)
        h-=24;
    return CTime(h,m,s);
}
CTime CTime::operator-(CTime &t)
{
    int h,m,s;
    s=second-t.second;
    m=minute-t.minute;
    h=hour-t.hour;
    if(s<0)
    {
        m--;
        s+=60;
    }
    if(m<0)
    {
        h--;
        m+=60;
    }
    if(h<0)
        h+=24;
    return CTime(h,m,s);
}
CTime CTime::operator+(int s)
{
    if((second+=s)>=60)
    {
        minute+=second/60;
        second%=60;
    }
    if(minute>=60)
    {
        hour+=minute/60;
        minute%=60;
    }
    if(hour>=24)
        hour%=24;
    return *this;
}
CTime CTime::operator-(int s)
{
    int hh,mm,ss;
    ss=s%60;
    mm=(s/60)%60;
    hh=(s/3600)%24;
    CTime t(hh,mm,ss);
    return *this-t;
}
CTime CTime::operator+=(CTime &t)
{
    *this=*this+t;
    return *this;
}
CTime CTime::operator-=(CTime &t)
{
    *this=*this-t;
    return *this;
}
CTime CTime::operator+=(int s)
{
    *this=*this+s;
    return *this;
}
CTime CTime::operator-=(int s)
{
    *this=*this-s;
    return *this;
}
CTime CTime::operator++(int a=1)
{
    CTime t(*this);
    if(++second>=60)
    {
        second=0;
        if(++minute>=60)
        {
            minute=0;
            if(++hour>=24)
                hour=0;
        }
    }

    return t;
}
CTime CTime::operator++()
{
    if(++second>=60)
    {
        second=0;
        if(++minute>=60)
        {
            minute=0;
            if(++hour>=24)
                hour=0;
        }

    }
    return *this;
}
CTime CTime::operator--(int)
{
    CTime t(*this);
    int s(second),m(minute),h(hour);
    if(--s<0)
    {
        s+=60;
        if(--m<0)
        {
            m+=60;
            if(--h<0)
                h+=24;
        }
    }
    return t;
}
CTime CTime::operator--()
{
    int s(second),m(minute),h(hour);
    if(--s<0)
    {
        s+=60;
        if(--m<0)
        {
            m+=60;
            if(--h<0)
                h+=24;
        }
    }
    return CTime(h,m,s);
}
istream &operator>>(istream &in,CTime &c)
{
    in>>c.hour>>c.minute>>c.second;
    return in;
}
ostream &operator<<(ostream &out,CTime &c)
{
    out<<c.hour<<':'<<c.minute<<':'<<c.second<<endl;
    return out;
}
int main()
{
    CTime t1(12,0,0),t2; //测试构造函数
    t2.setTime(11,59,59); //测试settime()
    t1.display(); //测试display()
    t2.display();
    cout<<(t1>t2?"t1>t2":"t1<t2")<<endl;  //测试operator>()
    CTime t3(22,1,10),t4(23,50,10);
    t3.display();
    t4.display();
    cout<<(t3>=t4?"t3>=t4":"t3<t4")<<endl; //测试operator>=()
    cout<<(t2<=t1?"t2<=t1":"t2>t1")<<endl; //测试operator<=()
    cout<<(t3==t4?"t3==t4":"t3!=t4")<<endl; //测试operator==()
    cout<<(t1!=t2?"t1!=t2":"t1==t2")<<endl; //测试operator!=()
    (t1+t2).display(); //测试operator+()
    (t3+t4).display();
    (t1-t2).display();//测试operator-()
    (t3-t4).display();
    (t4+600).display();
    (t1-20).display();
    (t1+=t2).display();
    (t1-=t2).display();
    (t1+=600).display();
    (t1-=300).display();
    CTime t5,t6;
    cin>>t5;
    t6=t5++;
    cout<<t5<<t6;
    cin>>t5;
    t6=--t5;
    cout<<t5<<t6;
    return 0;
}


运行结果:


学习小结:

哇,写了两百多行!

不错不错!

今天到这了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值