图书管理系统c语言
时间: 2023-07-03 13:05:11 浏览: 144
一个图书管理系统需要完成的主要功能包括:图书信息的添加、修改、删除、查询等;借阅信息的添加、归还、查询等。
下面是一个基于C语言的简单图书管理系统示例,包含添加图书、查看图书、借阅图书和归还图书等功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOOK_NUM 20 // 最大图书数量
#define BORROW_NUM 50 // 最大借阅数量
// 图书信息结构体
typedef struct {
char title[50]; // 标题
char author[20]; // 作者
float price; // 价格
int book_id; // 图书编号
} Book;
// 借阅信息结构体
typedef struct {
int book_id; // 图书编号
char name[20]; // 借阅人姓名
char borrow_date[20]; // 借阅日期
char return_date[20]; // 归还日期
} Borrow;
Book books[BOOK_NUM]; // 图书数组
Borrow borrows[BORROW_NUM]; // 借阅数组
int book_count = 0; // 图书数量
int borrow_count = 0; // 借阅数量
// 添加图书信息
void add_book() {
if (book_count >= BOOK_NUM) {
printf("图书数量已达上限,无法添加!\n");
return;
}
printf("请输入图书信息:\n");
printf("书名:");
scanf("%s", books[book_count].title);
printf("作者:");
scanf("%s", books[book_count].author);
printf("价格:");
scanf("%f", &books[book_count].price);
books[book_count].book_id = book_count + 1;
book_count++;
printf("添加成功!\n");
}
// 查看所有图书信息
void view_books() {
if (book_count == 0) {
printf("暂无图书信息!\n");
return;
}
printf("所有图书信息:\n");
printf("编号\t书名\t作者\t价格\n");
for (int i = 0; i < book_count; i++) {
printf("%d\t%s\t%s\t%.2f\n", books[i].book_id, books[i].title, books[i].author, books[i].price);
}
}
// 查找图书信息
int find_book(int book_id) {
for (int i = 0; i < book_count; i++) {
if (books[i].book_id == book_id) {
return i;
}
}
return -1;
}
// 借阅图书
void borrow_book() {
if (borrow_count >= BORROW_NUM) {
printf("借阅数量已达上限,无法借阅!\n");
return;
}
int book_id;
printf("请输入要借阅的图书编号:");
scanf("%d", &book_id);
int index = find_book(book_id);
if (index == -1) {
printf("未找到该图书信息!\n");
return;
}
if (borrows[index].book_id != 0) {
printf("该图书已被借阅!\n");
return;
}
printf("请输入借阅人姓名:");
scanf("%s", borrows[borrow_count].name);
printf("请输入借阅日期:");
scanf("%s", borrows[borrow_count].borrow_date);
borrows[borrow_count].book_id = book_id;
borrow_count++;
printf("借阅成功!\n");
}
// 归还图书
void return_book() {
int book_id;
printf("请输入要归还的图书编号:");
scanf("%d", &book_id);
int index = find_book(book_id);
if (index == -1) {
printf("未找到该图书信息!\n");
return;
}
if (borrows[index].book_id == 0) {
printf("该图书未被借阅!\n");
return;
}
printf("请输入归还日期:");
scanf("%s", borrows[index].return_date);
borrows[index].book_id = 0;
printf("归还成功!\n");
}
int main() {
int choice;
while (1) {
printf("图书管理系统\n");
printf("1. 添加图书\n");
printf("2. 查看所有图书\n");
printf("3. 借阅图书\n");
printf("4. 归还图书\n");
printf("5. 退出系统\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
view_books();
break;
case 3:
borrow_book();
break;
case 4:
return_book();
break;
case 5:
printf("感谢使用图书管理系统!\n");
exit(0);
default:
printf("无效操作,请重新输入!\n");
}
}
}
```
注意,这只是一个简单的示例,实际的图书管理系统需要更复杂的功能和更完善的设计。