在c++中,用键盘输入年月日三个整数,输出第二天的日期,需考虑闰年,并且采用if语句和iostearm头文件
时间: 2023-04-04 21:04:48 浏览: 121
您好,根据您的问题,我可以回答。以下是代码实现:
```
#include <iostream>
using namespace std;
int main() {
int year, month, day;
cin >> year >> month >> day;
if (month < 1 || month > 12 || day < 1 || day > 31) {
cout << "输入的日期不合法" << endl;
return 0;
}
bool isLeapYear = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
isLeapYear = true;
}
int daysOfMonth[13] = {0, 31, 28 + isLeapYear, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (day >= daysOfMonth[month]) {
cout << "输入的日期不合法" << endl;
return 0;
}
day++;
if (day > daysOfMonth[month]) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
cout << year << "年" << month << "月" << day << "日" << endl;
return 0;
}
```
希望能够帮到您!
阅读全文