#include <iostream>
using namespace std;
class Time{
private:
unsigned int short hour;
unsigned int short minute;
unsigned int short second;
public:
Time(int h=0,int m=0,int s=0)
{ hour=h;
minute=m;
second=s;
}
void setTime(int h,int m,int s)
{ hour=h;
minute=m;
second=s;
}
void display()
{ cout<<hour<<':'<<minute<<':'<<second<<endl;
}
//比较运算符的重载
//比较运算返回的是比较结果,是bool型的true或false
bool operator>(Time &t);
bool operator<(Time &t);
bool operator>=(Time &t);
bool operator<=(Time &t);
bool operator==(Time &t);
bool operator!=(Time &t);
//二目的加减运算符的重载
//返回t规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
Time operator+(Time &t);
Time operator-(Time &t);
Time operator+(int s);//返回s秒后的时间
Time operator-(int s);//返回s秒前的时间
//二目赋值运算符的重载
Time operator+=(Time &t);
Time operator-=(Time &t);
Time operator+=(int s);//返回s秒后的时间
Time operator-=(int s);//返回s秒前的时间
};
//二目运算符的重载
//加运算返回 t 规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
Time Time::operator+(Time &t)
{ int h,m,s;//这里要记得改为int,unsigned是没有符号的,即不会出现负数
h=hour+t.hour;
m=minute+t.minute;
s=second+t.second;
if(s>=60)//顺序不能颠倒
{ s=s-60;
m++;
}
if(m>=60)
{ m=60-m;
h++;
}
if(h>=24)
{ h=h-24;
}
Time t1(h,m,s);
return t1;
}
// 计算时间之差
Time Time::operator-(Time &t)
{ int h,m,s;//这里要记得改为int,unsigned是没有符号的,即不会出现负数
h=hour-t.hour;
m=minute-t.minute;
s=second-t.second;
if(s<0)//顺序不能颠倒
{ s=s+60;
m--;
}
if(m<0)
{ m=m+60;
h--;
}
if(h<0)
{ h=h+24;
}
Time t2(h,m,s);
return t2;
}
//返回s秒后的时间
Time Time::operator+(int s)
{ int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
Time t3(hh,mm,ss);
return *this+t3;
}
//返回s秒前的时间
Time Time::operator-(int s)
{ int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
Time t4(hh,mm,ss);
return *this-t4;
}
//赋值运算符的重载
//可以直接使用已经重载了的加减运算实现
//这种赋值, 例如 t1+=20,直接改变当前对象的值,所以在运算完成后,将*this作为返回值
Time Time::operator+=(Time &t)
{ *this=*this+t;
return *this;
}
Time Time::operator-=(Time &t)
{ *this=*this-t;
return *this;
}
Time Time::operator+=(int s)//返回s秒后的时间
{ *this=*this+s;
return *this;
}
Time Time::operator-=(int s)//返回s秒前的时间
{ *this=*this-s;
return *this;
}
//比较运算符的重载
bool Time::operator>(Time &t)//判断时间t1>t2
{ 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;
return false;
}
bool Time::operator<(Time &t)//判断时间t1<t2
{ 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;
return false;
}
bool Time::operator>=(Time &t)// 判断时间t1>=t2
{ if(*this<t) return false;
return true;
}
bool Time::operator<=(Time &t)// 判断时间t1<=t2
{ if(*this>t) return false;
return true;
}
bool Time::operator==(Time &t)// 判断时间t1==t2
{ if(*this>t||*this<t) return false;
return true;
}
bool Time::operator!=(Time &t)// 判断时间t1!=t2
{ if(*this==t) return false;
return true;
}
int main()
{
Time t5(8,20,25),t6(11,20,50),t7;
cout<<"t5为:";
t5.display();
cout<<"t6为:";
t6.display();
cout<<"下面比较两个时间大小:\n";
if(t5>t6) cout<<"t5>t6"<<endl;
if (t5<t6) cout<<"t5<t6"<<endl;
if (t5==t6) cout<<"t5=t6"<<endl;
if (t5!=t6) cout<<"t5≠t6"<<endl;
if (t5>=t6) cout<<"t5≥t6"<<endl;
if (t5<=t6) cout<<"t5≤t6"<<endl;
cout<<endl;
cout<<"t5+t6=";
t7=t5+t6;
t7.display();
cout<<"t5-t6=";
t7=t5-t6;
t7.display();
cout<<"t6+2000=";
t7=t6+2000;
t7.display();
cout<<"t6-5000=";
t7=t6-5000;
t7.display();
cout<<"t5+=t6的结果:";
t5+=t6;
t5.display();
cout<<"t5-=t6的结果:";
t5-=t6;
t5.display();
cout<<"t5+=2000的结果:";
t5+=2000;
t5.display();
cout<<"t5-=5000的结果:";
t5-=5000;
t5.display();
return 0;
}
time类
最新推荐文章于 2020-07-22 21:06:40 发布