/*
02.* 程序的版权和版本声明部分
03.* Copyright (c)2012, 烟台大学计算机学院学生
04.* All rightsreserved.
05.* 作者: 董万鹏
06.* 完成日期: 2013年 4 月 22 日
07.* 版本号: v1.0
08.* 输入描述:无
09.* 问题描述:无
10.* 程序输出:无
*/
#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);
//二目运算符的重载
CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
CTime operator-(CTime &c);//对照+理解
CTime operator+(int s);//返回s秒后的时间
CTime operator-(int s);//返回s秒前的时间
//一目运算符的重载
CTime operator++(int);//后置++,下一秒
CTime operator++();//前置++,下一秒,前置与后置返回值不一样
//赋值运算符的重载
CTime operator+=(CTime &c);
CTime operator+=(int s);
};
//下面实现所有的运算符重载代码。
//为简化编程,请注意通过调用已有函数,利用好各函数之间的关系
void CTime::setTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void CTime::display()
{
if(second>60)
{
int t=second/60;
second%=60;
minute+=t;
}
if(minute>60)
{
int x=minute/60;
minute%=60;
hour+=x;
}
if(hour>24)
{
hour%=24;
}
cout<<hour<<"点"<<minute<<"分"<<second<<"秒"<<endl;
}
bool CTime::operator>(CTime &t)
{
if(hour>t.hour)
return true;
else if(hour<t.hour)
return false;
else
{
if(minute>t.minute)
return true;
else if(minute<t.minute)
return false;
else
{
if(second>t.second)
return true;
else
return false;
}
}
}
bool CTime::operator<(CTime &t)
{
if(hour<t.hour)
return true;
else if(hour>t.hour)
return false;
else
{
if(minute<t.minute)
return true;
else if(minute>t.minute)
return false;
else
{
if(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 || minute!=t.minute ||second!=t.second)
return true;
else
return false;
}
CTime CTime::operator+(CTime &c)
{
CTime c1;
c1.hour=hour+c.hour;
c1.minute=minute+c.minute;
c1.second=second+c.second;
return c1;
}
CTime CTime::operator-(CTime &c)
{
CTime c1;
c1.hour=hour-c.hour;
c1.minute=minute-c.minute;
c1.second=second-c.second;
return c1;
}
CTime CTime::operator+(int s)
{
CTime t1;
cout<<"t1增加"<<s<<"秒后的时间是:"<<endl;
t1.hour=hour;
t1.minute=minute;
t1.second=second+s;
return t1;
}
CTime CTime::operator-(int s)
{
CTime t1;
int num=0;
cout<<"t1减少"<<s<<"秒后的时间是:"<<endl;
t1.hour=hour;
t1.minute=minute;
t1.second=second;
while(t1.second<s)
{
t1.second+=60;
++num;
}
t1.minute-=num;
t1.second-=s;
return t1;
}
CTime CTime::operator++(int)
{
CTime t(*this);
second++;
if(second>=60)
{
second-=60;
++minute;
}
if(minute>=60)
{
minute-=60;
++hour;
}
if(hour>23)
{
hour-=24;
}
return t;
}
CTime CTime::operator++()
{
if(++second>=60)
{
second-=60;
++minute;
}
if(minute>=60)
{
minute-=60;
++hour;
}
if(hour>=24)
{
hour-=24;
}
return *this;
}
CTime CTime::operator+=(CTime &c)
{
CTime c1;
c1.hour=hour+c.hour;
c1.minute=minute+c.minute;
c1.second=second+c.second;
return c1;
}
CTime CTime::operator+=(int s)
{
second+=s;
return *this;
}
int main()
{
CTime t1(8,20,25),t2(11,20,50),t;
cout<<"t1为:";
t1.display();
cout<<"t2为:";
t2.display();
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;
t=t1+t2;
cout<<"t1+t2后的时间是:";
t.display();
if(t1>t2)
{
t=t1-t2;
cout<<"t1-t2后的时间是:";
t.display();
}
else
{
t=t2-t1;
cout<<"t2-t1后的时间是:";
t.display();
}
t=t1+80;
t.display();
t=t1-100;
t.display();
t=t1++;
cout<<"t1++=";
t1.display();
cout<<"执行t1++之前的值为:";
t.display();
++t1;
cout<<"++t1= ";
t1.display();
t1+=t2;
cout<<"t1+=t2为:"<<endl;
t1.display();
t2+=5;
cout<<"t2+=5为:"<<endl;
t2.display();
return 0;
}