#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();
}
time类的重载
最新推荐文章于 2021-04-08 00:05:15 发布