#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);
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);
CTime operator++();
CTime operator--();
CTime operator++(int);
CTime operator--(int);
friend ostream& operator<<(ostream&,CTime &c);
friend istream& operator>>(istream&,CTime &c);
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;
}
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;
time.adjust_sec();
return time;
}
CTime CTime::operator-(CTime &t)
{
CTime time;
time.hour=hour-t.hour;
time.minute=minute-t.minute;
time.second=second-t.second;
time.adjust_sec();
return time;
}
CTime CTime::operator+(int s)
{
CTime time;
time.hour=hour;
time.minute=minute;
time.second=second+s;
time.adjust_sec();
return time;
}
CTime CTime::operator-(int s)
{
CTime time;
time.hour=hour;
time.minute=minute;
time.second=second-s;
time.adjust_sec();
return time;
}
CTime CTime::operator+=(CTime &t)
{
second+=t.second;
minute+=t.minute;
hour+=t.hour;
CTime::adjust_sec();
return *this;
}
CTime CTime::operator-=(CTime &t)
{
second+=t.second;
minute+=t.minute;
hour+=t.hour;
CTime::adjust_sec();
return *this;
}
CTime CTime::operator+=(int s)
{
second+=s;
CTime::adjust_sec();
return *this;
}
CTime CTime::operator-=(int s)
{
second-=s;
CTime::adjust_sec();
return *this;
}
CTime CTime::operator++()
{
second++;
CTime::adjust_sec();
return *this;
}
CTime CTime::operator--()
{
second--;
CTime::adjust_sec();
return *this;
}
CTime CTime::operator++(int)
{
CTime c;
c=*this;
second++;
CTime::adjust_sec();
return c;
}
CTime CTime::operator--(int)
{
CTime c;
c=*this;
second--;
CTime::adjust_sec();
return c;
}
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;
}
}
ostream& operator<<(ostream&,CTime&c)
{
cout<<c.hour<<':'<<c.minute<<':'<<c.second<<endl;
return cout;
}
istream& operator>>(istream&,CTime&c)
{
cout<<"输入小时,分钟和秒";
cin>>c.hour>>c.minute>>c.second;
return cin;
}
int main()
{
CTime t1,t2,t3;
cout<<"输入t1,t2"<<endl;
cin>>t1>>t2;
cout<<"t1,t2分别为"<<endl;
cout<<t1<<t2;
cout<<"t1+t2=";
t3=t1+t2;
cout<<t3;
t3=t1++;
cout<<t3;
t3=++t1;
cout<<t3;
cout<<(++t1);
return 0;
}
2015.5.13time类中的运算符重载(续)
最新推荐文章于 2021-08-21 10:18:08 发布