第九周项目二 Time类中的运算符重载(续)(2)定义Time类的>>和<<
来源:互联网 发布:软件登录itunes store 编辑:程序博客网 时间:2024/05/16 19:43
#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); //二目的加减运算符的重载 //返回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 istream& operator>>(istream&,CTime &); friend ostream& operator<<(ostream&,const CTime &); };//istream& operator>>(istream& input,CTime &t) { char ch1,ch2; while(1) { cout<<"请以hh:mm:ss格式输入时间:"<<endl; cin>>t.hour>>ch1>>t.minute>>ch2>>t.second; if (t.hour<24&&t.hour>=0&&ch1==':'&&t.minute<60&&t.minute>=0&&ch2==':'&&t.second<60&&t.second>=0) break; else cout<<"时间格式输入错误,请重新输入!"<<endl; } return input; }ostream& operator<<(ostream& output,const CTime &t) { output<<t.hour<<":"<<t.minute<<":"<<t.second; return output; }//CTime CTime::operator++(int) { CTime t=*this; *this+=1; return t;//!! }CTime CTime::operator++() { *this+=1; return *this;//!! }CTime CTime::operator--(int) { CTime t=*this; *this-=1; return t; }CTime CTime::operator--() { *this-=1; return *this; }//CTime::CTime(int h,int m,int s) { hour=h; minute=m; second=s; return; }void CTime::setTime(int h,int m,int s) { hour=h; minute=m; second=s; return; }void CTime::display() { cout<<hour<<": "<<minute<<": "<<second<<endl; return; }//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; else return true; }bool CTime::operator <= (CTime &t) { if (*this>t)return false; else return true; }bool CTime::operator == (CTime &t) { if (*this<t||*this>t)return false; else return true; }bool CTime::operator != (CTime &t) { if (*this==t)return false; else return true; }//CTime CTime::operator+(CTime &t) { int h,m,s; h=hour+t.hour; m=minute+t.minute; s=second+t.second; if (s>59) { m++; s-=60;//(莫忘了这一步) } if (m>59) { h++; m-=60; } if (h>23) { h-=24; } CTime t1(h,m,s);//注意不能写成 hour=h;minute=m;second=s; 应不改变原值 return t1; }CTime CTime::operator-(CTime &t) { int h,m,s; h=hour-t.hour; m=minute-t.minute; s=second-t.second; if (s<0) { m--; s+=60;//莫忘了这一步 } if (m<0) { h--; m+=60; } if (h<0) { h+=24; } CTime t1(h,m,s);//注意不能写成 hour=h;minute=m;second=s; 应不改变原值 return t1; }CTime CTime::operator+(int s) { int ss=s%60, mm=(s/60)%60, hh=s/3600; CTime t(hh,mm,ss); return *this+t; /*之前用的下面这段,有点复杂了,而且减法不好处理 int ss=second+s,mm=minute,hh=hour; if (ss>59) { mm+=ss/60; ss%=60; } if (mm>59) { hh+=mm/60; mm%=60; } if (hh>23) { hh%=24; } CTime t1(hh,mm,ss); return t1; */ }CTime CTime::operator-(int s) { int ss=s%60, mm=(s/60)%60, hh=s/3600; CTime t(hh,mm,ss); return *this-t; }//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; }int main() { CTime t1(20,30,41),t2(13,32,20),t3(20,11,36),tt1,tt2,tt3; cout<<"t1时刻为:"; t1.display(); cout<<"t2时刻为:"; t2.display(); cout<<"t3时刻为:"; t3.display(); cout<<endl; // 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; if (t1<=t2) cout<<"t1≤t2"<<endl; cout<<endl; // cout<<"t2与t3时刻比较大小:"<<endl; if (t2>t3) cout<<"t2>t3"<<endl; if (t1<t3) cout<<"t2<t3"<<endl; if (t2==t3) cout<<"t2=t3"<<endl; if (t2!=t3) cout<<"t2≠t3"<<endl; if (t2>=t3) cout<<"t2≥t3"<<endl; if (t2<=t3) cout<<"t2≤t3"<<endl; cout<<endl; //在测试下面的代码时,请采用单步执行的方法跟踪 cout<<"下面执行时间计算更改调试:"<<endl; cout<<"t1+t2="; tt1=t1+t2; tt1.display(); cout<<"t2-t3="; tt2=t2-t3; tt2.display(); cout<<"t1+2000s="; tt3=t1+2000; tt3.display(); cout<<"t1+2000s-5000s="; tt3=tt3-5000; tt3.display(); cout<<"t1+=t2: "; t1+=t2; t1.display(); cout<<"t2-=t3: "; t2-=t3; t2.display(); cout<<"t3+=100: "; t3+=100; t3.display(); cout<<"t3'-=600: "; t3-=600; t3.display(); cout<<"t1++ :"; tt1=t1++; tt1.display(); cout<<"++t1 :"; tt1=++t1; tt1.display(); cout<<"t1-- :"; tt1=t1--; tt1.display(); cout<<"--t1 :"; tt1=--t1; tt1.display(); cout<<endl; cin>>tt2; cout<<"输入的时刻显示:"<<tt2; return 0; }
运行结果
0 0
- 第九周项目二 Time类中的运算符重载(续)-(2)定义Time类的>>和<<
- 第九周项目二 Time类中的运算符重载(续)(2)定义Time类的>>和<<
- 15第九周项目二——Time类中的运算符的重载(续)
- 第九周 项目二:定义Time类中的<<和>>运算符重载
- 第九周项目二Time类中的运算符重载(续)(2)
- 第九周项目二Time类中的运算符重载(续)(1)
- 2015年第九周项目二:Time类中的运算符重载(续)(1)
- 第九周 项目二:Time类中的运算符重载(续)
- 第九周 项目二-Time类中的运算符重载(续)
- 第九周项目二--Time类中的运算符重载(续)
- 第九周 课后实践:项目二——Time类中的运算符重载(续)
- 第九周项目二——Time类中的运算符重载(续)
- 第九周项目二:Time类中的运算符重载(续)
- 第九周 项目二 Time类中的运算符重载(续)
- 第九周项目二 Time类中的运算符重载(续)
- 第九周项目二-Time类中的运算符重载(续)
- 第九周 项目二 Time类中的运算符重载(续)
- 第九周 项目二--Time类中的运算符重载(续)
- hdu5229 ZCC loves strings 博弈
- opencv 2.0 CvMat使用
- hdoj1814 Peaceful Commission【2-set】
- 二进制到十六进制转换程序
- ant的文件相关操作
- 第九周项目二 Time类中的运算符重载(续)(2)定义Time类的>>和<<
- 大连交大iptv
- 【万里征程——Windows App开发】如何使用粘贴板
- 不同profile之间EJB调用问题javax.naming.NameNotFoundException
- hello, world!
- 第十一周阅读程序二
- sass语法
- Struts2 - 上传任意多个文件
- 黑马程序员---Objective-C 成员变量的作用域