2015.5.9时间类的运算符的重载

#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 &t);
    CTime operator-=(CTime &t);
    CTime operator+=(int s);
    CTime operator-=(int s);
    void adjust_sec();
    void adjust_min(int t);
    void adjust_hou(int 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||(hour=t.hour&&minute>t.minute)||((hour=t.hour,minute=t.minute)&&second>t.second))
        return true;
    else
        return false;
}
bool CTime::operator<(CTime &t)
{
    if(hour<t.hour||(hour=t.hour&&minute<t.minute)||((hour=t.hour,minute=t.minute)&&second<t.second))
        return true;
    else
        return false;
}
bool CTime::operator>=(CTime &t)
{
    if(*this<t)
        return false;
    else
        return true;
}
bool CTime::operator<=(CTime &t)
{
    if(*this>t)
        return false;
    else
        return true;
}
bool CTime::operator==(CTime &t)
{
    if(*this>t||*this<t)
        return false;
    else
        return true;
}
bool CTime::operator!=(CTime &t)
{
    if(*this>t||*this<t)
        return true;
    else
        return false;
}
CTime CTime::operator+(CTime &t)
{
    CTime time;
    time.hour=hour+t.hour;
    time.minute=minute+t.minute;
    time.second=second+t.second;
    return time;
}
CTime CTime::operator-(CTime &t)
{
    CTime time;
    time.hour=hour-t.hour;
    time.minute=minute-t.minute;
    time.second=second-t.second;
    return time;
}
CTime CTime::operator+(int s)
{
    CTime time;
    time.hour=hour;
    time.minute=minute;
    time.second=second+s;
    return time;
}
CTime CTime::operator-(int s)
{
    CTime time;
    time.hour=hour;
    time.minute=minute;
    time.second=second-s;
    return time;
}
CTime CTime::operator+=(CTime &t)
{
    second+=t.second;
    minute+=t.minute;
    hour+=t.hour;
    return *this;
}
CTime CTime::operator-=(CTime &t)
{
    second+=t.second;
    minute+=t.minute;
    hour+=t.hour;
    return *this;
}
CTime CTime::operator+=(int s)
{
    second+=s;
    return *this;
}
CTime CTime::operator-=(int s)
{
    second-=s;
    return *this;
}
void CTime::adjust_sec()
{
    int m;
    if(second>=0)
    {
        m=second/60;
        second%=60;
        CTime::adjust_min(m);
    }
    else
    {
        m=second/60;
        m=m-1;
        second%=60;
        second+=60;
        CTime::adjust_min(m);
    }
}
void CTime::adjust_min(int t)
{
   int h;
   minute+=t;
   if(minute>=0)
   {
        h=minute/60;
        minute%=60;
        CTime::adjust_hou(h);
   }
   else
   {
        h=minute/60;
        h=h-1;
        minute%=60;
        minute+=60;
        CTime::adjust_hou(h);
   }
}
void CTime::adjust_hou(int t)
{
    hour+=t;
    if(hour>=0)
    {
        hour%=24;
    }
    else
    {
        hour%=24;
        hour+=24;
    }
}
int main()
{
    CTime t1(8,20,25),t2(11,20,50),t3;
    t1.display();
    t2.display();
    t3=t1+t2;
    t3.adjust_sec();
    t3.display();
    t3=t1-t2;
    t3.adjust_sec();
    t3.display();
    t3=t1+61;
    t3.adjust_sec();
    t3.display();
    t1+=t2;
    t1.adjust_sec();
    t1.display();
    t1+=61;
    t1.adjust_sec();
    t1.display();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值