#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);
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);
//二目的加減運算符的重載
//返回t規定的時、分、秒後的時間
//例t1(8,20,25),t2(11,20,50),t1+t2爲19:41:15
CTime operator+(CTime &t);
CTime operator-(CTime &t);//對照+理解
CTime operator+(int s);//返回s秒後的時間
CTime operator-(int s);//返回s秒前的時間
//二目賦值運算符的重載
CTime operator+=(CTime &c);
CTime operator-=(CTime &c);
CTime operator+=(int s);//返回s秒後的時間
CTime operator-=(int s);//返回s秒前的時間
//新增:一目運算符的重載
CTime operator++(int);//後置++,下一秒
CTime operator++();//前置++,下一秒,前置與後置返回值不一樣
CTime operator--( int);//後置--,前一秒
CTime operator--();//前置--,前一秒
};
//
CTime CTime::operator++(int)
{
CTime t=*this;
*this+=1;
return t;//!!
}
CTime CTime::operator++()
{
*this+=1;
return *this;//!!
}
CTime CTime::operator--(int)
{
CTime t=*this;
*this-=1;
return t;
}
CTime CTime::operator--()
{
*this-=1;
return *this;
}
//
CTime::CTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
return;
}
void CTime::setTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
return;
}
void CTime::display()
{
cout<<hour<<": "<<minute<<": "<<second<<endl;
return;
}
//
bool CTime::operator > (CTime &t)
{
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;
if (second<t.second)return false;
}
bool CTime::operator < (CTime &t)
{
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;
if (second>t.second)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)return false;
else return true;
}
//
CTime CTime::operator+(CTime &t)
{
int h,m,s;
h=hour+t.hour;
m=minute+t.minute;
s=second+t.second;
if (s>59)
{
m++;
s-=60;//(莫忘了這一步)
}
if (m>59)
{
h++;
m-=60;
}
if (h>23)
{
h-=24;
}
CTime t1(h,m,s);//注意不能寫成 hour=h;minute=m;second=s; 應不改變原值
return t1;
}
CTime CTime::operator-(CTime &t)
{
int h,m,s;
h=hour-t.hour;
m=minute-t.minute;
s=second-t.second;
if (s<0)
{
m--;
s+=60;//莫忘了這一步
}
if (m<0)
{
h--;
m+=60;
}
if (h<0)
{
h+=24;
}
CTime t1(h,m,s);//注意不能寫成 hour=h;minute=m;second=s; 應不改變原值
return t1;
}
CTime CTime::operator+(int s)
{
int ss=s%60, mm=(s/60)%60, hh=s/3600;
CTime t(hh,mm,ss);
return *this+t;
/*之前用的下面這段,有點複雜了,而且減法不好處理
int ss=second+s,mm=minute,hh=hour;
if (ss>59)
{
mm+=ss/60;
ss%=60;
}
if (mm>59)
{
hh+=mm/60;
mm%=60;
}
if (hh>23)
{
hh%=24;
}
CTime t1(hh,mm,ss);
return t1;
*/
}
CTime CTime::operator-(int s)
{
int ss=s%60, mm=(s/60)%60, hh=s/3600;
CTime t(hh,mm,ss);
return *this-t;
}
//
CTime CTime::operator+=(CTime &c)
{
*this=*this+c;
return *this;
}
CTime CTime::operator-=(CTime &c)
{
*this=*this-c;
return *this;
}
CTime CTime::operator+=(int s)
{
*this=*this+s;
return *this;
}
CTime CTime::operator-=(int s)
{
*this=*this-s;
return *this;
}
int main()
{
CTime t1(20,30,41),t2(13,32,20),t3(20,11,36),tt1,tt2,tt3;
cout<<"t1時刻爲:";
t1.display();
cout<<"t2時刻爲:";
t2.display();
cout<<"t3時刻爲:";
t3.display();
cout<<endl;
//
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;
if (t1<=t2)
cout<<"t1≤t2"<<endl;
cout<<endl;
//
cout<<"t2與t3時刻比較大小:"<<endl;
if (t2>t3)
cout<<"t2>t3"<<endl;
if (t1<t3)
cout<<"t2<t3"<<endl;
if (t2==t3)
cout<<"t2=t3"<<endl;
if (t2!=t3)
cout<<"t2≠t3"<<endl;
if (t2>=t3)
cout<<"t2≥t3"<<endl;
if (t2<=t3)
cout<<"t2≤t3"<<endl;
cout<<endl;
//在測試下面的代碼時,請採用單步執行的方法跟蹤
cout<<"下面執行時間計算更改調試:"<<endl;
cout<<"t1+t2=";
tt1=t1+t2;
tt1.display();
cout<<"t2-t3=";
tt2=t2-t3;
tt2.display();
cout<<"t1+2000s=";
tt3=t1+2000;
tt3.display();
cout<<"t1+2000s-5000s=";
tt3=tt3-5000;
tt3.display();
cout<<"t1+=t2: ";
t1+=t2;
t1.display();
cout<<"t2-=t3: ";
t2-=t3;
t2.display();
cout<<"t3+=100: ";
t3+=100;
t3.display();
cout<<"t3'-=600: ";
t3-=600;
t3.display();
cout<<"t1++ :";
tt1=t1++;
tt1.display();
cout<<"++t1 :";
tt1=++t1;
tt1.display();
cout<<"t1-- :";
tt1=t1--;
tt1.display();
cout<<"--t1 :";
tt1=--t1;
tt1.display();
return 0;
}
運行結果