#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;
}
2015.5.9时间类的运算符的重载
最新推荐文章于 2021-04-25 18:07:55 发布