使用C语言编写一个图书馆管理系统
时间: 2023-07-20 11:29:24 浏览: 54
好的,以下是一个简单的图书馆管理系统的C语言代码示例,其中包含添加、查找、显示和删除图书等基本功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100 // 最大书籍数量
// 书籍结构体
typedef struct {
char title[50];
char author[20];
char publisher[20];
char ISBN[20];
} Book;
// 全局变量
Book books[MAX_BOOKS]; // 存储所有书籍的数组
int bookCount = 0; // 当前书籍数量
// 添加书籍函数
void addBook() {
if (bookCount >= MAX_BOOKS) {
printf("书籍数量已达到上限,无法添加新书籍!\n");
return;
}
Book newBook;
printf("请输入书籍信息:\n");
printf("书名:");
scanf("%s", newBook.title);
printf("作者:");
scanf("%s", newBook.author);
printf("出版社:");
scanf("%s", newBook.publisher);
printf("ISBN:");
scanf("%s", newBook.ISBN);
books[bookCount] = newBook;
bookCount++;
printf("添加书籍成功!\n");
}
// 查找书籍函数
void findBook() {
char keyword[20];
printf("请输入关键词:");
scanf("%s", keyword);
int found = 0;
for (int i = 0; i < bookCount; i++) {
if (strstr(books[i].title, keyword) || strstr(books[i].author, keyword) || strstr(books[i].publisher, keyword) || strstr(books[i].ISBN, keyword)) {
printf("第 %d 本书:\n", i + 1);
printf("书名:%s\n", books[i].title);
printf("作者:%s\n", books[i].author);
printf("出版社:%s\n", books[i].publisher);
printf("ISBN:%s\n", books[i].ISBN);
found = 1;
}
}
if (!found) {
printf("未找到相关书籍!\n");
}
}
// 显示书籍函数
void showBooks() {
if (bookCount == 0) {
printf("当前没有任何书籍!\n");
return;
}
printf("当前库存书籍如下:\n");
for (int i = 0; i < bookCount; i++) {
printf("第 %d 本书:\n", i + 1);
printf("书名:%s\n", books[i].title);
printf("作者:%s\n", books[i].author);
printf("出版社:%s\n", books[i].publisher);
printf("ISBN:%s\n", books[i].ISBN);
}
}
// 删除书籍函数
void deleteBook() {
char ISBN[20];
printf("请输入要删除的书籍的ISBN:");
scanf("%s", ISBN);
int found = 0;
for (int i = 0; i < bookCount; i++) {
if (strcmp(books[i].ISBN, ISBN) == 0) {
for (int j = i; j < bookCount - 1; j++) {
books[j] = books[j + 1];
}
bookCount--;
found = 1;
printf("删除成功!\n");
break;
}
}
if (!found) {
printf("未找到要删除的书籍!\n");
}
}
// 主函数
int main() {
int choice = 0;
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:
addBook();
break;
case 2:
findBook();
break;
case 3:
showBooks();
break;
case 4:
deleteBook();
break;
case 5:
printf("感谢使用图书馆管理系统!\n");
return 0;
default:
printf("无效的选择,请重新输入!\n");
}
}
return 0;
}
```
此代码仅供参考,您可以根据自己的需求进行修改和完善。