给我编写C语言一个代码实现nextDate函数
时间: 2024-02-01 20:14:45 浏览: 77
C编写NextDate函数
下面是一个简单的C语言代码实现nextDate函数:
```c
#include <stdio.h>
void nextDate(int day, int month, int year)
{
int next_day, next_month, next_year;
// 计算下一天
if (day < 28) {
next_day = day + 1;
next_month = month;
next_year = year;
} else if (day == 28) {
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
next_day = 29;
} else {
next_day = 1;
next_month = 3;
}
} else {
next_day = 1;
next_month = month + 1;
}
next_year = year;
} else if (day == 29) {
next_day = 1;
next_month = 3;
next_year = year;
} else if (day == 30) {
if (month == 4 || month == 6 || month == 9 || month == 11) {
next_day = 1;
next_month = month + 1;
} else {
next_day = 31;
next_month = month;
}
next_year = year;
} else {
if (month == 12) {
next_day = 1;
next_month = 1;
next_year = year + 1;
} else {
next_day = 1;
next_month = month + 1;
next_year = year;
}
}
// 输出结果
printf("The next date is: %d-%02d-%02d\n", next_year, next_month, next_day);
}
int main() {
int day, month, year;
printf("Please enter a date (format: yyyy-mm-dd): ");
scanf("%d-%d-%d", &year, &month, &day);
nextDate(day, month, year);
return 0;
}
```
以上代码会要求用户输入一个日期,然后计算出这个日期的下一天,并输出结果。
阅读全文