6-2 輸出月份英文名 (30 分)
本題要求實現函數,可以返回一個給定月份的英文名稱。
函數接口定義:
char *getmonth( int n );
函數getmonth應返回存儲了n對應的月份英文名稱的字符串頭指針。如果傳入的參數n不是一個代表月份的數字,則返回空指針NULL。
裁判測試程序樣例:
include <stdio.h>
char *getmonth( int n );
int main()
{
int n;
char *s;
scanf("%d", &n);
s = getmonth(n);
if ( s==NULL ) printf("wrong input!\n");
else printf("%s\n", s);
return 0;
}
/* 你的代碼將被嵌在這里 */
輸入樣例1:
5
輸出樣例1:
May
輸入樣例2:
15
輸出樣例2:
wrong input!
答案:
char *getmonth( int n )
{
switch(n){
case 1:return "January";
case 2:return "February";
case 3:return "March";
case 4:return "April";
case 5:return "May";
case 6:return "June";
case 7:return "July";
case 8:return "August";
case 9:return "September";
case 10:return "October";
case 11:return "November";
case 12:return "December";
default:return NULL;
}
}