C. 时钟模拟(继承)

题目描述

定义计数器类,包含保护数据成员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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZZWWWFFF_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值