第九周项目二Time类中的运算符重载(续)(1)
2015-05-09 15:56
344 查看
问题及代码:
运行结果:
![](http://img.blog.csdn.net/20150509155704866?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFvank4MjY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
知识点总结:
前置++和后置++的区别,还有前置--和后置--的区别
学习心得:
前置++在输出时结果加1,后置++在输出结果时,不加1,前置--输出时-1,后置--输出时不-1.
/* * Copyright (c) 2014, 烟台大学计算机与控制工程学院 * All rights reserved. * 文件名称:test.cpp * 作 者:郝俊宇 * 完成日期:2015年 5 月9 日 * 版 本 号:v1.0 * 问题描述:在第八周项目二的基础上(1)定义对时间对象的自增和自减一目运算符 * 输入描述:无 * 程序输出:对应的结果 */ #include <iostream> 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--();//前置--,前一秒 }; 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)//判断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 CTime::operator<(CTime &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 CTime::operator>=(CTime &t)//判断t1>=t2 { if(*this<t) return false; return true; } bool CTime::operator<=(CTime &t)//判断t1<=t2 { if(*this>t) return false; return true; } bool CTime::operator==(CTime &t)//判断t1=t2 { if((*this>t)||(*this<t)) return false; return true; } bool CTime::operator!=(CTime &t)//判断t1!=t2 { if((*this>t)||(*this<t)) return true; return false; } CTime CTime::operator+(CTime &t)//计算t1+t2 { CTime c; c.hour=hour+t.hour; c.minute=minute+t.minute; c.second=second+t.second; if(c.second>=60) { c.minute++; c.second=c.second-60; } if(c.minute>=60) { c.hour++; c.minute=c.minute-60; } if(c.hour>=24) { c.hour=c.hour-24; } return c; } CTime CTime::operator-(CTime &t)//计算t1-t2 { 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 c(h,m,s); return c; } CTime CTime::operator+(int s)//增加s秒 { CTime c; c.hour=hour; c.minute=minute; c.second=second+s; if(c.second>=60) { c.minute=c.minute+c.second/60; c.second=c.second%60; } if(c.minute>=60) { c.hour=c.hour+c.minute/60; c.minute=c.minute%60; } if(c.hour>=24) { c.hour=c.hour-24; } return c; } CTime CTime::operator-(int s)//减去s秒 { int ss,mm,hh; ss=s%60; mm=(s/60)%60; hh=s/3600; CTime c(hh,mm,ss); return *this-c; } CTime CTime::operator+=(CTime &c) { *this=*this+c; return *this; } CTime CTime::operator-=(CTime &c) { *this=*this-c; return *this; } CTime CTime::operator+=(int s) { *this=*this+s; return *this; } CTime CTime::operator-=(int s) { *this=*this-s; return *this; } CTime CTime::operator++(int) { CTime t=*this; *this=*this+1; return t; } CTime CTime::operator++() { *this=*this+1; return *this; } CTime CTime::operator--(int) { CTime t=*this; *this=*this-1; return t; } CTime CTime::operator--() { *this=*this-1; return *this; } void CTime::display() { cout<<hour<<":"<<minute<<":"<<second<<endl; } 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; cout<<"t1++="; t=t1++; t.display(); cout<<"t2--="; t=t2--; t.display(); cout<<"++t1="; t=++t1; t.display(); cout<<"--t2="; t=--t2; t.display(); cout<<"t1+t2="; t=t1+t2; t.display(); cout<<"t1-t2="; t=t1-t2; t.display(); cout<<"t1+2000="; t=t1+2000; t.display(); cout<<"t1-5000="; t=t1-5000; t.display(); cout<<"t1+=t2,t1="; t1+=t2; t1.display(); cout<<"t1-=t2,t1="; t1-=t2; t1.display(); cout<<"t1+=2000,t1="; t1+=2000; t1.display(); cout<<"t1-=5000,t1="; t1-=5000; t1.display(); return 0; }
运行结果:
知识点总结:
前置++和后置++的区别,还有前置--和后置--的区别
学习心得:
前置++在输出时结果加1,后置++在输出结果时,不加1,前置--输出时-1,后置--输出时不-1.
相关文章推荐
- 第九周项目二Time类中的运算符重载(续)(2)
- 第九周项目2-Time类中的运算符重载(续)
- 第九周(项目三)——分数类中的运算符重载。
- 第九周项目二-Time类中的运算符重载(续)
- 第九周项目3-分数类中的运算符重载(续)
- 第九周项目2(2)-Time类中的运算符重载(续)
- 第九周 课后实践:项目二——Time类中的运算符重载(续)
- 第九周(项目一)——实现复数类中的运算符重载。
- 第九周【项目1-复数类中的运算符重载】
- 第九周项目2-Time类中的运算符重载(续)
- 第九周项目三 分数类中的运算符重载(续)
- 第九周项目3-分数类中的运算符重载(续)
- 第九周项目3:分数类中的运算符重载(续)
- 第九周 项目二 Time类中的运算符重载(续)
- 第九周项目三:分数类中的运算符重载(续)
- 第九周项目一--复数类中的运算符重载(续)
- 第九周项目三--分数类中的运算符重载(续)
- 第九周项目一 复数类中的运算符重载(续)
- 第九周 【项目1-复数类中的运算符重载(续)】
- 第九周项目三——分数类中的运算符重载(续)