第九周 项目2 Time类中的运算符重载(续)
2015-05-12 22:15
134 查看
问题及代码:
运行结果:
/*。 *Copyright(c)2014,烟台大学计算机学院 *All right reserved, *文件名:test.cpp *作者:liu_feng_zi_ *完成日期:2015年4月29日 *版本号:v1.0 *问题描述:定义对时间对象的自增和自减一目运算符 *输入描述: *程序输出: */ #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); 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 istream&operator>>(istream&in,CTime&t); friend ostream&operator<<(ostream&out,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; } 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) { 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) return false; return true; } CTime CTime::operator+(CTime &t) { CTime t1; t1.hour=hour+t.hour; t1.minute=minute+t.minute; t1.second=second+t.second; if(t1.second>=60) { t1.second%=60; t1.minute++; } if(t1.minute>=60) { t1.minute%=60; t1.hour++; } if(t1.hour>=24) t1.hour-=24; return t1; } CTime CTime::operator-(CTime &t) { CTime t1; t1.hour=hour-t.hour; t1.minute=minute-t.minute; t1.second=second-t.second; if(t1.second<0) { t1.second+=60; t1.minute--; } if(t1.minute<0) { t1.minute+=60; t1.hour--; } if(t1.hour<0) t1.hour+=24; return t1; } CTime CTime::operator+(int s) { CTime t1; t1.second=second+s; while(t1.second>=60) { t1.second%=60; t1.minute++; } while(t1.minute>=60) { t1.minute%=60; t1.hour++; } if(t1.hour>=24) t1.hour-=24; return t1; } CTime CTime::operator-(int s) { CTime t1; t1.second=second-s; while(t1.second<0) { t1.second+=60; t1.minute--; } while(t1.minute<0) { t1.minute+=60; t1.hour--; } if(t1.hour<0) t1.hour+=24; return t1; } 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 temp(*this); second++; if(second>=60) { second%=60; minute++; } if(minute>=60) { minute%=60; hour++; } if(hour>=24) hour-=24; return temp; } CTime CTime::operator++() { second++; if(second>=60) { second%=60; minute++; } if(minute>=60) { minute%=60; hour++; } if(hour>=24) hour-=24; return *this; } CTime CTime::operator--(int) { CTime temp(*this); if(second<0) { second+=60; minute--; } if(minute<0) { minute+=60; hour--; } if(hour<0) hour+=24; return temp; } 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) { in>>t.hour>>t.minute>>t.second; 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(11,20,50),t; cout<<"t1为:"<<t1; cout<<"t2为:"<<t2; 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; t=t1-t2; cout<<"t1-t2="<<t; t=t1+2000; cout<<"t1+2000="<<t; t=t1-5000; cout<<"t1-5000="<<t; t1+=t2; cout<<"t1+=t2="<<t1; t1-=t2; cout<<"t1-=t2="<<t1; t1+=2000; cout<<"t1+=2000="<<t1; t1-=5000; cout<<"t1-=5000="<<t1; t=t1++; cout<<t; t=++t1; cout<<t; t=++t2; cout<<t; t=t2++; cout<<t; return 0; }
运行结果:
相关文章推荐
- 第九周项目二-Time类中的运算符重载(续)
- 第九周 项目2-Time中的运算符重载(续)(1)
- 2015年第九周项目二:Time类中的运算符重载(续)(1)
- 第九周 项目二 Time类中的运算符重载(续)
- 第九周项目二:Time类中的运算符重载(续)
- 第九周项目2:Time类中的运算符重载(续)
- 第九周——运算符重载——项目二Time类(续)
- 第九周 项目二--Time类中的运算符重载(续)
- 第九周 项目二 Time类中的运算符重载(续)
- 第九周项目二 Time类中的运算符重载(续)-(1)定义自加++自减--一目运算符
- 第九周项目二 Time类中的运算符重载(续)(2)定义Time类的>>和<<
- 第九周项目二 Time类中的运算符重载(续)
- 第九周项目二:Time类中的运算符重载(续)
- 第九周 项目二:Time类中的运算符重载(续)
- 第九周 项目二-Time类中的运算符重载(续)
- 第九周项目二 Time类中的运算符重载(续)-(2)定义Time类的>>和<<
- 第九周 项目二:定义Time类中的<<和>>运算符重载
- 第九周项目二--Time类中的运算符重载(续)
- 第九周上级项目2 Time类中的运算符重载(续)
- 第八周【项目2-Time类中的运算符重载】实现Time类中的运算符重载