标签:c++ 生活
/*
*copyright(c) 2015,烟台大学计算机学院
*All rights reserved。
*文件名称:第九周(运算符重载时间类)
*作者:王忠
*完成日期:2015.5.13
*版本号:v1.0
*
*问题描述:实现Time类中的运算符重载。
*输入描述:
*程序输出:
#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) { hour=h; minute=m; second=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); //二目的加减运算符的重载 //返回t规定的时、分、秒后的时间 //例t1(8,20,25),t2(11,20,50),t1+t2为19: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--();//前置--,前一秒 friend ostream& operator <<(ostream& output,const CTime& t); friend istream& operator >>(istream& input,CTime& t); }; 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) { hour=hour+t.hour; minute=minute+t.minute; second=second+t.second; if(second>59) { minute++; second=second%60; } if(minute>59) { hour++; minute=minute%60; } if(hour>23) hour=hour%24; CTime t2(hour,minute,second); return t2; } CTime CTime::operator-(CTime &t) { hour=hour-t.hour; minute=minute-t.minute; second=second-t.second; if(second<0) { minute--; second=second+60; } if(minute<0) { hour--; minute=minute+60; } if(hour<0) hour=hour+24; CTime t2(hour,minute,second); return t2; } CTime CTime::operator+(int s)//返回s秒后的时间 { second=second+s; int ss=s/60; if(second>59) { minute+=ss; second=second%60; } int mm=minute/60; if(minute>59) { hour+=mm; minute=minute%60; } if(hour>23) hour=hour%24; CTime t2(hour,minute,second); return t2; } CTime CTime::operator-(int s)//返回s秒前的时间 { second=second-s; if(second<0) { minute--; second=second+60; } if(minute<0) { hour--; minute=minute+60; } if(hour<0) hour=hour+24; CTime t2(hour,minute,second); return t2; } 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)//返回s秒后的时间 { *this=*this+s; return *this; } CTime CTime::operator-=(int s)//返回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; } ostream& operator <<(ostream& output,const CTime& t) { output<<t.hour<<':'<<t.minute<<':'<<t.second<<endl; return output; } istream& operator >>(istream& input,CTime& t) { int h,m,s; char sign1,sign2; do { cout<<"input h:m:s"<<endl; input>>h>>sign1>>m>>sign2>>s; }while(!(sign1==':'||sign2==':')); t.hour=h; t.minute=m; t.second=s; return input; } int main() { CTime t1,t2,t; cin>>t1>>t2; cout<<"t1为:"<<t1<<endl; cout<<"t2为:"<<t2<<endl; t1++; cout<<"t1为:"<<t1<<endl; t2--; cout<<"t2为:"<<t2<<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<<t<<endl; t=t1-t2; t=t1+2000; cout<<t<<endl; t=t1-5000; t1+=t2; t1-=t2; t1+=2000; t1-=5000; return 0; }
这个是自己写出来的,自己写的测试 好棒
标签:c++ 生活
原文地址:http://blog.csdn.net/wangzhongwangmin/article/details/45688251