用C++编写一个函数求某月有多少天。要求该函数以年份和月份作为函数输入参数,以天数作为返回值。并在主函数中调用该函数输出天数结果。谢谢
时间: 2023-05-28 13:02:59 浏览: 103
C++计算某年某月的天数并输出
```c
#include <stdio.h>
int daysInMonth(int year, int month);
int main() {
int year, month, days;
printf("请输入年份和月份(用空格隔开): ");
scanf("%d %d", &year, &month);
days = daysInMonth(year, month);
printf("%d年%d月有%d天\n", year, month, days);
return 0;
}
int daysInMonth(int year, int month) {
int days;
switch(month) {
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;
} else {
days = 28;
}
break;
default:
printf("输入有误,请输入1-12之间的月份\n");
days = -1;
}
return days;
}
```
阅读全文