6-2366 输出月份英文名(20 分)
本题要求实现函数,可以返回一个给定月份的英文名称。
函数接口定义:
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!
作者: C课程组
单位: 浙江大学
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB
char *getmonth( int n )
{
char *month;
char **mon;
int i;
mon=(char **)malloc(12*sizeof(char *));
mon[0]=(char *)malloc(12*10*sizeof(char));
for(i=1;i<12;i++)
mon[i]=mon[0]+i*10;
strcpy(mon[0],"January");
strcpy(mon[1],"February");
strcpy(mon[2],"March");
strcpy(mon[3],"April");
strcpy(mon[4],"May");
strcpy(mon[5],"June");
strcpy(mon[6],"July");
strcpy(mon[7],"August");
strcpy(mon[8],"September");
strcpy(mon[9],"October");
strcpy(mon[10],"November");
strcpy(mon[11],"December");
month=NULL;
if(n<=0||n>12)
return month;
month=mon[n-1];
return month;
}
本文介绍了一个C语言函数getmonth,该函数接收一个整数参数并返回对应的月份英文名称。若输入不符合月份范围则返回NULL。示例展示了如何使用此函数进行月份名称的查询。
1606

被折叠的 条评论
为什么被折叠?



