首页 > 试题广场 >

重做复习题3,但用月份名的拼写代替月份号(别忘了可以使用st

[问答题]
设计一个结构模板,保存一个月份名、一个3个字母的该月份的缩写、该月的天数,以及月份号,但用月份名的拼写代替月份号(别忘了可以使用strcmp())。
推荐
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int days(char* p);
struct month{
 char name[10];
 char abbrev[4];
 int days;
 int monumb;
};
struct month months[12]={
 {"january",  "jan",31,1},
 {"february", "feb",28,2},
 {"march",  "mar",31,3},
 {"april",  "apr",30,4},
 {"may",   "may",31,5},
 {"june",  "jun",30,6},
 {"july",  "jul",31,7},
 {"august",  "aug",31,8},
 {"september", "sep",30,9},
 {"october",  "oct",31,10},
 {"november", "nov",30,11},
 {"december", "dec",31,12}
};
int main(void)
{
 char input[10];
 int daytotal;
 printf("Enter the name of a month: ");
 while(gets(input) != NULL && input[0] != '\0')
 {
 daytotal = days(input);
 if (daytotal > 0)
 printf("There are %d days through %s.\n", daytotal, input);
 else
 printf("%s is not valid input.\n", input);
 printf("Next month (empty line to quit): ");
 }
 puts("bye");
 return 0;
}
int days(char* p)
{
 int i=0,total=0;
 while( p[i] != '\0' )
 {
 p[i] = tolower( p[i] );
 i++;
 }
 for(i=0;i<12;i++)
 { 
 total += months[i].days ;
 if( strcmp( p, months[i].name) == 0 )
 return total;
 }
 return -1;
}

发表于 2018-05-05 22:07:48 回复(0)