[cpp]
/*
* 程序的版權和版本聲明部分
* Copyright (c)2012, 煙台大學計算機學院學生
* All rightsreserved.
* 文件名稱: object.cpp
* 作者:隋 鑫
* 完成日期: 2013年 4 月 19 日
* 版本號: 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);
//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 &c2);//返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2為:41:15
CTime operator-(CTime &c2);//對照+理解
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);
//CTime operator-=(int s);
};
//下面實現所有的運算符重載代碼。
//為簡化編程,請注意通過調用已有函數,利用好各函數之間的關系
CTime::CTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
bool CTime::operator > (CTime &t)
{
if(hour>t.hour)
return true;
else if(hour==t.hour&&minute>t.minute)
return true;
else if(minute==t.minute&&second>t.second)
return true;
else
return false;
}
bool CTime::operator < (CTime &t)
{
if(hour<t.hour)
return true;
else if(hour==t.hour&&minute<t.minute)
return true;
else if(minute==t.minute&&second<t.second)
return true;
else
return false;
}
bool CTime::operator >= (CTime &t)
{
if(hour>=t.hour)
return true;
else if(hour==t.hour&&minute>=t.minute)
return true;
else if(minute==t.minute&&second>=t.second)
return true;
else
return false;
}
bool CTime::operator <= (CTime &t)
{
if(hour<=t.hour)
return true;
else if(hour==t.hour&&minute<=t.minute)
return true;
else if(minute==t.minute&&second<=t.second)
return true;
else
return false;
}
bool CTime::operator == (CTime &t)
{
if(hour==t.hour&&minute==t.minute&&second==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(second!=t.second)
return true;
else
return false;
}
CTime CTime::operator+(CTime &c2)
{
CTime c;
c.second=second+c2.second;
c.minute=minute+c2.minute;
if(c.second>59)
c.second-=60;
c.minute++;
c.hour=hour+c2.hour;
if(c.minute>59)
c.minute-=60;
c.hour++;
if(c.hour>23)
c.hour-=24;
return c;
}
CTime CTime::operator-(CTime &c2)
{
CTime c;
c.second=second-c2.second;
c.minute=minute-c2.minute;
if(c.second>59)
c.second+=60;
c.minute--;
c.hour=hour-c2.hour;
if(c.minute>59)
c.minute+=60;
c.hour--;
if(c.hour>23)
c.hour+=24;
return c;
}
CTime CTime::operator+(int s)
{
CTime c;
c.second=second+s;
c.minute=minute;
c.hour=hour;
if(c.second>59)
c.minute=c.minute+c.second/60;
c.second%=60;
if(c.minute>69)
c.hour=c.hour+c.minute/60;
if(c.hour>23)
c.hour%=24;
return c;
}
CTime CTime::operator-(int s)
{
CTime c;
c.second=second-s%60;
c.minute=minute-s/60%60;
c.hour=hour-s/3600%24;
if(c.second>59)
c.second+=60;
c.minute--;
if(c.minute>59)
c.minute+=60;
c.hour--;
if(c.hour>23)
c.hour+=24;
return c;
}
CTime CTime::operator++( int)
{
CTime temp(*this);
second++;
{
if(second>=60)
{
second-=60;
++minute;
}
else if(minute>=60)
{
minute-=60;
++hour;
}
else if (hour>=24)
hour-=24;
}
return *this ;
}
CTime CTime::operator++()
{
{
if(++second>=60)
{
second-=60;
++minute;
}
else if(minute>=60)
{
minute-=60;
++hour;
}
else if (hour>=24)
hour-=24;
}
return *this ;
}
CTime CTime::operator--( int)
{
CTime temp(*this);
second--;
{
if(second>=60)
{
second+=60;
--minute;
}
else if(minute>=60)
{
minute+=60;
--hour;
}
else if (hour>=24)
hour+=24;
}
return *this ;
}
CTime CTime:: operator--()
{
{
if(--second>=60)
{
second+=60;
--minute;
}
else if(minute>=60)
{
minute+=60;
--hour;
}
else if (hour>=24)
hour+=24;
}
return *this ;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
CTime t1(8,20,25),t2(11,20,50),t;
int s=500;
cout<<"t1為:";
t1.display();
cout<<"t2為:";
t2.display();
cout<<"s為:"<<s<<"s"<<endl;
cout<<endl;
cout<<"下面比較兩個時間大小:\n";
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();
cout<<endl;
t=t1+s;
cout<<"t1+s=";
t.display();
t=t2+s;
cout<<"t2+s=";
t.display();
t=t1-s;
cout<<"t1-s=";
t.display();
t=t2-s;
cout<<"t2-s=";
t.display();
cout<<endl;
t=t1++;
cout<<"t1++=";
t.display();
t=t1--;
cout<<"t1--=";
t.display();
t=t1--;
cout<<"t1--=";
t.display();
t=t2--;
cout<<"t2--=";
t.display();
t=++t1;
cout<<"++t1=";
t.display();
t=++t2;
cout<<"++t2=";
t.display();
t=--t1;
cout<<"--t1=";
t.display();
t=--t2;
cout<<"--t2=";
t.display();
cout<<endl;
return 0;
}
/*
* 程序的版權和版本聲明部分
* Copyright (c)2012, 煙台大學計算機學院學生
* All rightsreserved.
* 文件名稱: object.cpp
* 作者:隋 鑫
* 完成日期: 2013年 4 月 19 日
* 版本號: 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);
//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 &c2);//返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2為:41:15
CTime operator-(CTime &c2);//對照+理解
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);
//CTime operator-=(int s);
};
//下面實現所有的運算符重載代碼。
//為簡化編程,請注意通過調用已有函數,利用好各函數之間的關系
CTime::CTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
bool CTime::operator > (CTime &t)
{
if(hour>t.hour)
return true;
else if(hour==t.hour&&minute>t.minute)
return true;
else if(minute==t.minute&&second>t.second)
return true;
else
return false;
}
bool CTime::operator < (CTime &t)
{
if(hour<t.hour)
return true;
else if(hour==t.hour&&minute<t.minute)
return true;
else if(minute==t.minute&&second<t.second)
return true;
else
return false;
}
bool CTime::operator >= (CTime &t)
{
if(hour>=t.hour)
return true;
else if(hour==t.hour&&minute>=t.minute)
return true;
else if(minute==t.minute&&second>=t.second)
return true;
else
return false;
}
bool CTime::operator <= (CTime &t)
{
if(hour<=t.hour)
return true;
else if(hour==t.hour&&minute<=t.minute)
return true;
else if(minute==t.minute&&second<=t.second)
return true;
else
return false;
}
bool CTime::operator == (CTime &t)
{
if(hour==t.hour&&minute==t.minute&&second==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(second!=t.second)
return true;
else
return false;
}
CTime CTime::operator+(CTime &c2)
{
CTime c;
c.second=second+c2.second;
c.minute=minute+c2.minute;
if(c.second>59)
c.second-=60;
c.minute++;
c.hour=hour+c2.hour;
if(c.minute>59)
c.minute-=60;
c.hour++;
if(c.hour>23)
c.hour-=24;
return c;
}
CTime CTime::operator-(CTime &c2)
{
CTime c;
c.second=second-c2.second;
c.minute=minute-c2.minute;
if(c.second>59)
c.second+=60;
c.minute--;
c.hour=hour-c2.hour;
if(c.minute>59)
c.minute+=60;
c.hour--;
if(c.hour>23)
c.hour+=24;
return c;
}
CTime CTime::operator+(int s)
{
CTime c;
c.second=second+s;
c.minute=minute;
c.hour=hour;
if(c.second>59)
c.minute=c.minute+c.second/60;
c.second%=60;
if(c.minute>69)
c.hour=c.hour+c.minute/60;
if(c.hour>23)
c.hour%=24;
return c;
}
CTime CTime::operator-(int s)
{
CTime c;
c.second=second-s%60;
c.minute=minute-s/60%60;
c.hour=hour-s/3600%24;
if(c.second>59)
c.second+=60;
c.minute--;
if(c.minute>59)
c.minute+=60;
c.hour--;
if(c.hour>23)
c.hour+=24;
return c;
}
CTime CTime::operator++( int)
{
CTime temp(*this);
second++;
{
if(second>=60)
{
second-=60;
++minute;
}
else if(minute>=60)
{
minute-=60;
++hour;
}
else if (hour>=24)
hour-=24;
}
return *this ;
}
CTime CTime::operator++()
{
{
if(++second>=60)
{
second-=60;
++minute;
}
else if(minute>=60)
{
minute-=60;
++hour;
}
else if (hour>=24)
hour-=24;
}
return *this ;
}
CTime CTime::operator--( int)
{
CTime temp(*this);
second--;
{
if(second>=60)
{
second+=60;
--minute;
}
else if(minute>=60)
{
minute+=60;
--hour;
}
else if (hour>=24)
hour+=24;
}
return *this ;
}
CTime CTime:: operator--()
{
{
if(--second>=60)
{
second+=60;
--minute;
}
else if(minute>=60)
{
minute+=60;
--hour;
}
else if (hour>=24)
hour+=24;
}
return *this ;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
CTime t1(8,20,25),t2(11,20,50),t;
int s=500;
cout<<"t1為:";
t1.display();
cout<<"t2為:";
t2.display();
cout<<"s為:"<<s<<"s"<<endl;
cout<<endl;
cout<<"下面比較兩個時間大小:\n";
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();
cout<<endl;
t=t1+s;
cout<<"t1+s=";
t.display();
t=t2+s;
cout<<"t2+s=";
t.display();
t=t1-s;
cout<<"t1-s=";
t.display();
t=t2-s;
cout<<"t2-s=";
t.display();
cout<<endl;
t=t1++;
cout<<"t1++=";
t.display();
t=t1--;
cout<<"t1--=";
t.display();
t=t1--;
cout<<"t1--=";
t.display();
t=t2--;
cout<<"t2--=";
t.display();
t=++t1;
cout<<"++t1=";
t.display();
t=++t2;
cout<<"++t2=";
t.display();
t=--t1;
cout<<"--t1=";
t.display();
t=--t2;
cout<<"--t2=";
t.display();
cout<<endl;
return 0;
}
運行結果: