您的位置:首页 > 其它

第九周项目2——Time类中的运算符重载(续)

2015-05-13 09:18 239 查看
问题及代码;

/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:time.cpp
*作    者:赵敏
*完成日期:2015年5月13日
*版 本 号:v1.0
*
*问题描述:实现Time类中的运算符重载
1)定义对时间对象的自增和自减一目运算符
//一目运算符的重载
CTime operator++(int);//后置++,下一秒
CTime operator++();//前置++,下一秒,前置与后置返回值不一样
CTime operator--( int);//后置--,前一秒
CTime operator--();//前置--,前一秒
(2)定义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);
void setTime(int h,int m,int s);
//二目的比较运算符重载
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 operator++(int);//后置++,下一秒
CTime operator++();//前置++,下一秒,前置与后置返回值不一样
CTime operator--( int);//后置--,前一秒
CTime operator--();//前置--,前一秒
friend ostream&operator<<(ostream&,CTime&);
friend istream&operator>>(istream&,CTime&);
};
ostream&operator<<(ostream& output,CTime&t)
{
output<<t.hour<<":"<<t.minute<<":"<<t.second;
return output;
}
istream&operator>>(istream& input,CTime&t)
{
char c1,c2;
do
{
cout<<"input time: ";
input>>t.hour>>c1>>t.minute>>c2>>t.second;
}while(!((c1==':')||(c2==':')));
return input;
}
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 false;
return true;
if(minute<t.minute)return false;
return true;
if(second<t.second)return false;
return true;
}
bool CTime::operator < (CTime &t)
{
if(hour>t.hour)return false;
return true;
if(minute>t.minute)return false;
return true;
if(second>t.second)return false;
return true;
}
bool CTime::operator <= (CTime &t) // 判断时间t1<=t2
{
if (*this > t) return false;
return true;
}
bool CTime::operator >= (CTime &t)
{
if(*this<t) return false;
return true;
}
bool CTime::operator == (CTime &t)
{
if(*this>t||*this<t)
return false;
return true;
}
bool CTime::operator != (CTime &t)
{
if(*this==t)
return false;
return true;
}
CTime CTime::operator+(CTime &t)
{
int h,m,s;
h=hour+t.hour;
m=minute+t.minute;
s=second+t.second;
if(s>59)
{
s-=60;
m++;
}
if(m>59)
{
m-=60;
h++;
}
if(h>23)
{
h-=24;
}
CTime t0(h,m,s);
return t0;
}
CTime CTime::operator-(CTime &t)
{
int h,m,s;
h=hour-t.hour;
m=minute-t.minute;
s=second-t.second;
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 hh,mm,ss;
ss=s%60;
mm=(s/60)%60;
hh=s/3600;
CTime t0(hh,mm,ss);
return *this+t0;
}
CTime CTime::operator-(int s)
{
int hh,mm,ss;
ss=s%60;
mm=(s/60)%60;
hh=s/3600;
CTime t0(hh,mm,ss);
return *this-t0;
}
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;
}
CTime CTime::operator++(int)
{
CTime temp(*this);
second++;
if(second>=60)
{
second-=60;
++minute;
}
return temp;
}
CTime CTime::operator++()
{
if(++second>=60)
{
second-=60;
++minute;
}
return *this;
}
CTime CTime::operator--( int)
{
CTime temp(*this);
second--;
if(second<0)
{
second+=60;
--minute;
}
return temp;
}
CTime CTime::operator--()
{
if(--second<0)
{
second+=60;
--minute;
}
return *this;
}
int main()
{
CTime t1,t2,t;
cin>>t1;
cin>>t2;
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="<<t1<<endl;
cout<<"t2="<<t2<<endl;
t=t1+t2;
cout<<"t1+t2: ";
cout<<t<<endl;
t=t1++;
cout<<"t1++: ";
cout<<t<<endl;
t=++t1;
cout<<"++t1: ";
cout<<t<<endl;
t=t2--;
cout<<"t2--: ";
cout<<t<<endl;
t=--t2;
cout<<"--t2: ";
cout<<t<<endl;
return 0;

}


运行结果:



学习心得:

有错误,但是完全不知道怎么改啊,求大神。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: