用c语言写出图书管理系统包括录入书籍速览书籍借阅书籍归还书籍查找书籍删除书籍退出系统
时间: 2023-08-31 19:11:26 浏览: 121
下面是一个简单的图书管理系统的C语言代码示例,包括录入书籍、速览书籍、借阅书籍、归还书籍、查找书籍、删除书籍和退出系统的功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char title[100];
char author[50];
int book_id;
int is_available;
} books[MAX_BOOKS];
int book_count = 0;
void add_book() {
if (book_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.book_id);
new_book.is_available = 1; // 默认可借阅
books[book_count++] = new_book;
printf("添加成功!\n");
}
void display_books() {
printf("图书信息如下:\n");
printf("书名\t作者\t编号\t状态\n");
for (int i = 0; i < book_count; i++) {
printf("%s\t%s\t%d\t%s\n", books[i].title, books[i].author, books[i].book_id, books[i].is_available ? "可借阅" : "已借出");
}
}
void borrow_book() {
printf("请输入要借阅的书籍编号:");
int book_id;
scanf("%d", &book_id);
for (int i = 0; i < book_count; i++) {
if (books[i].book_id == book_id) {
if (books[i].is_available) {
books[i].is_available = 0;
printf("借阅成功!\n");
} else {
printf("该书已借出!\n");
}
return;
}
}
printf("未找到对应书籍!\n");
}
void return_book() {
printf("请输入要归还的书籍编号:");
int book_id;
scanf("%d", &book_id);
for (int i = 0; i < book_count; i++) {
if (books[i].book_id == book_id) {
if (!books[i].is_available) {
books[i].is_available = 1;
printf("归还成功!\n");
} else {
printf("该书未借出!\n");
}
return;
}
}
printf("未找到对应书籍!\n");
}
void search_book() {
printf("请输入要查找的书籍编号:");
int book_id;
scanf("%d", &book_id);
for (int i = 0; i < book_count; i++) {
if (books[i].book_id == book_id) {
printf("书名:%s\n", books[i].title);
printf("作者:%s\n", books[i].author);
printf("编号:%d\n", books[i].book_id);
printf("状态:%s\n", books[i].is_available ? "可借阅" : "已借出");
return;
}
}
printf("未找到对应书籍!\n");
}
void delete_book() {
printf("请输入要删除的书籍编号:");
int book_id;
scanf("%d", &book_id);
for (int i = 0; i < book_count; i++) {
if (books[i].book_id == book_id) {
for (int j = i; j < book_count - 1; j++) {
books[j] = books[j + 1];
}
book_count--;
printf("删除成功!\n");
return;
}
}
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("6. 删除书籍\n");
printf("7. 退出系统\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
display_books();
break;
case 3:
borrow_book();
break;
case 4:
return_book();
break;
case 5:
search_book();
break;
case 6:
delete_book();
break;
case 7:
exit(0);
default:
printf("无效的选择!\n");
break;
}
}
return 0;
}
```
这个程序在基本的图书管理系统的基础上增加了查找书籍和删除书籍的功能。查找书籍时会让用户输入要查找的书籍编号,然后查找该编号对应的书籍,并输出该书籍的信息。删除书籍时会让用户输入要删除的书籍编号,然后查找该编号对应的书籍,并将其从结构体数组中删除。
阅读全文