4-2 电子时钟中的运算符重载
来源:互联网 发布:阿里云客服在哪申请 编辑:程序博客网 时间:2024/06/16 12:42
Problem Description
通过本题目的练习可以运算符重载的方法;
设计一个时间类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()中指定开始时间和结束时间,并调用相应成员函数,显示从开始时间到结束时间之间所有时间对象的值,其格式见示例输出。
Input
输入6个整数,之间用一个空格间隔;分别表示开始时间的时、分、秒和结束时间的时、分、秒的值
Output
从开始时间到结束时间之间所有时间对象的值;每个值占一行,格式为hh:mm:ss
Example Input
01 01 01 01 01 10
Example Output
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
Hint
输入
11 10 12 10 12 56
输出
The begin time is not earlier than the end time!
有很多函数不是必须的,但是题目要求写了。。。
#include <iostream>#include <stdio.h>using namespace std;class Time{private: int hour, minute, second;public: Time() { hour = 0; minute = 0; second = 0; } Time(int a, int b, int c) { hour = a; minute = b; second = c; } void sethour(int a) { if(a <= 0 || a > 12)//<=0都为12 hour = 12; else hour = a; } void setminute(int a) { if(a < 0 || a >= 60)//>=60都为0 minute = 0; else minute = a; } void setsecond(int a) { if(a < 0 || a >= 60) second = 0; else second = a; } void settime(int a, int b, int c) { hour = a; minute = b; second = c; } int gethour() { return hour; } int getminute() { return minute; } int getsecond() { return second; } void display() { printf("%02d:%02d:%02d\n", gethour(), getminute(), getsecond());//不足两位补0 } void tick() { second ++; if(second >= 60) { second = 0; minute++; if(minute >= 60) { minute = 0; hour++; } } } bool operator==(Time &t) { if(hour == t.hour && minute == t.minute && second == t.second) return true; else return false; } bool operator >(Time &t) { if(hour > t.hour) return true; else { if(minute > t.minute) return true; else { if(second > t.second) return true; else return false; } } }};int main(){ Time t1, t2; int a, b, c, d, e, f; cin>>a>>b>>c>>d>>e>>f; t1.settime(a, b, c); t2.settime(d, e, f); if(t1 > t2) cout<<"The begin time is not earlier than the end time!"<<endl; else { t1.display(); while(t2 > t1) { t1.tick(); t1.display(); } } return 0;}
- 4-2电子时钟中的运算符重载
- [2711]4-2 电子时钟中的运算符重载
- SDUTOJ 2711 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- SDUT-2711 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 2711 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- Java IO 经典教程 (下) (翻译自jenkov.com)
- 分布式资源管理器YARN简介(一)
- 什么情况下Java对象才是已经死亡?
- NYOJ92~图像有效区域
- Spring Web MVC详解
- 4-2 电子时钟中的运算符重载
- burpsuite拦截不了包(firxfox的不使用代理设置)
- 2017.12.17Day11
- Java集合之二Collection接口
- tensorflow 第一行代码
- 刷新和加载..
- 运行 MapReduce 样例
- 系统目录结构、文件类型及命令ls\alias的用法2.1-2.5
- IntelliJ IDEA 2017.3-2017.3.1 汉化包