time类
来源:互联网 发布:淘宝助理有客服吗 编辑:程序博客网 时间:2024/06/11 15:47
#include <iostream>using namespace std;class Time{private:unsigned int short hour; unsigned int short minute;unsigned int short second;public: Time(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; } void display() { cout<<hour<<':'<<minute<<':'<<second<<endl; } //比较运算符的重载 //比较运算返回的是比较结果,是bool型的true或false bool operator>(Time &t); bool operator<(Time &t); bool operator>=(Time &t); bool operator<=(Time &t); bool operator==(Time &t); bool operator!=(Time &t); //二目的加减运算符的重载 //返回t规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15 Time operator+(Time &t); Time operator-(Time &t); Time operator+(int s);//返回s秒后的时间 Time operator-(int s);//返回s秒前的时间 //二目赋值运算符的重载 Time operator+=(Time &t); Time operator-=(Time &t); Time operator+=(int s);//返回s秒后的时间 Time operator-=(int s);//返回s秒前的时间 };//二目运算符的重载 //加运算返回 t 规定的时、分、秒后的时间,例t1(8,20,25),t2(11,20,50),t1+t2为:41:15 Time Time::operator+(Time &t){ int h,m,s;//这里要记得改为int,unsigned是没有符号的,即不会出现负数 h=hour+t.hour; m=minute+t.minute; s=second+t.second; if(s>=60)//顺序不能颠倒 { s=s-60; m++; } if(m>=60) { m=60-m; h++; } if(h>=24) { h=h-24; } Time t1(h,m,s); return t1;}// 计算时间之差 Time Time::operator-(Time &t){ int h,m,s;//这里要记得改为int,unsigned是没有符号的,即不会出现负数 h=hour-t.hour; m=minute-t.minute; s=second-t.second; if(s<0)//顺序不能颠倒 { s=s+60; m--; } if(m<0) { m=m+60; h--; } if(h<0) { h=h+24; } Time t2(h,m,s); return t2;}//返回s秒后的时间 Time Time::operator+(int s){ int ss=s%60; int mm=(s/60)%60;int hh=s/3600;Time t3(hh,mm,ss);return *this+t3;}//返回s秒前的时间 Time Time::operator-(int s){ int ss=s%60; int mm=(s/60)%60;int hh=s/3600;Time t4(hh,mm,ss);return *this-t4;}//赋值运算符的重载 //可以直接使用已经重载了的加减运算实现 //这种赋值, 例如 t1+=20,直接改变当前对象的值,所以在运算完成后,将*this作为返回值 Time Time::operator+=(Time &t){ *this=*this+t; return *this;}Time Time::operator-=(Time &t){ *this=*this-t; return *this;}Time Time::operator+=(int s)//返回s秒后的时间 { *this=*this+s; return *this;}Time Time::operator-=(int s)//返回s秒前的时间{ *this=*this-s; return *this;}//比较运算符的重载 bool Time::operator>(Time &t)//判断时间t1>t2 { 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 Time::operator<(Time &t)//判断时间t1<t2 { 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 Time::operator>=(Time &t)// 判断时间t1>=t2 { if(*this<t) return false; return true;}bool Time::operator<=(Time &t)// 判断时间t1<=t2 { if(*this>t) return false; return true;}bool Time::operator==(Time &t)// 判断时间t1==t2 { if(*this>t||*this<t) return false; return true;}bool Time::operator!=(Time &t)// 判断时间t1!=t2 { if(*this==t) return false; return true;}int main() { Time t5(8,20,25),t6(11,20,50),t7; cout<<"t5为:"; t5.display(); cout<<"t6为:"; t6.display(); cout<<"下面比较两个时间大小:\n"; if(t5>t6) cout<<"t5>t6"<<endl; if (t5<t6) cout<<"t5<t6"<<endl; if (t5==t6) cout<<"t5=t6"<<endl; if (t5!=t6) cout<<"t5≠t6"<<endl; if (t5>=t6) cout<<"t5≥t6"<<endl; if (t5<=t6) cout<<"t5≤t6"<<endl; cout<<endl;cout<<"t5+t6="; t7=t5+t6;t7.display(); cout<<"t5-t6="; t7=t5-t6;t7.display(); cout<<"t6+2000="; t7=t6+2000;t7.display();cout<<"t6-5000="; t7=t6-5000;t7.display();cout<<"t5+=t6的结果:"; t5+=t6;t5.display();cout<<"t5-=t6的结果:"; t5-=t6; t5.display();cout<<"t5+=2000的结果:"; t5+=2000; t5.display();cout<<"t5-=5000的结果:"; t5-=5000; t5.display();return 0;}
0 0
- Time类
- Time类
- Time类
- time类
- Time类
- Time类
- time类
- Time类
- Time类
- Time类
- 项目二--Time类
- Unity Time类详解
- UNITY 5.2 Time类
- 解读Time类
- Time类解析
- Time类的定义
- Time、Random、Mathf类
- Unity 入门 Time类
- hdu 1074 状态压缩dp
- 粥中有道,读书笔记
- 微信公众平台通知插件,网店必备客户服务工具
- linux下安装mysql
- Socket通信原理
- time类
- 南阳理工OJ_题目891 找点
- nyoj119(RMQ算法+线段树)
- Cloud Chou's Tech Blog编译相关
- C++箴言:用传引用给const取代传值
- 续写某坑爹公司的教学笔记-Foundation整套Day01-04
- css使背景图片旋转
- jQuery制作的手风琴折叠菜单
- 《算法导论》笔记 第22章 22.2 广度优先搜索