#include<iostream>
using namespace std;
class CTime{
private:
unsigned short int hour;
unsigned short int minute;
unsigned short int second;
public:
CTime(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){};
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 &c);
CTime operator-(CTime &c);
CTime operator+(int c);
CTime operator-(int c);
CTime operator++();
CTime operator++(int);
CTime operator--();
CTime operator--(int);
CTime operator+=(CTime &c);
CTime operator-=(CTime &c);
CTime operator+=(int c);
CTime operator-=(int c);
};
void CTime::setTime(int h,int m,int s){
hour=h;
minute=m;
second=s;
}
bool CTime::operator >(CTime &t){
if(hour>t.hour)
return true;
else if(hour==t.hour && minute>t.minute)
return true;
else if(hour==t.hour && minute==t.minute && second>t.second)
return true;
else
return false;
}
bool CTime::operator <(CTime &t){
if(hour<t.hour)
return true;
else if(hour==t.hour && minute<t.minute)
return true;
else if(hour==t.hour && minute==t.minute && second<t.second)
return true;
else
return false;
}
bool CTime::operator >=(CTime &t){
if(hour>t.hour)
return true;
else if(hour==t.hour && minute>t.minute)
return true;
else if(hour==t.hour && minute==t.minute && second>t.second)
return true;
else if(hour==t.hour && minute==t.minute && second==t.second)
return true;
else
return false;
}
bool CTime::operator <=(CTime &t){
if(hour<t.hour)
return true;
else if(hour==t.hour && minute<t.minute)
return true;
else if(hour==t.hour && minute==t.minute && second<t.second)
return true;
else if(hour==t.hour && minute==t.minute && second==t.second)
return true;
else
return false;
}
bool CTime::operator ==(CTime &t){
if(hour==t.hour && minute==t.minute && second==t.second)
return true;
else
return false;
}
bool CTime::operator !=(CTime &t){
if(hour==t.hour && minute==t.minute && second==t.second)
return false;
else
return true;
}
CTime CTime::operator+(CTime &c){
CTime a;
a.hour=hour+c.hour;
a.minute=minute+c.minute;
a.second=second+c.second;
if(a.second>=60){
a.minute=a.minute+a.second/60;
a.second=a.second%60;
if(a.minute>=60)
{
a.hour=a.hour+a.minute/60;
a.minute=a.minute%60;
}
}
return a;
}
CTime CTime::operator-(CTime &c){
CTime a;
a.hour=hour-c.hour;
a.minute=minute-c.minute;
a.second=second-c.second;
if(a.second<=0){
a.second=60-a.second;
a.minute-=1;
if(a.minute<=0){
a.minute=60-a.minute;
a.hour-=1;
}
}
return a;
}
CTime CTime::operator+(int c){
CTime a;
a.second=second+c;
if(a.second>=60){
++a;
}
return a;
}
CTime CTime::operator-(int c){
CTime a;
a.second=second+c;
if(a.second<=0){
--a;
}
return a;
}
CTime CTime::operator++(){
if(++second>=60){
second-=60;
++minute;
}
return *this;
}
CTime CTime::operator++(int){
CTime temp(*this);
second++;
if(second>=60){
second-=60;
++minute;
}
return temp;
}
CTime CTime::operator--(){
if(--second<=0){
second=60;
--minute;
}
return *this;
}
CTime CTime::operator--(int){
CTime temp(*this);
second--;
if(second<=0){
second=60;
--minute;
}
return temp;
}
CTime CTime::operator+=(CTime &c){
hour+=c.hour;
minute+=c.minute;
second+=c.second;
if(second>=60){
minute=minute+second/60;
second=second%60;
if(minute>=60)
{
hour=hour+minute/60;
minute=minute%60;
}
}
return CTime(hour,minute,second);
}
CTime CTime::operator-=(CTime &c){
hour-=c.hour;
minute-=c.minute;
if(second<c.second){
second=60+second-c.second;
minute-=1;
}
return CTime(hour,minute,second);
}
CTime CTime::operator+=(int c){
second+=c;
return CTime(hour,minute,second);
}
CTime CTime::operator-=(int c){
second-=c;
return CTime(hour,minute,second);
}
void CTime::display(){
cout<<hour<<" "<<minute<<" "<<second<<endl;
}
int main()
{
CTime t1(8,20,25),t2(11,20,50),t3,t;
cout<<"t1为:";
t1.display();
cout<<"t2为:";
t2.display();
cout<<"下面比较两个时间大小:\n";
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;
cout<<endl;
cout<<"两项相加之后"<<endl;
t=t1+t2;
t.display();
cout<<"两项相减之后"<<endl;
t=t2-t1;
t.display();
cout<<"时间加加之后的景象"<<endl;
++t1;
t1.display();
t=t1++;
t.display();
cout<<"时间减减之后的景象"<<endl;
t=--t2;
t.display();
t=t2--;
t.display();
cout<<"+=的应用"<<endl;
t2+=t1;
t2.display();
cout<<"-=的应用"<<endl;
t2-=t1;
t2.display();
t3.setTime(1,2,3);
cout<<"加上或是减去之后"<<endl;
t3+=2;
t3.display();
t3-=2;
t3.display();
return 0;
}
运行结果: