您的位置:首页 > 其它

第八周(项目二)——Time类中的运算符重载。

2014-04-17 17:23 369 查看
/*
02.*烟台大学计算机学院学生
03.*All right reserved.
04.*文件名称*烟台大学计算机学院学生
05.*All right reserved.
06.*文件名称:Time类中的运算符重载。
07.*作者:王洪海
08.*完成日期:2013年4月17日
09.*版本号:v1.0
10.*对任务及求解方法的描述部分:Time类中的运算符重载。
11.*/

#include <iostream>

using namespace std;

class CTime
{
private:
unsigned short int hour;    // 时
unsigned short int minute;  // 分
unsigned short int sec;  // 秒
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 s1);//返回s秒后的时间
CTime operator-=(int s1);//返回s秒前的时间
};
CTime::CTime(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void CTime::settime(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
cout<<endl;
}
//下面实现所有的运算符重载代码。
bool CTime::operator > (CTime &t)
{
if(hour>t.hour)
return true;
if(hour<t.hour)
return false;
if(minute>t.minute)
return true;
if(minute<t.minute)
return false;
if(sec>t.sec)
return true;
if(sec<t.sec)
return false;
}
bool CTime::operator< (CTime &t)
{
if(hour<t.hour)
return true;
if(hour>t.hour)
return false;
if(minute<t.minute)
return true;
if(minute>t.minute)
return false;
if(sec<t.sec)
return true;
if(sec>t.sec)
return false;
}
bool CTime::operator>= (CTime &t)
{
if(*this<t)
return false;
else
return true;
}
bool CTime::operator<= (CTime &t)
{
if(*this>t)
return false;
else
return true;
}
bool CTime::operator == (CTime &t)
{
if(hour==t.hour&&minute==t.minute&&sec==t.sec)
return true;
else
return false;
}
bool CTime::operator!= (CTime &t)
{
if(*this==t)
return false;
else
return true;
}
CTime CTime::operator+(CTime &t)
{
CTime t1;
t1.hour=hour+t.hour;
t1.minute=minute+t.minute;
t1.sec=sec+t.sec;
if(t1.sec>=60)
{
++t1.minute;
t1.sec=t1.sec-60;
}
if(t1.minute>=60)
{
++t1.hour;
t1.minute=t1.minute-60;
}
if(t1.hour>24)
{
t1.hour=t1.hour-24;
}
return t1;
}
CTime CTime::operator-(CTime &t)
{
int h,m,s;
h=hour-t.hour;
m=minute-t.minute;
s=sec-t.sec;
if(s<0)
{
s=s+60;
m--;
}
if(m<0)
{
m+=60;
h--;
}
if(h<0)
{
h+=24;
}
CTime t2(h,m,s);
return t2;
}
CTime CTime::operator+(int s1)
{
int h,m,s;
s=s1%60;
m=s1/60;
h=s1/3600;
CTime t3(h,m,s);
return *this+t3;
}
CTime CTime::operator-(int s1)
{
int h,m,s;
s=s1%60;
m=s1/60;
h=s1/3600;
CTime t4(h,m,s);
return *this-t4;
}
CTime CTime::operator+=(CTime &c)
{
*this=*this+c;
return *this;
}
CTime CTime::operator-=(CTime &c)
{
*this=*this-c;
return *this;
}
CTime CTime::operator+=(int s1)
{
*this=*this+s1;
return *this;
}
CTime CTime::operator-=(int s1)
{
*this=*this-s1;
return *this;
}
//自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果
int main()
{
CTime t1(12,25,35),t2(9,20,45),t;
cout<<"t1为:";
t1.display();
cout<<"t2为:";
t2.display();
cout<<endl;
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<<endl;
t=t1+t2;
cout<<"t1与t2相加后的时间点为:";
t.display();
t=t1-t2;
cout<<"t1与t2相减后的时间点为:";
t.display();
cout<<"增加600秒后的时间点:"<<endl;
t=t1+600;
t.display();
cout<<"减去600秒后的时间点:"<<endl;
t=t1-600;
t.display();
cout<<"加上一个时间赋予本身:"<<endl;
t1+=t2;
t1.display();
cout<<"减去一个时间赋予本身:"<<endl;
t1-=t2;
t1.display();
cout<<"加上600秒后赋予本身:"<<endl;
t1+=600;
t1.display();
cout<<"减去600秒后赋予本身:"<<endl;
t1-=600;
t1.display();
return 0;
}


运行结果,如下图:



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