/*
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:main.cpp
* 作 者:王俊锐
* 完成日期:2016年6月1日
* 版 本 号:v1.0
*
* 问题描述:实现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);
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--();//前置--,前一秒
//用于计算时间
void handle();
};
CTime::CTime(int h,int m,int s):hour(h),minute(m),second(s){}
void CTime::setTime(int h,int m,int s)
{
if(hour>=0&&hour<24&&minute>=0&&minute<60&&second>=0&&second<60)
{
hour=h;
minute=m;
second=s;
}
else
cout<<"error!"<<endl;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second;
}
bool CTime::operator > (CTime &t)
{
if(hour>t.hour)
return true;
else if(hour<t.hour)
return false;
else
{
if(minute>t.minute)
return true;
else if(minute<t.minute)
return false;
else
{
if(second>t.second)
return true;
else
return false;
//秒数相等时我不知道如何是好
}
}
}
bool CTime::operator < (CTime &t)
{
if(hour<t.hour)
return true;
else if(hour>t.hour)
return false;
else
{
if(minute<t.minute)
return true;
else if(minute>t.minute)
return false;
else
{
if(second<t.second)
return true;
else
return false;
//秒数相等时我不知道如何是好
}
}
}
bool CTime::operator >= (CTime &t)
{
if(*this<t)
return false;
return true;
}
bool CTime::operator <= (CTime &t){return !(*this>t);}
bool CTime::operator == (CTime &t)
{
if(*this>t)
return false;
if(*this<t)
return false;
return true;
}
bool CTime::operator != (CTime &t)
{
if(*this==t)
return false;
return true;
}
CTime CTime::operator+(CTime &c)
{
CTime a;
a.hour=hour+c.hour;
a.minute=minute+c.minute;
a.second=second+c.second;
a.handle();
return a;
}
CTime CTime::operator-(CTime &c)
{
CTime a;
a.hour=hour-c.hour;
a.minute=minute-c.minute;
a.second=second-c.second;
a.handle();
return a;
}
CTime CTime::operator+(int s)
{
CTime a=*this;
a.second=second+s;
a.handle();
return a;
}
CTime CTime::operator-(int s)
{
CTime a=*this;
a.second=second-s;
a.handle();
return a;
}
CTime &CTime::operator+=(CTime &c)
{
CTime a;
a=*this+c;
return a;
}
CTime &CTime::operator-=(CTime &c)
{
CTime a;
a=*this-c;
return a;
}
CTime &CTime::operator+=(int s)
{
CTime a=*this;
a.second+=s;
a.handle();
return a;
}
CTime &CTime::operator-=(int s)
{
CTime a=*this;
a.second-=s;
a.handle();
return a;
}
CTime CTime::operator++(int)
{
CTime a=*this;
*this=*this+1;
return a;
}
CTime &CTime::operator++()
{
*this=*this+1;
return *this;
}
CTime CTime::operator--(int)
{
CTime a=*this;
*this=*this-1;
return a;
}
CTime &CTime::operator--()
{
*this=*this-1;
return *this;
}
void CTime::handle()
{
if(hour<0)
hour+=24;
if(minute<0)
minute+=60;
if(second<0)
second+=60;
hour=hour%24;
minute=minute%60;
second=second%60;
}
int main()
{
CTime t1(9,50,20),t2(20,20,50),t;
cout<<"t1为:";t1.display();
cout<<" t2为:";t2.display();
cout<<endl<<"下面比较两个时间大小:\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<<"t=t1++"<<endl;
t=t1++;
cout<<"t= ";
t.display();
cout<<" t1= ";
t1.display();
cout<<endl<<"t=++t1"<<endl;
t=++t1;
cout<<"t= ";
t.display();
cout<<" t1= ";
t1.display();
t=t1+t2;
cout<<endl<<"t1+t2= ";
t.display();
t=t1-t2;
cout<<endl<<"t1-t2= ";
t.display();
t=t1+3000;
cout<<endl<<"t1+3000= ";
t.display();
t=t1-5000;
cout<<endl<<"t1-5000= ";
t.display();
cout<<endl;
return 0;
}
第十二周项目二CTime类中的运算符重载
最新推荐文章于 2022-04-01 16:41:23 发布