Time类中的运算符重载
2013-04-19 10:10
190 查看
/* * 程序的版权和版本声明部分 * Copyright (c)2012, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称: object.cpp * 作者:隋 鑫 * 完成日期: 2013年 4 月 19 日 * 版本号: 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); //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); //二目运算符的重载 CTime operator+(CTime &c2);//返回c所规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15 CTime operator-(CTime &c2);//对照+理解 CTime operator+(int s);//返回s秒后的时间 CTime operator-(int s);//返回s秒前的时间 //一目运算符的重载 CTime operator++( int);//后置++,下一秒 CTime operator++();//前置++,下一秒,前置与后置返回值不一样 CTime operator--( int);//后置--,前一秒 CTime operator--();//前置--,前一秒 //赋值运算符的重载 //CTime operator+=(CTime &c); //CTime operator-=(CTime &c); //CTime operator+=(int s); //CTime operator-=(int s); }; //下面实现所有的运算符重载代码。 //为简化编程,请注意通过调用已有函数,利用好各函数之间的关系 CTime::CTime(int h,int m,int s) { hour=h; minute=m; second=s; } bool CTime::operator > (CTime &t) { if(hour>t.hour) return true; else if(hour==t.hour&&minute>t.minute) return true; else if(minute==t.minute&&second>t.second) return true; else return false; } bool CTime::operator < (CTime &t) { if(hour<t.hour) return true; else if(hour==t.hour&&minute<t.minute) return true; else if(minute==t.minute&&second<t.second) return true; else return false; } bool CTime::operator >= (CTime &t) { if(hour>=t.hour) return true; else if(hour==t.hour&&minute>=t.minute) return true; else if(minute==t.minute&&second>=t.second) return true; else return false; } bool CTime::operator <= (CTime &t) { if(hour<=t.hour) return true; else if(hour==t.hour&&minute<=t.minute) return true; else if(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; } bool CTime::operator != (CTime &t) { if(hour!=t.hour) return true; else if(minute!=t.minute) return true; else if(second!=t.second) return true; else return false; } CTime CTime::operator+(CTime &c2) { CTime c; c.second=second+c2.second; c.minute=minute+c2.minute; if(c.second>59) c.second-=60; c.minute++; c.hour=hour+c2.hour; if(c.minute>59) c.minute-=60; c.hour++; if(c.hour>23) c.hour-=24; return c; } CTime CTime::operator-(CTime &c2) { CTime c; c.second=second-c2.second; c.minute=minute-c2.minute; if(c.second>59) c.second+=60; c.minute--; c.hour=hour-c2.hour; if(c.minute>59) c.minute+=60; c.hour--; if(c.hour>23) c.hour+=24; return c; } CTime CTime::operator+(int s) { CTime c; c.second=second+s; c.minute=minute; c.hour=hour; if(c.second>59) c.minute=c.minute+c.second/60; c.second%=60; if(c.minute>69) c.hour=c.hour+c.minute/60; if(c.hour>23) c.hour%=24; return c; } CTime CTime::operator-(int s) { CTime c; c.second=second-s%60; c.minute=minute-s/60%60; c.hour=hour-s/3600%24; if(c.second>59) c.second+=60; c.minute--; if(c.minute>59) c.minute+=60; c.hour--; if(c.hour>23) c.hour+=24; return c; } CTime CTime::operator++( int) { CTime temp(*this); second++; { if(second>=60) { second-=60; ++minute; } else if(minute>=60) { minute-=60; ++hour; } else if (hour>=24) hour-=24; } return *this ; } CTime CTime::operator++() { { if(++second>=60) { second-=60; ++minute; } else if(minute>=60) { minute-=60; ++hour; } else if (hour>=24) hour-=24; } return *this ; } CTime CTime::operator--( int) { CTime temp(*this); second--; { if(second>=60) { second+=60; --minute; } else if(minute>=60) { minute+=60; --hour; } else if (hour>=24) hour+=24; } return *this ; } CTime CTime:: operator--() { { if(--second>=60) { second+=60; --minute; } else if(minute>=60) { minute+=60; --hour; } else if (hour>=24) hour+=24; } return *this ; } void CTime::display() { cout<<hour<<":"<<minute<<":"<<second<<endl; } int main() { CTime t1(8,20,25),t2(11,20,50),t; int s=500; cout<<"t1为:"; t1.display(); cout<<"t2为:"; t2.display(); cout<<"s为:"<<s<<"s"<<endl; cout<<endl; cout<<"下面比较两个时间大小:\n"; 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<<endl; t=t1+t2; cout<<"t1+t2="; t.display(); t=t1-t2; cout<<"t1-t2="; t.display(); cout<<endl; t=t1+s; cout<<"t1+s="; t.display(); t=t2+s; cout<<"t2+s="; t.display(); t=t1-s; cout<<"t1-s="; t.display(); t=t2-s; cout<<"t2-s="; t.display(); cout<<endl; t=t1++; cout<<"t1++="; t.display(); t=t1--; cout<<"t1--="; t.display(); t=t1--; cout<<"t1--="; t.display(); t=t2--; cout<<"t2--="; t.display(); t=++t1; cout<<"++t1="; t.display(); t=++t2; cout<<"++t2="; t.display(); t=--t1; cout<<"--t1="; t.display(); t=--t2; cout<<"--t2="; t.display(); cout<<endl; return 0; }
运行结果:
相关文章推荐
- 第九周项目2(1)-Time类中的运算符重载(续)
- 2-8-2 Time类中的运算符重载
- 第八周项目-2-time类中的运算符重载
- 第12周项目2——Time类中的运算符重载
- 第12周项目2-Time类中的运算符重载
- 第十二周【项目2-Time类中的运算符重载】
- 2013第八周上机任务【项目2-Time类中的运算符重载】
- 项目2-Time类中的运算符重载
- 八周 项目2 Time类中的运算符重载
- 第九周-项目2-Time类中的运算符重载
- 第九周项目2(2)-Time类中的运算符重载(续)
- 第八周项目2-Time类中的运算符重载
- Time类的运算符重载
- 第九周(项目二)——Time类中的运算符重载。
- 第八周——运算符重载——项目二 Time类
- 第8周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类中的运算符重载(续)
- 第12周项目2-Time类中的运算符重载
- Time类中的运算符重载
- 第八周项目2-TIME类中的运算符重载