您的位置:首页 > 其它

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

2014-04-16 22:12 375 查看
/* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:王英华
* 完成日期: 2013年04月16日
* 版本号: 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 display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
//二目的比较运算符重载
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;
}

bool CTime::operator > (CTime &t)
{
if(hour>t.hour)
return true;
else if(hour==t.hour&&minute>t.minute)
return true;
else if(minute==t.minute&&second>t.second)
return true;
else
return false;

}
bool CTime::operator < (CTime &t)
{
if(hour<t.hour)
return true;
else if(hour==t.hour&&minute<t.minute)
return true;
else if(minute==t.minute&&second<t.second)
return true;
else
return false;
}
bool CTime::operator >= (CTime &t)
{
if(hour>t.hour)
return true;
else if(hour==t.hour && minute>t.minute)
return true;
else if(hour==t.hour && minute==t.minute && second>t.second)
return true;
else if(hour==t.hour && minute==t.minute && second==t.second)
return true;
else
return false;
}
bool CTime::operator <= (CTime &t)
{
if(hour<t.hour)
return true;
else if(hour==t.hour && minute<t.minute)
return true;
else if(hour==t.hour && minute==t.minute && second<t.second)
return true;
else 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;
}
bool CTime::operator!=(CTime &t)
{
if(hour==t.hour&&minute==t.minute&&second==t.second)
return false;
else
return true;
}
CTime CTime::operator+(CTime &t)
{
CTime a;
a.hour=hour+t.hour;
a.minute=minute+t.minute;
a.second=second+t.second;
if(a.second>=60)
{
a.second=a.second-60;
a.minute+=1;
}
if(a.minute>=60)
{
a.minute=a.minute-60;
a.hour+=1;
}
if(a.hour>=24)
{
a.hour=a.hour-24;
}
return a;
}
CTime CTime::operator-(CTime &t)
{

CTime a;
a=*this;
if(a.second<t.second)
{
a.second=a.second+60-t.second;
a.minute-=1;
}
else
{
a.second-=t.second;
}
if(a.minute<t.minute)
{
a.minute=a.minute+60-t.minute;
a.hour-=1;
}
else
{
a.minute-=t.minute;
}
if(a.hour<t.hour)
{
a.hour=a.hour+12-t.hour;
}
else
{
a.hour-=t.hour;
}
return a;
}
CTime CTime::operator+(int s)
{
CTime c;
c=*this;
c.second+=s;
while(c.second>=60)
{
c.second-=60;
c.minute+=1;
}
while(c.minute>=60)
{
c.hour+=1;
c.minute-=60;
}
return c;
}
CTime CTime::operator-(int s)
{

CTime c;
c=*this;
while(c.second<s)
{
c.second+=60;
c.minute-=1;
if(c.minute<0)
{
c.hour-=1;
c.minute=59;
if(c.hour<0)
{
c.hour=11;
}
}
}
c.second-=s;
return c;
}
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)
{

*this=*this+s;
return *this;
}
CTime CTime::operator-=(int s)
{

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

}

运行结果:


心得体会:好不容易啊,终于给折腾出来了。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: