/*
02.*烟台大学计算机学院学生
03.*All right reserved.
04.*文件名称*烟台大学计算机学院学生
05.*All right reserved.
06.*文件名称:Time类中的运算符重载。
07.*作者:王洪海
08.*完成日期:2013年4月22日
09.*版本号:v1.0
10.*对任务及求解方法的描述部分:Time类中的运算符重载。
11.*/
#include <iostream>
using namespace std;
class CTime
{
private:
unsigned short int hour; // 时
unsigned short int minute; // 分
unsigned short int sec; // 秒
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为: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 s1);//返回s秒后的时间
CTime operator-=(int s1);//返回s秒前的时间
//一目运算符的重载
CTime operator++(int); //后置++,下一秒
CTime operator++(); //前置++,下一秒
CTime operator--(int); //后置--,前一秒
CTime operator--(); //前置--,前一秒
//“<<”和“>>”运算流的重载
friend ostream& operator<<(ostream &,CTime &);
friend istream& operator>>(istream &,CTime &);
};
CTime::CTime(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void CTime::settime(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
cout<<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(sec>t.sec)
return true;
if(sec<t.sec)
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(sec<t.sec)
return true;
if(sec>t.sec)
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(hour==t.hour&&minute==t.minute&&sec==t.sec)
return true;
else
return false;
}
bool CTime::operator!= (CTime &t)
{
if(*this==t)
return false;
else
return true;
}
CTime CTime::operator+(CTime &t)
{
CTime t1;
t1.hour=hour+t.hour;
t1.minute=minute+t.minute;
t1.sec=sec+t.sec;
if(t1.sec>=60)
{
++t1.minute;
t1.sec=t1.sec-60;
}
if(t1.minute>=60)
{
++t1.hour;
t1.minute=t1.minute-60;
}
if(t1.hour>24)
{
t1.hour=t1.hour-24;
}
return t1;
}
CTime CTime::operator-(CTime &t)
{
int h,m,s;
h=hour-t.hour;
m=minute-t.minute;
s=sec-t.sec;
if(s<0)
{
s=s+60;
m--;
}
if(m<0)
{
m+=60;
h--;
}
if(h<0)
{
h+=24;
}
CTime t2(h,m,s);
return t2;
}
CTime CTime::operator+(int s1)
{
int h,m,s;
s=s1%60;
m=s1/60;
h=s1/3600;
CTime t3(h,m,s);
return *this+t3;
}
CTime CTime::operator-(int s1)
{
int h,m,s;
s=s1%60;
m=s1/60;
h=s1/3600;
CTime t4(h,m,s);
return *this-t4;
}
CTime CTime::operator+=(CTime &c)
{
*this=*this+c;
return *this;
}
CTime CTime::operator-=(CTime &c)
{
*this=*this-c;
return *this;
}
CTime CTime::operator+=(int s1)
{
*this=*this+s1;
return *this;
}
CTime CTime::operator-=(int s1)
{
*this=*this-s1;
return *this;
}
CTime CTime::operator++(int)
{
CTime temp (*this);
sec++;
if(sec>=60)
{
sec-=60;
++minute;
}
if(minute>=60)
{
minute-=60;
++hour;
}
if(hour>=24)
hour-=24;
return temp;
}
CTime CTime::operator++()
{
if(sec++>=60)
{
sec-=60;
++minute;
}
if(minute>=60)
{
minute-=60;
++hour;
}
if(hour>=24)
hour-=24;
return *this;
}
CTime CTime::operator--(int)
{
CTime temp (*this);
sec--;
if(sec<0)
{
sec+=60;
minute-=1;
}
if(minute<0)
{
minute+=60;
hour-=1;
}
if(hour<0)
hour+=24;
return temp;
}
CTime CTime::operator--()
{
if(sec--<0)
{
sec+=60;
minute-=1;
}
if(minute<0)
{
minute+=60;
hour-=1;
}
if(hour<0)
{
hour+=24;
}
return *this;
}
ostream& operator<<(ostream &output,CTime &t)
{
output<<t.hour<<":"<<t.minute<<":"<<t.sec;
return output;
}
istream& operator>>(istream &input,CTime &t)
{
input>>t.hour>>t.minute>>t.sec;
return input;
}
//自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果
int main()
{
CTime t1(12,25,35),t2,t3,t;
cout<<" t1为:";
cout<<t1<<endl;
cout<<endl;
cout<<"请输入t2的值: ";
cin>>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;
if(t1!=t2) cout<<"t1不等于t2"<<endl;
cout<<endl;
t=t1+t2;
cout<<"t1与t2相加后的时间点为:";
cout<<t<<endl;
t=t1-t2;
cout<<"t1与t2相减后的时间点为:";
t.display();
cout<<"增加600秒后的时间点:"<<endl;
t=t1+600;
cout<<t<<endl;
cout<<"减去600秒后的时间点:"<<endl;
t=t1-600;
t.display();
cout<<"加上一个时间赋予本身:"<<endl;
t1+=t2;
t1.display();
cout<<"减去一个时间赋予本身:"<<endl;
t1-=t2;
cout<<t1<<endl;
cout<<"加上600秒后赋予本身:"<<endl;
t1+=600;
t1.display();
cout<<"减去600秒后赋予本身:"<<endl;
t1-=600;
cout<<t1<<endl;
cout<<endl;
t3=t2++;
cout<<"t2++: ";
cout<<t3<<endl;
cout<<"t2: ";
cout<<t2<<endl;
t3=++t2;
cout<<"++t2: ";
cout<<t3<<endl;
t3=t1--;
cout<<"t1--: ";
cout<<t3<<endl;
t3=--t1;
cout<<"--t1: ";
cout<<t3<<endl;
return 0;
}
运行结果,如下图: