c语言写一个单链表的图书管理系统多一些功能
时间: 2023-09-04 10:12:15 浏览: 59
好的,以下是一个使用单链表实现的图书管理系统,包含了一些额外的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义图书结构体
typedef struct book {
char name[50];
char author[50];
int year;
int stock;
struct book *next;
} Book;
// 创建一个新的图书
Book* create_book(char name[], char author[], int year, int stock) {
Book *new_book = (Book*)malloc(sizeof(Book));
strcpy(new_book->name, name);
strcpy(new_book->author, author);
new_book->year = year;
new_book->stock = stock;
new_book->next = NULL;
return new_book;
}
// 打印所有图书
void print_books(Book *head) {
Book *cur = head;
while (cur != NULL) {
printf("%s, %s, %d, %d\n", cur->name, cur->author, cur->year, cur->stock);
cur = cur->next;
}
}
// 在链表尾部添加一个新的图书
void add_book(Book **head, Book *new_book) {
Book *cur = *head;
if (cur == NULL) {
*head = new_book;
return;
}
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = new_book;
}
// 根据图书名字查找图书
Book* find_book(Book *head, char name[]) {
Book *cur = head;
while (cur != NULL) {
if (strcmp(cur->name, name) == 0) {
return cur;
}
cur = cur->next;
}
return NULL;
}
// 根据图书名字删除图书
void remove_book(Book **head, char name[]) {
Book *cur = *head;
if (cur == NULL) {
return;
}
if (strcmp(cur->name, name) == 0) {
*head = cur->next;
free(cur);
return;
}
while (cur->next != NULL && strcmp(cur->next->name, name) != 0) {
cur = cur->next;
}
if (cur->next != NULL) {
Book *to_remove = cur->next;
cur->next = to_remove->next;
free(to_remove);
}
}
// 借出一本图书
void borrow_book(Book **head, char name[]) {
Book *book = find_book(*head, name);
if (book == NULL) {
printf("Book not found.\n");
return;
}
if (book->stock <= 0) {
printf("Book out of stock.\n");
return;
}
book->stock--;
printf("Borrowed %s successfully.\n", book->name);
}
// 归还一本图书
void return_book(Book **head, char name[]) {
Book *book = find_book(*head, name);
if (book == NULL) {
printf("Book not found.\n");
return;
}
book->stock++;
printf("Returned %s successfully.\n", book->name);
}
// 主函数
int main() {
Book *head = NULL;
int choice;
while (1) {
printf("1. Add book\n");
printf("2. Remove book\n");
printf("3. Print books\n");
printf("4. Borrow book\n");
printf("5. Return book\n");
printf("0. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
{
char name[50], author[50];
int year, stock;
printf("Enter the name of the book: ");
scanf("%s", name);
printf("Enter the author of the book: ");
scanf("%s", author);
printf("Enter the year of publication: ");
scanf("%d", &year);
printf("Enter the stock: ");
scanf("%d", &stock);
Book *new_book = create_book(name, author, year, stock);
add_book(&head, new_book);
printf("Added %s successfully.\n", name);
break;
}
case 2:
{
char name[50];
printf("Enter the name of the book: ");
scanf("%s", name);
remove_book(&head, name);
printf("Removed %s successfully.\n", name);
break;
}
case 3:
{
printf("Name, Author, Year, Stock\n");
print_books(head);
break;
}
case 4:
{
char name[50];
printf("Enter the name of the book: ");
scanf("%s", name);
borrow_book(&head, name);
break;
}
case 5:
{
char name[50];
printf("Enter the name of the book: ");
scanf("%s", name);
return_book(&head, name);
break;
}
case 0:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
此图书管理系统除了基本的添加、删除、打印、借阅和归还功能,还包括了以下额外功能:
1. 每本图书记录了库存信息;
2. 可以根据图书名字查找图书;
3. 借出一本图书时会检查库存是否足够,如果不够则无法借出;
4. 归还一本图书时会增加库存;
5. 可以不断执行各种操作,直到选择退出程序。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)