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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值