4-2 电子时钟中的运算符重载
2015-08-19 10:46
459 查看
4-2 电子时钟中的运算符重载
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
通过本题目的练习可以运算符重载的方法;设计一个时间类Time,私有数据成员有hour(时)、minute(分)、second(秒);
公有成员函数有:setHour(int)设置数据成员hour的值,非法的输入默认为12;setMinue(int)设置数据成员minute的值,非法输入默认为0;setSecond(int)设置数据成员second的值,非法输入默认为0;setTime(int,int,int)设置时、分、秒三个数据成员的值;三个成员函数int getHour(); int getMinute(); int getSecond();分别用于获取时间对象的属性值。
定义两个构造函数Time(); 和 Time(int,int,int);
定义一个成员函数void displayTime(); 用于显示时间,注意格式为 hh:mm:ss,位数不够用0填充;
定义一个时钟增加1的成员函数 void tick(); 把second的值加1,并注意是否到60;
定义一个成员或友元函数 bool operator= =(….); 判断两个时间对象的值是否相等;
定义一个成员或友元函数bool operator>(…..); 判断第一个时间对象的值是否大于第二个时间对象的值。
在主函数main()中指定开始时间和结束时间,并调用相应成员函数,显示从开始时间到结束时间之间所有时间对象的值,其格式见示例输出。
输入
输入6个整数,之间用一个空格间隔;分别表示开始时间的时、分、秒和结束时间的时、分、秒的值输出
从开始时间到结束时间之间所有时间对象的值;每个值占一行,格式为hh:mm:ss示例输入
01 01 01 01 01 10
示例输出
01:01:01 01:01:02 01:01:03 01:01:04 01:01:05 01:01:06 01:01:07 01:01:08 01:01:09 01:01:10
提示
输入11 10 12 10 12 56
输出
The begin time is not earlier than the end time!
来源
黄晶晶#include<bits/stdc++.h> using namespace std; class Time { private: int hour,minute,second; public: Time(int a,int b,int c) { hour=a; minute=b; second=c; } void showtime() { printf("%02d:%02d:%02d\n",hour,minute,second); } Time operator ++ () { if (++second>=60) { second-=60; if (++minute>=60) { minute-=60; if (++hour>=24) hour-=24; } } return *this; } bool operator > (const Time &right) { if (hour>right.hour) return true; else if (hour==right.hour&&minute>right.minute) return true; else if (hour==right.hour&&minute==right.minute&&second>right.second) return true; return false; } }; int main() { int h1,m1,s1,h2,m2,s2; cin>>h1>>m1>>s1>>h2>>m2>>s2; Time p(h1,m1,s1),q(h2,m2,s2); if (!(p>q)) { while (!(p>q)) { p.showtime(); ++p; } } else cout<<"The begin time is not earlier than the end time!"<<endl; return 0; }
相关文章推荐
- iOS UI——通过图层layer设置 图片的形变属性(transform)
- 使用python+flask让你自己api(教程源代码)
- 素材管理
- sql server时间戳timestamp
- Jquery点击表格单位时选中其中的Radio的三个方法
- 把你的Project发布到GitHub上
- 利用GCD 创建单例
- 类的内存偏移
- JS实现方向键切换输入框焦点的方法
- html消除button之间的间距
- c语言面试题 指针30个常错题型
- 一个测试的Android学习笔记开篇以及期望8.31
- Linux--基础
- 学习
- Android图表类库:WilliamChart
- RMI 连接超时时间设定
- 空a标签在IE下无效之解决方法
- Win10预览版10525更改窗口标题栏配色方法图文分享
- 9秒学院crossAPP详解
- 彻底删除SQL Server注册表的方法