第八周项目2-Time类中的运算符重载
2015-05-17 10:44
501 查看
问题及代码: /* *Copyright (c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:test.cop *作者: *完成日期:2015年5月17日 *版本号: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){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为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秒前的时间 }; 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*3600+minute*60+second)>(t.hour*3600+t.minute*60+second)) return true; else return false; } bool CTime::operator<(CTime &t) { if((hour*3600+minute*60+second)<(t.hour*3600+t.minute*60+second)) return true; else return false; } bool CTime::operator<=(CTime &t) { return !(*this>t); } bool CTime::operator>=(CTime &t) { return !(*this<t); } bool CTime::operator!=(CTime &t) { if((*this>t)==true||(*this<t)==true) return true; else return false; } bool CTime::operator==(CTime &t) { return!(*this!=t); } CTime CTime::operator + (CTime &t) { int h,m,s; s=second+t.second; m=minute+t.minute; h=hour+t.hour; if (s>59) { s-=60; m++; } if (m>59) { m-=60; h++; } if (h>23) h-=24; CTime t0(h,m,s); return t0; } CTime CTime::operator - (CTime &t) { int h,m,s; s=second-t.second; m=minute-t.minute; h=hour-t.hour; if (s<0) { s+=60; m--; } if (m<0) { m+=60; h--; } if (h<0) h+=24; CTime t0(h,m,s); return t0; } CTime CTime::operator+(int s) { second+=s; if(second>59) { second=second/60; minute+=second%60; } if(minute>59) { minute=minute/60; hour+=minute%60; } if(hour>23) { hour=hour/24; } return *this; } CTime CTime::operator-(int s) { second-=s; if(second<0) { second=(60+second/60); minute+=second%60; } if(minute<0) { minute=(60+minute/60); hour+=minute%60; } if(hour<0) { hour=(24+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 t) { *this=*this+t; return *this; } CTime CTime::operator-=(int t) { *this=*this-t; return *this; } 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=t1-t2; t=t1+2000; t=t1-5000; t1+=t2; t1-=t2; t1+=2000; t1-=5000; return 0; }
运行结果:
相关文章推荐
- 第八周项目二——Time类中的运算符重载
- 第八周项目二——Time类中的运算符重载
- 第八周项目三-实现分数类的运算符的重载
- 第八周 项目一:任务一(成员函数完成运算符的重载)
- 第八周 项目一:任务二(友元函数完成运算符的重载)
- 第八周项目三分数类中的运算符重载
- 第八周 项目二:Time类中的运算符重载
- 第八周 项目2-Time类中的运算符
- 第八周 项目2-Time类中的运算符重载之二目比较运算符的重载
- 第八周-项目一-复数类中的运算符的重载
- 第八周项目1-实现复数类中运算符的重载
- 15第八周项目一——实现复数类中的运算符的重载(完整类)
- 15第九周项目二——Time类中的运算符的重载(续)
- 第八周项目1复数类运算符的重载
- 第八周项目一(2)友元函数对运算符的重载
- 第八周项目2 TIME类中的运算重载符
- 第八周项目四 【炒鸡难!!高能预警!】String类的构造(重载双目运算符)
- 第八周项目一:用类的成员函数完成运算符的重载(任务一)
- 第八周项目一の任务一----用类的成员函数完成运算符的重载
- 第八周项目1.2--用类的友元函数完成运算符的重载