第9周项目2-Time类中的运算符重载(续)
2015-05-17 20:56
447 查看
#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 operator++(int); CTime operator++(); CTime operator--(int); CTime operator--(); friend ostream& operator<<(ostream &out,CTime &t); friend istream& operator>>(istream &in,CTime &t); }; 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; } CTime CTime::operator++(int) { second++; 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++() { 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) { second--; if(second<0) { second+=60; minute--; } if(minute<0) { minute+=60; hour--; } if(hour<0) { hour+=24; } return *this; } CTime CTime::operator--() { if(--second<0) { second+=60; minute--; } if(minute<0) { minute+=60; hour--; } if(hour<0) { hour+=24; } return *this; } istream& operator>>(istream &in,CTime &t) { int h,m,s; char c; do { cout<<"请输入时间大小:"; in>>h>>c>>m>>c>>s; }while(!(c==':')); t.hour=h; t.minute=m; t.second=s; return in; } ostream& operator<<(ostream &out,CTime &t) { out<<t.hour<<':'<<t.minute<<':'<<t.second<<endl; return out; } int main() { CTime t1(8,20,25),t2; int h,m,s; cout<<"输入时间大小:"; cin>>h>>m>>s; t2.setTime(h,m,s); cout<<"两个时间分别为:"<<endl; cout<<t1<<endl<<t2<<endl; 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; t=t1-t2; cout<<"t1-t2="<<t; t1+=t2; cout<<"t1+=t2 "<<t1; t1-=t2; cout<<"t1-=t2 "<<t1; t1=t1+55; cout<<"t155秒后时间为:"<<t1; t2=t1-55; cout<<"t255秒前的时间为:"<<t2; t=t1++; cout<<"t1++="<<t; t=++t1; cout<<"++t1="<<t; t=t2--; cout<<"t2--="<<t; t=--t2; cout<<"--t2="<<t; return 0; }
运行结果:
相关文章推荐
- 第9周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类中的运算符重载续(1)
- 第9周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类的运算符重载续(2)
- 第9周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类中的运算符重载(续)
- 第九周(项目二)——Time类中的运算符重载。
- 第十周上机项目2-Time类中的运算符重载
- 第9周项目1——复数类中的运算符重载(续)
- 第9周项目1复数类的运算符重载续(1)
- 第9周项目1-复数类中的运算符重载(续)
- 第十二周【项目2-Time类中的运算符重载】
- 第八周项目2-Time类中的运算符重载
- 项目2-Time类中的运算符重载
- 第12周项目2-Time类中的运算符重载
- 第9周项目1-复数类中的运算符重载(续)
- 第九周项目二 Time类中的运算符重载(续)(2)定义Time类的>>和<<
- 第9周项目1复数类的运算符重载续(2)
- 第十一周项目——Time类中的运算符重载
- 第12周项目2——Time类中的运算符重载