用c语言写图书管理系统,要求有注册,登录,,借书,还书,查看用户借书详情,退出系统这几个功能,其中借书中可以查看可借图书
时间: 2024-05-03 08:16:52 浏览: 24
好,以下是一个简单的图书管理系统的代码示例,包含注册、登录、借书、还书、查看用户借书详情和退出系统这几个功能:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_USERS 100
typedef struct {
char title[50];
char author[50];
int available; // 可借数量
} Book;
typedef struct {
char username[50];
char password[50];
int borrowed_books[MAX_BOOKS]; // 用户借阅的书籍id
} User;
Book books[MAX_BOOKS];
User users[MAX_USERS];
int num_books = 0, num_users = 0;
void register_user() {
char username[50], password[50];
printf("请输入用户名: ");
scanf("%s", username);
printf("请输入密码: ");
scanf("%s", password);
strcpy(users[num_users].username, username);
strcpy(users[num_users].password, password);
printf("注册成功!\n");
num_users++;
}
int login() {
char username[50], password[50];
printf("请输入用户名: ");
scanf("%s", username);
printf("请输入密码: ");
scanf("%s", password);
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
printf("登录成功!\n");
return i;
}
}
printf("用户名或密码错误!\n");
return -1;
}
void borrow_book(int user_id) {
printf("可借图书列表:\n");
for (int i = 0; i < num_books; i++) {
if (books[i].available > 0) {
printf("%d. %s - %s(可借%d本)\n", i+1, books[i].title, books[i].author, books[i].available);
}
}
int book_id;
printf("请输入要借阅的图书编号: ");
scanf("%d", &book_id);
book_id--;
if (book_id < 0 || book_id >= num_books) {
printf("图书编号错误!\n");
return;
}
if (books[book_id].available <= 0) {
printf("该图书已借完!\n");
return;
}
books[book_id].available--;
users[user_id].borrowed_books[book_id] = 1;
printf("借阅成功!\n");
}
void return_book(int user_id) {
printf("您借阅的图书列表:\n");
for (int i = 0; i < num_books; i++) {
if (users[user_id].borrowed_books[i] == 1) {
printf("%d. %s - %s\n", i+1, books[i].title, books[i].author);
}
}
int book_id;
printf("请输入要归还的图书编号: ");
scanf("%d", &book_id);
book_id--;
if (book_id < 0 || book_id >= num_books) {
printf("图书编号错误!\n");
return;
}
if (users[user_id].borrowed_books[book_id] == 0) {
printf("您未借阅该图书!\n");
return;
}
books[book_id].available++;
users[user_id].borrowed_books[book_id] = 0;
printf("归还成功!\n");
}
void view_borrowed_books(int user_id) {
printf("您借阅的图书列表:\n");
for (int i = 0; i < num_books; i++) {
if (users[user_id].borrowed_books[i] == 1) {
printf("%s - %s\n", books[i].title, books[i].author);
}
}
}
int main() {
// 初始化图书列表
strcpy(books[num_books].title, "C语言程序设计");
strcpy(books[num_books].author, "谭浩强");
books[num_books].available = 3;
num_books++;
strcpy(books[num_books].title, "Java程序设计");
strcpy(books[num_books].author, "李刚");
books[num_books].available = 2;
num_books++;
strcpy(books[num_books].title, "计算机组成原理");
strcpy(books[num_books].author, "唐朔飞");
books[num_books].available = 1;
num_books++;
int user_id = -1;
while (1) {
printf("请选择操作:\n");
printf("1. 注册\n");
printf("2. 登录\n");
printf("3. 借书\n");
printf("4. 还书\n");
printf("5. 查看借阅详情\n");
printf("6. 退出系统\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
register_user();
break;
case 2:
user_id = login();
break;
case 3:
if (user_id == -1) {
printf("请先登录!\n");
break;
}
borrow_book(user_id);
break;
case 4:
if (user_id == -1) {
printf("请先登录!\n");
break;
}
return_book(user_id);
break;
case 5:
if (user_id == -1) {
printf("请先登录!\n");
break;
}
view_borrowed_books(user_id);
break;
case 6:
printf("退出系统!\n");
return 0;
default:
printf("输入错误!\n");
break;
}
}
}
```
注意:以上代码只是一个简单的示例,可能存在一些缺陷和不足,实际开发中需要根据具体需求进行修改和完善。
相关推荐
![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)
![](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)
![](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)