第8周项目2TIME类运算符重载
来源:互联网 发布:淘宝新店一天自然流量 编辑:程序博客网 时间:2024/06/05 14:45
/* * 程序的版权和版本声明部分: * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:李果 * 完成日期:2013 年 5月 8日 * 版 本 号:v1.0 * 对任务及求解方法的描述部分: * 输入描述: * 问题描述: * 程序输出:* 问题分析:略 * 算法设计:略 */ #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);//二目运算符的重载CTime operator+(CTime &c);//例t1(8,20,25),t2(11,20,50),t1+t2为:41:15CTime operator-(CTime &c);CTime operator+(int s);//返回s秒后的时间CTime operator-(int s);//返回s秒前的时间//一目运算符的重载CTime operator++( int);CTime operator++();CTime operator--( int);CTime operator--();//赋值运算符的重载 CTime operator+=(CTime &c);CTime operator-=(CTime &c);CTime operator+=(int s); CTime operator-=(int s); };///////////////////功能函数///////////////////////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; } void CTime::display() { cout<<hour<<':'<<minute<<':'<<second<<endl; } ///////////////////比较运算符(二目)的重载///////////////bool CTime::operator > (CTime &t) //此处函数头部生疏{if(hour>t.hour)return true;else if(hour<t.hour)return false;else if(minute>t.minute)return true;else if(minute<t.minute)return false;else if(second>t.second)return true;else return false;}bool CTime::operator < (CTime &t) {if(hour<t.hour)return true;else if(hour>t.hour)return false;else if(minute<t.minute)return true;else if(minute>t.minute)return false;else if(second<t.second)return true;else return false;}bool CTime::operator ==(CTime &t) {if(hour==t.hour&&minute==t.minute&&second==t.second)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 true;else return false;}bool CTime::operator <= (CTime &t){if(*this<t||*this==t)return true;else return false;}//////////////////////////////////////////////////*CTime CTime::operator + (CTime &t) //不知道如何返回{second=second+t.second;minute=minute+t.minute;hour=hour+t.hour;if(second>60){second=second-60;minute++;}if(minute>60){minute=minute-60;hour++;}if(hour>24)hour=hour-24;}*//////////////////////二目运算符的重载///////////////////CTime CTime::operator + (CTime &t) {int h,m,s;s=second+t.second;m=minute+t.minute;h=hour+t.hour;if(s>60){s=s-60;m++;}if(m>60){m=m-60;h++;}if(h>24)h=h-24;CTime tt(h,m,s);//以下两句感觉很好。return tt;}CTime CTime::operator - (CTime &t) {int h,m,s;s=second-t.second;m=minute-t.minute;h=hour-t.hour;if(s<0){s=s+60;m--;}if(m<0){m=m+60;h--;}if(h<0)//此处绕了h=h+24;CTime tt(h,m,s);return tt;}/*CTime CTime::operator + (int s) //这个函数写错了{int m,h;s=second+s;while(s>60){s=s-60;m++;}m=m+minute;while(m>60){m=m-60;h++;}h=hour+h;if(h>24)h=h-24;CTime tt(h,m,s);return tt;}*/CTime CTime::operator+(int s) { int ss=s%60; int mm=(s/60)%60; int hh=s/3600; CTime t0(hh,mm,ss); return *this+t0; } CTime CTime::operator -(int s)//抄的,自己写的不好{int ss=s%60; int mm=(s/60)%60; int hh=s/3600; CTime t0(hh,mm,ss); return *this-t0; }//////////////////一目运算符的重载/////////////////////////混乱了,还是参考老师的吧。/*CTime CTime::operator++( ){CTime t0(0,0,1);return *this+t0;}*/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; } //////////////////////赋值运算符的重载////////////////////CTime CTime::operator+=(CTime &c){//return *this+c;//很不理解为什么这样写不行???????*this=*this+c; return *this;}CTime CTime::operator-=(CTime &c){*this=*this-c; return *this;}CTime CTime::operator+=(int s){int ss=s%60; int mm=(s/60)%60; int hh=s/3600; CTime t0(hh,mm,ss); *this=*this+t0; return *this; }CTime CTime::operator-=(int s){int ss=s%60; int mm=(s/60)%60; int hh=s/3600; CTime t0(hh,mm,ss); *this=*this-t0; return *this; }////////////////////////////////////////////////////////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<<"下面对时间进行运算:\n";t=t1+t2;cout<<"t1+t2:";t.display();t=t2-t1;cout<<"t2-t1:";t.display();t=t-3600;cout<<"t-3600:";t.display();t--;cout<<"t--:";t.display();++t;cout<<"++t:";t.display();t+=t1;cout<<"t+=t1:";t.display();t+=3600;cout<<"t+=3600:";t.display();return 0;}