用链表和文件编写一个图书管理系统
时间: 2023-06-14 09:02:28 浏览: 137
好的,下面是一个简单的图书管理系统的实现,采用链表和文件存储数据。这个系统支持添加、删除、查询和显示所有图书信息的功能。
1.定义图书结构体和链表节点结构体
```c
#define MAX_TITLE_LENGTH 50
#define MAX_AUTHOR_LENGTH 20
#define MAX_ISBN_LENGTH 15
struct Book {
char title[MAX_TITLE_LENGTH];
char author[MAX_AUTHOR_LENGTH];
char isbn[MAX_ISBN_LENGTH];
int year;
int quantity;
};
struct ListNode {
struct Book book;
struct ListNode* next;
};
```
2.实现链表的添加、删除和查询操作
```c
// 添加图书
void add_book(struct ListNode** head_ref, struct Book book) {
struct ListNode* new_node = (struct ListNode*)malloc(sizeof(struct ListNode));
new_node->book = book;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
} else {
struct ListNode* last_node = *head_ref;
while (last_node->next != NULL) {
last_node = last_node->next;
}
last_node->next = new_node;
}
}
// 删除图书
void delete_book(struct ListNode** head_ref, char* isbn) {
struct ListNode* temp = *head_ref, *prev;
if (temp != NULL && strcmp(temp->book.isbn, isbn) == 0) {
*head_ref = temp->next;
free(temp);
return;
}
while (temp != NULL && strcmp(temp->book.isbn, isbn) != 0) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
return;
}
prev->next = temp->next;
free(temp);
}
// 查询图书
struct Book* find_book(struct ListNode* head, char* isbn) {
struct ListNode* temp = head;
while (temp != NULL) {
if (strcmp(temp->book.isbn, isbn) == 0) {
return &(temp->book);
}
temp = temp->next;
}
return NULL;
}
```
3.实现文件读写操作
```c
// 从文件中读取图书信息
void read_books(struct ListNode** head_ref, char* filename) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return;
}
char title[MAX_TITLE_LENGTH];
char author[MAX_AUTHOR_LENGTH];
char isbn[MAX_ISBN_LENGTH];
int year;
int quantity;
while (fscanf(fp, "%s %s %s %d %d", title, author, isbn, &year, &quantity) != EOF) {
struct Book book = { .year = year, .quantity = quantity };
strcpy(book.title, title);
strcpy(book.author, author);
strcpy(book.isbn, isbn);
add_book(head_ref, book);
}
fclose(fp);
}
// 将图书信息保存到文件中
void write_books(struct ListNode* head, char* filename) {
FILE* fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return;
}
struct ListNode* temp = head;
while (temp != NULL) {
fprintf(fp, "%s %s %s %d %d\n", temp->book.title, temp->book.author, temp->book.isbn, temp->book.year, temp->book.quantity);
temp = temp->next;
}
fclose(fp);
}
```
4.实现主函数,提供用户交互界面
```c
int main() {
struct ListNode* head = NULL;
read_books(&head, "books.txt");
int choice;
do {
printf("1. Add book\n");
printf("2. Delete book\n");
printf("3. Find book\n");
printf("4. Show all books\n");
printf("5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
struct Book book;
printf("Enter book title: ");
scanf("%s", book.title);
printf("Enter book author: ");
scanf("%s", book.author);
printf("Enter book ISBN: ");
scanf("%s", book.isbn);
printf("Enter publication year: ");
scanf("%d", &book.year);
printf("Enter quantity: ");
scanf("%d", &book.quantity);
add_book(&head, book);
printf("Book added successfully!\n");
break;
}
case 2: {
char isbn[MAX_ISBN_LENGTH];
printf("Enter book ISBN: ");
scanf("%s", isbn);
delete_book(&head, isbn);
printf("Book deleted successfully!\n");
break;
}
case 3: {
char isbn[MAX_ISBN_LENGTH];
printf("Enter book ISBN: ");
scanf("%s", isbn);
struct Book* book = find_book(head, isbn);
if (book == NULL) {
printf("Book not found!\n");
} else {
printf("Title: %s\n", book->title);
printf("Author: %s\n", book->author);
printf("ISBN: %s\n", book->isbn);
printf("Year: %d\n", book->year);
printf("Quantity: %d\n", book->quantity);
}
break;
}
case 4: {
struct ListNode* temp = head;
while (temp != NULL) {
printf("Title: %s\n", temp->book.title);
printf("Author: %s\n", temp->book.author);
printf("ISBN: %s\n", temp->book.isbn);
printf("Year: %d\n", temp->book.year);
printf("Quantity: %d\n", temp->book.quantity);
printf("\n");
temp = temp->next;
}
break;
}
case 5: {
printf("Saving books to file...\n");
write_books(head, "books.txt");
printf("Goodbye!\n");
break;
}
default: {
printf("Invalid choice!\n");
break;
}
}
} while (choice != 5);
return 0;
}
```
这就是一个简单的图书管理系统的实现。当用户选择退出程序时,所有图书信息会被保存到文件中。如果下次用户再次启动程序,之前保存的图书信息会被加载到链表中。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)