2015.5.13time类中的运算符重载(续)
来源:互联网 发布:神兽传说java下载 编辑:程序博客网 时间:2024/06/06 03:51
#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); void setTime(int h,int m,int s); bool operator>(CTime &t); bool operator<(CTime &t); bool operator>=(CTime &t); bool operator<=(CTime &t); bool operator==(CTime &t); bool operator!=(CTime &t); CTime operator+(CTime &t); CTime operator-(CTime &t); CTime operator+(int s); CTime operator-(int s); CTime operator+=(CTime &t); CTime operator-=(CTime &t); CTime operator+=(int s); CTime operator-=(int s); CTime operator++(); CTime operator--(); CTime operator++(int); CTime operator--(int); friend ostream& operator<<(ostream&,CTime &c); friend istream& operator>>(istream&,CTime &c); void adjust_sec(); void adjust_min(int t); void adjust_hou(int t);};CTime::CTime(int h,int m,int s){ hour=h; minute=m; second=s;}void CTime::setTime(int h,int m,int s){ hour=h; minute=m; second=s;}bool CTime::operator>(CTime &t){ if(hour>t.hour||(hour=t.hour&&minute>t.minute)||((hour=t.hour,minute=t.minute)&&second>t.second)) return true; else return false;}bool CTime::operator<(CTime &t){ if(hour<t.hour||(hour=t.hour&&minute<t.minute)||((hour=t.hour,minute=t.minute)&&second<t.second)) return true; else 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||*this<t) return true; else return false;}CTime CTime::operator+(CTime &t){ CTime time; time.hour=hour+t.hour; time.minute=minute+t.minute; time.second=second+t.second; time.adjust_sec(); return time;}CTime CTime::operator-(CTime &t){ CTime time; time.hour=hour-t.hour; time.minute=minute-t.minute; time.second=second-t.second; time.adjust_sec(); return time;}CTime CTime::operator+(int s){ CTime time; time.hour=hour; time.minute=minute; time.second=second+s; time.adjust_sec(); return time;}CTime CTime::operator-(int s){ CTime time; time.hour=hour; time.minute=minute; time.second=second-s; time.adjust_sec(); return time;}CTime CTime::operator+=(CTime &t){ second+=t.second; minute+=t.minute; hour+=t.hour; CTime::adjust_sec(); return *this;}CTime CTime::operator-=(CTime &t){ second+=t.second; minute+=t.minute; hour+=t.hour; CTime::adjust_sec(); return *this;}CTime CTime::operator+=(int s){ second+=s; CTime::adjust_sec(); return *this;}CTime CTime::operator-=(int s){ second-=s; CTime::adjust_sec(); return *this;}CTime CTime::operator++(){ second++; CTime::adjust_sec(); return *this;}CTime CTime::operator--(){ second--; CTime::adjust_sec(); return *this;}CTime CTime::operator++(int){ CTime c; c=*this; second++; CTime::adjust_sec(); return c;}CTime CTime::operator--(int){ CTime c; c=*this; second--; CTime::adjust_sec(); return c;}void CTime::adjust_sec(){ int m; if(second>=0) { m=second/60; second%=60; CTime::adjust_min(m); } else { m=second/60; m=m-1; second%=60; second+=60; CTime::adjust_min(m); }}void CTime::adjust_min(int t){ int h; minute+=t; if(minute>=0) { h=minute/60; minute%=60; CTime::adjust_hou(h); } else { h=minute/60; h=h-1; minute%=60; minute+=60; CTime::adjust_hou(h); }}void CTime::adjust_hou(int t){ hour+=t; if(hour>=0) { hour%=24; } else { hour%=24; hour+=24; }}ostream& operator<<(ostream&,CTime&c){ cout<<c.hour<<':'<<c.minute<<':'<<c.second<<endl; return cout;}istream& operator>>(istream&,CTime&c){ cout<<"输入小时,分钟和秒"; cin>>c.hour>>c.minute>>c.second; return cin;}int main(){ CTime t1,t2,t3; cout<<"输入t1,t2"<<endl; cin>>t1>>t2; cout<<"t1,t2分别为"<<endl; cout<<t1<<t2; cout<<"t1+t2="; t3=t1+t2; cout<<t3; t3=t1++; cout<<t3; t3=++t1; cout<<t3; cout<<(++t1); return 0;}
0 0
- 2015.5.13time类中的运算符重载(续)
- Time类中的运算符重载(续)
- Time 类中的运算符重载(续)
- 实现Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- 实现Time类中的运算符重载
- 实现Time类中的运算符重载
- Time 类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- Time类中的运算符重载
- 从汇编到AWK
- ios开发进阶,从Xcode开始
- android:windowSoftInputMode属性详解
- 使用CocoaPods来管理Objective-c的类库,自动联想补齐头文件
- Python学习
- 2015.5.13time类中的运算符重载(续)
- KVO和KVC
- C++ 下载文件 HTTP
- VB.Net语法笔记
- 2015年第九周项目三:分数类中的运算符重载(续)(2)
- 15第九周项目五——方程也是类
- JSP标准标签库JSTL总结
- Linux - Shell编程基础
- 相关度度量之皮尔逊相关系数