题目:时间流逝之增加一秒钟。模拟时间的流逝,例如:当前是2020年12月31日23:59:59,时间流逝(即增加一秒钟)后变成2021年1月1日00:00:00。
要求实现(1)Date类和Time类的构造函数;(2)Time类的成员函数void add_a_second(Date &);(3)<<运算符重载函数ostream& operator<<(ostream&, Time&)和ostream& operator<<(ostream&, Date&)(必须按照规定格式输出时间)。
注意:不得修改已有代码;不得再增加其他函数。
提示:在实现add_a_second函数时要注意:增加一秒,可能导致增加一分钟(而秒数需要减少60);而增加一分钟,可能导致增加一小时(而分钟数需要减少60);以此类推,天、月、年都要进行类似地判断和计算。由于不同月份的天数不同,且存在闰年的问题,所以计算月份的时候要考虑当前年份和月份(即以当前年份和月份为参数调用days函数计算该月的天数)。
参考代码已给出,在每个函数以及填写的参考代码旁边注释出代码的功能解释
不明白你的想法,你说是代码提示吗?如果使用VS开发C#程序,可以在函数(方法,属性等)前面的位置,输入三斜杠“///”就可以编辑提示性注释。当你调用这个函数时,编辑器会把函数的功能提示,参数提示给你显示出来,方便调用者开发
#include <iostream>
#include <iomanip>
class Time {
private:
int hour;
int minute;
int second;
public:
Time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
void Add_a_second(Date& date) {
second++;
if (second >= 60) {
second = 0;
minute++;
if (minute >= 60) {
minute = 0;
hour++;
if (hour >= 24) {
hour = 0;
date.AddDays(1);
}
}
}
}
void Print() const {
std::cout << std::setfill('0') << std::setw(2) << hour << ":"
<< std::setfill('0') << std::setw(2) << minute << ":"
<< std::setfill('0') << std::setw(2) << second << std::endl;
}
};
class Date {
private:
int year;
int month;
int day;
public:
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
void AddDays(int days) {
day += days;
while (true) {
int max_day = GetMaxDay();
if (day > max_day) {
day -= max_day;
month++;
if (month > 12) {
month = 1;
year++;
}
} else {
break;
}
}
}
int GetMaxDay() const {
static const int days[] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
if (month == 2 && IsLeapYear()) {
return 29;
}
return days[month];
}
bool IsLeapYear() const {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
void Print() const {
std::cout << std::setfill('0') << std::setw(4) << year << "-"
<< std::setfill('0') << std::setw(2) << month << "-"
<< std::setfill('0') << std::setw(2) << day << std::endl;
}
friend class Time;
};
int main() {
Date date(2023, 4, 11);
Time time(12, 30, 45);
std::cout << "Original date and time:" << std::endl;
date.Print();
time.Print();
time.Add_a_second(date);
std::cout << "Date and time after adding a second:" << std::endl;
date.Print();
time.Print();
return 0;
}
思路:
示例代码如下:
#include <iostream>
using namespace std;
class Date {
private:
int year, month, day;
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
void addOneDay() {
day++;
if (day > getDaysOfMonth()) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
int getDaysOfMonth() {
int days = 31;
if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
}
return days;
}
void print() {
cout << year << "年" << month << "月" << day << "日" << endl;
}
};
class Time {
private:
int hour, minute, second;
public:
Time(int h, int m, int s) : hour(h), minute(m), second(s) {}
void addOneSecond() {
second++;
if (second >= 60) {
second = 0;
minute++;
if (minute >= 60) {
minute = 0;
hour++;
if (hour >= 24) {
hour = 0;
}
}
}
}
void print() {
cout << hour << "时" << minute << "分" << second << "秒" << endl;
}
};
int main() {
Date date(2020, 12, 31);
Time time(23, 59, 59);
time.addOneSecond();
if (time.second == 0) {
date.addOneDay();
}
date.print();
time.print();
return 0;
}
注释已经写在代码旁边了,可以看到每个类的属性和方法都有注释来解释其功能。