题目描述
设计一个程序能计算一个日期加上若干天后是什么日期。
输入描述
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。
输出描述
输出m行,每行按yyyy-mm-dd的个数输出。
输入
1 2008 2 3 100
输出
2008-05-13
#include <iostream>
using namespace std;
//闰年判断
bool isLeapYear(int year) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
return true;
}
return false;
}
void nextDay(int &year, int &month, int &day) {
if (day >= 1 && day <= 27) {
day++;
} else if (day == 28) {
if (month != 2) {
day++;
} else {
if (!isLeapYear(year)) {
month++;
day = 1;
} else {
day++;
}
}
} else if(day == 29) {
if (month == 2) {
month = 3;
day = 1;
} else {
day++;
}
} else if (day == 30) {
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day++;
break;
default:
month++;
day = 1;
break;
}
} else {
if (month == 12) {
year++;
month = 1;
day = 1;
} else {
month++;
day = 1;
}
}
}
void addDay(int year, int month, int day, int add) {
int i;
for (i = 0; i < add; i++) {
nextDay(year, month, day);
}
cout << year << "-";
if (month < 10) {
cout << "0" << month << "-";
} else {
cout << month << "-";
}
if (day < 10) {
cout << "0" << day << endl;
} else {
cout << day << endl;
}
}
int main() {
int n;
int year, month, day, add;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> year >> month >> day >> add;
addDay(year, month, day, add);
}
return 0;
}