定义枚举型Month,其中的枚举项以英文月份名January、February等表示;实现如下的getDays()函数;使得下述程序能正确打印指定年月的天数。 说明:函数返回指定年月的天数,getDays(2022,April)应返回30,请注意区分闰年。 函数接口定义: int getDays(int year, enum Month m); 裁判测试程序样例: #include <stdio.h> #include <stdbool.h> bool isLeapYear(const int year){ //四年一闰,百年不闰,四百年又闰 if (year%400==0) return true; else if (year%100==0) return false; else if (year%4==0) return true; else return false; } //此处定义enum Month枚举类型 //此处定义getDays()函数 int main() { printf("Year %d, Month %d: %d days\n",1600,(int)February,getDays(1600,February)); printf("Year %d, Month %d: %d days\n",2100,(int)February,getDays(2100,February)); printf("Year %d, Month %d: %d days\n",2104,(int)February,getDays(2104,February)); printf("Year %d, Month %d: %d days\n",1997,(int)February,getDays(1997,February)); printf("Year %d, Month %d: %d days\n",1996,(int)February,getDays(1996,February)); for (int i=1;i<=12;i++){ printf("Year %d, Month %d: %d days\n",5102,(enum Month)i,getDays(5102,(enum Month)i)); } return 0; } 输入样例: 输出样例: Year 1600, Month 2: 29 days Year 2100, Month 2: 28 days Year 2104, Month 2: 29 days Year 1997, Month 2: 28 days Year 1996, Month 2: 29 days Year 5102, Month 1: 31 days Year 5102, Month 2: 28 days Year 5102, Month 3: 31 days Year 5102, Month 4: 30 days Year 5102, Month 5: 31 days Year 5102, Month 6: 30 days Year 5102, Month 7: 31 days Year 5102, Month 8: 31 days Year 5102, Month 9: 30 days Year 5102, Month 10: 31 days Year 5102, Month 11: 30 days Year 5102, Month 12: 31 days
时间: 2023-06-25 20:05:03 浏览: 378
参考代码如下:
```
#include <stdio.h>
#include <stdbool.h>
enum Month {January = 1, February, March, April, May, June, July, August, September, October, November, December};
bool isLeapYear(const int year){
//四年一闰,百年不闰,四百年又闰
if (year%400==0)
return true;
else if (year%100==0)
return false;
else if (year%4==0)
return true;
else
return false;
}
int getDays(int year, enum Month m) {
int days = 0;
switch(m) {
case January:
case March:
case May:
case July:
case August:
case October:
case December:
days = 31;
break;
case April:
case June:
case September:
case November:
days = 30;
break;
case February:
if (isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
break;
}
return days;
}
int main() {
printf("Year %d, Month %d: %d days\n",1600,(int)February,getDays(1600,February));
printf("Year %d, Month %d: %d days\n",2100,(int)February,getDays(2100,February));
printf("Year %d, Month %d: %d days\n",2104,(int)February,getDays(2104,February));
printf("Year %d, Month %d: %d days\n",1997,(int)February,getDays(1997,February));
printf("Year %d, Month %d: %d days\n",1996,(int)February,getDays(1996,February));
for (int i=1;i<=12;i++){
printf("Year %d, Month %d: %d days\n",5102,(enum Month)i,getDays(5102,(enum Month)i));
}
return 0;
}
```
输出结果:
```
Year 1600, Month 2: 29 days
Year 2100, Month 2: 28 days
Year 2104, Month 2: 29 days
Year 1997, Month 2: 28 days
Year 1996, Month 2: 29 days
Year 5102, Month 1: 31 days
Year 5102, Month 2: 28 days
Year 5102, Month 3: 31 days
Year 5102, Month 4: 30 days
Year 5102, Month 5: 31 days
Year 5102, Month 6: 30 days
Year 5102, Month 7: 31 days
Year 5102, Month 8: 31 days
Year 5102, Month 9: 30 days
Year 5102, Month 10: 31 days
Year 5102, Month 11: 30 days
Year 5102, Month 12: 31 days
```
阅读全文