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