c语言大作业图书管理系统
时间: 2023-12-11 14:33:15 浏览: 50
以下是一个简单的C语言图书管理系统的示例,它包括添加图书、查询图书、删除图书、借书和还书等功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100 // 最大图书数量
// 图书结构体
typedef struct {
char name[50]; // 图书名称
char id[20]; // 图书编号
float price; // 单价
char author[50]; // 作者
int status; // 存在状态,0表示未借出,1表示已借出
char borrower[50]; // 借书人姓名
char gender[10]; // 借书人性别
char student_id[20]; // 借书人学号
} Book;
Book books[MAX_BOOKS]; // 图书数组
int num_books = 0; // 当前图书数量
// 添加图书
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("图书数量已达到上限,无法添加新图书!\n");
return;
}
Book book;
printf("请输入图书名称:");
scanf("%s", book.name);
printf("请输入图书编号:");
scanf("%s", book.id);
printf("请输入图书单价:");
scanf("%f", &book.price);
printf("请输入图书作者:");
scanf("%s", book.author);
book.status = 0; // 新添加的图书状态默认为未借出
strcpy(book.borrower, "");
strcpy(book.gender, "");
strcpy(book.student_id, "");
books[num_books++] = book;
printf("添加图书成功!\n");
}
// 查询图书
void search_book() {
char id[20];
printf("请输入要查询的图书编号:");
scanf("%s", id);
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].id, id) == 0) {
printf("图书名称:%s\n", books[i].name);
printf("图书编号:%s\n", books[i].id);
printf("图书单价:%.2f\n", books[i].price);
printf("图书作者:%s\n", books[i].author);
if (books[i].status == 0) {
printf("图书状态:未借出\n");
} else {
printf("图书状态:已借出\n");
printf("借书人姓名:%s\n", books[i].borrower);
printf("借书人性别:%s\n", books[i].gender);
printf("借书人学号:%s\n", books[i].student_id);
}
return;
}
}
printf("未找到该图书!\n");
}
// 删除图书
void delete_book() {
char id[20];
printf("请输入要删除的图书编号:");
scanf("%s", id);
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].id, id) == 0) {
for (int j = i; j < num_books - 1; j++) {
books[j] = books[j + 1];
}
num_books--;
printf("删除图书成功!\n");
return;
}
}
printf("未找到该图书!\n");
}
// 借书
void borrow_book() {
char id[20];
printf("请输入要借阅的图书编号:");
scanf("%s", id);
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].id, id) == 0) {
if (books[i].status == 1) {
printf("该图书已被借出!\n");
return;
}
printf("请输入借书人姓名:");
scanf("%s", books[i].borrower);
printf("请输入借书人性别:");
scanf("%s", books[i].gender);
printf("请输入借书人学号:");
scanf("%s", books[i].student_id);
books[i].status = 1;
printf("借书成功!\n");
return;
}
}
printf("未找到该图书!\n");
}
// 还书
void return_book() {
char id[20];
printf("请输入要归还的图书编号:");
scanf("%s", id);
for (int i = 0; i < num_books; i++) {
if (strcmp(books[i].id, id) == 0) {
if (books[i].status == 0) {
printf("该图书未被借出!\n");
return;
}
books[i].status = 0;
strcpy(books[i].borrower, "");
strcpy(books[i].gender, "");
strcpy(books[i].student_id, "");
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("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
search_book();
break;
case 3:
delete_book();
break;
case 4:
borrow_book();
break;
case 5:
return_book();
break;
case 6:
exit(0);
default:
printf("无效的操作编号,请重新输入!\n");
break;
}
}
return 0;
}
```