第九周 项目二
来源:互联网 发布:泉州金蝶软件 编辑:程序博客网 时间:2024/05/16 20:27
<span style="font-family:Microsoft YaHei;font-size:18px;color:#3333ff;">//【项目2-Time类中的运算符重载(续)】//在Time类中的运算符重载基础上//(1)定义对时间对象的自增和自减一目运算符//[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片//一目运算符的重载//CTime operator++(int);//后置++,下一秒//CTime operator++();//前置++,下一秒,前置与后置返回值不一样//CTime operator--( int);//后置--,前一秒//CTime operator--();//前置--,前一秒/**Copyright (c)2014,烟台大学计算机与控制工程学院*All rights reserved.*dood luck*文件名称:d.cpp*作 者:张旺华*完成日期:2015年5月7日*版 本 号:v1.0**/#include <iostream>using namespace std;class CTime{private: int hour; // 时 int minute; // 分 int second; // 秒public: CTime(int h=0,int m=0,int s=0); void setTime(int h,int m,int s) { hour=h; minute=m; second=s; } void display(); friend CTime change(CTime &t); //二目的比较运算符重载 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 operator++(); CTime operator--(int); CTime operator++(int); 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::CTime(int h,int m,int s){ hour=h; minute=m; second=s;}CTime CTime:: operator--(){ *this=*this-1; return *this;}CTime CTime:: operator++(){ *this=*this+1; return *this;}CTime CTime::operator--(int){ CTime p=*this; *this=*this-1; return p;}CTime CTime::operator++(int){ CTime p=*this; *this=*this+1; return p;}void CTime::display(){ cout<<hour<<":"<<minute<<":"<<second<<endl;}CTime change(CTime &t){ while(t.second<0) { t.minute--; t.second+=60; } while(t.minute<0) { t.hour--; t.minute+=60; } while(t.hour<0) { t.hour+=24; } t.minute+=t.second/60; t.second%=60; t.hour+=t.minute/60; t.minute%=60; t.hour%=24; return t;}bool CTime::operator > (CTime &t){ if (hour>t.hour) return true; if (hour<t.hour) return false; if (minute>t.minute) return true; if (minute<t.minute) return false; if (second>t.second) return true; return false;}bool CTime::operator < (CTime &t){ if (hour<t.hour) return true; if (hour>t.hour) return false; if (minute<t.minute) return true; if (minute>t.minute) return false; if (second<t.second) return true; return false;}bool CTime::operator >= (CTime &t){ return !(*this<t);}bool CTime::operator <= (CTime &t){ return !(*this>t);}bool CTime::operator == (CTime &t){ return (*this>=t&&*this<=t);}bool CTime::operator != (CTime &t){ return !(*this>=t&&*this<=t);}//二目的加减运算符的重载//返回t规定的时、分、秒后的时间//例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15CTime CTime::operator+(CTime &t){ CTime p; p.second=second+t.second; p.minute=minute+t.minute; p.hour=hour+t.hour; return change(p);}CTime CTime::operator-(CTime &t)//对照+理解{ CTime p; p.second=second-t.second; p.minute=minute-t.minute; p.hour=hour-t.hour; return change(p);}CTime CTime::operator+(int s)//返回s秒后的时间{ CTime p=*this; p.second+=s; return change(p);}CTime CTime::operator-(int s)//返回s秒前的时间{ CTime p=*this; p.second-=s; return change(p);}//二目赋值运算符的重载CTime CTime::operator+=(CTime &t){ second=second+t.second; minute=minute+t.minute; hour=hour+t.hour; return change(*this);}CTime CTime::operator-=(CTime &t){ second=second-t.second; minute=minute-t.minute; hour=hour-t.hour; return change(*this);}CTime CTime::operator+=(int s)//返回s秒后的时间{ second+=s; return change(*this);}CTime CTime::operator-=(int s)//返回s秒前的时间{ second-=s; return change(*this);}int main(){ CTime t1(8,20,25); t1.display(); t1++; t1.display(); t1--; t1.display(); ++t1; t1.display(); --t1; t1.display(); return 0;}</span>
运行结果
学习心得:一开始我就犯了错误
CTime CTime:: operator--(){ --*this.second; return *this;}CTime CTime:: operator++(){ ++*this.second; return *this;}CTime CTime::operator--(int){ CTime p=*this; --*this.second; return p;}CTime CTime::operator++(int){ CTime p=*this; ++*this.second; return p;}这是我一开始写的,没有注意不能用*this.
后来也没有用
<span style="font-family:Microsoft YaHei;font-size:18px;color:#3333ff;">*this=*this+1;</span>
<span style="font-family:Microsoft YaHei;font-size:18px;color:#3333ff;"> 造成了错误,后改过了就没事了。</span>
<span style="font-family:Microsoft YaHei;color:#3333ff;"><span style="font-size: 18px;">但是换是不太清楚为什么是这样子。</span></span>
0 0
- 第九周项目二
- 第九周项目二
- 第九周项目二
- 第九周项目二
- 第九周项目二
- 第九周项目二
- 第九周 项目二
- 第九周项目二
- 第九周项目二
- 第九周项目二
- 第九周项目二
- 2014-第九周项目二
- 2014-第九周项目二
- 第九周上机任务 项目二
- 第九周项目二TIME重载
- 第九周——项目二
- 第九周项目二(求解答)
- 第九周 项目二 time类
- Centos 6.6(X64位)系统编译安装memcached 并安装PHP的memcache扩展
- 2013年蓝桥杯决赛试题
- windows读取bmp格式图像代码移植linux;windows代码移植linux,数据类型不匹配我问题;
- 《推荐系统实践》的笔记
- JM代码分析(二)
- 第九周 项目二
- Joseph(1443)
- html中表格table冻结行和列
- html css学习笔记-响应式布局的两种基本实现
- jq 积累
- L脚本语言的表对象
- 理解硬(软)中断的嵌套
- 【转——Linux中国】安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(一)
- litepal数据库开源框架简单用法和我的感悟