第八周项目二time类中的运算符重载
来源:互联网 发布:mac双系统如何删除win 编辑:程序博客网 时间:2024/05/17 08:45
/* * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 作 者:纪丽娜* 完成日期:2014 年 4 月 15 日 * 版 本 号:v1.0 * 问题描述:。*/ #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为20: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--();//前置--,前一秒};//下面实现所有的运算符重载代码。//显示时间void CTime::display(){cout<<hour<<':'<<minute<<':'<<second<<endl;}//二目的比较运算符重载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){ if (*this<t) return false; return true;}bool CTime::operator <= (CTime &t) // 判断时间t1<=t2{ if (*this > t) return false; return true;}bool CTime::operator ==(CTime &t){ if (*this<t || *this>t) return false; return true;}bool CTime::operator !=(CTime &t){ if (*this<t || *this>t)return true; return false;}//二目的加减运算符的重载//返回t规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为20:41:15CTime CTime::operator+(CTime &t){ CTime t3; t3.second=second+t.second; t3.minute=minute+t.minute; t3.hour=hour+t.hour; if (t3.second>59) { t3.second-=60; t3.minute++; } if (t3.minute>59) { t3.minute-=60; t3.hour++; } if (t3.hour>23) t3.hour-=24; return t3;}CTime CTime::operator-(CTime &t){ CTime c; c=*this; if(c.second<t.second) { c.second=c.second+60-t.second; c.minute-=1; } else { c.second-=t.second; } if(c.minute<t.minute) { c.minute=c.minute+60-t.minute; c.hour-=1; } else { c.minute-=t.minute; } if(c.hour<t.hour) { c.hour=c.hour+12-t.hour; } else { c.hour-=t.hour; } return c;}CTime CTime::operator+(int s)//返回s秒后的时间{ CTime t0; t0.second=s%60; t0.minute=s/60; t0.hour=s/3600; return *this+t0;}CTime CTime::operator-(int s)//返回s秒前的时间{ CTime t0; t0.second=s%60; t0.minute=s/60; t0.hour=s/3600; return *this-t0;}//二目运算符的重载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;}//一目运算符的重载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;}//自行编制用于测试的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; cout<<"下面表示两个时间加减t1..t2形式:\n"; t=t1+t2; cout<<"t1+t2="; t.display();t=t1-t2;cout<<"t1-t2="; t.display();t=t1+2000; //秒cout<<"t1+2000s="; t.display();t=t1-2000; //秒cout<<"t1-2000s=";t.display(); cout<<"下面表示一个时间的自加自减:\n"; t=t1++; cout<<"t1++=";t.display(); t=++t1; cout<<"++t1=";t.display(); t=t1--; cout<<"(t1--)=";t.display(); t=--t1; cout<<"(--t1)=";t.display();cout<<"下面表示两个时间加减t1.=t2形式:\n"; t1+=t2; cout<<"t1+=t2";t1.display(); t1-=t2; cout<<"t1-=t2";t1.display(); t1+=2000; cout<<"t1+=2000";t1.display(); t1-=2000; cout<<"t1-=5000";t1.display(); cout<<endl;}
心得:~~~~(>_<)~~~~
真是。。。。哎。。。!!!!!
做完这个题我竟然还活着!!!!
奇迹!!
0 0
- 第八周项目二----time类中的运算符重载
- 第八周 项目二:Time类中的运算符重载
- 第八周项目二time类中的运算符重载
- 第八周项目二-Time类中的运算符重载
- 第八周项目二 Time类中的运算符重载
- 第八周项目二:Time类中的运算符重载
- 第八周 项目二:Time类中的运算符重载
- 第八周项目二 Time类中的运算符重载
- 第八周 项目二 Time类中的运算符重载
- 第八周项目二 Time类中的运算符重载
- 第八周 项目二 Time类中的运算符重载
- 第八周 项目二 Time类中的运算符重载
- 第八周项目二Time类中的运算符重载
- 第八周项目二:Time类中的运算符重载
- 第八周项目二--Time类中的运算符重载
- 第八周项目二 Time类中的运算符重载
- 第八周项目二-Time类中的运算符重载
- 第八周 项目二 Time类中的运算符重载
- arm汇编--变量
- 可信认证的道德都去哪啦
- VisualSVN_TortoiseSVN_AnkhSvn_软件配置管理说明(实践)
- 当球员又做裁判?百度为自己提供信誉认证
- 星际之门(一)
- 第八周项目二time类中的运算符重载
- mysqldump导出删除auto_increment
- 从女人到认证
- SVN提示无法创建目录
- Ignatius and the Princess II
- 输入45个1--45的随机数
- 百度有意退出安全联盟 继续保护钓鱼网站利益?
- 地方的数据库了解该考虑三等奖港口螺丝刀九分裤
- NYOJ779兰州烧饼