您的位置:首页 > 编程语言

项目三  实现Time类中的运算符重载 (代码之后有惊喜,一定要读完)

2016-05-12 18:45 405 查看
/*copyright(c)2016.烟台大学计算机学院
* All rights reserved,
* 文件名称:text.Cpp
* 作者:舒文超
* 完成日期:2016年5月12日
* 版本号:vc++6.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);
//二目运算符的重载
CTime operator+(CTime &c);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15
CTime operator-(CTime &c);//对照+理解
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--();//前置--,前一秒
};
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;
}
void CTime::display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
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;
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;
if (minute>t.minute)
return false;
else
{
if (second<t.second)
return true;
else
return false;
}
}
}
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;
}
bool CTime::operator >= (CTime &t)
{
if (*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;
s=second+t.second;
m=minute+t.minute;
h=hour+t.hour;
if (s>59)
{
s-=60;
m++;
}
if (m>59)
{
m-=60;
h++;
}
while(h>23)
h-=24;
CTime t0(h,m,s);
return t0;
}
CTime CTime::operator+(int s)
{
int s1=s%60;
int m1=(s/60)%60;
int h1=s/3600;
CTime t0(h1,m1,s1);
return *this+t0;
}
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)
{
int ss=s%60;
int mm=(s/60)%60;
int hh=s/3600;
CTime t0(hh,mm,ss);
return *this-t0;
}
CTime CTime::operator++(int)//后置++(先操作再加)
{
CTime t=*this;
*this=*this+1;
return t;
}
CTime &CTime::operator++()//前置++(先减再操加)
{
*this=*this+1;
return *this;
}
CTime CTime::operator--(int)//后置--(先操作再减)
{
CTime t=*this;
*this=*this-1;
return t;
}
CTime &CTime::operator--()//前置--(先减再操作)
{
*this=*this-1;
return *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)
{
*this=*this+s;
return *this;
}
CTime &CTime::operator-=(int s)
{
*this=*this-s;
return *this;
}
int main()
{
char ch1,ch2;
int h,m,s;
CTime t1,t2,t3,t;
cout<<"请输入时间 1"<<endl;
while(1)
{
cout<<"格式(hh:mm:ss    0<=hh<24,0<=mm<60,0<=ss<60)"<<endl;;
cin>>h>>ch1>>m>>ch2>>s;
if(h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60&&ch1==':' && ch2==':')
{
t1.setTime(h,m,s);
break;
}
else
cout<<"时间格式不正确! 请重新输入"<<endl;
}
cout<<"请输入时间 2"<<endl;
while(1)
{
cout<<"格式(hh:mm:ss    0<=hh<24,0<=mm<60,0<=ss<60)"<<endl;;
cin>>h>>ch1>>m>>ch2>>s;
if(h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60&&ch1==':' && ch2==':')
{
t2.setTime(h,m,s);
break;
}
else
cout<<"时间格式不正确! 请重新输入"<<endl;
}
cout<<"t1:";
t1.display();
cout<<"t2:";
t2.display();
cout<<"比较时间大小"<<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;
if (t1<=t2) cout<<"t1≤t2"<<endl;
cout<<endl;
cout<<"t=t1++"<<endl;
t=t1++;
cout<<"t= ";
t.display();
cout<<"t1= ";
t1.display();
cout<<endl;
cout<<"t=++t1"<<endl;
t=++t1;
cout<<"t= ";
t.display();
cout<<"t1= ";
t1.display();
cout<<endl;
t3=t1+t2;
cout<<"t1+t2= ";
t3.display();
t3=t1-t2;
cout<<"t1-t2= ";
t3.display();
cout<<endl;
t3=t1+1000;
cout<<"t1+1000= ";
t3.display();
t3=t1-1000;
cout<<"t1-5000= ";
t3.display();
return 0;
}

必须要加一个敲完这段代码现在的感受:

此段代码总共历时一个半小时,这是自从学c++以来写的一个代码行数最多的一个程序。当写完的时候带着
忐忑欣喜 的心情 胆战心惊地编译了一下,51个错误

(你们是知道我当时的感受的

),仿佛一朝回到解放前,不再有爱了(这也是我见过的一个程序中出现的最多的错误数目了,原谅我的见识短浅)。当时心里止不住的躁动(还要不要改,还要不要改,还要不要改
重要的纠结说三遍

),最终内心实在是敌不过“要对这段代码负责” 的冲动(好吧,虽然不是处女座,但好像有着处女座般的强迫症),痛下决心:改 改 改(重要的决定说三遍)

左看看,右看看,始终没有在可能出错的地方看出错,最终决定自己再重新走一遍程序。。。

于是去了贺老师的博客看一下答案,对比了一下,我只想深深微笑。。。

是在定义成员函数的时候出了错,所有成员函数的定义我都没有在前面加类名和冒号那一块(





),居然就是这种简简单单的小失误,导致了51个错误,我本以为会是什么语法上的错误。

当改过来,看到下面没错误,没警告时,心情一下子便来了一个180°的转弯了,简直要上天啊。

以身试法,前车之鉴,在敲代码的时候一定要细心细心在细心,不要总觉得把上面的声明直接复制到下面去开始定义是对的。还有就是我们既然写了这段代码就要对这段代码 负责到底
,不要半途而废哪怕是错误多的吓人也要认认真真把它全部改掉,或许错误的根源就是一个很小很小的地方。当我们真的看到我们自己写的程序没有错误时,那种感觉是很难得到的,就像是当时高三做数学卷子,一下子解出了最后的压轴题。这或许就是我们程序员在以后的工作中可以与拿工资这件事相媲美的最开心的事情了(好吧,这么说可能有点尴尬,但是拿工资确实是一件特别特别开心的事),它们一个给了我们外部的满足,一个给了我们内心的满足,外部可以拿别的事来填充,但内心上的愉悦是无法替代的。

曾经听好多人都说,程序员太木讷,与电脑比与自己的女朋友亲多了。虽然是句玩笑,但是我想这并不是没有根据的。因为有些东西确实是它特有可以带给我们的。

最后以上面的一句话来结束这篇吐槽:写了代码就要对它负责到底!!!写了这段代码就要对它负责到底!!!写了这段代码就要对它负责到底!!!(重要的事情说三遍)

好吧,或许并不是什么惊喜,但绝对是实心的忠告!!!


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: