您的位置:首页 > 其它

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

2014-04-15 20:16 537 查看
/*
*程序的版权和版本声明部分:
*Copyright(c)2013,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:尚振伟
*完成日期:2014年4月15日
*版本号:v0.1
*对任务及求解方法的描述部分:
*输入描述:无
*问题描述:实现Time类中的运算符重载
*程序输入:
*程序输出:
*问题分析:
*算法设计:
*我的程序:
*/

#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)
{
hour=h;
minute=m;
second=s;
}
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秒前的时间
};
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;
}
if(hour==t.hour)
{
if(minute>t.minute)
{
return true;
}
}
if(hour==t.hour&&minute==t.minute)
{
if(second>t.second)
{
return true;
}
}
else
{
return false;
}
}
bool CTime::operator<(CTime &t)
{
if(hour<t.hour)
{
return true;
}
if(hour==t.hour)
{
if(minute<t.minute)
{
return true;
}
}
if(hour==t.hour&&minute==t.minute)
{
if(second<t.second)
{
return true;
}
}
else
{
return false;
}
}
bool CTime::operator>=(CTime &t)
{
if(!(*this<t))
{
return true;
}
else
{
return false;
}
}
bool CTime::operator <= (CTime &t)
{
if(!(*this>t))
{
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;
}
}
CTime CTime::operator+(CTime &t)
{
int h,m,s;
h=hour+t.hour;
m=minute+t.minute;
s=second+t.second;
while(s>=60)
{
s-=60;
m+=1;
}
while(m>=60)
{
m-=60;
h+=1;
}
while(h>=24)
{
h-=24;
}
CTime t1(h,m,s);
return t1;
}
CTime CTime::operator-(CTime &t)
{
int h,m,s;
h=hour-t.hour;
m=minute-t.minute;
s=second-t.second;
while(s<0)
{
s+=60;
m-=1;
}
while(m<0)
{
m+=60;
h-=1;
}
while(h<0)
{
h+=24;
}
CTime t1(h,m,s);
return t1;
}
CTime CTime::operator+(int s)
{
int h,m,s1;
s1=s%60;
m=(s/60)%60;
h=s/3600;
CTime t1(h,m,s1);
return *this+t1;
}
CTime CTime::operator-(int s)
{
int h1,m1,s1;
s1=s%60;
m1=(s/60)%60;
h1=s/3600;
CTime t1(h1,m1,s1);
return *this-t1;
}
CTime CTime::operator+=(CTime &c)
{
hour+=c.hour;
minute+=c.minute;
second+=c.second;
while(second>=60)
{
second-=60;
minute+=1;
}
while(minute>=60)
{
minute-=60;
hour+=1;
}
while(hour>=24)
{
hour-=24;
}
CTime t1(hour,minute,second);
return t1;
}
CTime CTime::operator-=(CTime &c)
{
*this=*this-c;
return *this;
}
CTime CTime::operator+=(int s)
{
second+=s;
while(second>60)
{
second-=60;
minute++;
while(minute>60)
{
minute-=60;
hour++;
while(hour>24)
{
hour-=24;
}
}
}
return CTime(hour,minute,second);
}
CTime CTime::operator-=(int s)
{
*this=*this-s;
return *this;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}

//自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果
int main()
{
CTime t1(8,20,25),t2(11,20,50),t;
cout<<"t1=";
t1.display();
cout<<"t2=";
t2.display();
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;
t=t1+t2;
t.display();
t=t1-t2;
t.display();
t=t1+6000;
t.display();
t=t1-600;
t.display();
t1+=t2;
t1.display();
t1-=t2;
t1.display();
t1+=1200;
t1.display();
t1-=600;
t1.display();
return 0;
}


结果展示:



心得体会:唉,被这道题困惑了一下午,刚开始思路什么都特别清晰,就是运行结果不对,越改越乱,今天要是不把它整出来,估计今夜要失眠。。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: