第8周-项目2-Time类中的运算符重载(续)-++、--、>>、<<
来源:互联网 发布:宁波奥派网络 编辑:程序博客网 时间:2024/06/10 01:04
问题及代码:
运行结果:
/* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:CTime.cpp *作 者:单昕昕 *完成日期:2015年5月5日 *版 本 号:v1.0 *问题描述:(1)定义对时间对象的自增和自减一目运算符一目运算符的重载 CTime operator++(int);//后置++,下一秒 CTime operator++();//前置++,下一秒,前置与后置返回值不一样 CTime operator--( int);//后置--,前一秒 CTime operator--();//前置--,前一秒 (2)定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。*程序输入:两个时间。*程序输出:时间运算结果。*/ //Sample Input//10:41:30 6:42:23#include<iostream>#include<cmath>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); 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--();//前置--,前一秒 friend istream& operator>>(istream&input,CTime &c); friend ostream& operator<<(ostream&output,const CTime &c);};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) 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 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; return false;}bool CTime::operator >= (CTime &t){ return !(*this < t);}bool CTime::operator <= (CTime &t){ return !(*this > t);}bool CTime::operator == (CTime &t){ return !((*this > t)||(*this < t));}bool CTime::operator != (CTime &t){ return ((*this > t)||(*this < t));}//二目的加减运算符的重载//返回t规定的时、分、秒后的时间//例t1(8,20,25),t2(11,20,50),t1+t2为19:41:15CTime CTime::operator+(CTime &t){ CTime time; time.hour=hour+t.hour; time.minute=minute+t.minute; time.second=second+t.second; int count=0; int i=time.second; if(time.second>=60) { while(i>=60) { i%=60; count=(time.second-i)/60; } time.minute+=count; time.second=i; } int j=time.minute; count=0; if (time.minute>=60) { while(j>=60) { j%=60; count=(time.minute-j)/60; } time.hour+=count; time.minute=j; } int k=time.hour; count=0; if (time.hour>=24) { while(k>=24) { k%=24; count=(time.hour-k)/24; } time.hour=k; } return time;}CTime CTime::operator-(CTime &t)//对照+理解{ CTime time; if(hour>t.hour) { time.hour=hour-t.hour; time.minute=minute-t.minute; time.second=second-t.second; if(time.second<0) { time.minute--; time.second+=60; } if(time.minute<0) { time.hour--; time.minute+=60; } } else { time.hour=t.hour-hour; time.minute=t.minute-minute; time.second=t.second-second; if(time.second<0) { time.minute--; time.second+=60; } if(time.minute<0) { time.hour--; time.minute+=60; } } return time;}CTime CTime::operator+(int s)//返回s秒后的时间{ CTime time; second+=s; int count=0; int i=second; if(second>=60) { while(i>=60) { i%=60; count=(second-i)/60; } minute+=count; second=i; } int j=minute; count=0; if (minute>=60) { while(j>=60) { j%=60; count=(minute-j)/60; } hour+=count; minute=j; } int k=hour; count=0; if (hour>=24) { while(k>=24) { k%=24; count=(hour-k)/24; } hour=k; } time.hour=hour; time.minute=minute; time.second=second; return time;}CTime CTime::operator-(int s)//返回s秒前的时间{ CTime time; second-=s; int count=0; int i=fabs(second); if(second>=60) { while(i>=60) { i%=60; count=(second-i)/60; } minute-=count; second=i; } int j=minute; count=0; if (minute>=60) { while(j>=60) { j%=60; count=(minute-j)/60; } hour-=count; minute=j; } int k=hour; count=0; if (hour>=24) { while(k>=24) { k%=24; count=(hour-k)/24; } hour=k; } time.hour=hour; time.minute=minute; time.second=second; return time;}//二目赋值运算符的重载CTime CTime::operator+=(CTime &c){ this->hour=this->hour+c.hour; this->minute=this->minute+c.minute; this->second=this->second+c.second; int count=0; int i=this->second; if(this->second>=60) { while(i>=60) { i%=60; count=(this->second-i)/60; } this->minute+=count; this->second=i; } int j=this->minute; count=0; if (this->minute>=60) { while(j>=60) { j%=60; count=(this->minute-j)/60; } this->hour+=count; this->minute=j; } int k=this->hour; count=0; if (this->hour>=24) { while(k>=24) { k%=24; count=(this->hour-k)/24; } this->hour=k; } return *this;}CTime CTime::operator-=(CTime &c){ if(this->hour>c.hour) { this->hour=this->hour-c.hour; this->minute=this->minute-c.minute; this->second=this->second-c.second; if(this->second<0) { this->minute--; this->second+=60; } if(this->minute<0) { this->hour--; this->minute+=60; } } else { this->hour=c.hour-this->hour; this->minute=this->minute-c.minute; this->second=this->second-c.second; if(this->second<0) { this->minute--; this->second+=60; } if(this->minute<0) { this->hour--; this->minute+=60; } } return *this;}CTime CTime::operator+=(int s)//返回s秒后的时间{ this->second+=s; int count=0; int i=this->second; if(this->second>=60) { while(i>=60) { i%=60; count=(this->second-i)/60; } this->minute+=count; this->second=i; } int j=this->minute; count=0; if (this->minute>=60) { while(j>=60) { j%=60; count=(this->minute-j)/60; } this->hour+=count; this->minute=j; } int k=this->hour; count=0; if (this->hour>=24) { while(k>=24) { k%=24; count=(this->hour-k)/24; } this->hour=k; } this->hour=hour; this->minute=minute; this->second=second; return *this;}CTime CTime::operator-=(int s)//返回s秒前的时间{ this->second-=s; int count=0; int i=fabs(this->second); if(this->second>=60) { while(i>=60) { i%=60; count=(this->second-i)/60; } this->minute-=count; this->second=i; } int j=this->minute; count=0; if (this->minute>=60) { while(j>=60) { j%=60; count=(this->minute-j)/60; } this->hour-=count; this->minute=j; } int k=this->hour; count=0; if (this->hour>=24) { while(k>=24) { k%=24; count=(this->hour-k)/24; } this->hour=k; } this->hour=hour; this->minute=minute; this->second=second; return *this;}CTime CTime::operator++(int)//后置++,下一秒{ CTime temp(*this); second++; if(second>=60) { second-=60; ++minute; } return temp;}CTime CTime::operator++()//前置++,下一秒,前置与后置返回值不一样{ if(++second>=60) { second-=60; ++minute; } return *this;}CTime CTime::operator--( int)//后置--,前一秒{ CTime temp(*this); second--; if(second<0) { second+=60; --minute; } return temp;}CTime CTime::operator--()//前置--,前一秒{ if(--second<0) { second+=60; --minute; } return *this;}//流插入istream& operator>>(istream&input,CTime &t){ int h,m,s; char sign; do { cout<<"please input a time(ep->h:m:s):"; input>>h>>sign>>m>>sign>>s; } while(!(sign==':')); t.hour=h; t.minute=m; t.second=s; return input;}//流提取ostream& operator<<(ostream&output,const CTime &t){ output<<t.hour<<":"<<t.minute<<":"<<t.second<<endl; return output;}//下面定义用于测试的main()函数int main(){ CTime t1(10,41,30),t2(6,42,23),t; cout<<"t1为:"<<t1; cout<<"t2为:"<<t2; 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; cout<<"下面对两个时间进行计算:"<<endl; t1++; cout<<"t1++:"<<t1; ++t2; cout<<"++t2:"<<t2; t1--; cout<<"t1--:"<<t1; --t2; cout<<"--t2:"<<t2; t=t1+t2; cout<<"t1+t2:"<<t; t=t1-t2; cout<<"t1-t2:"<<t; t=t1+333; cout<<"t1+333s:"<<t; t=t2-2222; cout<<"t2-2222s:"<<t; t1+=t2; cout<<"t1+=t2:"<<t1; t1-=t2; cout<<"t1-=t2:"<<t1; t1+=3333; cout<<"t1+=3333s:"<<t1; t2-=222; cout<<"t2-=222s:"<<t2;}
运行结果:
知识点总结:
重载单目运算符。
学习心得:
一开始看我自己的程序很奇怪为什么t1--木有变化,找了好久都找不出来bug,后来才发现我先t1++了,所以再--后又变成原来的数值。
0 0
- 第8周-项目2-Time类中的运算符重载(续)-++、--、>>、<<
- 第8周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类中的运算符重载(续)
- 第9周项目2-Time类中的运算符重载续(1)
- 第9周项目2-Time类中的运算符重载(续)
- 第8周项目2TIME类运算符重载
- 第9周项目2 Time类中的运算符重载(续)
- C++第8周项目2 -Time类中的运算符重载
- 第8周项目2:Time类中的运算符重载
- C++第8周(春)项目2 实现Time类中的运算符重载
- C++第8周(春)项目2 实现Time类中的运算符重载 .
- 第8周项目2Time类中的运算符重载
- 第8周项目2-Time类中的运算符重载
- 第8周项目2-Time类中的运算符重载
- 第8周项目2-Time类中的运算符重载
- spring boot系列一 demo测试
- 如何在Android的xml中添加带<<>>的字符串
- 【让我们再聊聊浏览器资源加载优化】
- iOS —— 检查版本更新
- com.sunspoter.lib.web.struts2.dispatcher.StreamResultX
- 第8周-项目2-Time类中的运算符重载(续)-++、--、>>、<<
- 如何在 C++ 程序中计算时间 .
- java虚拟机 垃圾收集 火车算法
- RAID 0+1硬盘阵列组建图解及RAID 0+1和1+0的区别
- 简单的MySQL连接池,支持高并发。
- spring @Autowired注解
- 剑指Offer之 - 链表中倒数第k个结点
- java转cocos2d-x:Helloworld
- ARC文件中加入-fno-objc-arc后程序依旧报错的原因