第八周 项目2-Time类中的运算符重载
2014-04-15 20:16
537 查看
/* *程序的版权和版本声明部分: *Copyright(c)2013,烟台大学计算机学院学生 *All rights reserved. *文件名称: *作者:尚振伟 *完成日期:2014年4月15日 *版本号:v0.1 *对任务及求解方法的描述部分: *输入描述:无 *问题描述:实现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) { 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为: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; } //下面实现所有的运算符重载代码。 bool CTime::operator>(CTime &t) { if(hour>t.hour) { return true; } if(hour==t.hour) { if(minute>t.minute) { return true; } } if(hour==t.hour&&minute==t.minute) { if(second>t.second) { return true; } } else { return false; } } bool CTime::operator<(CTime &t) { if(hour<t.hour) { return true; } if(hour==t.hour) { if(minute<t.minute) { return true; } } if(hour==t.hour&&minute==t.minute) { if(second<t.second) { return true; } } else { return false; } } bool CTime::operator>=(CTime &t) { if(!(*this<t)) { return true; } else { return false; } } bool CTime::operator <= (CTime &t) { if(!(*this>t)) { return true; } else { return false; } } 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) { if(hour!=t.hour||minute!=t.minute||second!=t.second) { return true; } else { return false; } } CTime CTime::operator+(CTime &t) { int h,m,s; h=hour+t.hour; m=minute+t.minute; s=second+t.second; while(s>=60) { s-=60; m+=1; } while(m>=60) { m-=60; h+=1; } while(h>=24) { h-=24; } CTime t1(h,m,s); return t1; } CTime CTime::operator-(CTime &t) { int h,m,s; h=hour-t.hour; m=minute-t.minute; s=second-t.second; while(s<0) { s+=60; m-=1; } while(m<0) { m+=60; h-=1; } while(h<0) { h+=24; } CTime t1(h,m,s); return t1; } CTime CTime::operator+(int s) { int h,m,s1; s1=s%60; m=(s/60)%60; h=s/3600; CTime t1(h,m,s1); return *this+t1; } CTime CTime::operator-(int s) { int h1,m1,s1; s1=s%60; m1=(s/60)%60; h1=s/3600; CTime t1(h1,m1,s1); return *this-t1; } CTime CTime::operator+=(CTime &c) { hour+=c.hour; minute+=c.minute; second+=c.second; while(second>=60) { second-=60; minute+=1; } while(minute>=60) { minute-=60; hour+=1; } while(hour>=24) { hour-=24; } CTime t1(hour,minute,second); return t1; } CTime CTime::operator-=(CTime &c) { *this=*this-c; return *this; } CTime CTime::operator+=(int s) { second+=s; while(second>60) { second-=60; minute++; while(minute>60) { minute-=60; hour++; while(hour>24) { hour-=24; } } } return CTime(hour,minute,second); } CTime CTime::operator-=(int s) { *this=*this-s; return *this; } void CTime::display() { cout<<hour<<":"<<minute<<":"<<second<<endl; } //自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果 int main() { CTime t1(8,20,25),t2(11,20,50),t; cout<<"t1="; t1.display(); cout<<"t2="; t2.display(); 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; t=t1+t2; t.display(); t=t1-t2; t.display(); t=t1+6000; t.display(); t=t1-600; t.display(); t1+=t2; t1.display(); t1-=t2; t1.display(); t1+=1200; t1.display(); t1-=600; t1.display(); return 0; }
结果展示:
心得体会:唉,被这道题困惑了一下午,刚开始思路什么都特别清晰,就是运行结果不对,越改越乱,今天要是不把它整出来,估计今夜要失眠。。。。。。。
相关文章推荐
- 各个银行信用卡网贷充值额度
- 过渡和动画
- VS2010中启动画面的设置
- 命令行生成微信app签名
- Linux 调用动态库(.SO文件)总结
- Art of Multiprocessor Programming 答案 ch8
- Job类
- MINI2440Linux驱动01-mini2440_adc
- 搞ACM的你伤不起(转载,不过这个神作实在是太经典了)
- VMware vSAN虚拟化存储初探
- 新手开车13招技巧
- codeforces Round #241(div2) A解题报告
- ubuntu11.04更新源
- poj3660
- c++的 gettimer() 在哪里? 如何用他计算方法运行时间?
- 随机函数(3)
- week 8
- 新手必看一位老司机的十年开车经验
- 可变参数函数
- Velocity布局祥解