欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《C primer plus》 Chapter 14.1

程序员文章站 2024-02-29 18:09:52
...
# include <stdio.h>
# include <string.h>
# define LEN 10
struct months
{
    char month[LEN];
    char abbr[LEN];
    int days;
    int number;
};
char *s_get(char *st,int n);
int main(void)
{
    struct months year[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 sumdays=0;
    int i,n;
    char name[LEN];
    //enum mon {January,Febuary,March,April,May,June,July,August,September,October,November,December};
    printf("Please enter a month(empty to quit):\n");
    while(s_get(name,LEN)!=NULL && name[0]!='\0')
    {
        for(i=0;i<12;i++)
        {
            if(strcmp(name,year[i].month)==0)
            {
                for(n=0;n<=i;n++)
                {
                    sumdays+=year[n].days;
                }
                printf("The sumdays before this month are %d days.\n",sumdays);
                break;
            }
        }
        printf("Please enter a month(empty to quit):\n");
    }
    puts("Bye!");
    return 0;
}
char *s_get(char *st,int n)
{
    char *find;
    char *ret_val;
    ret_val=fgets(st,n,stdin);
    if(ret_val)
    {
        find=strchr(st,'\n');
        if(find)
            *find='\0';
        else
            while(getchar()!='\n')
                continue;
    }
    return ret_val;
}