用C语言写基于顺序存储结构的图书信息表的创建和输出
时间: 2023-12-19 18:06:46 浏览: 96
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct {
char title[50];
char author[30];
int year;
float price;
} Book;
int main()
{
Book library[MAX_BOOKS];
int num_books = 0;
// 添加图书信息
printf("请输入图书信息(按Q或q结束)\n");
while (num_books < MAX_BOOKS) {
printf("图书%d:\n", num_books + 1);
printf("书名:");
if (scanf("%s", library[num_books].title) != 1) break;
if (strcmp(library[num_books].title, "Q") == 0 || strcmp(library[num_books].title, "q") == 0) break;
printf("作者:");
scanf("%s", library[num_books].author);
printf("出版年份:");
scanf("%d", &library[num_books].year);
printf("价格:");
scanf("%f", &library[num_books].price);
num_books++;
}
// 输出图书信息
printf("\n图书信息表:\n");
printf("书名\t\t作者\t\t出版年份\t价格\n");
for (int i = 0; i < num_books; i++) {
printf("%s\t\t%s\t\t%d\t\t%.2f\n", library[i].title, library[i].author, library[i].year, library[i].price);
}
return 0;
}
阅读全文