用C语言写一个图书管理系统
时间: 2023-10-03 10:06:54 浏览: 24
好的,下面是一个简单的图书管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
// 定义一个结构体表示书籍信息
struct book {
char title[50];
char author[50];
int year;
};
// 定义一个全局变量books表示所有书籍
struct book books[MAX_BOOKS];
// 定义一个全局变量count表示书籍数量
int count = 0;
// 添加书籍
void add_book() {
if (count >= MAX_BOOKS) {
printf("图书馆已满,无法添加书籍!\n");
return;
}
struct book new_book;
printf("请输入书名:");
scanf("%s", new_book.title);
printf("请输入作者:");
scanf("%s", new_book.author);
printf("请输入出版年份:");
scanf("%d", &new_book.year);
books[count] = new_book;
count++;
printf("添加书籍成功!\n");
}
// 查找书籍
void search_book() {
char keyword[50];
printf("请输入关键字:");
scanf("%s", keyword);
int found = 0;
for (int i = 0; i < count; i++) {
if (strstr(books[i].title, keyword) != NULL ||
strstr(books[i].author, keyword) != NULL) {
printf("书名:%s,作者:%s,出版年份:%d\n",
books[i].title, books[i].author, books[i].year);
found = 1;
}
}
if (!found) {
printf("没有找到相关书籍!\n");
}
}
// 显示所有书籍
void show_books() {
if (count == 0) {
printf("图书馆为空!\n");
return;
}
printf("所有书籍如下:\n");
for (int i = 0; i < count; i++) {
printf("书名:%s,作者:%s,出版年份:%d\n",
books[i].title, books[i].author, books[i].year);
}
}
// 删除书籍
void delete_book() {
char title[50];
printf("请输入要删除的书籍名称:");
scanf("%s", title);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(books[i].title, title) == 0) {
for (int j = i; j < count - 1; j++) {
books[j] = books[j + 1];
}
count--;
found = 1;
printf("删除书籍成功!\n");
break;
}
}
if (!found) {
printf("没有找到要删除的书籍!\n");
}
}
// 主函数
int main() {
int choice;
while (1) {
printf("\n");
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:
search_book();
break;
case 3:
show_books();
break;
case 4:
delete_book();
break;
case 5:
printf("谢谢使用!\n");
exit(0);
default:
printf("无效的操作!\n");
break;
}
}
return 0;
}
```
这个程序实现了基本的图书管理功能,包括添加、查找、显示和删除书籍。你可以根据自己的需要进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![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)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)