难点:秒 化 时间

#include “bits/stdc++.h”
using namespace std;
int mons[13];
bool isrun(int a)//闰年
{
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0)) { return 1; }
else return 0;
}
int give(int y, int k)//给出相应年月对应的月份天数
{
if (isrun(y)) { mons[2] = 29; }//闰年29,平年28
else { mons[2] = 28; }
mons[1] = mons[3] = mons[5] = mons[7] = mons[8] = mons[10] = mons[12] = 31;
mons[2] = mons[4] = mons[6] = mons[9] = mons[11] = 30;
return mons[k];
}
//题目描述
//自定义一个日期时间类DateTime,它由基类Date和Time两者公有派生。
// 编写主函数,说明派生类对象,并对派生类的成员函数以及两个基类的公有成员函数进行使用。要求:
//基类Time:
//有void SetTime(int h, int m, int s)和void PrintTime()共两个成员函数,分别用来设置时间和输出时间
//有Hours,Minutes和Seconds共三个成员变量,分别用来存储时,分,秒。
//默认的时间是0 : 0 : 0
class Time {
protected:
int Hours;
int Minutes;
int Seconds;
public:
void SetTime(int h, int m, int s)
{
Hours=h;
Minutes=m;
Seconds=s;
}
void PrintTime()
{
cout<< Hours<<":"<< Minutes<<":"<<Seconds;
}
Time()
{
Hours = 0;
Minutes = 0;
Seconds = 0;
}
};
//基类Date:
//有void SetDate(int y, int m, int d)和void PrintDate()共两个成员函数,分别用来设置日期和输出日期;
//有Year,Month,Day共三个成员变量,分别用来存储年,月,日。
//默认的日期是1 / 1 / 1
class Date {
protected:
int Year;
int Month;
int Day;
public:
void SetDate(int y, int m, int d) {
Year = y;
Month = m;
Day = d;
}
void PrintDate() {
cout << Year << “/” << Month << “/” << Day;
}
Date()
{
Year = 1;
Month = 1;
Day = 1;
}
};
//派生类DateTime:
//由基类Date和Time两者公有派生,新增加2个成员函数,
// 第一个是void incrementSeconds(int s)函数,用于在当前日期和时间的基础上增加s秒,产生新的日期和时间;
// 第二个是void PrintDateTime()函数,用于同时输出当前日期和时间。
//
//注意:0 <= s < 2147483647,时间采用24小时制,且不用补零,年月日之间用“ / ”分隔,时分秒之间用“:”分隔。
class DateTime :public Date, public Time {
public:
void incrementSeconds(int s) {
/Hours;
Minutes;
Seconds;
Year
Month
Day
/
int year = 0, month = 0, day = 0, hour = 0, mini = 0, second = 0;
Seconds += s;
if(Seconds>60)
{
mini = Seconds / 60;
Seconds %= 60;
}
Minutes += mini;
if (Minutes > 60)
{
hour = Minutes / 60;
Minutes %= 60;
}
Hours += hour;
if (Hours > 24)
{
day = Hours / 24;
Hours %= 24;
}
Day += day;
while (Day > give(Year, Month))
{
Day -= give(Year, Month);
Month++;
if (Month > 12) { Month = 1; Year++; }
}
}
void PrintDateTime()
{
this->PrintDate();
cout << " ";
this->PrintTime();
cout << endl;
}
};
// 输入
// 第一行首先输入n,表示要测试n个函数,接下来每输入一个函数名,便执行一个函数,
// 若输入的函数名有误,则输出“没有这个函数!”。
int main()
{

int n;
cin >> n;
DateTime dt;
string s;
while (n--)
{
	cin >> s;
	if (s == "PrintDateTime")
	{
		dt.PrintDateTime();
	}
	else if (s == "incrementSeconds")
	{
		int x;
		cin >> x;
		dt.incrementSeconds(x);
	}
	else if (s == "SetTime")
	{
		int h, m, w;
		cin >> h >> m >> w;
		dt.SetTime(h, m, w);
	}
	else if (s == "PrintTime")
	{
		dt.PrintTime();
		cout << endl;
	}
	else if (s == "SetDate")
	{
		int y, c, d;
		cin>> y>> c>>d;
		dt.SetDate(y, c, d);
	}
	else if (s == "PrintDate")
	{
		dt.PrintDate();
		cout << endl;
	}
	else
	{
		cout << "没有这个函数!" << endl;
	}
}
return 0;

}
// 输出
// 对应函数执行的结果。
//
// 样例输入
//
// 5
// PrintDateTime
// incrementSeconds
// 10
// PrintDateTime
// SetTime
// 2 2 2
// PrintDateTime
//
// 样例输出
//
// 1 / 1 / 1 0:0 : 0
// 1 / 1 / 1 0 : 0 : 10
// 1 / 1 / 1 2 : 2 : 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值