题目三:图书管理系统设计
图书管理信息包括:图书名称、图书编号、单价、作者、存在状态、借书人姓名、性别、学号等。
主要功能:
- 新进图书基本信息的输入。
- 图书基本信息的查询。
- 对撤销图书信息的删除。
- 为借书人办理注册。
- 办理借书手续。
- 办理还书手续
要求: 使用二进制文件方式存储数据,系统以菜单方式工作。
图书基本信息的查询
首先展示所有图书信息,再提供两个选项:1,根据图书名称查询图书;2,根据作者查询图书信息
booksManagementSystem.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_PEOPLE 100
#define BOOKS_FILE_NAME "books.bin"
#define PEOPLE_FILE_NAME "people.bin"
// 定义图书结构体
typedef struct {
char name[50]; // 图书名称
int number; // 图书编号
double price; // 单价
char author[50]; // 作者
int status; // 存在状态:0 表示可借,1 表示已借出
char borrowerName[50]; // 借书人姓名
char borrowerGender[10];// 借书人性别
int borrowerID; // 借书人学号
} Book;
// 定义借书人结构体
typedef struct {
char name[50]; // 姓名
char gender[10]; // 性别
int studentID; // 学号
} Person;
// 打印图书信息
void printBook(Book book);
// 打印借书人信息
void printPerson(Person person);
// 添加新图书的基本信息
void addBook(Book* books, int* count);
// 查询图书信息
void searchBook(Book* books, int count);
// 按书名查询图书信息
void searchBookByName(Book* books, int count);
// 按作者查询图书信息
void searchBookByAuthor(Book* books, int count);
// 删除指定编号的图书信息
void deleteBookByNumber(Book* books, int* count);
// 注册借书人信息
void registerBorrower(Person* people, int* count);
// 办理借书手续
void borrowBook(Book* books, int count, Person* people, int peopleCount);
// 办理还书手续
void returnBook(Book* books, int count);
// 查看所有借书人信息
void viewBorrowers(Person* people, int count);
// 将图书信息保存到文件
void saveBooksToFile(Book* books, int count);
// 从文件加载图书信息
void loadBooksFromFile(Book* books, int* count);
// 将借书人信息保存到文件
void savePeopleToFile(Person* people, int count);
// 从文件加载借书人信息
void loadPeopleFromFile(Person* people, int* count);
//菜单
void menu();
booksManagementSystem.c
#include"booksManagementSystem.h"
// 打印图书信息
void printBook(Book book) {
printf("图书名称: %s\n", book.name);
printf("图书编号: %d\n", book.number);
printf("单价: %.2f\n", book.price);
printf("作者: %s\n", book.author);
if (book.status == 0) {
printf("状态: 可借\n");
}
else {
printf("状态: 已借出\n");
printf("借书人姓名: %s\n", book.borrowerName);
printf("借书人性别: %s\n", book.borrowerGender);
printf("借书人学号: %d\n", book.borrowerID);
}
printf("\n");
}
// 打印借书人信息
void printPerson(Person person) {
printf("姓名: %s\n", person.name);
printf("性别: %s\n", person.gender);
printf("学号: %d\n", person.studentID);
printf("\n");
}
// 添加新图书的基本信息
void addBook(Book* books, int* count) {
if (*count >= MAX_BOOKS) {
printf("已达到最大容量,无法添加更多图书。\n");
return;
}
Book* book = &books[*count];
printf("请输入图书名称: ");
scanf("%s", book->name);
printf("请输入图书编号: ");
scanf("%d", &book->number);
printf("请输入单价: ");
scanf("%lf", &book->price);
printf("请输入作者: ");
scanf("%s", book->author);
book->status = 0; // 默认可借
book->borrowerName[0] = '\0';
book->borrowerGender[0] = '\0';
book->borrowerID = 0;
(*count)++;
printf("图书信息录入成功\n");
}
// 查询图书信息
void searchBook(Book* books, int count) {
printf("\n所有图书信息:\n");
for (int i = 0; i < count; i++) {
printBook(books[i]);
}
int choice;
printf("\n请选择查询方式 (1-2):\n");
printf("1. 根据图书名称查询\n");
printf("2. 根据作者查询\n");
printf("按其他为无效选择,返回上级菜单。\n");
printf("请选择 (1-2): ");
scanf("%d", &choice);
switch (choice) {
case 1:
searchBookByName(books, count);
break;
case 2:
searchBookByAuthor(books, count);
break;
default:
printf("无效的选择,请重新选择。\n");
}
}
// 按书名查询图书信息
void searchBookByName(Book* books, int count) {
char name[50];
printf("请输入书名进行查询: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(books[i].name, name) == 0) {
printBook(books[i]);
found = 1;
}
}
if (!found) {
printf("没有找到符合条件的图书。\n");
}
}
// 按作者查询图书信息
void searchBookByAuthor(Book* books, int count) {
char author[50];
printf("请输入作者进行查询: ");
scanf("%s", author);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(books[i].author, author) == 0) {
printBook(books[i]);
found = 1;
}
}
if (!found) {
printf("没有找到符合条件的图书。\n");
}
}
// 删除指定编号的图书信息
void deleteBookByNumber(Book* books, int* count) {
int number;
printf("请输入要删除的图书编号: ");
scanf("%d", &number);
int deleted = 0;
for (int i = 0; i < *count; i++) {
if (books[i].number == number) {
for (int j = i; j < *count - 1; j++) {
books[j] = books[j + 1];
}
(*count)--;
printf("图书编号 %d 的图书信息已经删除。\n", number);
deleted = 1;
break;
}
}
if (!deleted) {
printf("没有找到符合条件的图书信息。\n");
}
}
// 注册借书人信息
void registerBorrower(Person* people, int* count) {
if (*count >= MAX_PEOPLE) {
printf("已达到最大容量,无法添加更多借书人。\n");
return;
}
Person* person = &people[*count];
printf("请输入借书人姓名: ");
scanf("%s", person->name);
printf("请输入借书人性别: ");
scanf("%s", person->gender);
printf("请输入借书人学号: ");
scanf("%d", &person->studentID);
(*count)++;
printf("借书人信息录入成功\n");
}
// 办理借书手续
void borrowBook(Book* books, int count, Person* people, int peopleCount) {
int number;
printf("请输入要借的图书编号: ");
scanf("%d", &number);
int found = 0;
for (int i = 0; i < count; i++) {
if (books[i].number == number && books[i].status == 0) {
printf("请输入借书人学号: ");
int studentID;
scanf("%d", &studentID);
int borrowerFound = 0;
for (int j = 0; j < peopleCount; j++) {
if (people[j].studentID == studentID) {
strcpy(books[i].borrowerName, people[j].name);
strcpy(books[i].borrowerGender, people[j].gender);
books[i].borrowerID = studentID;
books[i].status = 1;
printf("借书成功\n");
found = 1;
borrowerFound = 1;
break;
}
}
if (!borrowerFound) {
printf("没有找到该借书人信息。\n");
}
break;
}
}
if (!found) {
printf("没有找到符合条件的图书或图书已借出。\n");
}
}
// 办理还书手续
void returnBook(Book* books, int count) {
int number;
printf("请输入要还的图书编号: ");
scanf("%d", &number);
int found = 0;
for (int i = 0; i < count; i++) {
if (books[i].number == number && books[i].status == 1) {
books[i].status = 0;
books[i].borrowerName[0] = '\0';
books[i].borrowerGender[0] = '\0';
books[i].borrowerID = 0;
printf("还书成功\n");
found = 1;
break;
}
}
if (!found) {
printf("没有找到符合条件的图书或图书未借出。\n");
}
}
// 查看所有借书人信息
void viewBorrowers(Person* people, int count) {
if (count == 0) {
printf("目前没有借书人信息。\n");
return;
}
printf("借书人信息如下:\n");
for (int i = 0; i < count; i++) {
printPerson(people[i]);
}
}
// 将图书信息保存到文件
void saveBooksToFile(Book* books, int count) {
FILE* fp = fopen(BOOKS_FILE_NAME, "wb");
if (fp == NULL) {
printf("打开文件失败。\n");
return;
}
fwrite(books, sizeof(Book), count, fp);
fclose(fp);
printf("图书信息保存成功。\n");
}
// 从文件加载图书信息
void loadBooksFromFile(Book* books, int* count) {
FILE* fp = fopen(BOOKS_FILE_NAME, "rb");
if (fp == NULL) {
printf("打开文件失败。\n");
return;
}
fseek(fp, 0, SEEK_END);
*count = ftell(fp) / sizeof(Book);
fseek(fp, 0, SEEK_SET);
fread(books, sizeof(Book), *count, fp);
fclose(fp);
printf("图书信息加载成功。\n");
}
// 将借书人信息保存到文件
void savePeopleToFile(Person* people, int count) {
FILE* fp = fopen(PEOPLE_FILE_NAME, "wb");
if (fp == NULL) {
printf("打开借书人文件失败。\n");
return;
}
fwrite(people, sizeof(Person), count, fp);
fclose(fp);
printf("借书人信息保存成功。\n");
}
// 从文件加载借书人信息
void loadPeopleFromFile(Person* people, int* count) {
FILE* fp = fopen(PEOPLE_FILE_NAME, "rb");
if (fp == NULL) {
printf("打开借书人文件失败。\n");
return;
}
fseek(fp, 0, SEEK_END);
*count = ftell(fp) / sizeof(Person);
fseek(fp, 0, SEEK_SET);
fread(people, sizeof(Person), *count, fp);
fclose(fp);
printf("借书人信息加载成功。\n");
}
void menu() {
int choice;
int bookCount = 0;
int peopleCount = 0;
Book books[MAX_BOOKS];
Person people[MAX_PEOPLE];
loadBooksFromFile(books, &bookCount);
loadPeopleFromFile(people, &peopleCount);
do {
printf("\n");
printf("+---------------------------+\n");
printf("| 图书管理系统 |\n");
printf("+---------------------------+\n");
printf("| 1. 新进图书基本信息的输入 |\n");
printf("| 2. 图书基本信息的查询 |\n");
printf("| 3. 对撤销图书信息的删除 |\n");
printf("| 4. 为借书人办理注册 |\n");
printf("| 5. 办理借书手续 |\n");
printf("| 6. 办理还书手续 |\n");
printf("| 7. 查看借书人信息 |\n");
printf("| 8. 保存并退出 |\n");
printf("+---------------------------+\n");
printf("请选择操作 (1-8): ");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook(books, &bookCount);
break;
case 2:
searchBook(books, bookCount);
break;
case 3:
deleteBookByNumber(books, &bookCount);
break;
case 4:
registerBorrower(people, &peopleCount);
break;
case 5:
borrowBook(books, bookCount, people, peopleCount);
break;
case 6:
returnBook(books, bookCount);
break;
case 7:
viewBorrowers(people, peopleCount);
break;
case 8:
saveBooksToFile(books, bookCount);
savePeopleToFile(people, peopleCount);
printf("程序退出。\n");
break;
default:
printf("无效的选择,请重新选择。\n");
}
} while (choice != 8);
}
test.c
#include"booksManagementSystem.h"
int main() {
menu();
return 0;
}
删除
注册,借书,还书
补充查询借书人信息,即借书人信息的保存和读取