第12周项目2:Time类中的运算符重载
2016-05-25 19:23
337 查看
/* * Copyright(c)2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名称:第12周项目2:Time类中的运算符重载 * 作者:马康泰 * 完成日期:2016.5.25 * 版本号:v1.0 * * 问题描述:实现Time类中的运算符重载。 * 输入描述: * 程序输出: */ #include<iostream> using namespace std; class CTime { private: short int hour; // 时 short int minute; // 分 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 judge_is_time(); void trans_time_standard1(); void trans_time_standard2(); //二目的比较运算符重载 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--();//前置--,前一秒 }; 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() { if(hour<10)cout<<"0"; cout<<hour<<":"; if(minute<10)cout<<"0"; cout<<minute<<":"; if(second<10)cout<<"0"; cout<<second<<endl; } bool CTime::judge_is_time() { if((hour>=0&&hour<=23)&&(minute>=0&&minute<=59)&&(second>=0&&second<=59)) return true; return false; } void CTime::trans_time_standard1() { if(!judge_is_time()) { if(second>=60) { minute+=second/60; second%=60; if(minute>=60) { hour+=minute/60; minute%=60; if(hour>=24) hour%=24; } } } } void CTime::trans_time_standard2() { if(second<0) { second+=60; minute-=1; if(minute<0) { minute+=60; hour-=1; if(hour<0) hour+=24; } } } bool CTime::operator > (CTime &t) { if(this->hour>=t.hour) { if(this->hour>t.hour) return true; else { if(this->minute>=t.minute) { if(this->minute>t.minute) return true; else { if(this->second>t.second) return true; else return false; } } return false; } } return false; } bool CTime::operator < (CTime &t) { if(this->hour<=t.hour) { if(this->hour<t.hour) return true; else { if(this->minute<=t.minute) { if(this->minute<t.minute) return true; else { if(this->second<t.second) return true; else return false; } } return false; } } return false; } bool CTime::operator <= (CTime &t) { return !(*this > t); } bool CTime::operator >= (CTime &t) { return !(*this < t); } //二目的加减运算符的重载 //返回t规定的时、分、秒后的时间 //例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15 CTime CTime::operator+(CTime &t) { CTime ty; ty.hour=this->hour+t.hour; ty.minute=this->minute+t.minute; ty.second=this->second+t.second; ty.trans_time_standard1(); return ty; }; CTime CTime::operator-(CTime &t)//对照+理解 { CTime ty; ty.hour=this->hour-t.hour; ty.minute=this->minute-t.minute; ty.second=this->second-t.second; ty.trans_time_standard2(); return ty; } CTime CTime::operator+(int s)//返回s秒后的时间 { this->second+=s; this->trans_time_standard1(); return *this; } CTime CTime::operator-(int s)//返回s秒前的时间 { this->second-=s; this->trans_time_standard2(); 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)//返回s秒后的时间 { *this=*this+s; return *this; } CTime &CTime::operator-=(int s)//返回s秒前的时间 { *this=*this-s; return *this; } //一目运算符的重载 CTime CTime::operator++(int)//后置++,下一秒 { CTime ty=*this; *this=*this+1; ty.display(); return ty; } CTime &CTime::operator++()//前置++,下一秒 { *this=*this+1; return *this; } CTime CTime::operator--(int)//后置--,前一秒 { CTime ty=*this; *this=*this-1; ty.display(); return ty; } CTime &CTime::operator--()//前置--,前一秒 { *this=*this-1; return *this; } int main() { CTime t1(8,20,25),t2(11,20,50),t3; if(t1<t2) cout<<"t1<t2"<<endl; cout<<"二目的加减运算符的重载"<<endl; t3=t1+t2; t3.display(); t3=t2-t1; t3.display(); t3=t1+2; t3.display(); t3=t2-2; t3.display(); t3=t1-2; t3=t2+2; cout<<"二目赋值运算符的重载"<<endl; t1+=t2; t1.display(); t2.display(); cout<<endl; t1-=t2; t1.display(); t2.display(); cout<<endl; t1+=2; t1.display(); t2.display(); cout<<endl; t2-=2; t1.display(); t2.display(); t1-=2; t2+=2; t1.display(); t2.display(); cout<<"一目运算符的重载"<<endl; ++t1; t1.display(); --t1; t1.display(); cout<<endl; t2++; t2--; return 0; }
<img src="https://img-blog.csdn.net/20160525192257800?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
相关文章推荐
- NKOI 3686 最大子段和
- DOMNodeInserted监听div内容改变
- ios 浅析录音技术(后台、互斥、上传)
- 实现动态更新ProgressBar的水平进度条
- 思念忙闲总诗解(2006.6.21)
- 一图看懂layout_gravity和gravity的区别
- 通讯录——动态版
- Android Handler个人见解
- 面试题:一个数组插入另一个数组指定的位置
- 分布式session
- ABBYY FineReader 12 软件推荐(图片文字识别)
- 常用快捷键—Webstorm入门指南
- gradle和gradle plugin对应表
- linux使用ipvsadm实现LVS
- Ubuntu Android 开发(三) 使用SDK更新SDK 镜像
- hihocoder 1057 : Performance Log
- 正则表达式 U贪婪模式
- photoshop 常用快捷键
- mongodb 分组查询
- Mysqldump原理