引用博主编程小程https://blog.csdn.net/qq_42795061/article/details/123301427?spm=1001.2014.3001.5506
在此基础上增加了权限认证和用户管理模块
最高级管理员账号:root
密码:123456
main.c
#include <stdio.h>
#include <stdlib.h>
#define CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <conio.h>
#include"Book_Management_System.h"
int main()
{
// Sleep(3000);//给电脑运行时间,以免卡死
int iItem;
printf("");//显示画面比较慢,加个这个快速反应
while(1){
bool flag=ShowLogin();//登陆验证身份界面
if(flag)break;
}
while(1)
{
Sleep(100);
ShowMainMeun();
scanf("%d",&iItem);
getchar();
switch(iItem)
{
case 1:
MangaeBook();//图书管理主界面
break;
case 2:
MangaeReader();
break;
case 3:
BorrowReturnManger();
break;
case 4:
MangaeUser();
break;
case 0:
return;
default:
printf("请输入正确的选项");
Sleep(3000);
}
}
return 0;
}
Book_Management_System.h
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include<stdbool.h>
#define BOOK_NUM 200 //记录书籍最大值
#define READER_NUM 100 //记录读者最大值
#define USER_NUM 100 //记录用户最大值
#define BOOK_DATA astBook[i].iNum,astBook[i].acName,astBook[i].acAuthor,astBook[i].acPress,astBook[i].iAmount
#define LEN_BOOK sizeof(struct Book) // 书籍结构体的大小
#define READER_DATA astReader[i].iNum,astReader[i].acName,astReader[i].acSex,astReader[i].iMax,astReader[i].iAmount
#define LEN_READER sizeof(struct Reader) //读者结构体的大小
#define USER_DATA astUser[i].acZhang,astUser[i].passWord,astUser[i].role
#define LEN_USER sizeof(struct User) // 用户结构体的大小
typedef struct Book
{
int iNum; //图书编号
char acName[15]; //图书名称
char acAuthor[15];//图书作者
char acPress[15];//图书出版社
int iAmount; //图书库存量
}Book;
typedef struct Reader
{
int iNum;//读者编号
char acName[15];//读者姓名
char acSex[15];//读者性别
int iMax;//读者最大可借阅量
int iAmount;//读者当前可借阅量
int Bookid[10];//读者已借阅图书列表
}Reader;
typedef struct User
{
char acZhang[15]; //用户账号
char passWord[15];//用户密码
int role; //用户权限
}User;
//显示图书馆主菜单
void ShowMainMeun();
//读取图书信息到文件里面
int ReadBookFile(const char* pcMode);
//显示图书管理子菜单
void ShowBookMenu();
//增加图书
void AddBook();
//删除图书
void DeleteBook();
//查询图书
int SearchBook();
//显示图书信息
void ShowBook();
//显示修改图书选项子菜单
void ShowModifyBookMenu();
// 保存图书信息到文件
void SaveBookFile(int iBookId);
//修改图书信息
void ModifyBook();
//书籍管理
void MangaeBook();
//显示读者管理子菜单
void ShowReaderMeun();
//读取读者信息到文件里面
int ReadReaderFile(const char* fopenMode);
//增加读者
void AddReader();
//删除读者
void DeleteReader();
//查询读者
int SearchReader();
//显示读者信息
void ShowReader();
//显示修改读者选项子菜单
void ShowModifyReaderMenu();
//保存读者信息到文件
void SaveReaderFile(int iReaderId);
//修改读者信息
void ModifyReader();
//读者管理
void MangaeReader();
//显示借还功能子菜单
void ShowBorrowReturnMenu();
//借书
void BorrowBook();
//还书
void ReturnBook();
//借还书管理
void BorrowReturnManger();
//光标函数
void gotoxy(int x, int y);
//登陆界面
bool ShowLogin();
//判断权限
bool needRole(int roleRequire,int role);
//用户界面
void ShowUserMeun();
//读取用户信息到文件里面
int ReadUserFile(const char* pcUser);
//查询用户
int SearchUser(char* Zhang);
//增加管理员
void AddUser();
//删除用户
void DeleteUser();
//显示用户
void ShowUser();
//修改用户
void ModifyUser();
//用户管理
void MangaeUser();
Book_Management_System.c
#include<stdio.h>
#include<windows.h>
#include<stdio.h>
#include"Book_Management_System.h"
Book astBook[BOOK_NUM];
Reader astReader[READER_NUM];
User astUser[USER_NUM];
int role = 0;
void ShowMainMeun()
{
system("cls");
printf("\n\n\n\n");
printf("********图书馆管理系统********\n");
printf("************主菜单************\n");
printf("*******1.图书管理模块*********\n");
printf("*******2.读者管理模块*********\n");
printf("*******3.借书还书模块*********\n");
printf("*******4.用户管理模块*********\n");
printf("*******0.退出系统*************\n");
printf("\n");
printf("请选择(0-4)\n");
}
//读取图书信息到文件里面
int ReadBookFile(const char* pcMode)
{
int iBookCord = 0;
FILE* pfBook;
pfBook=fopen("book.txt",pcMode);
if(pfBook==NULL)
{
return -1;
}
while(!feof(pfBook))//判断文件是否为空
{
if(fread(&astBook[iBookCord],LEN_BOOK,1,pfBook))//从文件pfBook中读取数据到ast...中
{
iBookCord++;
}
}
fclose(pfBook);
return iBookCord;
}
//显示图书管理子菜单
void ShowBookMenu()
{
system("cls");
printf("\5n");
printf("********欢迎进入书籍管理界面********\n");
printf("***************子菜单***************\n");
printf("*************1.查询图书*************\n");
printf("*************2.增加图书*************\n");
printf("*************3.删除图书*************\n");
printf("*************4.显示图书*************\n");
printf("*************5.修改图书*************\n");
printf("*************0.返回主菜单***********\n");
printf("\n");
printf("请选择(0-5)\n");
}
//增加图书
void AddBook()
{
if(!needRole(1,role))return;
FILE* pfBook;
int iBookRecord,i,iFlagcord;
char Flag;
system("cls");
iBookRecord = ReadBookFile("ab+");
if(iBookRecord == -1)
{
printf("打开文件失败!");
return;
}
else if(iBookRecord == 0)
{
printf("没有图书记录!");
}
else
{
ShowBook();
}
// printf("-----------------------------%d\n",iBookRecord);
pfBook = fopen("book.txt","ab+");
if(pfBook == NULL)
{
printf("文件打开失败!");
return;
}
fflush(stdin);
while(1)
{
printf("请选择是否输入信息(y/n)?");
scanf("%c",&Flag);
getchar();
if(Flag == 'n')
{
break;
}
if(Flag != 'y')
{
// printf("没有此选项,请重新输入!");
continue;
}
if(iBookRecord == BOOK_NUM)
{
printf("图书已达到最大数量");
fclose(pfBook);
return;
}
printf("请输入图书编号:");
do
{
iFlagcord = 0;
scanf("%d",&astBook[iBookRecord].iNum);
getchar();
for(i=0;i<iBookRecord;i++)
{
if(astBook[i].iNum == astBook[iBookRecord].iNum)
{
printf("书籍已存在,请重新输入");
iFlagcord = 1;
break;
}
}
}while(iFlagcord == 1);
printf("请输入图书名称:");
scanf("%s",astBook[iBookRecord].acName);
// fflush(stdin);
getchar();
printf("请输入图书作者:");
scanf("%s",astBook[iBookRecord].acAuthor);
// fflush(stdin);
getchar();
printf("请输入图书出版社:");
scanf("%s",astBook[iBookRecord].acPress);
// fflush(stdin);
getchar();
printf("请输入图书库存量:");
scanf("%d",&astBook[iBookRecord].iAmount);
// fflush(stdin);
getchar();
if(fwrite(&astBook[iBookRecord],LEN_BOOK,1,pfBook)!=1)
{
printf("无法保存该信息!\n");
break;
}
else
{
printf("已保存该信息!\n");
iBookRecord++;
}
}
fclose(pfBook);
printf("添加图书完毕!\n");
// ShowBook();
// getchar();
}
//删除图书
void DeleteBook()
{
if(!needRole(1,role))return;
FILE* pfBook;
int i,iBookId,iBookRecord;
char cFlag;
system("cls");
iBookId = SearchBook();
if(iBookId == -1)
{
return;
}
iBookRecord = ReadBookFile("rb");
printf("已经找到该图书,是否删除,输入?(y/n)");
scanf("%c",&cFlag);
getchar();
int index = iBookId;
if(cFlag == 'n')
{
return;
}
else if(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)//fwrite()返回写入数据个数
{
printf("无法保存!");
return;
}
}
fclose(pfBook);
printf("%d号图书已经删除!\n",index);
}
}
//查询图书
int SearchBook()
{
int iBookNum,iBookRecord,iBookId,i;
// system("cls");
iBookRecord = ReadBookFile("rb");
if(iBookRecord == -1)
{
printf("文件打开失败!\n");
printf("");
// _getch();
return -2;
}
else if(iBookRecord == 0)
{
printf("没有图书记录\n");
printf("");
Sleep(500);
return -3;
}
printf("请输入要查询的图书编号:");
scanf("%d",&iBookNum);
getchar();
for(i=0;i<iBookRecord;i++)
{
if(iBookNum == astBook[i].iNum)
{
iBookId = i;
printf("%d号信图书息如下:\n",iBookNum);
printf("----------------------------------\n");
printf("%-10s%-16s%-10s%-20s%-4s\n","图书编号","书名","作者","出版社","库存量");
printf("%-10d%-16s%-10s%-20s%-4d\n",BOOK_DATA);
printf("----------------------------------\n");
break;
}
}
if(i == iBookRecord)
{
printf("未查询到%d号图书\n",iBookNum);
iBookId = -1;
}
return iBookId;
}
//显示图书信息
void ShowBook()
{
int i,iBookRecord;
// system("cls");
iBookRecord = ReadBookFile("rb");
if(iBookRecord == -1)
{
printf("文件打开失败!\n");
}
if(iBookRecord == 0)
{
printf("文件未记录图书信息!\n");
}
else
{
printf("*************************************\n");
printf("%-10s%-16s%-10s%-20s%-4s\n","图书馆编号","书名","作者","出版社","库存量");
for(i=0;i<iBookRecord;i++)
{
printf("%-10d%-16s%-10s%-20s%-4d\n",BOOK_DATA);
}
printf("**************************************\n");
printf("\n\n");
}
getchar();
}
//显示修改图书选项子菜单
void ShowModifyBookMenu()
{
printf("\n\n\n");
printf(" 1.编号 \n");
printf(" 2.书名 \n");
printf(" 3.作者 \n");
printf(" 4.出版社 \n");
printf(" 5.图书库存量 \n");
printf(" 请输入要修改的信息(1~5) \n");
}
// 保存图书信息到文件
void SaveBookFile(int iBookId)
{
FILE* pfBook;
pfBook = fopen("book.txt","rb+");
if(pfBook!=NULL)
{
fseek(pfBook,LEN_BOOK*iBookId,SEEK_SET);
if(fwrite(&astBook[iBookId],LEN_BOOK,1,pfBook)!=1)
{
printf("无法保存该信息!\n");
}
}
fclose(pfBook);
}
//修改图书信息
void ModifyBook()
{
needRole(1,role);
int iBookNum,iBookRecord,iBookId,i;
int cFlag;
system("cls");
iBookRecord = ReadBookFile("rb");
if(iBookRecord == -1)
{
printf("打开文件失败!\n");
}
iBookId = SearchBook();
if(iBookNum == -1)
{
printf("未找到该书\n");
}
printf("请问是否修改该书(y/n)?\n");
char Flag;
scanf("%c",&Flag);
getchar();
if(Flag == 'n')
{
return;
}
ShowModifyBookMenu();
scanf("%d",&cFlag);
getchar();
int bFlag = 1;
switch(cFlag)
{
case 1:
while(bFlag)
{
printf("请输入修改后的编号:");
scanf("%d",&iBookNum);
getchar();
for(i=0;i <iBookRecord;i++)
{
if(astBook[i].iNum == iBookNum&&i != iBookId)
{
printf("该编号已存在,请重新输入\n");
break;
}
}
if(i == iBookRecord)
{
bFlag = 0;
astBook[iBookId].iNum = iBookNum;
}
}
break;
case 2:
printf("请输入新的书名:\n");
scanf("%c",&astBook[iBookId].acName);
getchar();
break;
case 3:
printf("请输入新的作者:\n");
scanf("%c",&astBook[iBookId].acAuthor);
getchar();
break;
case 4:
printf("请输入新的出版社:\n");
scanf("%c",&astBook[iBookId].acPress);
getchar();
break;
case 5:
printf("请输入新的存量:\n");
scanf("%d",&astBook[iBookId].iAmount);
getchar();
break;
default:
printf("操作有误,请重新输入:\n");
scanf("%d",&cFlag);
getchar();
}
SaveBookFile(iBookId);
printf("图书修改成功!\n");
}
//书籍管理
void MangaeBook()
{
int iltem;
int Flag = 1;
do
{
ShowBookMenu();
scanf("%d",&iltem);
getchar();
switch(iltem)
{
case 1:
SearchBook();
break;
case 2:
AddBook();
break;
case 3:
DeleteBook();
break;
case 4:
ShowBook();
break;
case 5:
ModifyBook();
break;
case 0:
Flag = 0;
break;
default:
printf("请输入正确的数字\n");
}
if(Flag!=0)
{
printf("请按任意键返回子菜单");
// Sleep(500);
// scanf("%d",&iltem);
getchar();
}
}while(Flag == 1);
}
//读者管理
//显示读者管理子菜单
void ShowReaderMeun()
{
system("cls");
printf("\n\n\n\n");
printf("********欢迎进入读者管理界面********\n");
printf("***************子菜单***************\n");
printf("*************1.查询读者*************\n");
printf("*************2.增加读者*************\n");
printf("*************3.删除读者*************\n");
printf("*************4.显示读者*************\n");
printf("*************5.修改读者*************\n");
printf("*************0.返回主菜单***********\n");
printf("\n");
printf("请选择(0-5)\n");
}
//读取读者信息到文件里面
int ReadReaderFile(const char* fopenMode)
{
int iReaderCord = 0;
FILE* pfReader;
pfReader = fopen("reader.txt",fopenMode);
if(pfReader == NULL)
{
return -1;
}
while(!feof(pfReader))
{
if(fread(&astReader[iReaderCord],LEN_READER,1,pfReader))
{
iReaderCord++;
}
}
fclose(pfReader);
return iReaderCord;
}
//增加读者
void AddReader()
{
if(role == 0)
{
printf("您没有权限!");
Sleep(500);
return;
}
int i,iReaderCord;
int iReaderFlag;
char cFlag;
FILE* pfReader;
system("cls");
iReaderCord=ReadReaderFile("rb+");
if(iReaderCord == -1)
{
printf("文件打开失败!");
return;
}else if(iReaderCord == 0)
{
printf("没有读者记录!\n");
}else
{
ShowReader();
printf("正在添加读者!");
}
pfReader = fopen("reader.txt","ab+");
if(pfReader==NULL){
printf("录入文件异常!");
return;
}
while(1)
{
printf("确认是否增加读者(y/n): ");
scanf("%c",&cFlag);
getchar();
// Sleep(1000);
// cFlag=getchar();
if(cFlag == 'n')
{
break;
}
if(cFlag != 'y')
{
printf("输入错误,请重新输入!\n");
continue;
}
if(iReaderCord == READER_NUM)
{
printf("读者添加已满!\n");
fclose(pfReader);
// getchar();
return;
}
printf("请输入新读者的编号:");
do
{
iReaderFlag = 0;
scanf("%d",&astReader[iReaderCord].iNum);
getchar();
for(i = 0;i<iReaderCord;i++)
{
if(astReader[i].iNum==astReader[iReaderCord].iNum)
{
printf("该编号已存在,请重新输入\n");
iReaderFlag = 1;
break;
}
}
}while(iReaderFlag == 1);
printf("请输入读者姓名:");
scanf("%s",astReader[iReaderCord].acName);
getchar();
printf("请输入读者性别:");
scanf("%s",astReader[iReaderCord].acSex);//ast...是字符数组 所以不用&s
getchar();
printf("请输入读者当前可借阅量:");
scanf("%d",&astReader[iReaderCord].iAmount);
getchar();
printf("请输入读者最大可借阅量:");
scanf("%d",&astReader[iReaderCord].iMax);
getchar();
if(fwrite(&astReader[iReaderCord],LEN_READER,1,pfReader)!=1)
{
printf("保存失败!");
Sleep(500);
// getchar();
continue;
}else
{
iReaderCord++;
}
}
fclose(pfReader);
printf("ok!添加读者完毕");
}
//删除读者
void DeleteReader()
{
if(role == 0)
{
printf("您没有权限!");
Sleep(500);
return;
}
FILE* pfReader;
int i,iReaderId,iReaderCord;
char cFlag;
iReaderId = SearchReader();
if(iReaderId == -1)
{
printf("没有该读者!\n");
return;
}else if(iReaderId == -3)
{
printf("读者记录为空!\n");
return;
}
iReaderCord = ReadReaderFile("rb");
printf("请确认是否要删除%d号读者(y/n)",iReaderId);
fflush(stdin);
cFlag = getchar();
// getchar();
if(cFlag == 'n')
{
return;
}
int index = astReader[iReaderId].iNum;
if(cFlag == 'y')
{
for(i = iReaderId;i<iReaderCord-1;i++)
{
astReader[i]=astReader[i+1];
}
iReaderCord--;
}
pfReader = fopen("reader.txt","wb");
if(pfReader!= NULL)
{
for(i=0;i<iReaderCord;i++)
{
if(fwrite(&astReader[i],LEN_READER,1,pfReader)!=1)
{
printf("保存失败!\n");
}
}
fclose(pfReader);
printf("%d号读者已经删除",index);
}
}
//查询读者
int SearchReader()
{
int iReaderNum,iReaderRecord,iReaderId,i;
// system("cls");
iReaderRecord = ReadReaderFile("rb");
if(iReaderRecord == -1)
{
printf("文件打开失败\n");
// _getch();
return -2;
}
if(iReaderRecord == 0)
{
printf("没有读者记录\n");
// _getch();
return -3;
}
printf("请输入要查询的读者编号:");
scanf("%d",&iReaderNum);
getchar();
for(i=0;i<iReaderRecord;i++)
{
if(astReader[i].iNum == iReaderNum)
{
iReaderId = i;
printf("%d号读者信息如下:\n",iReaderNum);
printf("----------------------------------\n");
printf("%-10s%-16s%-10s%-20s%-4s\n","读者编号","读者姓名","读者性别","最大可借阅量","当前可借阅量");
printf("%-10d%-16s%-10s%-20d%-4d\n",READER_DATA);
printf("----------------------------------\n");
break;
}
}
if(i==iReaderRecord)
{
printf("未查询到%d号读者",iReaderNum);
iReaderId = -1;
}
return iReaderId;
}
//显示读者信息
void ShowReader()
{
int iReaderCord,i;
system("cls");
iReaderCord = ReadReaderFile("rb");
if(iReaderCord == -1)
{
printf("文件打开失败!");
return;
// getchar();
}
if(iReaderCord == 0)
{
printf("文件未记录读者信息!");
return;
// getchar();
}
printf("----------------------------------\n");
printf("%d",iReaderCord);
printf("%-10s%-16s%-10s%-20s%-4s\n","读者编号","读者姓名","读者性别","最大可借阅量","当前可借阅量");
for(i = 0;i<iReaderCord;i++)
{
printf("%-10d%-16s%-10s%-20d%-4d\n",READER_DATA);
}
printf("----------------------------------\n");
}
//显示修改读者选项子菜单
void ShowModifyReaderMenu()
{
printf("\n\n\n");
printf(" 1.编号 \n");
printf(" 2.姓名 \n");
printf(" 3.性别 \n");
printf(" 4.最大可借阅量 \n");
printf(" 请输入要修改的信息(1~4) \n");
printf(" 5.退出 \n");
}
//保存读者信息到文件
void SaveReaderFile(int iReaderId)
{
FILE* pfReader;
pfReader = fopen("reader.txt","wb+");
if(pfReader != NULL)
{
fseek(pfReader,LEN_READER*iReaderId,SEEK_SET);
if(fwrite(&astReader[iReaderId],LEN_READER,1,pfReader)!=1)
{
printf("文件保存失败!\n");
Sleep(500);
// getchar();
return;
}
}
fclose(pfReader);
}
//修改读者信息
void ModifyReader()
{
if(role == 0)
{
printf("您没有权限!");
Sleep(500);
return;
}
int iReaderId,iReaderCord,iltem,iNum,i,iFlag,iMax;
iReaderId = SearchReader();
if(iReaderId == -1)
{
return;
}
iReaderCord = ReadReaderFile("rb");
ShowModifyReaderMenu();
scanf("%d",&iltem);
getchar();
switch(iltem)
{
case 1:
printf("请输入新的编号\n");
do
{
iFlag = 0;
scanf("%d",&iNum);
getchar();
for(i=0;i<iReaderCord;i++)
{
if(astReader[i].iNum==iNum&&i!=iReaderId)
{
printf("该编号已存在,请重新输入");
iFlag = 1;
}
}
}while(iFlag = 1);
astReader[iReaderId].iNum = iNum;
break;
case 2:
printf("请输入新的姓名\n");
scanf("%c",&astReader[iReaderId].acName);
getchar();
break;
case 3:
printf("请输入新的性别(F/M)\n");
while(1)
{
if(!scanf("%c",astReader[iReaderId].acSex)) {getchar();break;}
if(strcmp(astReader[iReaderId].acSex,"F")==0||strcmp(astReader[iReaderId].acSex,"M")==0)
{
break;
}
printf("请输入正确的性别(F/M)");
}
break;
case 4:
printf("请输入最大可借阅量(5~10)");
while(1)
{
scanf("%d",iMax);
getchar();
int BrrowNum;
BrrowNum = astReader[iReaderId].iMax-astReader[iReaderId].iAmount;
if(iMax<BrrowNum)
{
printf("已借阅书籍超过最大限制,需要还书!\n");
Sleep(500);
// getchar();
return;
}
if(iMax>=5&&iMax<=10)
{
astReader[iReaderId].iMax = iMax;
astReader[iReaderId].iAmount = iMax-BrrowNum;
for(i=BrrowNum;i<iMax;i++)
{
astReader[iReaderId].Bookid[i]=0;
}
break;
}
printf("错误,请重新输入正确的最大借书量");
}
break;
case 5:
return;
}
SaveReaderFile(iReaderId);
printf("修改完成!\n");
}
//读者管理
void MangaeReader()
{
int litem;
int Flag = 1;
do
{
ShowReaderMeun();
scanf("%d",&litem);
getchar();
switch(litem)
{
case 1:
SearchReader();
break;
case 2:
AddReader();
break;
case 3:
DeleteReader();
break;
case 4:
ShowReader();
break;
case 5:
ModifyReader();
break;
case 0:
Flag = 0;
break;
defalut:
printf("请输入正确的选项(0~5)\n");
}
if(Flag != 0)
{
printf("按回车返回子菜单!");
getchar();
}
}while(Flag == 1);
}
//显示借还功能子菜单
void ShowBorrowReturnMenu()
{
printf("------------欢迎使用借还书模块----------\n");
printf(" \n");
printf("1.借书 \n");
printf("2.还书 \n");
printf("0.返回 \n");
printf("请输入选项(0~2) \n");
printf("----------------------------------------\n");
}
//借书
void BorrowBook()
{
int iReaderId,iBookId,iBrrow,i,j,iBookRecord;
// system("cls");
iReaderId = SearchReader();
iBookRecord = ReadBookFile("rb");
if(iReaderId == -1)
{
printf("没有找到该读者!\n");
return;
}
iBrrow = astReader[iReaderId].iMax-astReader[iReaderId].iAmount;
if(iBrrow == 0)
{
printf("您还未借阅书籍\n");
}else
{
printf("您当前借阅的书籍有:\n");
for(i=0;i<iBrrow;i++)
{
printf(" %d",astReader[iReaderId].Bookid[i]);
for(j = 0;j<iBookRecord;j++)
{
if(astBook[j].iNum == astReader[iReaderId].Bookid[i])
{
printf(" %s",astBook[j].acName);
break;
}
}
printf("\n");
}
printf("\n");
}
if(astReader[iReaderId].iAmount == 0)
{
printf("您借阅书籍额度已用完!\n");
return;
}
printf("按回车输入信息\n");
getchar();
int Flag = 1;
do
{
iBookId = SearchBook();
if(iBookId == -1)
{
printf("请重新输入\n");
}else if(astBook[iBookId].iAmount == 0)
{
printf("剩余借阅量已为0,无法借阅");
return;
}else
{
Flag = 0;
}
}while(Flag == 1);
printf("图书借阅成功!\n");
//借书
astReader[iReaderId].Bookid[iBrrow]=astBook[iBookId].iNum;
astReader[iReaderId].iAmount--;
astBook[iBookId].iAmount--;
printf("%d号书剩余库存为:%d\n",astBook[iBookId].iNum,astBook[iBookId].iAmount);
SaveBookFile(iBookId);
SaveReaderFile(iReaderId);
}
//还书
void ReturnBook()
{
int iReaderId,iBookId,iBrrow,i;
system("cls");
iReaderId = SearchReader();
if(iReaderId == -1)
{
printf("没有找到该读者!\n");
return;
}
iBrrow = astReader[iReaderId].iMax-astReader[iReaderId].iAmount;
if(iBrrow == 0)
{
printf("您还未借阅书籍,无需归还\n");
return;
}else
{
printf("您当前借阅的书籍有:\n");
for(i=0;i<iBrrow;i++)
{
printf("%d\n",astReader[iReaderId].Bookid[i]);
}
}
printf("按回车输入要归还书籍信息\n");
getchar();
int Flag = 1;
do
{
iBookId = SearchBook();
if(iBookId == -1)
{
printf("请重新输入\n");
}else
{
Flag = 0;
}
}while(Flag == 1);
for(i = 0;i<iBrrow;i++)
{
if(astReader[iReaderId].Bookid[i]==astBook[iBookId].iNum)
{
for(int j = i;j<iBrrow;j++)
{
astReader[iReaderId].Bookid[j]=astReader[iReaderId].Bookid[j+1];
}
astReader[iReaderId].Bookid[iBrrow-1]=0;
astReader[iReaderId].iAmount++;
astBook[iBookId].iAmount++;
}
}
if(i == iBrrow)
{
printf("您未借阅这本书,无需归还!\n");
return;
}
SaveBookFile(iBookId);
SaveReaderFile(iReaderId);
printf("图书归还成功!\n");
}
//借还书管理
void BorrowReturnManger()
{
system("cls");
int iltem;
int Flag = 1;
do
{
ShowBorrowReturnMenu();
scanf("%d",&iltem);
switch(iltem)
{
case 1:
BorrowBook();
break;
case 2:
ReturnBook();
break;
case 0:
Flag = 0;
break;
defalut:
printf("请输入正确的选项(0~2)\n");
}
if(Flag != 0)
{
printf("按回车返回子菜单!");
getchar();
}
}while(Flag == 1);
}
void gotoxy(int x, int y)
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}
bool needRole(int roleRequire,int role)
{
if(role < roleRequire)
{
printf("您没有权限!");
Sleep(500);
return false;
}else
{
return true;
}
}
bool ShowLogin()
{
char zhangNum[15],passWord[15];
system("cls");
printf("\n\n\n\n");
printf("********图书登陆界面********\n");
printf("账号:\n");
printf("密码:\n");
gotoxy(5,5);
scanf("%s",zhangNum);
getchar();
gotoxy(5,6);
scanf("%s",passWord);
getchar();
if(strcmp(zhangNum,"root")==0&&strcmp(passWord,"123456")==0)
{
role = 2;
printf("管理员登陆成功!");
Sleep(1000);
return true;
}else
{
printf("登陆失败!");
Sleep(1000);
return false;
}
printf("\n");
printf("请选择(0-3)\n");
}
//显示用户管理子菜单
void ShowUserMeun()
{
system("cls");
printf("\n\n\n\n");
printf("********欢迎进入用户管理界面********\n");
printf("***************子菜单***************\n");
printf("*************1.增加用户*************\n");
printf("*************2.删除用户*************\n");
printf("*************3.显示用户*************\n");
printf("*************4.修改用户*************\n");
printf("*************0.返回主菜单***********\n");
printf("\n");
printf("请选择(0-4)\n");
}
//读取用户信息到文件里面
int ReadUserFile(const char* pcUser)
{
int iUserCord = 0;
FILE* pfUser;
pfUser = fopen("user.txt",pcUser);
if(pfUser == NULL)
{
return -1;
}
while(!feof(pfUser))
{
if(fread(&astUser[iUserCord],LEN_USER,1,pfUser)==1)
{
iUserCord++;
}
}
fclose(pfUser);
return iUserCord;
}
//查询用户
int SearchUser(char* Zhang)
{
int iUserCord,i;
iUserCord = ReadUserFile("rb");
if(iUserCord == -1)
{
printf("文件打开失败");
Sleep(1000);
return -1;
}
// printf("请输入要查询的用户账号:");
for(i=0;i<iUserCord;i++)
{
if(strcmp(astUser[i].acZhang,Zhang) == 0)
{
return 1;
}
}
return 0;
}
//增加用户
void AddUser()
{
if(!needRole(1,role))return;
int iNum,iUserCord,cFlag;
FILE* pfUser;
system("cls");
iUserCord = ReadUserFile("rb");
if(iUserCord == -1)
{
printf("打开文件失败!");
return;
}else if(iUserCord == 0)
{
printf("没有用户记录!\n");
}
else
{
ShowUser();
}
if(iUserCord == USER_NUM)
{
printf("已达到最大值,不能再添加用户!");
return;
}
pfUser = fopen("user.txt","ab+");
if(pfUser == NULL)
{
printf("文件打开失败!");
return;
}
printf("输入新的用户账号:");
scanf("%s",astUser[iUserCord].acZhang);
getchar();
cFlag = SearchUser(astUser[iUserCord].acZhang);
if(cFlag == -1)
{
return;
}
while(cFlag)
{
printf("该账号已存在,请重新输入:\n");
scanf("%s",astUser[iUserCord].acZhang);
getchar();
cFlag = SearchUser(astUser[iUserCord].acZhang);
}
printf("请输入该用户密码:");
scanf("%s",astUser[iUserCord].passWord);
getchar();
while(1)
{
printf("请输入该用户权限等级:");
scanf("%d",&astUser[iUserCord].role);
getchar();
if(astUser[iUserCord].role < role)
{
break;
}
printf("等级权限不够,请输入较低权限\n");
}
if(fwrite(&astUser[iUserCord],LEN_USER,1,pfUser)!=1)
{
printf("无法保存该信息!\n");
return;
}
else
{
printf("已保存该信息!\n");
iUserCord++;
}
fclose(pfUser);
printf("添加用户完毕!\n");
}
//删除用户
void DeleteUser()
{
if(!needRole(1,role))return;
int iUserCord,i,iUserNum;
char Zhang[15];
system("cls");
iUserCord = ReadUserFile("rb");
if(iUserCord == -1)
{
return;
}else if(iUserCord == 0)
{
printf("还未记录用户,不用删除!");
return;
}
printf("请输入要删除的用户账号:");
while(1)
{
scanf("%s",Zhang);
getchar();
for(i =0;i<iUserCord;i++)
{
if(strcmp(astUser[i].acZhang,Zhang) == 0&&astUser[iUserCord].role < role)
{
iUserNum = i;
break;
}
}
if(i == iUserCord)
{
printf("未找到该账号或权限不够,请重新输入:");
}else
{
break;
}
}
for(i = iUserNum;i<iUserCord;i++)
{
astUser[i]=astUser[i+1];
}
iUserCord--;
FILE* pfUser;
pfUser = fopen("user.txt","wb");
if(pfUser == NULL)
{
printf("文件打开失败!");
return;
}
for(i=0;i<iUserCord;i++)
{
if(fwrite(&astUser[i],LEN_USER,1,pfUser)!=1)
{
printf("文件保存失败!");
return;
}
}
fclose(pfUser);
printf("账号为%s的用户已删除!",Zhang);
}
//显示用户
void ShowUser()
{
int iUserCord,i;
system("cls");
iUserCord = ReadUserFile("rb");
printf("%d",iUserCord);
if(iUserCord == 0)
{
printf("还未记录用户!\n");
return;
}
printf("-----------------------------\n");
for(i =0;i<iUserCord;i++)
{
if(astUser[i].role<role)
{
printf("该用户的账号为:");
printf("%s\n",astUser[i].acZhang);
printf("该用户的密码为:");
printf("%s\n",astUser[i].passWord);
printf("该用户的权限等级为:");
printf("%d\n",astUser[i].role);
printf("-----------------------------\n");
}
}
}
//修改用户
void ModifyUser()
{
if(!needRole(1,role))return;
system("cls");
int iUserCord,iUserNum,cFlag,i;
char Zhang[15];
FILE* pfUser;
iUserCord = ReadUserFile("rb+");
printf("请输入要修改用户的账号:");
cFlag = 1;
do
{
scanf("%s",Zhang);
getchar();
for(i=0;i<iUserCord;i++)
{
if(strcmp(astUser[i].acZhang,Zhang) == 0&&astUser[i].role < role)
{
iUserNum = i;
cFlag = 0;
break;
}
}
if(i == iUserCord)
{
printf("未找到该用户或您的权限不够,请重新输入:\n");
}
}while(cFlag == 1);
printf("请输入修改后的权限等级:");
while(1)
{
scanf("%d",&astUser[iUserNum].role);
getchar();
if(astUser[iUserNum].role < role)
{
break;
}else
{
printf("您权限等级不够,请重新输入:\n");
}
}
pfUser = fopen("user.txt","rb+");
if(pfUser == NULL)
{
return;
}
fseek(pfUser,LEN_USER*iUserNum,SEEK_SET);
if(fwrite(&astUser[iUserNum],LEN_USER,1,pfUser)!=1)
{
printf("保存失败\n");
return;
}
fclose(pfUser);
printf("修改成功!");
}
void MangaeUser()
{
int iltem,cFlag;
system("cls");
do
{
cFlag = 1;
ShowUserMeun();
scanf("%d",&iltem);
getchar();
switch(iltem)
{
case 1:
AddUser();
break;
case 2:
DeleteUser();
break;
case 3:
ShowUser();
break;
case 4:
ModifyUser();
break;
case 0:
cFlag = 0;
break;
defalut:
printf("请输入正确的选项(0~4):\n");
}
if(cFlag != 0)
{
printf("按回车键返回子菜单!\n");
getchar();
}
}while(cFlag == 1);
}
c语言编程的易错点
scanf()后面要加getchar()来吸收缓存中的回车