/*
* Copyright (c) 2013, 煙臺大學計算機學院
* All rights reserved.
* 作 者:紀麗娜
* 完成日期:2014 年 4 月 15 日
* 版 本 號:v1.0
* 問題描述:。
*/ #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);
//二目的加減運算符的重載
//返回t規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2爲20: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--();//前置--,前一秒
};
//下面實現所有的運算符重載代碼。
//顯示時間
void CTime::display()
{cout<<hour<<':'<<minute<<':'<<second<<endl;}
//二目的比較運算符重載
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;
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;
return false;
}
bool CTime::operator >=(CTime &t)
{
if (*this<t) return false;
return true;
}
bool CTime::operator <= (CTime &t) // 判斷時間t1<=t2
{
if (*this > t) return false;
return true;
}
bool CTime::operator ==(CTime &t)
{
if (*this<t || *this>t) return false;
return true;
}
bool CTime::operator !=(CTime &t)
{
if (*this<t || *this>t)return true;
return false;
}
//二目的加減運算符的重載
//返回t規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2爲20:41:15
CTime CTime::operator+(CTime &t)
{
CTime t3;
t3.second=second+t.second;
t3.minute=minute+t.minute;
t3.hour=hour+t.hour;
if (t3.second>59)
{
t3.second-=60;
t3.minute++;
}
if (t3.minute>59)
{
t3.minute-=60;
t3.hour++;
}
if (t3.hour>23) t3.hour-=24;
return t3;
}
CTime CTime::operator-(CTime &t)
{
CTime c;
c=*this;
if(c.second<t.second)
{
c.second=c.second+60-t.second;
c.minute-=1;
}
else
{
c.second-=t.second;
}
if(c.minute<t.minute)
{
c.minute=c.minute+60-t.minute;
c.hour-=1;
}
else
{
c.minute-=t.minute;
}
if(c.hour<t.hour)
{
c.hour=c.hour+12-t.hour;
}
else
{
c.hour-=t.hour;
}
return c;
}
CTime CTime::operator+(int s)//返回s秒後的時間
{
CTime t0;
t0.second=s%60;
t0.minute=s/60;
t0.hour=s/3600;
return *this+t0;
}
CTime CTime::operator-(int s)//返回s秒前的時間
{
CTime t0;
t0.second=s%60;
t0.minute=s/60;
t0.hour=s/3600;
return *this-t0;
}
//二目運算符的重載
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;
}
//一目運算符的重載
CTime CTime::operator++(int)//後置++,下一秒
{
CTime t=*this;
*this=*this+1;
return t;
}
CTime CTime::operator++()//前置++,下一秒
{
*this=*this+1;
return *this;
}
CTime CTime::operator--(int)//後置--,前一秒
{
CTime t=*this;
*this=*this-1;
return t;
}
CTime CTime::operator--()//前置--,前一秒
{
*this=*this-1;
return *this;
}
//自行編制用於測試的main()函數,有些結果不必依賴display()函數,提倡用單步執行查看結果
int main()
{
CTime t1(8,20,25),t2(11,20,50),t;
cout<<"t1爲:";
t1.display();
cout<<"t2爲:";
t2.display();
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<<"下面表示兩個時間加減t1..t2形式:\n";
t=t1+t2;
cout<<"t1+t2=";
t.display();
t=t1-t2;
cout<<"t1-t2=";
t.display();
t=t1+2000; //秒
cout<<"t1+2000s=";
t.display();
t=t1-2000; //秒
cout<<"t1-2000s=";
t.display();
cout<<"下面表示一個時間的自加自減:\n";
t=t1++;
cout<<"t1++=";
t.display();
t=++t1;
cout<<"++t1=";
t.display();
t=t1--;
cout<<"(t1--)=";
t.display();
t=--t1;
cout<<"(--t1)=";
t.display();
cout<<"下面表示兩個時間加減t1.=t2形式:\n";
t1+=t2;
cout<<"t1+=t2";
t1.display();
t1-=t2;
cout<<"t1-=t2";
t1.display();
t1+=2000;
cout<<"t1+=2000";
t1.display();
t1-=2000;
cout<<"t1-=5000";
t1.display();
cout<<endl;
}
心得:~~~~(>_<)~~~~
真是。。。。哎。。。!!!!!
做完這個題我竟然還活着!!!!
奇蹟!!