用户借书要求从书库里面删除,单独存储,不能还其他用户借的书,以及还没有借过的书(要求从书库删除 ,每个人借的书独立存储)以上的要求请用C语言代码实现
时间: 2024-03-25 13:40:02 浏览: 21
好的,以下是一份 C 语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100 // 最大书籍数量
#define MAX_NAME_LEN 20 // 最大用户名长度
// 定义书籍结构体
typedef struct Book {
char name[MAX_NAME_LEN]; // 书名
int borrowed; // 是否被借出
struct Book *next; // 指向下一本书的指针
} Book;
// 定义用户结构体
typedef struct User {
char name[MAX_NAME_LEN]; // 用户名
Book *books; // 用户借的书
struct User *next; // 指向下一个用户的指针
} User;
// 定义全局变量
Book books[MAX_BOOKS]; // 书籍数组
int book_count = 0; // 书籍数量
User *users = NULL; // 用户链表
// 添加一本书
void add_book(char *name) {
if (book_count < MAX_BOOKS) {
Book *book = &books[book_count++]; // 获取一本新书的指针
strcpy(book->name, name); // 设置书名
book->borrowed = 0; // 初始化为未借出状态
book->next = NULL; // 下一本书的指针为空
}
}
// 根据书名查找书籍
Book *find_book(char *name) {
for (int i = 0; i < book_count; i++) {
if (strcmp(books[i].name, name) == 0) {
return &books[i]; // 返回找到的书籍指针
}
}
return NULL; // 没有找到,返回空指针
}
// 添加一个用户
void add_user(char *name) {
User *user = (User *) malloc(sizeof(User)); // 获取一个新用户的指针
strcpy(user->name, name); // 设置用户名
user->books = NULL; // 初始化为未借书状态
user->next = users; // 将新用户插入到用户链表表头
users = user;
}
// 根据用户名查找用户
User *find_user(char *name) {
for (User *user = users; user != NULL; user = user->next) {
if (strcmp(user->name, name) == 0) {
return user; // 返回找到的用户指针
}
}
return NULL; // 没有找到,返回空指针
}
// 用户借书
void borrow_book(char *user_name, char *book_name) {
User *user = find_user(user_name); // 查找用户
if (!user) {
printf("User %s does not exist.\n", user_name);
return;
}
Book *book = find_book(book_name); // 查找书籍
if (!book) {
printf("Book %s does not exist.\n", book_name);
return;
}
if (book->borrowed) {
printf("Book %s has been borrowed by another user.\n", book_name);
return;
}
book->borrowed = 1; // 设置为已借出状态
Book *borrowed_book = (Book *) malloc(sizeof(Book)); // 获取一本借阅的书的指针
memcpy(borrowed_book, book, sizeof(Book)); // 复制书籍信息
borrowed_book->next = user->books; // 将借阅的书插入到用户的书单表头
user->books = borrowed_book;
printf("User %s has successfully borrowed book %s.\n", user_name, book_name);
}
// 用户还书
void return_book(char *user_name, char *book_name) {
User *user = find_user(user_name); // 查找用户
if (!user) {
printf("User %s does not exist.\n", user_name);
return;
}
Book **pbook = &user->books; // 获取用户的书单指针的地址
while (*pbook != NULL) {
if (strcmp((*pbook)->name, book_name) == 0) {
Book *book = *pbook; // 获取要还的书的指针
if (book->borrowed == 0) {
printf("Book %s has not been borrowed by user %s.\n", book_name, user_name);
return;
}
book->borrowed = 0; // 设置为未借出状态
*pbook = book->next; // 将要还的书从用户的书单中删除
book->next = NULL; // 将要还的书的下一本书的指针置为空
printf("User %s has successfully returned book %s.\n", user_name, book_name);
return;
}
pbook = &(*pbook)->next;
}
printf("User %s has not borrowed book %s.\n", user_name, book_name);
}
int main() {
add_book("book1");
add_book("book2");
add_book("book3");
add_user("user1");
add_user("user2");
borrow_book("user1", "book1");
borrow_book("user2", "book2");
return_book("user1", "book1");
return_book("user1", "book2");
return 0;
}
```
此示例实现了一个简单的图书管理系统,用户可以借书和还书,每个用户借的书独立存储,且借出的书会从书库中删除,每本书的借出状态会被记录在书籍结构体中。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)