/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:score.cpp
*作 者:惠睿
*完成日期:2015年5月18日
*版 本 号:v1.0
*
*问题描述:在第八周项目的基础上,加入要求的运算符重载定义。
*程序输入:无输入。
*程序输出:输出调用函数后的值。
*/
#include<iostream>
#include<cmath>
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 operator++(int);//后置++,下一秒
CTime operator++();//前置++,下一秒,前置与后置返回值不一样
CTime operator--( int);//后置--,前一秒
CTime operator--();//前置--,前一秒
friend istream& operator>>(istream&input,CTime &c);
friend ostream& operator<<(ostream&output,const CTime &c);
};
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;
if (hour<t.hour) return false;
if (minute>t.minute) return true;
if (minute<t.minute) return false;
if (second>t.second) return true;
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 (second<t.second) return true;
return false;
}
bool CTime::operator >= (CTime &t)
{
return !(*this < t);
}
bool CTime::operator <= (CTime &t)
{
return !(*this > t);
}
bool CTime::operator == (CTime &t)
{
return !((*this > t)||(*this < t));
}
bool CTime::operator != (CTime &t)
{
return ((*this > t)||(*this < t));
}
//二目的加减运算符的重载
//返回t规定的时、分、秒后的时间
//例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15
CTime CTime::operator+(CTime &t)
{
CTime time;
time.hour=hour+t.hour;
time.minute=minute+t.minute;
time.second=second+t.second;
int count=0;
int i=time.second;
if(time.second>=60)
{
while(i>=60)
{
i%=60;
count=(time.second-i)/60;
}
time.minute+=count;
time.second=i;
}
int j=time.minute;
count=0;
if (time.minute>=60)
{
while(j>=60)
{
j%=60;
count=(time.minute-j)/60;
}
time.hour+=count;
time.minute=j;
}
int k=time.hour;
count=0;
if (time.hour>=24)
{
while(k>=24)
{
k%=24;
count=(time.hour-k)/24;
}
time.hour=k;
}
return time;
}
CTime CTime::operator-(CTime &t)//对照+理解
{
CTime time;
if(hour>t.hour)
{
time.hour=hour-t.hour;
time.minute=minute-t.minute;
time.second=second-t.second;
if(time.second<0)
{
time.minute--;
time.second+=60;
}
if(time.minute<0)
{
time.hour--;
time.minute+=60;
}
}
else
{
time.hour=t.hour-hour;
time.minute=t.minute-minute;
time.second=t.second-second;
if(time.second<0)
{
time.minute--;
time.second+=60;
}
if(time.minute<0)
{
time.hour--;
time.minute+=60;
}
}
return time;
}
CTime CTime::operator+(int s)//返回s秒后的时间
{
CTime time;
second+=s;
int count=0;
int i=second;
if(second>=60)
{
while(i>=60)
{
i%=60;
count=(second-i)/60;
}
minute+=count;
second=i;
}
int j=minute;
count=0;
if (minute>=60)
{
while(j>=60)
{
j%=60;
count=(minute-j)/60;
}
hour+=count;
minute=j;
}
int k=hour;
count=0;
if (hour>=24)
{
while(k>=24)
{
k%=24;
count=(hour-k)/24;
}
hour=k;
}
time.hour=hour;
time.minute=minute;
time.second=second;
return time;
}
CTime CTime::operator-(int s)//返回s秒前的时间
{
CTime time;
second-=s;
int count=0;
int i=fabs(second);
if(second>=60)
{
while(i>=60)
{
i%=60;
count=(second-i)/60;
}
minute-=count;
second=i;
}
int j=minute;
count=0;
if (minute>=60)
{
while(j>=60)
{
j%=60;
count=(minute-j)/60;
}
hour-=count;
minute=j;
}
int k=hour;
count=0;
if (hour>=24)
{
while(k>=24)
{
k%=24;
count=(hour-k)/24;
}
hour=k;
}
time.hour=hour;
time.minute=minute;
time.second=second;
return time;
}
//二目赋值运算符的重载
CTime CTime::operator+=(CTime &c)
{
this->hour=this->hour+c.hour;
this->minute=this->minute+c.minute;
this->second=this->second+c.second;
int count=0;
int i=this->second;
if(this->second>=60)
{
while(i>=60)
{
i%=60;
count=(this->second-i)/60;
}
this->minute+=count;
this->second=i;
}
int j=this->minute;
count=0;
if (this->minute>=60)
{
while(j>=60)
{
j%=60;
count=(this->minute-j)/60;
}
this->hour+=count;
this->minute=j;
}
int k=this->hour;
count=0;
if (this->hour>=24)
{
while(k>=24)
{
k%=24;
count=(this->hour-k)/24;
}
this->hour=k;
}
return *this;
}
CTime CTime::operator-=(CTime &c)
{
if(this->hour>c.hour)
{
this->hour=this->hour-c.hour;
this->minute=this->minute-c.minute;
this->second=this->second-c.second;
if(this->second<0)
{
this->minute--;
this->second+=60;
}
if(this->minute<0)
{
this->hour--;
this->minute+=60;
}
}
else
{
this->hour=c.hour-this->hour;
this->minute=this->minute-c.minute;
this->second=this->second-c.second;
if(this->second<0)
{
this->minute--;
this->second+=60;
}
if(this->minute<0)
{
this->hour--;
this->minute+=60;
}
}
return *this;
}
CTime CTime::operator+=(int s)//返回s秒后的时间
{
this->second+=s;
int count=0;
int i=this->second;
if(this->second>=60)
{
while(i>=60)
{
i%=60;
count=(this->second-i)/60;
}
this->minute+=count;
this->second=i;
}
int j=this->minute;
count=0;
if (this->minute>=60)
{
while(j>=60)
{
j%=60;
count=(this->minute-j)/60;
}
this->hour+=count;
this->minute=j;
}
int k=this->hour;
count=0;
if (this->hour>=24)
{
while(k>=24)
{
k%=24;
count=(this->hour-k)/24;
}
this->hour=k;
}
this->hour=hour;
this->minute=minute;
this->second=second;
return *this;
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{
this->second-=s;
int count=0;
int i=fabs(this->second);
if(this->second>=60)
{
while(i>=60)
{
i%=60;
count=(this->second-i)/60;
}
this->minute-=count;
this->second=i;
}
int j=this->minute;
count=0;
if (this->minute>=60)
{
while(j>=60)
{
j%=60;
count=(this->minute-j)/60;
}
this->hour-=count;
this->minute=j;
}
int k=this->hour;
count=0;
if (this->hour>=24)
{
while(k>=24)
{
k%=24;
count=(this->hour-k)/24;
}
this->hour=k;
}
this->hour=hour;
this->minute=minute;
this->second=second;
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;
}
//流插入
istream& operator>>(istream&input,CTime &t)
{
int h,m,s;
char sign;
do
{
cout<<"please input a time(ep->h:m:s):";
input>>h>>sign>>m>>sign>>s;
}
while(!(sign==':'));
t.hour=h;
t.minute=m;
t.second=s;
return input;
}
//流提取
ostream& operator<<(ostream&output,const CTime &t)
{
output<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;
return output;
}
//下面定义用于测试的main()函数
int main()
{
CTime t1(10,41,30),t2(6,42,23),t;
cout<<"t1为:"<<t1;
cout<<"t2为:"<<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<<"下面对两个时间进行计算:"<<endl;
t1++;
cout<<"t1++:"<<t1;
++t2;
cout<<"++t2:"<<t2;
t1--;
cout<<"t1--:"<<t1;
--t2;
cout<<"--t2:"<<t2;
t=t1+t2;
cout<<"t1+t2:"<<t;
t=t1-t2;
cout<<"t1-t2:"<<t;
t=t1+333;
cout<<"t1+333s:"<<t;
t=t2-2222;
cout<<"t2-2222s:"<<t;
t1+=t2;
cout<<"t1+=t2:"<<t1;
t1-=t2;
cout<<"t1-=t2:"<<t1;
t1+=3333;
cout<<"t1+=3333s:"<<t1;
t2-=222;
cout<<"t2-=222s:"<<t2;
}
运行结果: