c primer plus 第十四章课后编程1题

// Created by mack bookAir on 2019/10/13.
// Copyright © 2019 mack bookAir. All rights reserved.
//用月份名的拼写代替月份号,重新编写复习题
//如果要用月份缩写,可以调用结构成员mon进行对比.
#include <stdio.h>
#include <string.h> //提供字符串函数原型
//结构原型
struct year{
char month[20];
char mon[4];
int days;
};
//初始化结构数组
struct year months[12] = {
{“January”,“jan”,31},
{“February”, “feb”,28},
{“March”,“mar”,31},
{“April”,“apr”,30},
{“May”,“may”,31},
{“June”,“jun”,30},
{“July”,“jul”,31},
{“August”,“aug”,31},
{“September”,“sep”,30},
{“October”,“oct”,31},
{“November”,“nov”,30},
{“December”,“dec”,31}
};

char *s_gets(char *st, int n);

int main(void)
{
int total=0;
int i=0,j;
char month1[20];
puts(“请输入月份:”);
s_gets(month1,20);
//对比输入内容和结构初始化内容
while(strcmp(month1,months[i].month)!=0)
i++;
//因为计算输入的当月所以+1
j=i+1;
while(j–>=0)
total+=months[j].days;
printf(“一年中到%s为止,总天数为%d天。\n”,month1,total);

return 0;
}
//输入函数,清除,需要输入字符串之外的,所有字符.
char *s_gets(char *st, int n)
{
char *ret_val;
char *find;

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;
}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/102538541