<img src="https://img-blog.csdn.net/20150513081909860?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2QxMjAy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称: test.cpp
*作 者:陈丹
*完成日期:2015年5月13日
*版本号:v1.0
*
*问题描述:在Time类中的运算符重载基础上
(1)定义对时间对象的自增和自减一目运算符
(2)定义Time类中的<<和>>运算符重载,实现时间的输入输出
*输入描述:
*程序输出:
*/
#include <iostream>
#include <iomanip>
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);
friend ostream& operator << (ostream&output,const CTime &c);
//二目的比较运算符重载
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--();//前置--,前一秒
};
void CTime::setTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
ostream& operator << (ostream&output,const CTime &c)
{
output<<setw(2)<<setfill('0')<<c.hour<<":";
output<<setw(2)<<setfill('0')<<c.minute<<":";
output<<setw(2)<<setfill('0')<<c.second;
return output;
}
bool CTime::operator > (CTime &t)
{
int t1,t2;
t1=hour*3600+minute*60+second;
t2=t.hour*3600+t.minute*60+t.second;
if(t1>t2)
return true;
else
return false;
}
bool CTime::operator < (CTime &t)
{
int t1,t2;
t1=hour*3600+minute*60+second;
t2=t.hour*3600+t.minute*60+t.second;
if(t1<t2)
return true;
else
return false;
}
bool CTime::operator <= (CTime &t)
{
return !(*this>t);
}
bool CTime::operator >= (CTime &t)
{
return !(*this<t);
}
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)
{
return !(*this==t);
}
CTime CTime::operator + (CTime &t)
{
CTime t1;
t1.second=second+t.second;
t1.minute=minute+t.minute;
t1.hour=hour+t.hour;
if(t1.second>59)
{
t1.second%=60;
t1.minute++;
}
if(t1.minute>59)
{
t1.minute%=60;
t1.hour++;
}
if(t1.hour>23)
{
hour%=24;
}
return t1;
}
CTime CTime::operator - (CTime &t)
{
int h,m,s;
s=second-t.second;
m=minute-t.minute;
h=hour-t.hour;
if (s<0)
{
s+=60;
m--;
}
if (m<0)
{
m+=60;
h--;
}
while (h<0) h+=24;
CTime t0(h,m,s);
return t0;
}
CTime CTime::operator+(int s)
{
CTime t1(0,0,s);
return *this+t1;
}
CTime CTime::operator-(int s)
{
CTime t1(0,0,s);
return *this-t1;
}
//可以直接使用已经重载了的加减运算实现
//这种赋值, 例如 t1+=20,直接改变当前对象的值,所以在运算完成后,将*this作为返回值
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)//返回s秒后的时间
{
int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
CTime t0(hh,mm,ss);
*this=*this+t0;
return *this;
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{
int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
CTime t0(hh,mm,ss),t;
*this=*this-t0;
return *this;
}
//下面定义用于测试的main()函数
//一目运算符的重载
CTime CTime::operator++(int)//后置++,下一秒
{
CTime c=*this;
*this+=1;
return c;
}
CTime CTime::operator++()//前置++,下一秒,前置与后置返回值不一样
{
*this+=1;
return *this;
}
CTime CTime::operator--( int)//后置--,前一秒
{
CTime c=*this;
*this-=1;
return c;
}
CTime CTime::operator--()//前置--,前一秒
{
*this-=1;
return *this;
}
int main()
{
CTime t1,t2,t3;
int s=20;
t1.setTime(8,20,25);
t2.setTime(11,20,50);
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<<"="<<t1+t2<<endl;
cout<<t1<<"-"<<t2<<"="<<t1-t2<<endl;
cout<<t1<<"+"<<s<<"s"<<"=";
t3=t1+s;
cout<<t3<<endl;
cout<<t1<<"-"<<s<<"s"<<"=";
t3=t1-s;
cout<<t3<<endl;
cout<<t1<<"+="<<t2<<"=";
t1+=t2;
cout<<t1<<endl;
cout<<t1<<"-="<<t2<<"=";
t1-=t2;
cout<<t1<<endl;
cout<<t1<<"+="<<s<<"s"<<" = ";
t1+=s;
cout<<t1<<endl;
cout<<t1<<"-="<<s<<"s"<<" = ";
t1-=s;
cout<<t1<<endl;
cout<<t1<<"++ = ";
cout<<(t1++)<<endl;
cout<<"++"<<t1<<" = ";
cout<<(++t1)<<endl;
cout<<t2<<"-- = ";
cout<<(t2--)<<endl;
cout<<"--"<<t2<<" = ";
cout<<(--t2)<<endl;
return 0;
}
运行结果: