用c语言小型图书管理系统
时间: 2023-12-11 09:32:05 浏览: 37
以下是一个简单的C语言小型图书管理系统的设计和实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100 // 最大图书数量
// 图书结构体
typedef struct {
char id[20]; // 图书编号
char name[50]; // 图书名称
double price; // 图书价格
} Book;
// 图书列表结构体
typedef struct {
Book books[MAX_BOOKS]; // 图书数组
int count; // 当前图书数量
BookList;
// 初始化图书列表
void init(BookList *list) {
list->count = 0;
}
// 添加图书
void add(BookList *list, Book book) {
if (list->count >= MAX_BOOKS) {
printf("图书数量已达上限,无法添加!\n");
return; }
list->books[list->count] = book;
list->count++;
printf("添加成功!\n");
}
// 删除图书
void removeBook(BookList *list, char *id) {
int index = -1;
for (int i = 0; i < list->count; i++) {
if (strcmp(list->books[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到指定图书,无法删除!\n");
return;
}
for (int i = index; i < list->count - 1; i++) {
list->books[i] = list->books[i + 1];
}
list->count--;
printf("删除成功!\n");
}
// 修改图书
void update(BookList *list, char *id, Book book) {
int index = -1;
for (int i = 0; i < list->count; i++) {
if (strcmp(list->books[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到指定图书,无法修改!\n");
return;
}
list->books[index] = book;
printf("修改成功!\n");
}
// 查询图书
void query(BookList *list, char *id) {
int index = -1;
for (int i = 0; i < list->count; i++) {
if (strcmp(list->books[i].id, id) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到指定图书!\n");
return;
}
printf("图书编号:%s\n", list->books[index].id);
printf("图书名称:%s\n", list->books[index].name);
printf("图书价格:%lf\n", list->books[index].price);
}
// 展示所有图书
void display(BookList *list) {
printf("图书编号\t图书名称\t图书价格\n");
for (int i = 0; i < list->count; i++) {
printf("%s\t%s\t%lf\n", list->books[i].id, list->books[i].name, list->books[i].price);
}
}
int main() {
BookList list;
init(&list);
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);
if (choice == 1) {
Book book;
printf("请输入图书编号:");
scanf("%s", book.id);
printf("请输入图书名称:");
scanf("%s", book.name);
printf("请输入图书价格:");
scanf("%lf", &book.price);
add(&list, book);
} else if (choice == 2) {
char id[20];
printf("请输入要删除的图书编号:");
scanf("%s", id);
removeBook(&list, id);
} else if (choice == 3) {
char id[20];
printf("请输入要修改的图书编号:");
scanf("%s", id);
Book book;
printf("请输入新的图书编号:");
scanf("%s", book.id);
printf("请输入新的图书名称:");
scanf("%s", book.name);
printf("请输入新的图书价格:");
scanf("%lf", &book.price);
update(&list, id, book);
} else if (choice == 4) {
char id[20];
printf("请输入要查询的图书编号:");
scanf("%s", id);
query(&list, id);
} else if (choice == 5) {
display(&list);
} else if (choice == 6) {
break;
} else {
printf("无效的选择!\n");
}
}
return 0;
}
```
相关推荐
![](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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)