Time类中的运算符重载
2014-04-19 08:23
302 查看
/* * 程序的版权和版本声明部分: * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:任子仪 * 完成日期:2014年 4月 19日 * 版 本 号:v12.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为:41:15 CTime operator+(CTime &t); CTime operator-(CTime &t);//对照+理解 CTime operator+(int s);//返回s秒后的时间 CTime operator-(int s);//返回s秒前的时间 }; //下面实现所有的运算符重载代码。 //自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果 //下面实现所有的运算符重载代码。 //比较运算返回的是比较结果,是bool型的true或false 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; } CTime CTime::operator+(CTime &t) { CTime t1(0,0,0); t1.second=t.second+second; while(t1.second>=60) { t1.minute++; t1.second-=60; } t1.minute=minute+t.minute; while(t1.minute>=60) { t1.hour++; t1.minute-=60; } t1.hour=hour+t.hour; while(t1.hour>=24) { t1.hour-=24; } return t1; } CTime CTime::operator-(CTime &t) { CTime t1(0,0,0); t1.second=second-t.second; while(t1.second<0) { t1.minute--; t1.second+=60; } t1.minute=minute-t.minute; while(t1.minute<0) { t1.hour--; t1.minute+=60; } t1.hour=hour-t.hour; while(t1.hour<0) { t1.hour+=24; } return t1; } CTime CTime::operator+(int s) { CTime t1=*this; t1.second=second+s; while(t1.second>=60) { t1.minute++; t1.second-=60; } while(t1.minute>=60) { t1.hour++; t1.minute-=60; } while(t1.hour>=24) { t1.hour-=24; } return t1; } CTime CTime::operator-(int s) { CTime t1=*this; t1.second=second-s; while(t1.second<0) { t1.minute--; t1.second+=60; } while(t1.minute<0) { t1.hour--; t1.minute+=60; } while(t1.hour<0) { t1.hour+=24; } return t1; } // 判断时间t1<=t2 bool CTime::operator > (CTime &t) { if (hour>t.hour||(hour==t.hour&&minute>t.minute)||(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(hour<t.hour||(hour==t.hour&&minute<t.minute)||(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 (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; } //自行编制用于测试的main()函数,有些结果不必依赖display()函数,提倡用单步执行查看结果 int main() { CTime t1(9,8,7),t2(8,7,6),t; int s=1; 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; t=t1+t2; t.display(); t=t1-t2; t.display(); t=t1+s; t.display(); t=t1-s; t.display(); return 0; }
示例图片:
相关文章推荐
- 第12周项目2——Time类中的运算符重载
- 第12周项目2-Time类中的运算符重载
- 第十二周【项目2-Time类中的运算符重载】
- 第八周【项目2-Time类中的运算符重载】实现Time类中的运算符重载
- 第九周-项目2-Time类中的运算符重载
- 第九周项目2(1)-Time类中的运算符重载(续)
- 2-8-2 Time类中的运算符重载
- 第八周项目-2-time类中的运算符重载
- 2013第八周上机任务【项目2-Time类中的运算符重载】
- 项目2-Time类中的运算符重载
- 八周 项目2 Time类中的运算符重载
- 第九周(项目二)——Time类中的运算符重载。
- 第八周——运算符重载——项目二 Time类
- 第九周项目2(2)-Time类中的运算符重载(续)
- 第八周项目2-Time类中的运算符重载
- 第12周项目2-Time类中的运算符重载
- Time类中的运算符重载
- Time类的运算符重载
- 第8周项目2-Time类中的运算符重载
- 第8周项目2-Time类中的运算符重载(续)