01./* 02.* 程序的版权和版本声明部分: 03.* Copyright (c) 2011, 烟台大学计算机学院 04.* All rights reserved. 05.* 文件名称:test.cpp 06.* 作 者:刘芳 07.* 完成日期:2014 年04 月22 日 08.* 版 本 号:v1.0 09.* 对任务及求解方法的描述部分: 10.* 输入描述:无 11.* 问题描述: 12.* 程序输出: 13.* 问题分析:略 14.* 算法设计:略 15.*/
#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为: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秒前的时间 }; //下面实现所有的运算符重载代码。 //自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果 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(!operator<(t)) return true; else return false; } bool CTime::operator <=(CTime &t) { if(!operator>(t)) return true; else return false; } bool CTime::operator == (CTime &t) { if(hour==t.hour&&minute==t.minute&&second==t.second) return true; else return false; } bool CTime::operator != (CTime &t) { if(!operator == (t)) return true; else return false; } CTime::CTime(int h,int m,int s ) { hour=h; minute=m; second=s; } CTime CTime::operator+(CTime &t) { CTime b; int q,w,e; q=second+t.second; w=minute+t.minute; e=hour+t.hour; if(q<=59) b.second=q; if(q>59) { b.second=q%60; w=(q/60)+w; } if(w<=59) b.minute=w; if(w>59) { b.minute=w%60; e=(w/60)+e; }if(e<=23) b.hour=e; if(e>23) b.hour=e%24; return b; //if(b.second>=60&&b.minute<59 } CTime CTime::operator-(CTime &t) { CTime t4; int s=0,m=0,h=0; s=second-t.second; { if(s>=0) t4.second=s; else { s=(s+60); t4.second=s; m--; } } m=(minute-t.minute+m); { if(m>=0) t4.minute=m; else { m=(m+60); h--; t4.minute=m; } } h=hour-t.hour+h; { if(h>=0) t4.hour=h; else { h=h+24; t4.hour=h; } } return t4; } void CTime::display() { cout<<hour<<":"<<minute<<":"<<second<<endl; } CTime CTime::operator+(int s) { CTime b; int q,w,e; q=second+s; w=minute; e=hour; if(q<60) b.second=q; if(q>59) { b.second=(q%60); w=(q/60)+minute; } if(w<60) { b.minute=w; } if(w>59) { b.minute=(w%60); e=(w/60)+hour; } if(e<=23) b.hour=e; if(e>23) { b.hour=e%24; } return b; } CTime CTime::operator-(int s) { CTime b; int q,w,e; q=second-s; w=minute; e=hour; if(q>=0&&q<60) b.second=q; while(q<0) { q=q+60; w--; b.second=q; } if(w>=0&&w<60) { b.minute=w; } while(w<0) { w=w+60; e--; b.minute=w; } if(e>=0&&e<=23) b.hour=e; while(e<0) { b.hour=e+24; } return b; } CTime CTime::operator+=(CTime &c) { CTime t; //int h,m,s; second+=c.second; minute+=c.minute; hour+=c.hour; while(second>59) { second-=60; minute++; } while(minute>59) { minute-=60; hour++; } while(hour>23) { hour-=24; } return *this; } CTime CTime::operator-=(CTime &c) { CTime t; int h,m,s; s=second-c.second; m=minute-c.minute; h=hour-c.hour; if(s>=0) { t.second=s; } if(s<0) { t.second=s+60; m=m-1; } if(m>=0) { t.minute=m; } if(m<0) { t.minute=m+60; h=h-1; } if(h>=0) { t.hour=h; } if(h<0) { t.hour=h+24; } return t; } int main() { int a,b,c,d,m; CTime t1,t2,t3,t4,t5; cout<<"请输入t1的时,分,秒:"<<endl; cin>>a>>b>>c; t1.setTime(a,b,c); cout<<"请输入t2的时,分,秒:"<<endl; cin>>a>>b>>c; t2.setTime(a,b,c); 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<<"t1+t2="; t3=t1+t2; t3.display(); cout<<"t1-t2="; t3=t1-t2; t3.display(); cout<<"请输入增加的秒数:"<<endl; cin>>d; t4=t1+d; t4.display(); cout<<"请输入减少的秒数:"<<endl; cin>>m; t5=t1-m; t5.display(); t1+=t2; t1.display(); t1-=t2; t1.display(); return 0; }
8-2-Time类中的运算符重载
最新推荐文章于 2023-04-11 20:07:08 发布