编写图书管理程序,输入至少5册书籍,可以按名称和作者查询书籍信息,提供信息录入、删除、查询功能,将书籍信息存入文件,程序运行从文件读出数据。
时间: 2024-02-15 12:04:54 浏览: 26
好的,下面是一个完整的图书管理程序,包括书籍信息的录入、删除、查询和存储功能:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char name[50];
char author[50];
float price;
char publisher[50];
char publish_date[20];
};
struct Library {
struct Book books[MAX_BOOKS];
int count;
};
struct Library library;
void add_book() {
struct Book book;
printf("请输入书名:");
scanf("%s", book.name);
printf("请输入作者:");
scanf("%s", book.author);
printf("请输入价格:");
scanf("%f", &book.price);
printf("请输入出版社:");
scanf("%s", book.publisher);
printf("请输入出版年月:");
scanf("%s", book.publish_date);
library.books[library.count++] = book;
printf("添加成功!\n");
}
void delete_book() {
char name[50];
int found = 0;
printf("请输入要删除的书名:");
scanf("%s", name);
for (int i = 0; i < library.count; i++) {
if (strcmp(name, library.books[i].name) == 0) {
for (int j = i; j < library.count - 1; j++) {
library.books[j] = library.books[j + 1];
}
library.count--;
found = 1;
printf("删除成功!\n");
break;
}
}
if (!found) {
printf("未找到该书!\n");
}
}
void find_book_by_name() {
char name[50];
int found = 0;
printf("请输入书名:");
scanf("%s", name);
for (int i = 0; i < library.count; i++) {
if (strcmp(name, library.books[i].name) == 0) {
printf("书名:%s,作者:%s,价格:%.2f,出版社:%s,出版年月:%s\n",
library.books[i].name, library.books[i].author, library.books[i].price,
library.books[i].publisher, library.books[i].publish_date);
found = 1;
}
}
if (!found) {
printf("未找到该书!\n");
}
}
void find_book_by_author() {
char author[50];
int found = 0;
printf("请输入作者:");
scanf("%s", author);
for (int i = 0; i < library.count; i++) {
if (strcmp(author, library.books[i].author) == 0) {
printf("书名:%s,作者:%s,价格:%.2f,出版社:%s,出版年月:%s\n",
library.books[i].name, library.books[i].author, library.books[i].price,
library.books[i].publisher, library.books[i].publish_date);
found = 1;
}
}
if (!found) {
printf("未找到该作者的书!\n");
}
}
void save_books() {
FILE* fp = fopen("library.dat", "wb");
fwrite(&library, sizeof(library), 1, fp);
fclose(fp);
}
void load_books() {
FILE* fp = fopen("library.dat", "rb");
if (fp != NULL) {
fread(&library, sizeof(library), 1, fp);
fclose(fp);
}
}
int main() {
int choice;
load_books();
while (1) {
printf("请选择操作:\n");
printf("1. 录入书籍信息\n");
printf("2. 删除书籍信息\n");
printf("3. 按名称查询书籍信息\n");
printf("4. 按作者查询书籍信息\n");
printf("5. 退出程序\n");
printf("请输入数字选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
delete_book();
break;
case 3:
find_book_by_name();
break;
case 4:
find_book_by_author();
break;
case 5:
save_books();
printf("程序已退出!\n");
return 0;
default:
printf("无效选择,请重新输入!\n");
break;
}
}
}
```
上面的代码中,我们定义了一个 `main` 函数来实现程序的主要功能。在程序运行时,首先会调用 `load_books` 函数从文件中读取书籍信息。然后,通过一个循环来不断读取用户的选择,根据选择调用相应的函数完成相应的操作。最后,当用户选择退出时,程序会调用 `save_books` 函数将书籍信息存储到文件中。
完整的代码可以编译并运行,测试各种功能是否正常工作。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)