time类的重载

#include <iostream>

using namespace std;
class time{
private:
    int hh;
    int mm;
    int ss;
public:
    //time(int hh=0,int mm=0,int ss=0);
    time(int h,int m,int s);
    void set_time(int h,int m,int s);
    void add_an_hour();
    void add_an_minute();
    void add_an_secende();
    time operator+(time&c);
    time operator-(time&c);
    time operator+(int s);
    time operator-(int s);
    bool operator>(time&c);
    bool operator<(time&c);
    bool operator==(time&c);
    bool operator!=(time&c);
    bool operator>=(time&c);
    bool operator<=(time&c);
    time operator++(int);//后置
    time operator++();//前置
    time operator--(int);//后置
    time operator--();//前置
    time operator+=(time &t);
    time operator-=(time &t);
    time operator+=(int s);
    time operator-=(int s);
    friend istream &operator>>(istream&in,time&t);
    friend ostream &operator<<(ostream&out,time&t);
    void show_time();
};
void time::show_time()
{
    cout<<hh<<':'<<mm<<':'<<ss<<endl;
}
time::time(int h,int m,int s):hh(h),mm(m),ss(s){}//待参数的默认构造函数
// 设置时间
void time::set_time(int h,int m,int s)
{
        hh=h;
        mm=m;
        ss=s;
}
void time::add_an_hour()
{
    hh+=1;
    if(hh>23)hh=0;
}
void time::add_an_minute()
{
    mm+=1;
    if(mm>59)
    {
        mm=0;
        hh++;
    }
}
void time::add_an_secende()
{
    ss++;
    if(ss>59)
    {
        ss=0;
        mm++;
    }
}
time time::operator+(time&c)
{
    hh+=c.hh;
    mm+=c.mm;
    ss+=c.ss;
    if (ss>59)
    {
        ss=0;
        mm++;
    }
    if(mm>59)
    {
        mm=0;
        hh++;
    }
    if(hh>24)
    {
        hh%=24;
    }
    return *this;
}
time time::operator-(time&c)
{
    hh-=c.hh;
    mm-=c.mm;
    ss-=c.ss;
        if (ss>59)
    {
        ss=0;
        mm++;
    }
    if(mm>59)
    {
        mm=0;
        hh++;
    }
    if(hh>24)
    {
        hh%=24;
    }
    return *this;
}
time time::operator+(int s)
{
    ss+=s;
    if(ss>59)
    {
        ss=0;
        mm++;
    }
     if(mm>59)
    {
        mm=0;
        hh++;
    }
    if(hh>24)
    {
        hh%=24;
    }
    return *this;
}
time time::operator-(int s)
{
    int s_s=s%60;
    int s_m=s/60%60;
    int s_h=s/3600;
    if(s_h>hh)return time(0,0,0);
    else if((s_h==hh&&s_m>mm)||(s_h==hh&&s_m==mm&&s_s>ss))return time(0,0,0);
    else{
        if(ss>s_s)ss-=s_s;
        else{
            ss+=60;
            ss-=s_s;
            mm--;
            if(mm<0)
            {
                hh--;
                mm+=60;
            }
        }
    }
    return *this;
}
bool time::operator>(time &c)//大于
{
    if(hh<c.hh)return false;
    else if(hh==c.hh)
    {
        if(mm<c.mm)return false;
        else if(mm==c.mm)
        {
            if(ss<=c.ss)return false;
        }
        else return true;
    }
    else return true;

}
bool time::operator<(time &c)//小于
{
    if(hh>c.hh)return false;
    else if(hh==c.hh)
    {
        if(mm>c.mm)return false;
        else if(mm==c.mm)
        {
            if(ss>=c.ss)return false;
        }
        else return true;
    }
    else return true;
}
bool time::operator!=(time&c)//不等于
{
    if(*this>c||*this<c)return true;
    else return false;
}
bool time::operator==(time&c)//等于
{
    if(!(*this>c||*this<c))return true;
    else return true;
}
bool time::operator>=(time&c)//大于等于
{
    if(*this>c||*this==c)return true;
    else return false;
}
bool time::operator<=(time&c)//小于等于
{
    if(*this<c||*this==c)return true;
    else return false;
}
time time::operator++(int)//后置
{
    time temp=*this;
    *this=*this+1;
    return temp;
}
time time::operator++()//前置
{
    *this=*this+1;
    return *this;
}
time time::operator--(int)//后置
{
    time temp=*this;
    *this=*this-1;
    return temp;
}
time time::operator--()
{
    *this=*this-1;
    return *this;
}
time time::operator+=(time&t)
{
    *this=*this+t;
    return *this;
}
time time::operator+=(int s)
{
    *this=*this+s;
    return *this;
}
time time::operator-=(time&t)
{
    *this=*this-t;
    return *this;
}
time time::operator-=(int s)
{
    *this=*this-s;
    return *this;
}
int main()
{
    time t1(23,15,20);
    t1.show_time();
    time t2(1,1,1);
    t1+=t2;
    t1.show_time();
    if(t1>t2)t1.show_time();
    else t2.show_time();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值