您的位置:首页 > 其它

第8周项目2-Time类中的运算符重载

2015-04-29 09:04 507 查看
/*
*Copyright(c)2014,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作者:王元阳
*完成日期:2015年 4月29日
*版本号:v1.0
*问题描述:定义一个定义完整的类(是可以当作独立的产品发布,成为众多项目中的“基础工程”)。这样的类在(2)的基础上,扩展+、-、*、/运算符的功能,使之能与double型数据进行运算。设Complex c; double d; c+d和d+c的结果为“将d视为实部为d的复数同c相加”,其他-、*、/运算符类似。
*输入描述:
*程序输出:编写的程序
*/

#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为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::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;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
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;
if (t1>=t2) cout<<"t1≥t2"<<endl;
if (t1<=t2) cout<<"t1≤t2"<<endl;
cout<<endl;
//在测试下面的代码时,请采用单步执行的方法跟踪
t=t1+t2;
t=t1-t2;
t=t1+2000;
t=t1-5000;
t1+=t2;
t1-=t2;
t1+=2000;
t1-=5000;
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: