第八周项目2-Time类中的运算符重载
2015-05-13 09:05
423 查看
#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 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 &t); CTime operator-(CTime &t); CTime operator+(int s); CTime operator-(int s); 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; } void CTime::setTime(int h,int m,int s) { hour=h; minute=m; second=s; } 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; else 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; else return false; } bool CTime::operator>=(CTime &t) { if(*this<t) return false; return true; } bool CTime::operator<=(CTime &t) { 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; } CTime CTime::operator+(CTime &t) { CTime c; c.hour=hour+t.hour; c.minute=minute+t.minute; c.second=second+t.second; if(c.second>60) { c.minute=c.minute+c.second/60; c.second=c.second%60; } if(c.minute>60) { c.hour=c.hour+c.minute/60; c.minute=c.minute%60; } if(c.hour>24) { c.hour=c.hour%24; } return c; } CTime CTime::operator-(CTime &t) { CTime c; c.hour=hour-t.hour; c.minute=minute-t.minute; c.second=second-t.second; if(c.second<0) { c.second+=60; c.minute--; } if(c.minute<0) { c.minute+=60; c.hour--; } if(c.hour<0) { c.hour+=24; } return c; } CTime CTime::operator+(int s) { second+=s; if(second>60) { minute=minute+second/60; second=second%60; } if(minute>60) { hour=hour+minute/60; minute=minute%60; } if(hour>24) { hour=hour%24; } return *this; } CTime CTime::operator-(int s) { second-=s; if(second<0) { second+=60; minute--; } if(minute<0) { minute+=60; hour--; } if(hour<0) { hour+=24; } 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) { *this+=s; return *this; } CTime CTime::operator-=(int s) { *this-=s; return *this; } int main() { CTime t1(8,20,25),t2; int h,m,s; cout<<"输入时间大小:"; cin>>h>>m>>s; t2.setTime(h,m,s); cout<<"两个时间分别为:"<<endl; t1.display(); t2.display(); cout<<"下面比较两个时间大小:"<<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; if(t1!=t2) cout<<"t1!=t2"<<endl; CTime t; cout<<"下面是时间的加减运算符的重载:"<<endl; t=t1+t2; cout<<"t1+t2="; t.display(); t=t1-t2; cout<<"t1-t2="; t.display(); t1+=t2; cout<<"t1+=t2 "; t1.display(); t1-=t2; cout<<"t1-=t2 "; t1.display(); t1=t1+55; cout<<"t155秒后时间为:"; t1.display(); t2=t1-55; cout<<"t255秒前的时间为:"; t2.display(); return 0; }
运算结果:
相关文章推荐
- 第八周项目2——time类中的运算符重载
- 第八周 项目2-Time类中的运算符重载
- 第八周【项目2-Time类中的运算符重载】实现Time类中的运算符重载
- 第八周项目2-time类中的运算符重载
- 第八周 【项目2-Time类中的运算符重载】
- 第八周项目 二 【项目2-Time类中的运算符重载】
- 15第八周项目二——Time类中的运算符重载
- 第八周项目-2-time类中的运算符重载
- 第八周项目2-Time类中的运算符重载
- 第八周项目2-Time类中的运算符重载
- 第八周【项目2-Time类中的运算符重载】
- 第八周上机实践项目2——Time类中的运算符重载
- 2013第八周上机任务【项目2-Time类中的运算符重载】
- 第八周 项目2 Time类中的运算符重载
- 第八周——运算符重载——项目二 Time类
- 第八周(项目二)——Time类中的运算符重载。
- 第八周项目2-Time类中的运算符重载
- 第八周 项目2 TIME类中的运算符重载
- 第八周项目2-TIME类中的运算符重载
- 第八周上机实践项目2-Time类中的运算符重载