#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_NAME 50
// 定义图书结构体
typedef struct {
int ID;//编号
char fenlei[MAX_NAME];//分类
char title[MAX_NAME];//书名
char author[MAX_NAME];//作者
char publisher[MAX_NAME];//出版社
int quantity;//数量
float price;//单价
} Book;
Book books[MAX_BOOKS];
int numBook = 0;//数量
// 函数声明
void readBookData(Book books[]);
void displayMenu(Book books[]);
void searchByTitle(Book books[]);
void displayAllBooks(Book books[]);
void sortByQuantity(Book books[]);
void calculateTotalPrices(Book books[]);
void modifyBookData(Book books[]);
void borrowBook(Book books[]);
void AddBook(Book* book);
void WriteFile(Book* books);
int main() {
Book books[MAX_BOOKS];
readBookData(books);
displayMenu(books);
return 0;
}
//写入文件
void WriteFile(Book* books){
// 将修改后的图书写入文件
FILE *file = fopen("D:\\JK0112.txt", "w");
if (file == NULL) {
printf("无法打开文件!\n");
return;
}
for (int i = 0; i < numBook; i++) {
fprintf(file, "%d %s %s %s %s %d %f\n", books[i].ID, books[i].fenlei, books[i].title, books[i].author,
books[i].publisher,
books[i].quantity, books[i].price);
}
fclose(file);
printf("图书信息已保存。\n");
}
//添加图书
void AddBook(Book* book){
printf("输入编号:");
scanf("%d", &book[numBook].ID);
for(int i = 0; i < numBook; i++){
if(book[i].ID == book[numBook].ID) {
printf("编号重复,添加失败\n");
return;
}
}
printf("输入分类:");
scanf("%s", book[numBook].fenlei);
printf("输入书名:");
scanf("%s", book[numBook].title);
printf("输入作者名:");
scanf("%s", book[numBook].author);
printf("输入出版社名:");
scanf("%s", book[numBook].publisher);
printf("输入数量:");
scanf("%d", &book[numBook].quantity);
printf("输入单价:");
scanf("%f", &book[numBook].price);
printf("添加成功!\n");
numBook++;
printf("%d\n", numBook);
}
// 从文件中读取图书数据
void readBookData(Book books[]) {
FILE *file = fopen("D:\\JK0112.txt", "r");
if (file == NULL) {
printf("无法打开文件!\n");
return;
}
numBook = 0;
// 读取文件中的数据并保存到结构体数组中
while (fscanf(file, "%d %s %s %s %s %d %f\n", &books[numBook].ID,books[numBook].fenlei,books[numBook].title, books[numBook].author, books[numBook].publisher,
&books[numBook].quantity, &books[numBook].price) != EOF) {
numBook++;
}
fclose(file);
}
// 显示菜单
void displayMenu(Book books[]) {
int choice;
do {
printf("\n======= 图书管理系统 =======\n");
printf("1. 添加图书\n");
printf("2. 按书名查询\n");
printf("3. 显示全部\n");
printf("4. 按图书数量排序\n");
printf("5. 统计图书总价\n");
printf("6. 修改图书信息\n");
printf("7. 借阅图书\n");
printf("0. 退出\n");
printf("===========================\n");
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
AddBook(books);
break;
case 2:
searchByTitle(books);
break;
case 3:
displayAllBooks(books);
break;
case 4:
sortByQuantity(books);
break;
case 5:
calculateTotalPrices(books);
break;
case 6:
modifyBookData(books);
break;
case 7:
borrowBook(books);
break;
case 0:
WriteFile(books);
printf("谢谢使用,再见!\n");
break;
default:
printf("无效的选项,请重试。\n");
break;
}
} while (choice != 0);
}
// 按书名查询并显示满足条件的记录
void searchByTitle(Book books[]) {
char searchTitle[MAX_NAME];
int i, found = 0;
printf("请输入待查询的书名:");
// fflush(stdin);
scanf("%s",searchTitle);
// gets(searchTitle);
printf("查询结果:\n");
printf("编号\t分类\t\t书名\t作者\t出版社\t\t数量\t价格\n");
for (i = 0; i < numBook; i++) {
if (strcmp(books[i].title, searchTitle) == 0) {
printf("%d\t%s\t%s\t%s\t%s\t%d\t\t%.2f", books[i].ID, books[i].fenlei, books[i].title, books[i].author,
books[i].publisher,
books[i].quantity, books[i].price);
found = 1;
}
}
if (!found) {
printf("未找到匹配的记录。\n");
}
}
// 显示全部记录
void displayAllBooks(Book books[]) {
int i;
printf("所有图书记录:\n");
printf("编号\t分类\t\t书名\t作者\t出版社\t\t数量\t价格\n");
for (i = 0; i < numBook; i++) {
printf("%d\t%s\t%s\t%s\t%s\t%d\t\t%.2f\n", books[i].ID, books[i].fenlei, books[i].title, books[i].author,
books[i].publisher,
books[i].quantity, books[i].price);
}
}
// 按图书数量排序
void sortByQuantity(Book books[]) {
int i, j;
Book temp;
// 使用冒泡排序按图书数量排序
for (i = 0; i < numBook - 1; i++) {
for (j = 0; j < numBook - i - 1; j++) {
if (books[j].quantity > books[j + 1].quantity) {
temp = books[j];
books[j] = books[j + 1];
books[j + 1] = temp;
}
}
}
printf("按图书数量排序后的结果:\n");
displayAllBooks(books);
}
// 统计每本书的总价及所有图书的总价
void calculateTotalPrices(Book books[]) {
int i;
float totalPrice = 0.0;
for (i = 0; i < numBook; i++) {
float bookPrice = books[i].quantity * books[i].price;
printf("书名:%s,总价:%.2f\n", books[i].title, bookPrice);
totalPrice += bookPrice;
}
printf("所有图书的总价为:%.2f\n", totalPrice);
}
// 修改图书信息
void modifyBookData(Book books[]) {
char searchTitle[MAX_NAME];
int i, found = 0;
printf("请输入要修改的书名:");
//fflush(stdin);
scanf("%s",searchTitle);
for (i = 0; i < numBook; i++) {
if (strcmp(books[i].title, searchTitle) == 0) {
printf("请输入要修改的字段名:\n");
printf("1. 分类\n");
printf("2. 书名\n");
printf("3. 作者\n");
printf("4. 出版社\n");
printf("5. 数量\n");
printf("6. 单价\n");
int choice;
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入新的分类:");
fflush(stdin);
gets(books[i].fenlei);
break;
case 2:
printf("请输入新的书名:");
fflush(stdin);
gets(books[i].title);
break;
case 3:
printf("请输入新的作者:");
fflush(stdin);
gets(books[i].author);
break;
case 4:
printf("请输入新的出版社:");
scanf("%s", books[i].publisher);
break;
case 5:
printf("请输入新的数量:");
scanf("%d", &books[i].quantity);
break;
case 6:
printf("请输入新的单价:");
scanf("%f", &books[i].price);
break;
default:
printf("无效的选项,请重试。\n");
break;
}
found = 1;
break;
}
}
if (!found) {
printf("未找到要修改的图书。\n");
} else {
// 将修改后的图书写入文件
WriteFile(books);
printf("图书信息已成功修改并保存。\n");
}
}
// 借阅图书
void borrowBook(Book books[]) {
char searchTitle[MAX_NAME];
int i, found = 0;
int count = 0;
printf("请输入要借阅的书名:");
scanf("%s",searchTitle);
for (i = 0; i < numBook; i++) {
if (strcmp(books[i].title, searchTitle) == 0) {
found = 1;
count++;
break;
}
}
if (found&&count<=numBook) {
printf("成功借阅图书:%s\n", books[i].title);
} else {
printf("无法借阅。\n");
}
}
[C/C++]结课作业-图书管理系统
于 2023-06-20 15:53:18 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)