第八周 项目2-Time类中的运算符重载
来源:互联网 发布:三国志13武将数据错误 编辑:程序博客网 时间:2024/05/17 08:58
/**程序的版权和版本声明部分:*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;}
结果展示:
心得体会:唉,被这道题困惑了一下午,刚开始思路什么都特别清晰,就是运行结果不对,越改越乱,今天要是不把它整出来,估计今夜要失眠。。。。。。。
0 0
- 第八周 项目2 TIME类中的运算符重载
- 第八周 项目2 Time类中的运算符重载
- 第八周项目2-TIME类中的运算符重载
- 第八周 项目2-Time类中的运算符重载
- 第八周项目2-time类中的运算符重载
- 第八周项目2 TIME类中的运算重载符
- 第八周【项目2-Time类中的运算符重载】
- 第八周【项目2-Time类中的运算符重载】
- 第八周项目2-Time类中的运算符重载
- 第八周 项目2 Time类中的运算符重载
- 第八周项目2 Time类中的运算符重载
- 第八周项目2-Time类中的运算符重载
- 第八周 【项目2-Time类中的运算符重载】
- 第八周项目2:Time类中的运算符重载
- 第八周项目2-Time类中的运算符重载
- 第八周项目-2-time类中的运算符重载
- 第八周项目2-Time类中的运算符重载
- 第八周项目二----time类中的运算符重载
- 学开车不能急于求成,心急上路
- 可变参数函数
- 新手必看一位老司机的十年开车经验
- Floodlight加载和运行模块的原理
- c++的 gettimer() 在哪里? 如何用他计算方法运行时间?
- 第八周 项目2-Time类中的运算符重载
- 程序猿的24个段子
- 深入浅出Java Dom4j读取XML
- Velocity布局祥解
- week7
- 新手开车13招技巧
- 随机函数(3)
- ubuntu11.04更新源
- 最简单的WebService开发