请你帮忙优化一下上述代码
时间: 2024-03-14 17:45:16 浏览: 101
好的,这里给出一些优化建议:
1. 使用 fgets 函数代替 scanf 函数,可以避免输入字符串时出现空格等问题。
2. 在输入书的简介时,使用 fgets 函数,可以输入包含空格的多个单词。
3. 在输出书的信息时,使用更加清晰的格式,可以更好地展示书的信息,例如使用表格的形式。
4. 对于输入的各项信息,可以添加一些验证,确保输入的数据类型正确、输入的字符串不超过规定长度等。
5. 可以添加一些异常处理,例如输入错误时,输出相应的错误信息,而不是直接退出程序。
6. 可以使用动态数组来存储书的信息,避免书的数量达到上限后无法继续添加书的问题。
7. 可以使用文件来存储书的信息,避免程序关闭后信息丢失的问题。
下面给出一个修改后的代码,仅供参考:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_DESCRIPTION 50
struct Book {
char number[20];
char name[50];
char author[20];
float price;
int quantity;
char description[MAX_DESCRIPTION];
};
struct Book *books = NULL;
int num_books = 0;
int max_books = 0;
void add_book()
{
if (num_books == max_books) {
printf("Error: the maximum number of books has been reached.\n");
return;
}
struct Book new_book;
printf("Enter the book number: ");
fgets(new_book.number, sizeof(new_book.number), stdin);
printf("Enter the book name: ");
fgets(new_book.name, sizeof(new_book.name), stdin);
printf("Enter the book author: ");
fgets(new_book.author, sizeof(new_book.author), stdin);
printf("Enter the book price: ");
scanf("%f", &new_book.price);
printf("Enter the book quantity: ");
scanf("%d", &new_book.quantity);
printf("Enter the book description (50 characters or less): ");
fgets(new_book.description, sizeof(new_book.description), stdin);
fgets(new_book.description, sizeof(new_book.description), stdin);
if (strlen(new_book.number) == 0 || strlen(new_book.name) == 0 ||
strlen(new_book.author) == 0 || new_book.price <= 0 ||
new_book.quantity <= 0 || strlen(new_book.description) == 0) {
printf("Error: invalid input.\n");
return;
}
books[num_books] = new_book;
num_books++;
printf("The book has been added successfully.\n");
}
void search_book()
{
char keyword[50];
printf("Enter the keyword to search: ");
scanf("%s", keyword);
int found = 0;
printf("+----------------------------------------+\n");
printf("| %-10s | %-20s | %-10s |\n", "Book Number", "Book Name", "Book Author");
printf("+----------------------------------------+\n");
for (int i = 0; i < num_books; i++) {
if (strstr(books[i].name, keyword) != NULL ||
strstr(books[i].author, keyword) != NULL) {
printf("| %-10s | %-20s | %-10s |\n", books[i].number, books[i].name, books[i].author);
found = 1;
}
}
printf("+----------------------------------------+\n");
if (!found) {
printf("No matching books found.\n");
}
}
void list_books()
{
if (num_books == 0) {
printf("No books found.\n");
return;
}
printf("+------------+----------------------+------------+----------+----------+----------------------+\n");
printf("| Book Number | Book Name | Book Author | Price | Quantity | Description |\n");
printf("+------------+----------------------+------------+----------+----------+----------------------+\n");
for (int i = 0; i < num_books; i++) {
printf("| %-10s | %-20s | %-10s | %-8.2f | %-8d | %-20s |\n",
books[i].number, books[i].name, books[i].author,
books[i].price, books[i].quantity, books[i].description);
}
printf("+------------+----------------------+------------+----------+----------+----------------------+\n");
}
void load_books()
{
FILE *fp = fopen("books.dat", "rb");
if (fp == NULL) {
printf("Error: cannot open file books.dat.\n");
return;
}
fread(&num_books, sizeof(int), 1, fp);
if (num_books > 0) {
max_books = num_books;
books = malloc(num_books * sizeof(struct Book));
fread(books, sizeof(struct Book), num_books, fp);
}
fclose(fp);
}
void save_books()
{
FILE *fp = fopen("books.dat", "wb");
if (fp == NULL) {
printf("Error: cannot open file books.dat.\n");
return;
}
fwrite(&num_books, sizeof(int), 1, fp);
if (num_books > 0) {
fwrite(books, sizeof(struct Book), num_books, fp);
}
fclose(fp);
}
void free_books()
{
if (books != NULL) {
free(books);
}
}
int main()
{
load_books();
while (1) {
printf("----------------------------------------\n");
printf("1. Add Book\n");
printf("2. Search Book\n");
printf("3. List Books\n");
printf("4. Exit\n");
printf("----------------------------------------\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
search_book();
break;
case 3:
list_books();
break;
case 4:
save_books();
free_books();
exit(0);
default:
printf("Error: invalid choice.\n");
break;
}
}
return 0;
}
```
这里使用了动态数组来存储书的信息,如果需要存储更多的书,可以通过重新分配内存来扩展数组的大小。同时,使用了文件来存储书的信息,程序启动时会从文件中读取已有的书的信息,程序退出时会将所有书的信息写入文件中。这样可以确保即使程序关闭,书的信息也不会丢失。
阅读全文