写一个c语言图书管理系统
时间: 2023-07-13 17:35:22 浏览: 62
好的,下面是一个简单的图书管理系统的 C 代码示例:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100 // 最大书籍数量
#define MAX_TITLE_LEN 50 // 书名最大长度
// 书籍结构体
struct Book {
char title[MAX_TITLE_LEN]; // 书名
int id; // 书籍编号
int quantity; // 库存数量
};
// 函数声明
void addBook(struct Book books[], int *count);
void removeBook(struct Book books[], int *count);
void searchBook(struct Book books[], int count);
void displayBooks(struct Book books[], int count);
int main() {
struct Book books[MAX_BOOKS]; // 存储所有的书籍
int count = 0; // 记录当前书籍数量
int choice; // 用户选择的操作
printf("欢迎使用图书管理系统\n");
// 循环读取用户输入的操作
while (1) {
printf("\n请选择操作:\n");
printf("1. 添加书籍\n");
printf("2. 删除书籍\n");
printf("3. 查找书籍\n");
printf("4. 显示所有书籍\n");
printf("5. 退出\n");
printf("输入数字选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook(books, &count);
break;
case 2:
removeBook(books, &count);
break;
case 3:
searchBook(books, count);
break;
case 4:
displayBooks(books, count);
break;
case 5:
printf("谢谢使用!\n");
return 0;
default:
printf("无效的操作,请重新选择!\n");
break;
}
}
return 0;
}
// 添加书籍
void addBook(struct Book books[], int *count) {
if (*count >= MAX_BOOKS) {
printf("已达到最大书籍数量,无法继续添加!\n");
return;
}
printf("请输入书名:");
scanf("%s", books[*count].title);
printf("请输入书籍编号:");
scanf("%d", &books[*count].id);
printf("请输入库存数量:");
scanf("%d", &books[*count].quantity);
printf("成功添加一本书籍!\n");
(*count)++; // 数量加 1
}
// 删除书籍
void removeBook(struct Book books[], int *count) {
int id, i;
printf("请输入要删除的书籍编号:");
scanf("%d", &id);
for (i = 0; i < *count; i++) {
if (books[i].id == id) {
// 将最后一本书的信息覆盖在要删除的位置上
books[i] = books[(*count) - 1];
printf("成功删除一本书籍!\n");
(*count)--; // 数量减 1
return;
}
}
printf("没有找到要删除的书籍!\n");
}
// 查找书籍
void searchBook(struct Book books[], int count) {
int id, i;
printf("请输入要查找的书籍编号:");
scanf("%d", &id);
for (i = 0; i < count; i++) {
if (books[i].id == id) {
printf("书名:%s\n", books[i].title);
printf("书籍编号:%d\n", books[i].id);
printf("库存数量:%d\n", books[i].quantity);
return;
}
}
printf("没有找到要查找的书籍!\n");
}
// 显示所有书籍
void displayBooks(struct Book books[], int count) {
int i;
printf("共有 %d 本书:\n", count);
for (i = 0; i < count; i++) {
printf("书名:%s\n", books[i].title);
printf("书籍编号:%d\n", books[i].id);
printf("库存数量:%d\n", books[i].quantity);
printf("\n");
}
}
```
这个图书管理系统可以进行添加、删除、查找和显示书籍等操作。请根据自己的需求进行修改和完善。
相关推荐
![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://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)