图书借阅管理系统C语言
时间: 2023-07-09 17:31:09 浏览: 98
好的,下面是一个简单的图书借阅管理系统的C语言实现示例,包括借阅、归还、查询等基本功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_NAME 50
#define MAX_ID 10
typedef struct {
char book_id[MAX_ID];
char book_name[MAX_NAME];
int count;
int borrowed; // 已借出数量
} Book;
typedef struct {
char reader_id[MAX_ID];
char reader_name[MAX_NAME];
Book books[MAX_BOOKS];
int book_count;
} Reader;
Reader readers[MAX_BOOKS];
int reader_count = 0;
void add_reader() {
Reader reader;
printf("请输入读者ID:");
scanf("%s", reader.reader_id);
printf("请输入读者姓名:");
scanf("%s", reader.reader_name);
reader.book_count = 0;
readers[reader_count++] = reader;
printf("读者添加成功!\n");
}
void add_book() {
Book book;
printf("请输入图书ID:");
scanf("%s", book.book_id);
printf("请输入图书名称:");
scanf("%s", book.book_name);
printf("请输入图书总数:");
scanf("%d", &book.count);
book.borrowed = 0;
int i;
for (i = 0; i < reader_count; i++) {
readers[i].books[readers[i].book_count++] = book;
}
printf("图书添加成功!\n");
}
void borrow_book() {
char reader_id[MAX_ID];
char book_id[MAX_ID];
printf("请输入读者ID:");
scanf("%s", reader_id);
printf("请输入图书ID:");
scanf("%s", book_id);
int i, j;
for (i = 0; i < reader_count; i++) {
if (strcmp(readers[i].reader_id, reader_id) == 0) {
for (j = 0; j < readers[i].book_count; j++) {
if (strcmp(readers[i].books[j].book_id, book_id) == 0) {
if (readers[i].books[j].borrowed >= readers[i].books[j].count) {
printf("该图书已全部借出!\n");
return;
} else {
readers[i].books[j].borrowed++;
printf("借阅成功!\n");
return;
}
}
}
printf("未找到该图书!\n");
return;
}
}
printf("未找到该读者!\n");
}
void return_book() {
char reader_id[MAX_ID];
char book_id[MAX_ID];
printf("请输入读者ID:");
scanf("%s", reader_id);
printf("请输入图书ID:");
scanf("%s", book_id);
int i, j;
for (i = 0; i < reader_count; i++) {
if (strcmp(readers[i].reader_id, reader_id) == 0) {
for (j = 0; j < readers[i].book_count; j++) {
if (strcmp(readers[i].books[j].book_id, book_id) == 0) {
if (readers[i].books[j].borrowed <= 0) {
printf("该图书未被借出!\n");
return;
} else {
readers[i].books[j].borrowed--;
printf("归还成功!\n");
return;
}
}
}
printf("未找到该图书!\n");
return;
}
}
printf("未找到该读者!\n");
}
void list_books() {
char reader_id[MAX_ID];
printf("请输入读者ID:");
scanf("%s", reader_id);
int i, j;
for (i = 0; i < reader_count; i++) {
if (strcmp(readers[i].reader_id, reader_id) == 0) {
printf("图书ID\t\t图书名称\t\t总数\t\t已借出数量\n");
for (j = 0; j < readers[i].book_count; j++) {
printf("%s\t\t%s\t\t%d\t\t%d\n", readers[i].books[j].book_id, readers[i].books[j].book_name, readers[i].books[j].count, readers[i].books[j].borrowed);
}
return;
}
}
printf("未找到该读者!\n");
}
void menu() {
printf("1. 添加读者\n");
printf("2. 添加图书\n");
printf("3. 借阅图书\n");
printf("4. 归还图书\n");
printf("5. 查看图书\n");
printf("6. 退出\n");
printf("请选择操作:");
}
int main() {
while (1) {
menu();
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_reader();
break;
case 2:
add_book();
break;
case 3:
borrow_book();
break;
case 4:
return_book();
break;
case 5:
list_books();
break;
case 6:
return 0;
default:
printf("无效操作,请重新选择!\n");
}
}
return 0;
}
```
以上是一个简单的图书借阅管理系统的C语言实现示例,包含了添加读者、添加图书、借阅图书、归还图书、查看图书等基本功能。希望对您有所帮助!
阅读全文