C Primer Plus 第14章_结构和其它数据形式_代码和练习题

14.1 示例问题:创建图书目录

处理一本书, 书名-作者-价格, 输入输出
book.c–一本书的图书目录
在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* s_gets(char* st, int n);
#define MAXTITL 41              			/* 书名的最大长度 + 1 */
#define MAXAUTL 31							/* 作者姓名的最大长度 + 1 */

/* 设计程序时,最重要的步骤之一是表示数据的方法 */

/* 建立结构声明 */
struct book {
   								// 结构模板: 标记是book
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
};

int main(void)
{
   
	struct book library;					// 定义结构变量

	printf("Please enter the book title.\n");
	s_gets(library.title, MAXTITL);

	printf("Now enter the author.\n");
	s_gets(library.author, MAXAUTL);

	printf("Now enter the value.\n");
	scanf("%f", &library.value);

	printf("%s by %s: $%.2f\n", library.title, library.author, library.value);
	printf("%s: \"%s\" ($%.2f)\n", library.title, library.author, library.value);
	printf("Done.\n");

	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) {
   							// 如果地址不是NULL
			*find = '\0';					// 在此处放置一个空字符
		}
		else {
   
			while (getchar() != '\n')		// 处理输入行中剩余的字符
			{
   
				continue;
			}
		}
	}
	return ret_val;
}

14.2 结构数组

处理多本书,输入输出。
manybook.c–包含多本书的图书目录
在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* s_gets(char* st, int n);
#define MAXTITL 41
#define MAXAUTL 31
#define MAXBKS 100								// 书籍的最大数量

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

int main(void)
{
   
	struct book library[MAXBKS];					// book类型结构的数组
	int count = 0;
	int index;

	printf("Please enter the book title.\n");
	printf("Press [enter] at the start of a line to stop.\n");
	
	/* 表达式library[count].title[0] != '\0'判断字符串中的首字符是否是空字符(即该字符串是否是空字符串) */
	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("%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);
		}
	}
	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) {
   
		find = strchr(st, '\n');			// 查找换行符

		if (find) {
   							// 如果地址不是NULL
			*find = '\0';					// 在此处放置一个空字符
		}
		else {
   
			while (getchar() != '\n')		// 处理输入行中剩余的字符
			{
   
				continue;
			}
		}
	}
	return ret_val;
}

14.5 嵌套结构

在一个结构中包含另一个结构(即嵌套结构)很方便。
friend.c–嵌套结构示例
在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define LEN 20

const char* msgs[5] = {
   
	"	Thank you for the wonderful evening, ",
	"You certainly prove that a ",
	"is a special kind of guy. We must get together",
	"over a delicious ",
	" and have a few laughs"
};

struct name {
   
	char first[LEN];
	char last[LEN];
};

struct guy {
   
	struct name handle;
	char favfood[LEN];
	char job[LEN];
	float income;
};

int main(void)
{
   
	struct guy fellows = {
   
		{
   "Ewen" , "Villard"},
		"grilled salmon",
		"personality coach",
		68112.00
	};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值