用c++写一个程序输入一个日期后面输入增加的天数,最后输出增加后的日期
时间: 2023-05-25 08:04:48 浏览: 96
```c
#include <stdio.h>
int main()
{
int year, month, day;
int add_days, days_in_month;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入日期:");
scanf("%d", &day);
printf("请输入增加的天数:");
scanf("%d", &add_days);
// 计算当前日期是当年的第几天
int days = 0;
for (int i = 1; i < month; i++) {
switch (i) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days += 31; break;
case 4: case 6: case 9: case 11:
days += 30; break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
days += 29; // 闰年2月29天
else
days += 28; // 平年2月28天
break;
}
}
days += day;
// 增加天数
days += add_days;
// 计算增加后的日期
day = days % 100; days /= 100;
month = days % 100; days /= 100;
year += days;
// 处理月份和天数
if (month > 12) {
year += month / 12;
month %= 12;
}
days_in_month = 31; // 假定是31天
switch (month) {
case 4: case 6: case 9: case 11:
days_in_month = 30; break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
days_in_month = 29;
else
days_in_month = 28;
break;
}
if (day > days_in_month) {
month++;
day -= days_in_month;
if (month > 12) {
year += month / 12;
month %= 12;
}
}
// 输出增加后的日期
printf("增加后的日期为:%04d年%02d月%02d日\n", year, month, day);
return 0;
}
```
阅读全文