圖書管理系統
使用雙向循環鏈表實現一個簡單的圖書管理系統,圖書管理系統有如下功能:
1.添加書籍
2.刪除書籍
3.修改書籍信息
4.查詢書籍信息
5.借書
6.還書
#include <stdio.h>
#include <stdlib.h>
#include <string.h>// 書籍結構體
struct book {char name[20]; // 書名char authorname[20]; // 作者名float price; // 價格int num; // 總數量int borrowed; // 已借出數量
};// 雙向循環鏈表節點
struct node {struct book data;struct node *prev;struct node *next;
};// 全局變量:鏈表頭指針
struct node *head = NULL;// 函數聲明
void add_book();
void delete_book();
void modify_book();
void search_book();
void borrow_book();
void return_book();
void display_all();
void free_list();
struct node* find_book(char *name);int main() {int choice;while (1) {printf("\n圖書管理系統\n");printf("1. 添加書籍\n");printf("2. 刪除書籍\n");printf("3. 修改書籍信息\n");printf("4. 查詢書籍信息\n");printf("5. 借書\n");printf("6. 還書\n");printf("7. 顯示所有書籍\n");printf("0. 退出\n");printf("請選擇操作: ");scanf("%d", &choice);switch (choice) {case 1: add_book(); break;case 2: delete_book(); break;case 3: modify_book(); break;case 4: search_book(); break;case 5: borrow_book(); break;case 6: return_book(); break;case 7: display_all(); break;case 0: free_list(); return 0;default: printf("無效選擇!\n");}}return 0;
}// 添加書籍
void add_book() {struct node *new_node = (struct node*)malloc(sizeof(struct node));if (!new_node) {printf("內存分配失敗!\n");return;}printf("請輸入書名: ");scanf("%s", new_node->data.name);printf("請輸入作者名: ");scanf("%s", new_node->data.authorname);printf("請輸入價格: ");scanf("%f", &new_node->data.price);printf("請輸入總數量: ");scanf("%d", &new_node->data.num);new_node->data.borrowed = 0;if (!head) {head = new_node;head->next = head;head->prev = head;} else {new_node->next = head;new_node->prev = head->prev;head->prev->next = new_node;head->prev = new_node;}printf("書籍添加成功!\n");
}// 刪除書籍
void delete_book() {if (!head) {printf("沒有書籍可刪除!\n");return;}char name[20];printf("請輸入要刪除的書名: ");scanf("%s", name);struct node *current = head;do {if (strcmp(current->data.name, name) == 0) {if (current->data.borrowed > 0) {printf("該書還有借出未還,不能刪除!\n");return;}if (current == head) {if (head->next == head) {head = NULL;} else {head = head->next;}}current->prev->next = current->next;current->next->prev = current->prev;free(current);printf("書籍刪除成功!\n");return;}current = current->next;} while (current != head);printf("未找到該書!\n");
}// 修改書籍信息
void modify_book() {if (!head) {printf("沒有書籍可修改!\n");return;}char name[20];printf("請輸入要修改的書名: ");scanf("%s", name);struct node *book = find_book(name);if (!book) {printf("未找到該書!\n");return;}printf("當前信息:\n");printf("書名: %s\n", book->data.name);printf("作者: %s\n", book->data.authorname);printf("價格: %.2f\n", book->data.price);printf("總數量: %d\n", book->data.num);printf("已借出: %d\n", book->data.borrowed);printf("\n請輸入新的作者名: ");scanf("%s", book->data.authorname);printf("請輸入新的價格: ");scanf("%f", &book->data.price);printf("請輸入新的總數量: ");scanf("%d", &book->data.num);printf("書籍信息修改成功!\n");
}// 查詢書籍信息
void search_book() {if (!head) {printf("沒有書籍可查詢!\n");return;}char name[20];printf("請輸入要查詢的書名: ");scanf("%s", name);struct node *book = find_book(name);if (!book) {printf("未找到該書!\n");return;}printf("\n書籍信息:\n");printf("書名: %s\n", book->data.name);printf("作者: %s\n", book->data.authorname);printf("價格: %.2f\n", book->data.price);printf("總數量: %d\n", book->data.num);printf("可借數量: %d\n", book->data.num - book->data.borrowed);
}// 借書
void borrow_book() {if (!head) {printf("沒有書籍可借!\n");return;}char name[20];printf("請輸入要借的書名: ");scanf("%s", name);struct node *book = find_book(name);if (!book) {printf("未找到該書!\n");return;}int available = book->data.num - book->data.borrowed;printf("當前可借數量: %d\n", available);if (available <= 0) {printf("該書已全部借出!\n");return;}int quantity;printf("請輸入要借的數量: ");scanf("%d", &quantity);if (quantity <= 0) {printf("借書數量必須大于0!\n");return;}if (quantity > available) {printf("借書數量超過可借數量!\n");return;}book->data.borrowed += quantity;printf("借書成功! 當前已借出: %d\n", book->data.borrowed);
}// 還書
void return_book() {if (!head) {printf("沒有書籍可還!\n");return;}char name[20];printf("請輸入要還的書名: ");scanf("%s", name);struct node *book = find_book(name);if (!book) {printf("未找到該書!\n");return;}if (book->data.borrowed <= 0) {printf("該書沒有借出記錄!\n");return;}printf("當前已借出數量: %d\n", book->data.borrowed);int quantity;printf("請輸入要還的數量: ");scanf("%d", &quantity);if (quantity <= 0) {printf("還書數量必須大于0!\n");return;}if (quantity > book->data.borrowed) {printf("還書數量超過已借出數量!\n");return;}book->data.borrowed -= quantity;printf("還書成功! 當前已借出: %d\n", book->data.borrowed);
}// 顯示所有書籍
void display_all() {if (!head) {printf("沒有書籍可顯示!\n");return;}struct node *current = head;printf("\n所有書籍信息:\n");do {printf("\n書名: %s\n", current->data.name);printf("作者: %s\n", current->data.authorname);printf("價格: %.2f\n", current->data.price);printf("總數量: %d\n", current->data.num);printf("可借數量: %d\n", current->data.num - current->data.borrowed);current = current->next;} while (current != head);
}// 釋放鏈表內存
void free_list() {if (!head) return;struct node *current = head;struct node *temp;do {temp = current;current = current->next;free(temp);} while (current != head);
}// 查找書籍
struct node* find_book(char *name) {if (!head) return NULL;struct node *current = head;do {if (strcmp(current->data.name, name) == 0) {return current;}current = current->next;} while (current != head);return NULL;
}