【项目2-Time类中的运算符重载】实现Time类中的运算符重载
2014-04-17 16:36
411 查看
/* * 程序的版权和版本声明部分 * Copyright (c)2014, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称: array.cpp * 作 者: 刘江 * 完成日期:2013年4月17日 * 版本号: 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); 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为: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秒前的时间 }; //下面实现所有的运算符重载代码。 void CTime::display() { cout<<hour<<":"<<minute<<":"<<second<<endl; } 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; if(second<t.second) 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; if(second>t.second) return false; } 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; } 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; } CTime CTime::operator+(CTime &t) { int h1,m1,s1; h1=hour+t.hour; m1=minute+t.minute; s1=second+t.second; if(s1>59) { s1-=60; m1++; } if(m1>59) { m1-=60; h1++; } if(h1>23) { h1-=24; } CTime tt(h1,m1,s1); return tt; } CTime CTime::operator-(CTime &t) { int h1,m1,s1; h1=hour-t.hour; m1=minute-t.minute; s1=second-t.second; if(s1<0) { s1+=60; m1--; } if(m1<0) { m1+=60; h1--; } if(h1<0) { h1+=24; } CTime tt(h1,m1,s1); return tt; } CTime CTime::operator+(int s) { int h1,m1,s1; s1=s%60; m1=(s/60)%60; h1=s/3600; CTime tt(h1,m1,s1); return *this+tt; } CTime CTime::operator-(int s) { int h1,m1,s1; s1=s%60; m1=(s/60)%60; h1=s/3600; CTime tt(h1,m1,s1); return *this-tt; } 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; } //自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果 int main() { CTime t1(8,20,25),t2(11,20,50),t; cout<<"t1为:"; t1.display(); cout<<"t2为:"; t2.display(); 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;t.display(); t=t1-t2;t.display(); t=t1+2000;t.display(); t=t1-5000;t.display(); t1+=t2;t1.display(); t1-=t2;t1.display(); t1+=2000;t1.display(); t1-=5000;t1.display(); return 0; }
运行结果:
心得体会:
对于老贺的借鉴了一部分,发现自己想的太多了,而且错误百出,但绝对会抄之有道
相关文章推荐
- 第八周【项目2-Time类中的运算符重载】实现Time类中的运算符重载
- 项目三 实现Time类中的运算符重载 (代码之后有惊喜,一定要读完)
- 第八周项目二-实现Time类中的运算符重载
- 第二学期第八周项目2--实现Time类中的运算符重载
- C++第8周(春)项目2 实现Time类中的运算符重载
- 项目二-Time类定义Time类中的<<和>>运算符重载
- C++项目——实现Time类中的运算符重载
- 第8周项目二实现Time中的运算符重载
- 第八周C++上机报告(项目三__实现Time类中的运算符重载)
- 第九周项目二 Time类中的运算符重载(续)-(2)定义Time类的>>和<<
- 第九周项目二 Time类中的运算符重载(续)(2)定义Time类的>>和<<
- C++第8周(春)项目2 实现Time类中的运算符重载 .
- 第八周 项目一 实现复数类中的运算符重载1
- 项目1-实现复数类中的运算符重载 3
- 第8周项目1-实现复数类中的运算符重载
- 第十二周项目三:Time类中的运算符重载
- 第八周 课后实践:项目二——Time类中的运算符重载
- 第八周 项目1.2 用友元函数实现复数类中的运算符重载
- 第九周上级项目2 Time类中的运算符重载(续)
- 第12周项目 实现复数类中的运算符重载2