第九周项目2(1)-Time类中的运算符重载(续)
2015-05-04 21:34
453 查看
/* *Copyright (c) 2014, 烟台大学计算机学院 *All rights reserved. *文件名称:week9-2-1.cpp *作者:高赞 *完成日期:2015年 5 月 4 日 *版本号:v1.1 * * 问题描述:在Time类中的运算符重载基础上,定义对时间对象的自增和自减一目运算符 */ #include <iostream> #include "CTime.h" using namespace std; 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; t=t1+t2; cout<<"t1+t2="; t.display(); t=t1-t2; cout<<"t1-t2="; t.display(); t=t1+2000; cout<<"t1+2000s="; t.display(); t=t1-5000; cout<<"t1-5000s="; t.display(); t1+=t2; cout<<"t1+=t2,t1="; t1.display(); t1-=t2; cout<<"t1-=t2,t1="; t1.display(); t1+=2000; cout<<"t1+=2000s,t1="; t1.display(); t1-=7000; cout<<"t1-=7000s,t1="; t1.display(); ++t1; cout<<"++t1 ="; t1.display(); --t1; cout<<"--t1 ="; t1.display(); t1++; cout<<"t1++ ="; t1.display(); t1--; cout<<"t1-- ="; t1.display(); return 0; }
CTime.h
#ifndef CTIME_H_INCLUDED #define CTIME_H_INCLUDED class CTime { private: int hour; // 时 int minute; // 分 int sec; // 秒 public: CTime(int h=0,int m=0,int s=0):hour(h),minute(m),sec(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秒前的时间 //一目运算符的重载 CTime operator++(int);//后置++,下一秒 CTime operator++();//前置++,下一秒,前置与后置返回值不一样 CTime operator--(int);//后置--,前一秒 CTime operator--();//前置--,前一秒 }; #endif // CTIME_H_INCLUDED
CTime.cpp
#include <iostream> #include "CTime.h" using namespace std; void CTime::setTime(int h,int m,int s) { hour=h; minute=m; sec=s; } void CTime::display() { cout<<hour<<":"<<minute<<":"<<sec<<endl; } bool CTime::operator > (CTime &t) { bool b; if(hour>t.hour) b=true; else if(hour<t.hour) b=false; else { if(minute>t.minute) b=true; else if(minute<t.minute) b=false; else { if(sec>t.sec) b=true; else if(sec<=t.sec) b=false; } } return b; } bool CTime::operator < (CTime &t) { bool b; if(hour<t.hour) b=true; else if(hour>t.hour) b=false; else { if(minute<t.minute) b=true; else if(minute>t.minute) b=false; else { if(sec<t.sec) b=true; else if(sec>=t.sec) b=false; } } return b; } 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||*this<t) return true; else return false; } CTime CTime::operator+(CTime &t) { int h=hour+t.hour,m=minute+t.minute,s=sec+t.sec; if(s>=60) { s-=60; ++m; } if(m>=60) { m-=60; ++h; } if(h>=24) h-=24; return CTime(h,m,s); } CTime CTime::operator-(CTime &t) { int h=hour-t.hour,m=minute-t.minute,s=sec-t.sec; if(s<0) { s+=60; --m; } if(m<0) { m+=60; --h; } if(h<0) h+=24; return CTime(h,m,s); } CTime CTime::operator+(int S) { int h,m,s; s=S%60; S=S/60; m=S%60; h=S/60; CTime t2(h,m,s); return (*this+t2); } CTime CTime::operator-(int S) { int h,m,s; s=S%60; S=S/60; m=S%60; h=S/60; CTime t2(h,m,s); return (*this-t2); } CTime CTime::operator+=(CTime &t) { hour+=t.hour,minute+=t.minute,sec+=t.sec; if(sec>=60) { sec-=60; ++minute; } if(minute>=60) { minute-=60; ++hour; } if(hour>=24) hour-=24; return *this; } CTime CTime::operator-=(CTime &t) { hour-=t.hour,minute-=t.minute,sec-=t.sec; if(sec<0) { sec+=60; --minute; } if(minute<0) { minute+=60; --hour; } if(hour<0) hour+=24; return *this; } CTime CTime::operator+=(int S) { int h,m,s; s=S%60; S=S/60; m=S%60; h=S/60; CTime t2(h,m,s); return (*this+=t2); } CTime CTime::operator-=(int S) { int h,m,s; s=S%60; S=S/60; m=S%60; h=S/60; CTime t2(h,m,s); return (*this-=t2); } //一目运算符的重载 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; }
相关文章推荐
- 第九周项目二——Time类中的运算符重载(续)
- 第九周项目二 Time类中的运算符重载(续)(2)定义Time类的>>和<<
- 第九周-项目2-Time类中的运算符重载
- 第九周 【项目2-Time类中的运算符重载(续)】
- 第九周项目2-Time类中的运算符重载(续)
- 第九周(项目二)——Time类中的运算符重载。
- 第九周 项目2-Time类中的运算符重载(1)
- 第九周项目2——Time类中的运算符重载(续)
- 第九周上机实践项目2——Time类中的运算符重载(续)
- 第九周 项目2-Time类中的运算符重载(2)
- 第九周项目二——Time类中的运算符重载(利用输入输出运算符重载)
- 第九周【项目2-Time类中的运算符重载(续)】
- 第九周上机实践项目2-Time类中的运算符重载(续)
- 第九周项目二 Time类中的运算符重载(续)-(2)定义Time类的>>和<<
- 第九周项目2——Time类中的运算符重载(续)
- 第九周项目2-Time类中的运算符重载(续)
- 第九周项目2(2)-Time类中的运算符重载(续)
- 第九周 课后实践:项目二——Time类中的运算符重载(续)
- 第九周项目2-Time类中的运算符重载(续)
- 第九周项目2-Time类中的运算符重载(续)