在Time类中的运算符重载基础上
(1)定义对时间对象的自增和自减一目运算符
//一目运算符的重载
CTime operator++(int);//后置++,下一秒
CTime operator++();//前置++,下一秒,前置与后置返回值不一样
CTime operator--( int);//后置--,前一秒
CTime operator--();//前置--,前一秒
(2)定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
代码
/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:辛彬
* 完成日期:2015 年 5 月 8 日
* 版 本 号: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);
//二目的比较运算符重载
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++(int);//后置++,下一秒
CTime operator++();//前置++,下一秒,前置与后置返回值不一样
CTime operator--(int);//后置--,前一秒
CTime operator--();//前置--,前一秒
//返回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秒前的时间
friend ostream& operator<<(ostream&,const CTime&);
friend istream& operator>>(istream&,CTime&);
};
CTime::CTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void CTime::setTime(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)
return false;
else if(minute>t.hour)
return true;
else if(minute<t.minute)
return false;
else if(second>t.hour)
return true;
else if(second<t.hour)
return false;
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.hour)
return true;
else if(minute>t.minute)
return false;
else if(second<t.hour)
return true;
else if(second>t.hour)
return false;
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)
{
int h=hour+t.hour;
int m=minute+t.minute;
int s=second+t.second;
if(s>=60)
{
s-=60;
m++;
}
if(m>=60)
{
m-=60;
h++;
}
if(h>=24)
{
h-=24;
}
CTime c(h,m,s);
return c;
}
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--;
}
if (h<0) h+=24;
CTime t0(h,m,s);
return t0;
}
CTime CTime::operator+(int s)
{
int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
CTime t(hh,mm,ss);
return *this+t;
}
CTime CTime::operator-(int s)
{
int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
CTime t(hh,mm,ss);
return *this-t;
}
CTime CTime::operator+=(CTime &t)
{
*this=*this+t;
return *this;
}
CTime CTime::operator-=(CTime &t)
{
*this=*this-t;
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+=1;
return t;
}
CTime CTime::operator++()
{
*this+=1;
return *this;
}
CTime CTime::operator--(int)
{
CTime t(*this);
*this-=1;
return t;
}
CTime CTime::operator--()
{
*this-=1;
return *this;
}
ostream& operator<<(ostream& out,const CTime& c)
{
out<<c.hour<<":"<<c.minute<<":"<<c.second;
return out;
}
istream& operator>>(istream& in,CTime& c)
{
cout<<"输入时,分,秒:";
in>>c.hour>>c.minute>>c.second;
return in;
}
int main()
{
CTime t1(8,20,25),t2(11,20,50),t;
cout<<"t1为:"<<t1<<endl;
cout<<"t2为:"<<t2<<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<<endl;
t=t1-t2;
cout<<"t1-t2="<<t<<endl;
t=t1+2000;
cout<<"t1+2000="<<t<<endl;
t=t1-5000;
cout<<"t1-5000="<<t<<endl;
t1+=t2;
cout<<"t1+=t2="<<t1<<endl;
t1-=t2;
cout<<"t1-=t2="<<t1<<endl;
t1+=2000;
cout<<"t1+=2000="<<t1<<endl;
t1-=5000;
cout<<"t1-=5000="<<t1<<endl;
cout<<"t1++="<<t1++<<endl;
cout<<"++t1="<<++t1<<endl;
cout<<"t2--="<<t2--<<endl;
cout<<"--t2="<<--t2<<endl;
return 0;
}
运行结果
原文地址:http://blog.csdn.net/blue_skyrim/article/details/45577423