/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称: test.cpp
*作 者:满星辰
*完成日期:2015 年 4 月 28 日
*版本号:v1.0
*
*问题描述:实现Time类中的运算符重载。
如 > < >= <= == != + -bablablabla....
*输入描述:
*程序输出:
*/
#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为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秒前的时间
};
void CTime::setTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second;
}
bool CTime::operator > (CTime &t)
{
int t1,t2;
t1=hour*3600+minute*60+second;
t2=t.hour*3600+t.minute*60+t.second;
if(t1>t2)
return true;
else
return false;
}
bool CTime::operator < (CTime &t)
{
int t1,t2;
t1=hour*3600+minute*60+second;
t2=t.hour*3600+t.minute*60+t.second;
if(t1<t2)
return true;
else
return false;
}
bool CTime::operator <= (CTime &t)
{
return !(*this>t);
}
bool CTime::operator >= (CTime &t)
{
return !(*this<t);
}
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)
{
return !(*this==t);
}
CTime CTime::operator + (CTime &t)
{
CTime t1;
t1.second=second+t.second;
t1.minute=minute+t.minute;
t1.hour=hour+t.hour;
if(t1.second>59)
{
t1.second%=60;
t1.minute++;
}
if(t1.minute>59)
{
t1.minute%=60;
t1.hour++;
}
if(t1.hour>23)
{
hour%=24;
}
return t1;
}
CTime CTime::operator - (CTime &t)
{
int h,m,s;
s=second-t.second;
m=minute-t.minute;
h=hour-t.hour;
if (s<0)
{
s+=60;
m--;
}
if (m<0)
{
m+=60;
h--;
}
while (h<0) h+=24;
CTime t0(h,m,s);
return t0;
}
CTime CTime::operator+(int s)
{
CTime t1(0,0,s);
return *this+t1;
}
CTime CTime::operator-(int s)
{
CTime t1(0,0,s);
return *this-t1;
}
//可以直接使用已经重载了的加减运算实现
//这种赋值, 例如 t1+=20,直接改变当前对象的值,所以在运算完成后,将*this作为返回值
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)//返回s秒后的时间
{
int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
CTime t0(hh,mm,ss);
*this=*this+t0;
return *this;
}
CTime CTime::operator-=(int s)//返回s秒前的时间
{
int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
CTime t0(hh,mm,ss),t;
*this=*this-t0;
return *this;
}
//下面定义用于测试的main()函数
//一目运算符的重载
CTime CTime::operator++(int)//后置++,下一秒
{
CTime c=*this;
*this+=1;
return c;
}
CTime CTime::operator++()//前置++,下一秒,前置与后置返回值不一样
{
*this+=1;
return *this;
}
CTime CTime::operator--( int)//后置--,前一秒
{
CTime c=*this;
*this-=1;
return c;
}
CTime CTime::operator--()//前置--,前一秒
{
*this-=1;
return *this;
}
//下面定义用于测试的main()函数
int main()
{
CTime t1,t2,t3;
int s=20;
t1.setTime(8,20,25);
t2.setTime(11,20,50);
cout<<"t1的时间为:";
t1.display();
cout<<endl;
cout<<"t2的时间为:";
t2.display();
cout<<endl;
cout<<"t1是否比t2大?"<<endl;
cout<<((t1>t2)?"是":"否")<<endl;
cout<<"t1是否比t2小?"<<endl;
cout<<((t1<t2)?"是":"否")<<endl;
cout<<"t1是否大于或等于t2?"<<endl;
cout<<((t1>=t2)?"是":"否")<<endl;
cout<<"t1是否小于或等于t2?"<<endl;
cout<<((t1<=t2)?"是":"否")<<endl;
cout<<"t1是否等于t2?"<<endl;
cout<<((t1==t2)?"是":"否")<<endl;
cout<<"t1是否不等于t2?"<<endl;
cout<<((t1!=t2)?"是":"否")<<endl;
t3=t1+t2;
t3.display();
cout<<endl;
t3=t1-t2;
t3.display();
cout<<endl;
t3=t1+s;
t3.display();
cout<<endl;
t3=t1-s;
t3.display();
cout<<endl;
t3+=t1;
t3.display();
cout<<endl;
t3-=t1;
t3.display();
cout<<endl;
t3+=s;
t3.display();
cout<<endl;
t3-=s;
}
图片:
心得:
做了很久。。。累啊。。。报错是必须的,都是小错。。。
在减法上弄了很久,参考了上一个项目
定义了时间类之间的减法,在进行时间类与整型的计算时可以把整型当时间类用。。。