第9周项目2-Time类中的运算符重载(续)

#include <iostream>

using namespace std;

class CTime
{
private:
     short int hour;
    short int minute;
    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);
    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 operator++(int);
    CTime operator++();
    CTime operator--(int);
    CTime operator--();
    friend ostream& operator<<(ostream &out,CTime &t);
    friend istream& operator>>(istream &in,CTime &t);
};

CTime::CTime(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
}

void CTime::setTime(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
}

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;
    else 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;
    else 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(*this<t||*this>t) return false;
   return true;
}

bool CTime::operator!=(CTime &t)
{
   if(*this<t||*this>t) return true;
   return false;
}

CTime CTime::operator+(CTime &t)
{
    CTime c;
    c.hour=hour+t.hour;
    c.minute=minute+t.minute;
    c.second=second+t.second;
    if(c.second>60)
    {
        c.minute=c.minute+c.second/60;
        c.second=c.second%60;
    }
    if(c.minute>60)
    {
        c.hour=c.hour+c.minute/60;
        c.minute=c.minute%60;
    }
    if(c.hour>24)
    {
        c.hour=c.hour%24;
    }
    return c;
}

CTime CTime::operator-(CTime &t)
{
    CTime c;
    c.hour=hour-t.hour;
    c.minute=minute-t.minute;
    c.second=second-t.second;
    if(c.second<0)
    {
        c.second+=60;
        c.minute--;
    }
    if(c.minute<0)
    {
        c.minute+=60;
        c.hour--;
    }
    if(c.hour<0)
    {
        c.hour+=24;
    }
    return c;
}

CTime CTime::operator+(int s)
{
    second+=s;
    if(second>60)
    {
        minute=minute+second/60;
        second=second%60;
    }
    if(minute>60)
    {
        hour=hour+minute/60;
        minute=minute%60;
    }
    if(hour>24)
    {
        hour=hour%24;
    }
    return *this;
}

CTime CTime::operator-(int s)
{
    second-=s;
    if(second<0)
    {
        second+=60;
        minute--;
    }
    if(minute<0)
    {
        minute+=60;
        hour--;
    }
    if(hour<0)
    {
        hour+=24;
    }
    return *this;
}

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+=s;
    return *this;
}

CTime CTime::operator-=(int s)
{
    *this-=s;
    return *this;
}

CTime CTime::operator++(int)
{
    second++;
    if(second>60)
    {
        minute=minute+second/60;
        second=second%60;
    }
    if(minute>60)
    {
        hour=hour+minute/60;
        minute=minute%60;
    }
    if(hour>24)
    {
        hour=hour%24;
    }
    return *this;
}

CTime CTime::operator++()
{
    if(++second>60)
    {
        minute=minute+second/60;
        second=second%60;
    }
    if(minute>60)
    {
        hour=hour+minute/60;
        minute=minute%60;
    }
    if(hour>24)
    {
        hour=hour%24;
    }
    return *this;
}
CTime CTime::operator--(int)
{
    second--;
   if(second<0)
    {
        second+=60;
        minute--;
    }
    if(minute<0)
    {
        minute+=60;
        hour--;
    }
    if(hour<0)
    {
        hour+=24;
    }
    return *this;
}

CTime CTime::operator--()
{
   if(--second<0)
    {
        second+=60;
        minute--;
    }
    if(minute<0)
    {
        minute+=60;
        hour--;
    }
    if(hour<0)
    {
        hour+=24;
    }
    return *this;
}

istream& operator>>(istream &in,CTime &t)
{
    int h,m,s;
    char c;
    do
    {
        cout<<"请输入时间大小:";
        in>>h>>c>>m>>c>>s;
    }while(!(c==':'));
    t.hour=h;
    t.minute=m;
    t.second=s;
    return in;
}

ostream& operator<<(ostream &out,CTime &t)
{
    out<<t.hour<<':'<<t.minute<<':'<<t.second<<endl;
    return out;
}
int main()
{
    CTime t1(8,20,25),t2;
    int h,m,s;
    cout<<"输入时间大小:";
    cin>>h>>m>>s;
    t2.setTime(h,m,s);
    cout<<"两个时间分别为:"<<endl;
    cout<<t1<<endl<<t2<<endl;
    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;
    CTime t;
    cout<<"下面是时间的加减运算符的重载:"<<endl;
    t=t1+t2;
    cout<<"t1+t2="<<t;
    t=t1-t2;
    cout<<"t1-t2="<<t;
    t1+=t2;
    cout<<"t1+=t2 "<<t1;
    t1-=t2;
    cout<<"t1-=t2 "<<t1;
    t1=t1+55;
    cout<<"t155秒后时间为:"<<t1;
    t2=t1-55;
    cout<<"t255秒前的时间为:"<<t2;
    t=t1++;
    cout<<"t1++="<<t;
    t=++t1;
    cout<<"++t1="<<t;
    t=t2--;
    cout<<"t2--="<<t;
    t=--t2;
    cout<<"--t2="<<t;
    return 0;
}



运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值