题目描述
定义计数器类,包含保护数据成员value,公有函数increment计数加1。
定义循环计算器继承计数器类,增加私有数据成员:最小值minValue,maxValue,
重写公有函数increment,使得value在minValue~maxValue区间内循环+1。
定义时钟类,数据成员是私有循环计数器对象小时hour、分钟minute、秒second,公有函数time(int s)计算当前时间经过s秒之后的时间,即hour,minute,second的新value值。
定义时钟类对象,输入当前时间和经过的秒数,调用time函数计算新时间。
根据题目要求,增加必要的构造函数、析构函数和其他所需函数。
因为clock和time是系统内置函数,为了避免重名,请不要使用clock或者time作为类名或者函数名
输入
第一行测试次数n
2行一组,第一行为当前时间(小时 分钟 秒),第二行为经过的秒数。
输出
输出n行
每行对应每组当前时间和经过秒数后计算得到的新时间(小时:分钟:秒)。
输入样例1
2
8 19 20
20
23 30 0
1801
输出样例1
8:19:40
0:0:1
该题主要考察继承的应用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
using namespace std;
class count
{
protected:
int value;
public:
void increment() { value++; }
};
class cycle :public count
{
private:
int minv, maxv;
public:
void set(int v, int minv, int maxv);
int getv() { return value; }
int getmaxv() { return maxv; }
void increment() { this->value++; };
int getminv() { return minv; }
void setv(int v) { this->value = v; }
};
class Clock
{
private:
cycle hour, minute, second;
public:
Clock(int h, int m, int s);
void Time(int s);
};
void Clock::Time(int s)
{
for (int i = 0; i < s; i++)
{
second.increment();//注意在边界的情况
if (second.getv() == second.getmaxv())
{
second.setv(second.getminv());
minute.increment();
if (minute.getv() == minute.getmaxv())
{
minute.setv(minute.getminv());
hour.increment();
if (hour.getv() == hour.getmaxv())
{
hour.setv(hour.getminv());
}
}
}
}
cout << hour.getv() << ":" << minute.getv() << ":" << second.getv() << endl;
}
Clock::Clock(int h, int m, int s)
{
hour.set(h, 0, 24);
minute.set(m, 0, 60);
second.set(s, 0, 60);
}
void cycle::set(int v, int minv, int maxv)
{
this->maxv = maxv;
this->minv = minv;
this->value = v;
}
int main()
{
int n, t, h, m, s;
cin >> n;
while (n--)
{
cin >> h >> m >> s;
Clock ck(h, m, s);
cin >> t;
ck.Time(t);
}
return 0;
}