#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);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
CTime operator-(CTime &c);//对照+理解6
CTime operator+(int s);//返回s秒后的时间
CTime operator-(int s);//返回s秒前的时间
//一目运算符的重载
CTime operator++(int);//后置++,下一秒
CTime operator++();//前置++,下一秒
CTime operator--(int);//后置--,前一秒
CTime operator--();//前置--,前一秒
//赋值运算符的重载
CTime operator+=(CTime &c);
CTime operator-=(CTime &c);
CTime operator+=(int s);//返回s秒后的时间
CTime operator-=(int s);//返回s秒前的时间
};
//下面实现所有的运算符重载代码。
//为简化编程,请注意通过调用已有函数,利用好各函数之间的关系
void CTime::display()
{
cout << hour <<":" << minute << ":" << second << endl;
}
void CTime::setTime(int h,int m,int s)
{
hour = h;
minute = m;
second = s;
}
CTime CTime::operator+(CTime &c)
{
CTime time;
time.hour = hour + c.hour;
time.minute = minute + c.minute;
time.second = second + c.second;
while(time.second >= 60)
{
time.second = time.second - 60;
time.minute ++;
}
while(time.minute >= 60)
{
time.minute = time.minute - 60;
time.hour ++;
}
return time;
}
CTime CTime::operator-(CTime &c)
{
CTime time;
int s1,s2;
s1 = second + minute * 60 + hour * 3600;
s2 = c.second + c.minute * 60 + c.hour * 3600;
if(s1 > s2) //
time.second = s1 - s2;
else
time.second = s2 - s1;
while(time.second >= 60)
{
time.second = time.second - 60;
time.minute ++;
}
while(time.minute >= 60)
{
time.minute = time.minute - 60;
time.hour ++;
}
return time;
}
CTime CTime::operator+(int s)
{
second = second + s;
while(second >= 60)
{
second = second - 60;
minute ++;
}
while(minute >= 60)
{
minute = minute - 60;
hour ++;
}
return *this;
}
CTime CTime::operator-(int s)
{
while(second < s)
{
minute --;
while(minute < 0)
{
hour--;
minute = minute + 60;
}
second = second + 60;
}
second = second - s;
return *this;
}
CTime CTime::operator++(int)
{
CTime temp(*this);
second++;
if(second >= 60)
{
second = second - 60;
++ minute;
}
return temp;
}
CTime CTime::operator++()
{
if(++ second >= 60)
{
second = second - 60;
++ minute;
}
return *this;
}
CTime CTime::operator--(int)
{
CTime temp(*this);
if(second-- < 0)
{
minute--;
if(--minute < 0)
{
hour--;
minute = minute + 60;
}
second = second + 60;
}
second --;
return temp;
}
CTime CTime::operator--()
{
if(-- second < 0)
{
-- minute;
if(--minute < 0)
{
hour--;
minute = minute + 60;
}
second = second + 60;
}
return *this;
}
CTime CTime::operator+=(CTime &c)
{
hour = hour + c.hour;
minute = minute + c.minute;
second = second + c.second;
while(second >= 60)
{
second = second - 60;
minute ++;
}
while(minute >= 60)
{
minute = minute - 60;
hour ++;
}
return *this;
}
CTime CTime::operator-=(CTime &c)
{
CTime time;
second = second + minute * 60 + hour * 3600;
c.second = c.second + c.minute * 60 + c.hour * 3600;
if(second > c.second )
{
second = second - c.second ;
while(second >= 60)
{
second = second - 60;
minute ++;
}
while(minute >= 60)
{
minute = minute - 60;
hour ++;
}
time = *this;
}
else
{
c.second = c.second - second;
while(c.second >= 60)
{
c.second = c.second - 60;
c.minute ++;
}
while(c.minute >= 60)
{
c.minute = c.minute - 60;
c.hour ++;
}
time = c;
}
return time;
}
CTime CTime::operator+=(int s)
{
second = second + s;
while(second >= 60)
{
second = second - 60;
minute ++;
}
while(minute >= 60)
{
minute = minute - 60;
hour ++;
}
return *this;
}
CTime CTime::operator-=(int s)
{
int min;
min = minute;
while(second < s)
{
minute = minute --;
while(min < 0)
{
hour--;
minute = minute + 60;
}
second = second + 60;
}
second = second - s;
return *this;
}
bool CTime::operator > (CTime &t)
{
if(hour > t.hour) return true;
else if (minute > t.minute) return true;
else if (minute > t.second) return true;
else
return false;
}
bool CTime::operator < (CTime &t)
{
if(hour < t.hour) return true;
else if (minute < t.minute) return true;
else if (minute < t.second) return true;
else
return false;
}
bool CTime::operator == (CTime &t)
{
if(hour == t.hour && minute == t.minute && second == t.second)
return true;
return false;
}
bool CTime::operator >= (CTime &t)
{
if(operator < (t) != 1)
return true;
return false;
}
bool CTime::operator <= (CTime &t)
{
if(operator > (t) != 1)
return true;
return false;
}
bool CTime::operator != (CTime &t)
{
if(hour != t.hour || minute != t.minute || second != t.second)
return true;
return false;
}
void main()
{
CTime t1(8,20,25), t2(11,20,50),t;
cout << "t1为:";
t1.display();
cout << "t2为:";
t2.display();
cout << "比较两个时间大小:" << 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;
if (t1 <= t2) cout << "t1 ≤ t2" << endl;
cout<<endl;
t = t1 + t2;
cout << "t1 + t2 = ";
t.display();
t = t1 - t2;
cout << "t1 - t2 = ";
t.display();
t = t1;
t = t + 35;
cout<< "t1 + 35 = ";
t .display();
t = t2;
t = t - 8;
cout<< "t2 - 8 = " ;
t.display();
t = t1;
t = t ++;
cout << endl;
cout << "t1++ = ";
t.display();
t = t1;
t = ++t;
cout << "++t1 = ";
t.display();
t = t2;
t = t --;
cout << "t2-- = ";
t.display();
t2 = --t;
cout << "--t2 = ";
t.display();
t1 +=t2;
cout << endl;
cout << "t1 += t2,t1 = " ;
t1.display();
t2.display ();
t2 -= t1;
cout << "t2 -= t1,t2 = " ;
t2.display();
t1 += 5;
cout << "t1 += 5 = " ;
t1.display();
t1 -= 6;
cout << "t1 -= 6 = " ;
t1.display();
system("pause");
}