/*
* 程序的版权和版本声明部分:
* Copyright (c) 2013.烟台大学计算机学院
* All rights reserved.
* 文件名称:Time 类中的运算符重载
* 作 者:冯冬影
* 完成日期:2014 年 4月 14日
* 版 本 号: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);
//二目的加减运算符的重载
//返回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 s);//返回s秒后的时间
CTime operator-=(int s);//返回s秒前的时间
};
//下面实现所有的运算符重载代码。
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;
}
void CTime::display()
{
cout<<"("<<hour<<","<<minute<<","<<second<<")"<<endl;
}
CTime CTime::operator+(CTime &t)
{
CTime T;
T.second=second+t.second;
T.minute=minute+t.minute;
T.hour=hour+t.hour;
if(T.second>=60)
{
T.second=T.second-60;
T.minute=T.minute+1;
}
if(T.minute>=60)
{
T.minute=T.minute-60;
T.hour=T.hour+1;
}
if(T.hour>=24)
T.hour=T.hour-24;
return T;
}
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)//返回s秒后的时间
{
CTime T;
T.hour=s/3600;
T.minute=s/60;
T.second=s%60;
return (*this+T);
}
CTime CTime::operator-(int s)//返回s秒前的时间
{
CTime T;
T.hour=s/3600;
T.minute=s/60;
T.second=s%60;
return (*this-T);;
}
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秒后的时间
{
*this=*this+s;
return *this;
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{
*this=*this-s;
return *this;
}
bool CTime::operator > (CTime &t)
{
if(hour>t.hour)
return true;
else
return false;
if(minute>t.minute)
return true;
else
return false;
if(second>t.second)
return true;
else
return false;
}
bool CTime::operator < (CTime &t)
{
if(hour<t.hour)
return true;
else
return false;
if(minute<t.minute)
return true;
else
return false;
if(second<t.second)
return true;
else
return false;
}
bool CTime::operator >= (CTime &t)
{
if(hour>=t.hour)
return true;
else
return false;
if(minute>=t.minute)
return true;
else
return false;
if(second>=t.second)
return true;
else
return false;
}
bool CTime::operator <= (CTime &t)
{
if(hour<=t.hour)
return true;
else
return false;
if(minute<=t.minute)
return true;
else
return false;
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;
}
//自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果
int main()
{
CTime t1(18,20,25),t2(12,20,50),t4(12,25,30),t3;
cout<<"t1=";
t1.display();
cout<<"t2=";
t2.display();
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<<"t4=";
t4.display();
t3=t1+t2;
cout<<"t1+t2=";
t3.display();
t3=t1-t2;
cout<<"t1-t2=";
t3.display();
int a=360;
t3=t1+a;
cout<<"a="<<a<<endl;
cout<<"t1+a=";
t3.display();
t1+=a;
cout<<"t1自加"<<a<<"之后为:";
t1.display();
t3=t4-a;
cout<<"t4-a=";
t3.display();
t4-=a;
cout<<"t4自减"<<a<<"之后为:";
t4.display();
return 0;
}
运行结果