C Primer Plus 第六版(中文版)第十四章(注释版)编程练习答案(14.18.1-14.18.3)

C Primer Plus 第六版(中文版)第十四章(注释版)编程练习答案

欢迎提问交流

14.18.1

#include<stdio.h>
#include<string.h>
#include<ctype.h>

struct month
{
	char monthname[10];
	char abbrev[5];
	int days;
	int num_month;
};

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

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

int main(void)
{
	char input[20];
	int total;

	puts("输入月份名字:");

	while (s_gets(input, 20) != NULL && input[0] != '\0')
	{
		total = days(input);//统计输入的月份有多少天
		printf("我们有 %d 天,月份名称 %s.\n", total, input);
		printf("next month (empty line to quit)\n");
	}
	puts("bye");
	return 0;
}

int days(char * pt)//传递进去一个字符串,指针pt指向(数组)字符串
{
	int i;
	int mon_num = 0;//月份号
	int total = 0;

	pt[0] = toupper(pt[0]);//取出第一个字符转化成大写再赋给本身

	for (int i = 1; pt[i] != '\0'; i++)//碰到空字符\0再停止,一直输出字符串里的字符
	{
		pt[i] = tolower(pt[i]);//剩余字符转为小写
	}

	for (i = 0; i < 12; i++)
	{
		if (strcmp(pt, months[i].monthname) == 0)//pt指针指向的(人为输入的)字符串与结构体中已有的月份进行比较
		{
			mon_num = months[i].num_month;//找到月份
		}

	}

	for( i = 0; i < mon_num; i ++)//对找到的月份的天数求和
	{
		total += months[i].days;
	}

	return total;
}

char * s_gets(char *st, int n)
{
	char * ret_val;
	int i = 0;

	ret_val = fgets(st, n ,stdin);
	if (ret_val)
	{
		while (st[i] != '\n' && st[i] != '\0')
		{
			i++;
			if (st[i] == '\n')
			{
				st[i] == '\0';
			}
			else
				while (getchar() != '\n')
				{
					continue;
				}
		}
	}

	return ret_val;

}

14.18.2

在这里插入
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

char * s_gets(char *st, int n);
int days(int year, int month, int days);
int leapyear(int year);


struct month
{
	char monthname[10];
	char abbrev[5];
	int days;
	int num_month;
};

 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)
{

	int year, month, day;
	char mname[10];
	int i;

	puts("请输入年:");

	while (scanf_s("%d", &year) == 1)//输入正确返回值为1
	{

		puts("请输入月份:");
		if (scanf_s("%d", &month) == 1)//按数字输入
		{
			printf("\n");
		}
		else//那么我们可能输入的是字符串了现在
		{
			s_gets(mname, 10);
			for (i = 0; i < 12; i++)//那么就需要和12个月的全称去比较了,看看输入的那一个月
			{
				if (strcmp(mname, months[i].monthname) == 0 || strcmp(mname, months[i].abbrev))//全称、简称都一样
				{
					month = months[i].num_month;//找到了我们想找到的对应月份
					break;
				}
			}
		}
		puts("请输入日期:");
		if (scanf_s("%d", &day) != 1)//输入错误
		{
			exit(1);
		}
		printf("%d 天我们有。\n", days(year, month, day));
		
		while (getchar()!='\n')//跳过多余输入
		{
			continue;
		}
		puts("请输入下一组要查看信息:q来退出");
	}

	puts("bye.");
	return 0;
}

int days(int year,int month,int days)
{
	int total = 0;
	int i;

	leapyear(year) ? (months[1].days = 29) : (months[1].days = 28);//判断是不是闰年,是那么二月份29天,

	for (i = 0; i < month - 1; i++)//数据统计1-3月份(不可能+到3月份全部所以-1),假定计算3.8月份的天数和,把1、2月份的日期+3月份的8天
	{
		total += months[i].days;
	}
	return total + days;//+的是当前3月份的天数

}

int leapyear(int year)//对闰年判断,返回1真,0假
{
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))//对闰年判断
	{
		return 1;
	}
	else
		return 0;
}

char * s_gets(char *st, int n)
{
	char * ret_val;
	int i = 0;

	ret_val = fgets(st, n ,stdin);
	
	if (ret_val!= NULL)
	{
		while (st[i] != '\n' && st[i] != '\0')
		{
			i++;
			if (st[i] == '\n')
			{
				st[i] == '\0';
			}
			else
				while (getchar() != '\n')
				{
					continue;
				}
		}
	}

	return ret_val;

}代码片

14.18.3

在这里插入代码片
#include <stdio.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100

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

struct book
{
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

int main(void)
{
	struct book library[MAXBKS];

	int count = 0;
	int index;
	int seek, top;
	struct book temp;

	printf("Please enter the book title.\n");
	printf("Press [enter] at the start of a line to stop.\n");

	while ( count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')//循环录入信息
	{
		printf("Now enter the author.\n");
		s_gets(library[count].author,MAXAUTL);//内容不为空,可以开始输入作者

		printf("Now enter the value.\n");
		scanf_s("%f", &library[count++].value);//数量自增加,每次循环加一次

		while (getchar() != '\n')
			continue;
		
		if (count < MAXBKS)//书本数量未到上限
		{
			printf("Enter the next title.\n");
		}

	}

	if (count > 0)//有书
	{
		printf("Here is the list of your books:\n");
		for (index = 0; index < count; index++)
		{
			printf("%s by %s: $%.2f\n", library[index].title,
				library[index].author, library[index].value);//循环把所有书本的详细信息输出
		}
		
		printf("............................................\n");
		printf("Here is the list of your books sorted by value(from low to high):\n");
		
		
		for (top = 0; top < count -1; top++)//外层--对价格进行比较,循环嵌套的方式:第一本开始
		{
			for (seek = top + 1; seek < count; seek++)//内层--第二本书和第一本比:第二本开始
			{
				if(library[top].value > library[seek].value)//第一本书价格大于比较的这本书
				{
					//交换过程,1与2,1与3,1与4....使得第一本永远是最便宜的
					temp = library[top];
					library[top] = library[seek];
					library[seek] = temp;
				}
			}
		}
		for (index = 0; index < count; index++)
		{
			printf("%s by %s: $%.2f\n", library[index].title,
				library[index].author, library[index].value);//循环把所有书本的详细信息输出
		}
	
		printf("............................................\n");
		printf("Here is the list of your books sorted by title letters:\n");
		for (top = 0; top < count - 1; top++)
		{
			for (seek = top + 1; seek < count; seek++)
			{
				if (strcmp(library[top].author, library[seek].author) > 0)//字符串比较
				{
					temp = library[top];
					library[top] = library[seek];
					library[seek] = temp;
				}
			}

		}
		for (index = 0; index < count; index++)
		{
			printf("%s by %s: $%.2f\n", library[index].title,
				library[index].author, library[index].value);//循环把所有书本的详细信息输出
		}
	}
	else
	{
		printf("No books? Too bad.\n");
	}

	return 0;
}

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

	char * ret_val;
	char *find;

	ret_val = fgets(st, n, stdin);

	if (ret_val !=NULL)
	{
		find = strchr(st, '\n');
		if (find)
		{
			*find = '\0';
		}
		else
			while (getchar() != '\n')  //清除IO缓冲区 
				continue;

	}
	return ret_val;

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扳手的海角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值