实现环境:VS2022
设计思想:
1.本系统实现了图书馆管理的图书管理模块、读者管理模块、以及借书还书模块
2.图书管理模块实现了对图书的增删改查以及显示信息、读者管理模块实现了对读者信息的增删改查和显示信息、借书还书模块实现了读者的借书和还书功能
设计过程:
本系统所用到的宏定义:
#define BOOK_DATA astBook[i].iNum,astBook[i].acName,astBook[i].acAuthor,astBook[i].acPress,astBook[i].iAmout
#define READER_DATA astReader[i].iNum,astReader[i].acName,astReader[i].acSex,astReader[i].iMax,astReader[i].iAmout
#define BOOK_NUM 100
#define READER_NUM 100
#define LEN_BOOK sizeof(struct Book)
#define LEN_READER sizeof(struct Reader)
#define COUNT astReader[i].iMax-astReader[i].iAmout//读者当前已借阅图书数目
#define IBORROW astReader[iReaderId].iMax-astReader[iReaderId].iAmout//读者当前已借阅数
定义两个存储图书和读者信息的结构体:
//定义两个全局结构体数组用来记录所有的图书和读者信息
struct Book astBook[BOOK_NUM];
struct Reader astReader[READER_NUM];
//这里的BOOK_NUM和READER_NUM是两个宏定义,你也可以自行设计大小
图书信息:
//图书信息
struct Book
{
int iNum;//图书编号
char acName[15];//图书名称
char acAuthor[15];//图书作者
char acPress[15];//图书的出版社
int iAmout;//图书的库存量
};
读者信息:
//读者信息
struct Reader
{
int iNum;//读者编号
char acName[15];//读者姓名
char acSex[4];//读者性别
int iMax;//读者最大的可借阅数量
int iAmout;//读者当前可借阅数量
int BookId[10];//读者已经借阅的读书列表
}
代码实现:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<Windows.h>
#include<conio.h>
#define BOOK_DATA astBook[i].iNum,astBook[i].acName,astBook[i].acAuthor,astBook[i].acPress,astBook[i].iAmout
#define READER_DATA astReader[i].iNum,astReader[i].acName,astReader[i].acSex,astReader[i].iMax,astReader[i].iAmout
#define BOOK_NUM 100
#define READER_NUM 100
#define LEN_BOOK sizeof(struct Book)
#define LEN_READER sizeof(struct Reader)
#define COUNT astReader[i].iMax-astReader[i].iAmout//读者当前已借阅图书数目
#define IBORROW astReader[iReaderId].iMax-astReader[iReaderId].iAmout//读者当前已借阅数
//图书信息
struct Book
{
int iNum;//图书编号
char acName[15];//图书名称
char acAuthor[15];//图书作者
char acPress[15];//图书的出版社
int iAmout;//图书的库存量
};
//读者信息
struct Reader
{
int iNum;//读者编号
char acName[15];//读者姓名
char acSex[4];//读者性别
int iMax;//读者最大的可借阅数量
int iAmout;//读者当前可借阅数量
int BookId[10];//读者已经借阅的读书列表
};
//定义两个全局结构体数组用来记录所有的图书和读者信息
struct Book astBook[BOOK_NUM];
struct Reader astReader[READER_NUM];
void ShowMainMenu()//显示主函数的界面(图书馆管理系统的主界面)
{
system("cls");
printf("\n\n\n\n\n\n");
printf("\t\t|*********************************|\n");
printf("\t\t|*************欢迎进入************|\n");
printf("\t\t|**********图书馆管理系统*********|\n");
printf("\t\t|*************主菜单**************|\n");
printf("\t\t|**********1.图书管理*************|\n");
printf("\t\t|**********2.读者管理*************|\n");
printf("\t\t|**********3.借书还书登记*********|\n");
printf("\t\t|**********0.退出系统*************|\n");
printf("\t\t|*********************************|\n");
printf("\t\t请选择:0-3\n");
}
void ShowBookMenu()//显示图书管理的子菜单
{
system("cls");
printf("\n\n\n\n\n\n");
printf("\t\t|*********************************|\n");
printf("\t\t|*************欢迎进入************|\n");
printf("\t\t|**********图书馆管理系统*********|\n");
printf("\t\t|*************子菜单**************|\n");
printf("\t\t|**********图书管理模块***********|\n");
printf("\t\t|**********1.显示图书信息*********|\n");
printf("\t\t|**********2.增加图书信息*********|\n");
printf("\t\t|**********3.查询图书信息*********|\n");
printf("\t\t|**********4.删除图书信息*********|\n");
printf("\t\t|**********5.修改图书信息*********|\n");
printf("\t\t|**********0.返回主菜单***********|\n");
printf("\t\t|*********************************|\n");
printf("\t\t请选择:0-5\n");
}
//把图书信息从文件读取到数组中
int ReadBookFile(const char* pcMode)
{
int iBookRecord = 0;//文件中的记录条数
FILE* pfBook;
pfBook=fopen("book.txt", pcMode);
if (pfBook == NULL)
{
//printf("文件打开失败!\n");
return -1;
}
while (!feof(pfBook))//feof:读到文件尾
{
if (fread(&astBook[iBookRecord], LEN_BOOK, 1, pfBook))
{
iBookRecord++;
}
}
fclose(pfBook);
return iBookRecord;
}
void ShowBook()//显示图书信息
{
int i, iBookRecord;//信息数(条)
system("cls");
iBookRecord = ReadBookFile("rb");
if (iBookRecord == -1)
{
printf("该文件打开失败,请先新增图书信息!\n");
}
else if (iBookRecord == 0)
{
printf("文件中没有图书信息!\n");
}
else
{
printf("\t|**********************|\n");
printf("\n\n");
printf("\t\t%-6s%-16s%-10s%-20s%-4s\n", "编号", "书名", "作者", "出版社", "库存量");
for (i = 0; i < iBookRecord; i++)
{
printf("\t\t%-6d%-16s%-10s%-20s%-4d\n", BOOK_DATA);
}
}
}
void AddBook()//新增图书信息
{
FILE* pfBook;
int iBookRecord;//文件中的记录条数
int iFlagExist;//图书是否存在
int i;
char cFlag;//是否输入图书信息的标志
system("cls");
iBookRecord = ReadBookFile("ab+");//ab+:以追加方式打开,或者新建二进制文件
if (iBookRecord == -1)
{
printf("文件打开失败!\n");
return;
}
else if (iBookRecord == 0)
{
printf("没有图书记录!\n");
}
else//记录不为0
{
ShowBook();//显示图书信息
}
//循环录入图书信息
printf("请选择是否输入图书信息(y(Y)/n(N)):");
cFlag = getchar();
getchar();
if (cFlag == 'n'||cFlag=='N')
{
return;
}
pfBook = fopen("book.txt", "ab+");
if (pfBook == NULL)
{
printf("文件打开失败!\n");
return;
}
while (cFlag == 'y' || cFlag == 'Y')
{
printf("录入数据:\n");
//录入数据
//判断记录是否已满
if (iBookRecord >= BOOK_NUM)
{
printf("记录已满!\n");
fclose(pfBook);
return;
}
//图书编号不能重复
printf("请输入图书编号:");
do
{
iFlagExist = 0;
scanf("%d", &astBook[iBookRecord].iNum);
getchar();
for (i = 0; i < iBookRecord; i++)
{
if (astBook[i].iNum == astBook[iBookRecord].iNum)//编号重复
{
iFlagExist = 1;
printf("该图书编号已经存在,请重新输入:\n");
break;
}
}
} while (iFlagExist == 1);//重复就重新进入循环,重新输入
printf("请输入图书名称:");
scanf("%s", astBook[iBookRecord].acName);
getchar();
printf("请输入图书作者:");
scanf("%s", astBook[iBookRecord].acAuthor);
getchar();
printf("请输入图书出版社:");
scanf("%s", astBook[iBookRecord].acPress);
getchar();
printf("请输入图书库存量:");
scanf("%d", &astBook[iBookRecord].iAmout);
getchar();
//保存信息
if (fwrite(&astBook[iBookRecord], LEN_BOOK, 1, pfBook)!=1)
{
printf("无法保存该信息!\n");
return;
}
else
{
printf("%d号图书信息已经保存!\n", astBook[iBookRecord].iNum);
iBookRecord++;
}
printf("继续输入信息吗?(y(Y)/n(N))\n");
cFlag = getchar();
getchar();
}
fclose(pfBook);
printf("添加图书信息执行完毕!\n");
}
int SearchBook()//查询图书信息
{
int iBookNum;//待查找图书编号
int iBookRecord;//记录条数
int iBookid;//图书所在数组下标
int i;
system("cls");
iBookRecord = ReadBookFile("rb");
if (iBookRecord == -1)
{
printf("文件打开失败!\n");
printf("按任意键返回子菜单");
getchar();
return -2;//文件打开失败,返回-2
}
else if (iBookRecord == 0)
{
printf("没有图书记录!\n");
printf("按任意键返回子菜单");
_getch();
return -3;//没有记录,返回-3
}
//进入查找程序
printf("请输入图书编号:");
scanf("%d", &iBookNum);
getchar();
for (i = 0; i < iBookRecord; i++)
{
if (iBookNum == astBook[i].iNum)
{
iBookid = i;
printf("\t%d号图书信息如下:\n", iBookNum);
printf("---------------------------------------------------\n");
printf("\t\t%-6s%-16s%-10s%-20s%-4s\n", "编号", "书名", "作者", "出版社", "库存量");
printf("\t\t%-6d%-16s%-10s%-20s%-4d\n", BOOK_DATA);
printf("---------------------------------------------------\n");
break;
}
}
if(i==iBookRecord)
{
printf("没有找到%d号图书信息!\n", iBookNum);
iBookid = -1;
}
return iBookid;
}
void DeleteBook()//删除图书信息
{
FILE* pfBook;//文件指针
int iBookid;//图书所在数组下标
int iBookRecord;//记录条数
int i;
char cFlag;
system("cls");
iBookid = SearchBook();
if (iBookid == -1)
{
printf("要删除的图书信息不存在!\n");
return;
}
iBookRecord = ReadBookFile("rb");
printf("已经找到该图书,是否删除?(y/n)");
cFlag = getchar();
getchar();
//保存被删除的图书信息(编号)
int index = astBook[iBookid].iNum;
if (cFlag == 'n' || cFlag == 'N')
{
return;
}
else if (cFlag == 'y' || cFlag == 'Y')
{
for (i = iBookid; i < iBookRecord-1; i++)
{
astBook[i] = astBook[i + 1];//数组依次前移
}
iBookRecord--;
}
//保存
pfBook = fopen("book.txt", "wb");
if (pfBook != NULL)
{
for (i = 0; i < iBookRecord; i++)
{
if (fwrite(&astBook[i], LEN_BOOK, 1, pfBook) != 1)
{
printf("无法保存该信息!\n");
return;
}
}
fclose(pfBook);
printf("%d号图书信息已经删除!\n",index);
}
}
void ShowModifyBookMenu()
{
printf("\n\n\n\n");
printf("\t\t|**************************|\t\n");
printf("\t\t|*********1.编号***********|\t\n");
printf("\t\t\*********2.书名***********|\t\n");
printf("\t\t|*********3.作者***********|\t\n");
printf("\t\t|*********4.出版社*********|\t\n");
printf("\t\t|*********5.库存量*********|\t\n");
printf("\t\t|**************************|\t\n");
printf("\t请输入要修改的信息:(输入相应编号1-5)\n");
}
//将信息保存到文件
void SaveBookFile(int iBookId)
{
FILE* pfBook;
pfBook = fopen("book.txt", "rb+");
if (pfBook != NULL)
{
fseek(pfBook, iBookId*LEN_BOOK, SEEK_SET);//将文件指针移动到要修改的位置
if (fwrite(&astBook[iBookId], LEN_BOOK, 1, pfBook) != 1)
{
printf("无法保存该信息!\n");
}
}
fclose(pfBook);
}
void ModifyBook()//修改图书信息
{
int iBookId;//图书所在数组的下标
int iBookRecord;//记录条数
int iFlagExist;//
int iltem;//选项编号
int iNum;//修改后的图书编号
int i;
system("sys");
iBookId = SearchBook();
if (iBookId == -1)//没有找到图书信息
{
return;
}
if(iBookId==-3)//没有图书记录
{
return;
}
iBookRecord = ReadBookFile("rb");
ShowModifyBookMenu();//显示修改选项的菜单
scanf("%d", &iltem);//输入你的选项
getchar();
while (iltem)
{
switch (iltem)
{
case 1://编号
printf("请输入图书编号:");
do
{
iFlagExist = 0;
scanf("%d", &iNum);
getchar();
for (i = 0; i < iBookRecord; i++)
{
if (iNum == astBook[i].iNum && i != iBookId)
{
printf("输入的编号与已存在的图书编号冲突,请重新输入:");
iFlagExist = 1;
break;
}
}
} while (iFlagExist == 1);
astBook[iBookId].iNum = iNum;
break;
case 2://书名
printf("请输入图书名称:");
scanf("%s", astBook[iBookId].acName);
getchar();
break;
case 3://作者
printf("请输入图书作者:");
scanf("%s", astBook[iBookId].acAuthor);
getchar();
break;
case 4://出版社
printf("请输入图书出版社:");
scanf("%s", astBook[iBookId].acPress);
getchar();
break;
case 5://库存量
printf("请输入图书库存量:");
scanf("%d", &astBook[iBookId].iAmout);
getchar();
break;
default:
printf("\t请输入正确的数字!\n\t\t");
}
printf("按任意键返回");
_getch();//从控件中获取字符但不回显
ShowModifyBookMenu();//显示修改选项的菜单
scanf("%d", &iltem);
getchar();
}
SaveBookFile(iBookId);//保存信息到文件
printf("图书修改成功\n");
}
void MangerBook()//图书管理模块
{
int iltem;
ShowBookMenu();
scanf("%d", &iltem);
getchar();
while (iltem)
{
switch (iltem)
{
case 1:
ShowBook();//显示图书信息
break;
case 2:
AddBook();//新增图书信息
break;
case 3:
SearchBook();//查询图书信息
break;
case 4:
DeleteBook();//删除图书信息
break;
case 5:
ModifyBook();//修改图书信息
break;
default:
printf("\t\t请输入正确的数字!\n\t\t");
}
printf("按任意键返回");
_getch();//从控件中获取字符但不回显
ShowBookMenu();
scanf("%d", &iltem);
getchar();
}
}
void ShowReaderMenu()
{
system("cls");
printf("\n\n\n\n\n\n");
printf("\t\t|*********************************|\n");
printf("\t\t|*************欢迎进入************|\n");
printf("\t\t|**********图书馆管理系统*********|\n");
printf("\t\t|*************子菜单**************|\n");
printf("\t\t|**********读者管理模块***********|\n");
printf("\t\t|**********1.显示读者信息*********|\n");
printf("\t\t|**********2.增加读者信息*********|\n");
printf("\t\t|**********3.查询读者信息*********|\n");
printf("\t\t|**********4.删除读者信息*********|\n");
printf("\t\t|**********5.修改读者信息*********|\n");
printf("\t\t|**********0.返回主菜单***********|\n");
printf("\t\t请选择:0-5\n");
}
int ReadReaderFile(const char* pcMode)
{
int iReaderRecord = 0;//文件中的记录条数
FILE* pfReader;
pfReader = fopen("reader.txt", pcMode);
if (pfReader == NULL)
{
//printf("文件打开失败!\n");
return -1;
}
while (!feof(pfReader))//feof:读到文件尾
{
if (fread(&astReader[iReaderRecord], LEN_READER, 1, pfReader))
{
iReaderRecord++;
}
}
fclose(pfReader);
return iReaderRecord;
}
void ShowReader()//显示读者信息
{
int i, iReaderRecord;//读者数
system("cls");
iReaderRecord = ReadReaderFile("rb");
if (iReaderRecord == -1)
{
printf("该文件打开失败,请先新增读者信息!\n");
}
else if (iReaderRecord == 0)
{
printf("文件中没有读者信息!\n");
}
else
{
printf("\t|**********************|\n");
printf("\n\n");
printf("\t\t%-10s%-10s%-10s%-25s%-25s%-25s\n", "编号", "姓名", "性别", "最大可借阅数","当前可借阅数", "已经借阅的图书列表");
for (i = 0; i < iReaderRecord; i++)
{
printf("\t\t%-10d%-10s%-10s%-25d%-25d", READER_DATA);
for (int j = 0; j < COUNT; j++)
{
printf("%d ", astReader[i].BookId[j]);
}
printf("\n");
}
}
}
void AddReader()//新增读者信息
{
FILE* pfReader;
int iReaderRecord;//文件中的记录条数
int iFlagExist;//读者是否存在
int i;
char cFlag;//是否输入读者信息的标志
system("cls");
iReaderRecord = ReadReaderFile("ab+");//ab+:以追加方式打开,或者新建二进制文件
if (iReaderRecord == -1)
{
printf("文件打开失败!\n");
return;
}
else if (iReaderRecord == 0)
{
printf("没有读者记录!\n");
}
else//记录不为0
{
ShowReader();//显示读者信息
}
//循环录入读者信息
printf("请选择是否输入读者信息(y(Y)/n(N)):");
cFlag = getchar();
getchar();
if (cFlag == 'n' || cFlag == 'N')
{
return;
}
pfReader = fopen("reader.txt", "ab+");
if (pfReader == NULL)
{
printf("文件打开失败!\n");
return;
}
while (cFlag == 'y' || cFlag == 'Y')
{
printf("录入数据:\n");
//录入数据
//判断记录是否已满
if (iReaderRecord >= READER_NUM)
{
printf("记录已满!\n");
fclose(pfReader);
return;
}
//读者编号不能重复
printf("请输入读者编号:");
do
{
iFlagExist = 0;
scanf("%d", &astReader[iReaderRecord].iNum);
getchar();
for (i = 0; i < iReaderRecord; i++)
{
if (astReader[i].iNum == astReader[iReaderRecord].iNum)//编号重复
{
iFlagExist = 1;
printf("该读者编号已经存在,请重新输入:\n");
break;
}
}
} while (iFlagExist == 1);//重复就重新进入循环,重新输入
printf("请输入读者名字:");
scanf("%s", astReader[iReaderRecord].acName);
getchar();
printf("请输入读者性别:");
scanf("%s", astReader[iReaderRecord].acSex);
getchar();
printf("请输入读者最大可借阅数:");
scanf("%d", &astReader[iReaderRecord].iMax);
getchar();
printf("请输入读者当前可借阅数:");
scanf("%d", &astReader[iReaderRecord].iAmout);
getchar();
//保存信息
if (fwrite(&astReader[iReaderRecord], LEN_READER, 1, pfReader) != 1)
{
printf("无法保存该信息!\n");
return;
}
else
{
printf("%d号读者信息已经保存!\n", astReader[iReaderRecord].iNum);
iReaderRecord++;
}
printf("继续输入信息吗?(y(Y)/n(N))\n");
cFlag = getchar();
getchar();
}
fclose(pfReader);
printf("添加读者信息执行完毕!\n");
}
int SearchReader()//查询读者信息
{
int iReaderNum;//待查找读者编号
int iReaderRecord;//记录条数
int iReaderid;//读者所在数组下标
int i;
system("cls");
iReaderRecord = ReadReaderFile("rb");
if (iReaderRecord == -1)
{
printf("文件打开失败!\n");
printf("按任意键返回子菜单");
getchar();
return -2;//文件打开失败,返回-2
}
else if (iReaderRecord == 0)
{
printf("没有读者记录!\n");
printf("按任意键返回子菜单");
_getch();
return -3;//没有记录,返回-3
}
//进入查找程序
printf("请输入读者编号:");
scanf("%d", &iReaderNum);
getchar();
for (i = 0; i < iReaderRecord; i++)
{
if (iReaderNum == astReader[i].iNum)
{
iReaderid = i;
printf("\t%d号读者信息如下:\n", iReaderNum);
printf("---------------------------------------------------\n");
printf("\t\t%-10s%-10s%-10s%-25s%-25s%-25s\n", "编号", "姓名", "性别", "最大可借阅数", "当前可借阅数", "已经借阅的图书列表");
printf("\t\t%-10d%-10s%-10s%-25d%-25d", READER_DATA);
for (int j = 0; j < astReader[iReaderid].iMax - astReader[iReaderid].iAmout; j++)
{
printf("%d ", astReader[iReaderid].BookId[j]);
}
printf("\n");
printf("---------------------------------------------------\n");
break;
}
}
if (i == iReaderRecord)
{
printf("没有找到%d号读者信息!\n", iReaderNum);
iReaderid = -1;
}
printf("按任意键继续:\n");
_getch();
return iReaderid;
}
void DeleteReader()//删除读者信息
{
FILE* pfReader;//文件指针
int iReaderid;//读者所在数组下标
int iReaderRecord;//记录条数
int i;
char cFlag;
system("cls");
iReaderid = SearchReader();//查找函数里有scanf可以输入你要查找的读者编号
if (iReaderid == -1)
{
printf("要删除的读者信息不存在!\n");
return;
}
iReaderRecord = ReadReaderFile("rb");
printf("已经找到该读者,是否删除?(y/n)");
cFlag = getchar();
getchar();
//保存被删除的读者信息(编号)
int index = astReader[iReaderid].iNum;
if (cFlag == 'n' || cFlag == 'N')
{
return;
}
else if (cFlag == 'y' || cFlag == 'Y')
{
for (i = iReaderid; i < iReaderRecord - 1; i++)
{
astReader[i] = astReader[i + 1];//数组依次前移
}
iReaderRecord--;
}
//保存
pfReader = fopen("reader.txt", "wb");
if (pfReader != NULL)
{
for (i = 0; i < iReaderRecord; i++)
{
if (fwrite(&astReader[i], LEN_READER, 1, pfReader) != 1)
{
printf("无法保存该信息!\n");
return;
}
}
fclose(pfReader);
printf("%d号读者信息已经删除!\n", index);
}
}
void ShowModifyReaderMenu()//显示修改选项的菜单
{
system("cls");
printf("\n\n\n\n");
printf("\t\t|*****************************|\t\n");
printf("\t\t|**********1.编号*************|\t\n");
printf("\t\t|**********2.姓名*************|\t\n");
printf("\t\t|**********3.性别*************|\t\n");
printf("\t\t|**********4.最大可借阅数*****|\t\n");
printf("\t\t|**********5.借阅书列表*******|\t\n");
printf("\t\t|**********0.返回上一级菜单***|\t\n");
printf("\t\t|*****************************|\t\n");
printf("\t请输入要修改的信息:(输入相应编号0-5)\n");
}
void ShowModifyBookIdMenu()//显示修改读者借阅图书列表的菜单
{
system("cls");
printf("\n\n\n\n");
printf("\t\t|*********************************|\t\n");
printf("\t\t|*********1.增加图书信息**********|\t\n");
printf("\t\t|*********2.删除图书信息**********|\t\n");
printf("\t\t|*********3.修改图书信息**********|\t\n");
printf("\t\t|*********0.返回上一级菜单********|\t\n");
printf("\t\t|*********************************|\t\n");
printf("\t请输入要修改的信息:(输入相应编号0-3)\n");
}
void SaveReaderFile(int iReaderId)//保存信息到文件
{
FILE* pfReader;
pfReader = fopen("reader.txt", "rb+");
if (pfReader != NULL)
{
fseek(pfReader, iReaderId * LEN_READER, SEEK_SET);//将文件指针移动到要修改的位置
if (fwrite(&astReader[iReaderId], LEN_READER, 1, pfReader) != 1)
{
printf("无法保存该信息!\n");
}
}
fclose(pfReader);
}
void ModifyReader()//修改读者信息
{
int iReaderId;//读者所在数组的下标
int iReaderRecord;//记录条数
int iFlagExist;//
int iltem;//选项编号
int iNum;//修改后的读者编号
int i;
system("sys");
iReaderId = SearchReader();
if (iReaderId == -1)//没有找到读者信息
{
return;
}
if (iReaderId == -3)//没有读者记录
{
return;
}
iReaderRecord = ReadReaderFile("rb");
ShowModifyReaderMenu();//显示修改选项的菜单
scanf("%d", &iltem);//输入你的选项
getchar();
while (iltem)
{
switch (iltem)
{
case 1://编号
printf("请输入读者编号:");
do
{
iFlagExist = 0;
scanf("%d", &iNum);
getchar();
for (i = 0; i < iReaderRecord; i++)
{
if (iNum == astReader[i].iNum && i != iReaderId)
{
printf("输入的编号与已存在的读者编号冲突,请重新输入:");
iFlagExist = 1;
break;
}
}
} while (iFlagExist == 1);
astReader[iReaderId].iNum = iNum;
break;
case 2://姓名
printf("请输入读者姓名:");
scanf("%s", astReader[iReaderId].acName);
getchar();
break;
case 3://性别
printf("请输入读者性别:(f/m)");
while (scanf("%s", astReader[iReaderId].acSex) > 0)
{
getchar();
if (strcmp(astReader[iReaderId].acSex, "f") == 0|| strcmp(astReader[iReaderId].acSex, "F") == 0 || strcmp(astReader[iReaderId].acSex, "m") == 0|| strcmp(astReader[iReaderId].acSex, "M") == 0)
{
break;
}
}
getchar();
break;
case 4://
int iMax;
printf("请输入读者最大借阅数(5-10):\n");
while (scanf("%d", &iMax) > 0)
{
getchar();
if (iMax >= 5 && iMax <= 10)
{
if (IBORROW > iMax)
{
printf("该读者当前借阅数大于该数目,应先还书再修改!\n");
return;
}
else
{
astReader[iReaderId].iMax = iMax;
astReader[iReaderId].iAmout = astReader[iReaderId].iMax - IBORROW;//iMax改变,iAmout也跟着改变
for (i = IBORROW; i < iMax; i++)
{
astReader[iReaderId].BookId[i] = 0;
}
break;
}
}
else
{
printf("您输入的最大借阅数目不在规定范围内,请重新输入(5-10):");
}
}
printf("按任意键继续:\n");
_getch();
break;
//case 5://借阅书列表
// ModifyBookId(iReaderId);
break;
default:
printf("\t请输入正确的数字!\n\t\t");
}
printf("按任意键返回");
_getch();//从控件中获取字符但不回显
ShowModifyReaderMenu();//显示修改选项的菜单
scanf("%d", &iltem);
getchar();
}
SaveReaderFile(iReaderId);//保存信息到文件
printf("读者信息修改成功\n");
}
void MangerReader()//读者管理模块
{
int iltem;
ShowReaderMenu();
scanf("%d", &iltem);
getchar();
while (iltem)
{
switch (iltem)
{
case 1:
ShowReader();//显示读者信息
break;
case 2:
AddReader();//新增读者信息
break;
case 3:
SearchReader();//查询读者信息
break;
case 4:
DeleteReader();//删除读者信息
break;
case 5:
ModifyReader();//修改读者信息
break;
default:
printf("\t\t请输入正确的数字!\n\t\t");
}
printf("按任意键返回");
_getch();//从控件中获取字符但不回显
ShowReaderMenu();
scanf("%d", &iltem);
getchar();
}
}
void ShowBRMenu()//借书还书模块菜单
{
system("cls");
printf("\n\n\n\n\n\n");
printf("\t\t|*********************************|\n");
printf("\t\t|*************欢迎进入************|\n");
printf("\t\t|**********图书馆管理系统*********|\n");
printf("\t\t|*************子菜单**************|\n");
printf("\t\t|***********借书还书模块**********|\n");
printf("\t\t|**********1.借书*****************|\n");
printf("\t\t|**********2.还书*****************|\n");
printf("\t\t|**********0.返回主菜单***********|\n");
printf("\t\t|*********************************|\n");
printf("\t\t请选择:0-5\n");
}
void BorrowBook()//借书
{
system("cls");
int iBookId;//图书所在数组下标
int iReaderId;//读者所在数组下标
int i;
//读者是否存在(如果在,还要看可借阅数)
iReaderId = SearchReader();//查询读者
if (iReaderId == -1)
{
return;
}
if (astReader[iReaderId].iAmout == 0)//当前可借阅数为0
{
printf("该读者当前可借阅数量为0,请先还书!\n");
return;
}
if (IBORROW == 0)
{
printf("该读者目前没有借阅任何书!\n");
}
else
{
printf("\t该读者当前所借书为:");
for (i = 0; i < IBORROW; i++)
{
printf("%d ", astReader[iReaderId].BookId[i]);
}
}
printf("按任意键输入要借阅的图书信息:\n");
_getch();
//图书是否存在(如果在。还要看库存量)
iBookId = SearchBook();
if (iBookId == -1)
{
return;
}
if (astBook[iBookId].iAmout == 0)//图书库存量为0
{
printf("该图书库存量为0,图书不可借阅!\n");
return;
}
//借书
astReader[iReaderId].BookId[IBORROW] = astBook[iBookId].iNum;
astReader[iReaderId].iAmout--;//读者当前可借阅数-1
astBook[iBookId].iAmout--;//图书库存量-1
if (astReader[iReaderId].iAmout == 0)
{
printf("该读者当前可借阅数量为0,请先还书!\n");
return;
}
//保存
SaveBookFile(iBookId);
SaveReaderFile(iReaderId);
printf("借书成功!\n");
}
int SearchReaderBookId(int iReaderId,int iBookId)//查找图书在读者已借阅图书列表中的下标
{
int i;
for (i = 0; i < IBORROW; i++)
{
if (astReader[iReaderId].BookId[i] == astBook[iBookId].iNum)
{
break;
}
}
if (i == IBORROW)
{
i = -1;
}
return i;
}
void ReturnBook()//还书
{
int iBookId;//图书所在数组的下标
int iReaderId;//读者所在数组的下标
int iReaderBookId;//图书在读者已借阅图书列表中的下标
int i;
//找读者
iReaderId = SearchReader();
if (iReaderId == -1)
{
return;
}
if (IBORROW == 0)
{
printf("\t该读者没有借书,无需归还!\n");
}
else
{
printf("\t该读者当前所借书为:");
for (i = 0; i < IBORROW; i++)
{
printf("%d ", astReader[iReaderId].BookId[i]);
}
printf("\n");
}
printf("按任意键输入要归还的图书信息:\n");
_getch();
//找图书
iBookId = SearchBook();
if (iBookId == -1)
{
return;
}
//图书是否在读者已借阅列表中
iReaderBookId = SearchReaderBookId(iReaderId,iBookId);
if (iReaderBookId == -1)
{
return;
}
//还书
astBook[iBookId].iAmout++;//图书库存量++
astReader[iReaderId].iAmout++;//读者当前可借阅量++
for (i = iReaderBookId; i < IBORROW; i++)//已借阅列表依次前移
{
astReader[iReaderId].BookId[i] = astReader[iReaderId].BookId[i + 1];
}
astReader[iReaderId].BookId[IBORROW] = 0;
SaveBookFile(iBookId);
SaveReaderFile(iReaderId);
printf("还书成功!\n");
}
void BorrowReturnBook()//借书还书模块
{
int iltem;
ShowBRMenu();
scanf("%d", &iltem);
getchar();
while (iltem)
{
switch (iltem)
{
case 1:
BorrowBook();//借书
break;
case 2:
ReturnBook();//还书
break;
default:
printf("\t\t请输入正确的数字!\n\t\t");
}
printf("按任意键返回子菜单");
_getch();//从控件中获取字符但不回显
ShowBRMenu();
scanf("%d", &iltem);
getchar();
}
}
int main()
{
printf("\033[1;33m");
ShowMainMenu();
int iltem;
scanf("%d", &iltem);
getchar();
while (iltem)
{
switch (iltem)
{
case 1:
MangerBook();//图书管理模块
break;
case 2:
MangerReader();//读者管理模块
break;
case 3:
BorrowReturnBook();//借书还书模块
break;
default:
printf("\t\t请输入正确的数字!\n\t\t程序将在3秒后跳转到主菜单");
Sleep(3000);
}
ShowMainMenu();
scanf("%d", &iltem);
getchar();
}
return 0;
}
标签:astReader,管理系统,int,C语言,图书馆,读者,printf,图书,getchar
From: https://blog.csdn.net/weixin_75188368/article/details/136820138