C项目---图书管理系统
作者:互联网
head.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <assert.h>
#define N 2
#define MULT 2
#define BOOK_DATA sp->data[i].B_Num,sp->data[i].B_Name, sp->data[i].B_Author, sp->data[i].B_Press, sp->data[i].B_Stock
#define MaxBROOW 2
#define READ_DATA sp->infor[i].R_Name,sp->infor[i].R_StuNum,sp->infor[i].R_Sex
struct Book
{
int B_Stock;
char B_Num[6];
char B_Author[7];
char B_Press[11];
char B_Name[17];
};
struct Reader
{
char R_Sex[3];
int R_Max; //最大借阅量
int CurBorrow; //当前已借阅量
char R_Name[7];
char R_StuNum[11]; //学号
char BorrowBookName[2][20]; //读者所借书籍
};
struct ManageBooks
{
int cursize; //当前书类
int Maxsize; //书的最大种类
int CurReader; //当前读者数量
int MaxReader; //最大读者数量
struct Book*data;
struct Reader*infor;
};
void ShowMainMenu();
void ManageBooks(struct ManageBooks*sp);
void ShowSubMenu();
void AddBook(struct ManageBooks*sp);
void SaveBookInfor(struct ManageBooks*sp);
void InitBooks(struct ManageBooks*sp);
void ReadBookInfor(struct ManageBooks*sp);
void ShwBook(struct ManageBooks*sp);
void DelBook(struct ManageBooks*sp);
void FreeManageBooks(struct ManageBooks*sp);
bool BookCheck_Duplicate(struct ManageBooks*sp);
void ModBook(struct ManageBooks*sp);
void LokBook(struct ManageBooks*sp);
void Managereaders(struct ManageBooks*sp);
void ShwReadsMenu();
bool ReaderCheck_Duplicate(struct ManageBooks*sp);
void NewReaders(struct ManageBooks*sp);
void SaveReaderInfor(struct ManageBooks*sp);
void ReadReaderInfor(struct ManageBooks*sp);
void DelReaders(struct ManageBooks*sp);
void ShwReaders(struct ManageBooks*sp);
void ModReaders(struct ManageBooks*sp);
void LokReaders(struct ManageBooks*sp);
void Borrow_Return(struct ManageBooks*sp);
void BorrowReturnMenu();
void BorrowBook(struct ManageBooks*sp);
void ReturnBook(struct ManageBooks*sp);
test.cpp
#include "head.h"
void ShowMainMenu()
{
printf("***************欢迎使用图书管理系统**************\n");
printf("*************************************************\n");
printf("****************** 1. 图书管理 *****************\n");
printf("*************************************************\n");
printf("****************** 2. 读者管理 *****************\n");
printf("*************************************************\n");
printf("****************** 3. 借书还书 *****************\n");
printf("*************************************************\n");
printf("****************** 0. 退 出 *****************\n");
printf("*************************************************\n");
}
void ShowSubMenu()
{
printf("***************** 1. 增加图书 ********************\n");
printf("***************** 2. 删除图书 ********************\n");
printf("***************** 3. 修改图书 ********************\n");
printf("***************** 4. 查询图书 ********************\n");
printf("***************** 5. 显示图书 ********************\n");
printf("***************** 0.返回主菜单 ********************\n");
}
void ManageBooks(struct ManageBooks*sp)
{
ShowSubMenu();
int choice = 0;
scanf("%d", &choice);
while (choice != 0)
{
switch (choice)
{
case 1:
AddBook(sp);
break;
case 2:
DelBook(sp);
break;
case 3:
ModBook(sp);
break;
case 4:
LokBook(sp);
break;
case 5:
ShwBook(sp);
break;
default:
printf("亲,没有这个选线哦,请您重新选择\n");
Sleep(1000);
}
ShowSubMenu();
scanf("%d", &choice);
}
}
void InitBooks(struct ManageBooks*sp)
{
assert(sp != NULL);
sp->cursize = 0;
sp->Maxsize = N;
sp->CurReader = 0;
sp->MaxReader = N;
struct Book*kp = (struct Book*)calloc(N, sizeof(struct Book));
struct Reader*rp = (struct Reader*)calloc(N, sizeof(struct Reader));
if (kp != NULL && rp != NULL)
{
sp->data = kp;
sp->infor = rp;
}
rp[0].CurBorrow = 0;
rp[0].R_Max = N;
rp[1].CurBorrow = 0;
rp[1].R_Max = N;
}
bool BookCheck_Duplicate(struct ManageBooks*sp)
{
for (int i = 0; i < sp->cursize; i++)
{
if (!strcmp(sp->data[sp->cursize].B_Num, sp->data[i].B_Num))
{
return true;
}
}
return false;
}
bool ReaderCheck_Duplicate(struct ManageBooks*sp)
{
for (int i = 0; i < sp->CurReader; i++)
{
if (!strcmp(sp->infor[sp->CurReader].R_StuNum, sp->infor[i].R_StuNum))
{
return true;
}
}
return false;
}
void AddBook(struct ManageBooks*sp)
{
assert(sp != NULL);
while (1)
{
printf("请输入需要添加的书名\n");
char tmp[16] = { 0 };
scanf("%s", tmp);
for (int i = 0; i < sp->cursize; i++)
{
if (!strcmp(tmp, sp->data[i].B_Name))
{
sp->data[i].B_Stock += 1;
printf("此书已收录,库存+1\n");
goto Tag;
}
}
if (sp->Maxsize == sp->cursize)
{
struct Book*bp = (struct Book*)realloc(sp->data, sizeof(struct Book)*sp->Maxsize * MULT);
if (bp != NULL)
{
sp->data = bp;
}
sp->Maxsize *= 2;
}
strcpy(sp->data[sp->cursize].B_Name, tmp);
printf("请输入出版社\n");
scanf("%s", sp->data[sp->cursize].B_Press);
printf("请输入图书编号(5位数),例如:00001\n");
scanf("%s", sp->data[sp->cursize].B_Num);
while (BookCheck_Duplicate(sp))
{
printf("输入重复!请重新输入!\n");
scanf("%s", sp->data[sp->cursize].B_Num);
}
printf("请输入作者\n");
scanf("%s", sp->data[sp->cursize].B_Author);
sp->data[sp->cursize].B_Stock = 1;
sp->cursize += 1;
Tag:printf("是否继续录入?,是请输入Y\\y, 否请则输入其他\n");
char judg = 0;
fflush(stdin);
scanf("%c", &judg);
if (!(judg == 'Y' || judg == 'y'))
{
break;
}
}
SaveBookInfor(sp);
}
void NewReaders(struct ManageBooks*sp)
{
while (1)
{
if (sp->CurReader == sp->MaxReader)
{
sp->infor = (struct Reader*)realloc(sp->infor, sizeof(struct Reader)*sp->MaxReader*MULT);
sp->MaxReader *= 2;
}
sp->infor[sp->CurReader].CurBorrow = 0;
sp->infor[sp->CurReader].R_Max = MaxBROOW;
printf("请输入读者的名字!\n");
scanf("%s", sp->infor[sp->CurReader].R_Name);
printf("请输入读者的性别!\n");
scanf("%s", sp->infor[sp->CurReader].R_Sex);
printf("请输入读者的学号!例如:2020032000\n");
scanf("%s", sp->infor[sp->CurReader].R_StuNum);
while (ReaderCheck_Duplicate(sp))
{
printf("学号重复,请重新输入学号!,例如:2020032000\n");
scanf("%s", sp->infor[sp->CurReader].R_StuNum);
}
sp->CurReader += 1;
printf("是否继续录入?,是请输入Y\\y, 否请则输入其他\n");
char judg = 0;
fflush(stdin);
scanf("%c", &judg);
if (!(judg == 'Y' || judg == 'y'))
{
break;
}
}
SaveReaderInfor(sp);
}
void SaveReaderInfor(struct ManageBooks*sp)
{
FILE*fp = fopen("读者基本信息.txt", "w");
if (NULL == fp)
{
printf("打开失败!");
exit(1);
}
fprintf(fp, "%d ", sp->CurReader);
for (int i = 0; i < sp->CurReader; i++)
{
fprintf(fp, "%s ", sp->infor[i].R_Name);
fprintf(fp, "%s ", sp->infor[i].R_StuNum);
fprintf(fp, "%s ", sp->infor[i].R_Sex);
fprintf(fp, "%d ", sp->infor[i].CurBorrow);
for (int j = 0; j < sp->infor[i].CurBorrow; j++)
{
fprintf(fp, "%s ", sp->infor[i].BorrowBookName[j]);
}
}
fclose(fp);
fp == NULL;
}
void SaveBookInfor(struct ManageBooks*sp)
{
FILE*fp = fopen("图书信息.txt", "w");
if (NULL == fp)
{
printf("打开失败!");
exit(1);
}
fprintf(fp, "%d ", sp->cursize);
for (int i = 0; i < sp->cursize; i++)
{
fprintf(fp, " %s ", sp->data[i].B_Author);
fprintf(fp, " %s ", sp->data[i].B_Name);
fprintf(fp, " %s ", sp->data[i].B_Num);
fprintf(fp, " %s ", sp->data[i].B_Press);
fprintf(fp, " %d ", sp->data[i].B_Stock);
}
fclose(fp);
fp == NULL;
}
void ReadBookInfor(struct ManageBooks*sp)
{
FILE*fp = fopen("图书信息.txt", "r");
if (fp == NULL)
{
return;
}
fscanf(fp, " %d ", &sp->cursize);
if (sp->cursize > sp->Maxsize)
{
sp->data = (struct Book*)realloc(sp->data, sizeof(struct Book)*sp->cursize);
sp->Maxsize = sp->cursize;
}
for (int i = 0; i < sp->cursize; i++)
{
fscanf(fp, "%s", sp->data[i].B_Author);
fscanf(fp, "%s", sp->data[i].B_Name);
fscanf(fp, "%s", sp->data[i].B_Num);
fscanf(fp, "%s", sp->data[i].B_Press);
fscanf(fp, "%d", &sp->data[i].B_Stock);
}
fclose(fp);
fp == NULL;
}
void ReadReaderInfor(struct ManageBooks*sp)
{
FILE*fp = fopen("读者基本信息.txt", "r");
if (NULL == fp)
{
printf("无读者信息!\n");
return;
}
fscanf(fp, "%d", &sp->CurReader);
sp->infor = (struct Reader*)malloc(sizeof(struct Reader)*sp->CurReader);
sp->MaxReader = sp->CurReader;
for (int i = 0; i < sp->CurReader; i++)
{
sp->infor[i].R_Max = N;
fscanf(fp, "%s", sp->infor[i].R_Name);
fscanf(fp, "%s", sp->infor[i].R_StuNum);
fscanf(fp, "%s", sp->infor[i].R_Sex);
fscanf(fp, "%d", &sp->infor[i].CurBorrow);
for (int j = 0; j < sp->infor[i].CurBorrow; j++)
{
fscanf(fp, "%s", sp->infor[i].BorrowBookName[j]);
}
}
fclose(fp);
fp == NULL;
}
void ShwBook(struct ManageBooks*sp)
{
assert(sp != NULL);
if (sp->cursize == 0)
{
printf("没有图书信息!\n");
}
else
{
printf("\t***********************************************\n");
printf("\n\n");
printf("\t%-6s %-10s %-10s %-12s %-4s\n", "编号", "书名", "作者", "出版社", "库存");
for (int i = 0; i < sp->cursize; i++)
{
printf("\t%-6s %-10s %-10s %-12s %-5d\n", BOOK_DATA);
}
printf("\n\n");
printf("\t***********************************************\n");
printf("\n\n");
}
}
void ShwReaders(struct ManageBooks*sp)
{
if (sp->CurReader == 0)
{
printf("无读者信息!\n");
}
else
{
printf("\t*************************************\n");
printf("\n\n");
printf("\t%-8s %-11s %-6s %-16s\n", "姓名", "学号", "性别", "所借书籍");
for (int i = 0; i < sp->CurReader; i++)
{
printf("\t%-8s %-11s %-6s", READ_DATA);
if (sp->infor[i].CurBorrow == 0)
{
printf(" %d\n", 0);
}
if (sp->infor[i].CurBorrow == 1)
{
printf(" %-8s\n", sp->infor[i].BorrowBookName[0]);
}
if (sp->infor[i].CurBorrow == 2)
{
printf("%-8s %8s\n", sp->infor[i].BorrowBookName[0], sp->infor[i].BorrowBookName[1]);
}
}
printf("\n\n");
printf("\t*************************************\n");
printf("\n\n");
}
}
void DelBook(struct ManageBooks*sp)
{
char bookname[17] = { 0 };
printf("输入要删除书的名字!\n");
scanf("%s", bookname);
if (sp->cursize == 0)
{
printf("无图书信息!\n");
}
else
{
for (int i = 0; i < sp->cursize; i++)
{
if (!strcmp(bookname, sp->data[i].B_Name))
{
for (; i < sp->cursize; i++)
{
sp->data[i] = sp->data[i + 1];
}
break;
}
}
sp->cursize -= 1;
SaveBookInfor(sp);
printf("删除成功!\n");
}
}
void DelReaders(struct ManageBooks*sp)
{
printf("请输入读者的学号!\n");
char stunam[11] = { 0 };
scanf("%s", stunam);
for (int i = 0; i < sp->CurReader; i++)
{
if (!strcmp(stunam, sp->infor[i].R_StuNum))
{
for (; i < sp->CurReader; i++)
{
sp->infor[i] = sp->infor[i + 1];
}
sp->CurReader -= 1;
SaveReaderInfor(sp);
printf("删除成功!\n");
return;
}
}
printf("查无此人!\n");
}
void ModBook(struct ManageBooks*sp)
{
printf("请输入要修改图书信息书籍的编号!\n");
char num[6] = { 0 };
char choice = 0;
scanf("%s", num);
for (int i = 0; i < sp->cursize; i++)
{
if (!strcmp(num, sp->data[i].B_Num))
{
printf("输入A修改图书的名称和作者,输入B修改图书的出版社信息\n");
fflush(stdin);
scanf("%c", &choice);
if (choice == 'A')
{
printf("请输入修改后图书的名称!\n");
scanf("%s", sp->data[i].B_Name);
printf("请输入修改后图书的作者!\n");
scanf("%s", sp->data[i].B_Author);
}
if (choice == 'B')
{
printf("请输入修改后图书的出版社!\n");
scanf("%s", sp->data[i].B_Press);
}
SaveBookInfor(sp);
return;
}
}
printf("无此图书信息!\n");
}
void ModReaders(struct ManageBooks*sp)
{
char stunum[11] = { 0 };
printf("请输入需要修改信息的读者学号!\n");
scanf("%s", stunum);
for (int i = 0; i < sp->CurReader; i++)
{
if (!strcmp(stunum, sp->infor[i].R_StuNum))
{
printf("请输入修改后读者的姓名!\n");
scanf("%s", sp->infor[i].R_Name);
printf("请输入修改后读者的性别!\n");
scanf("%s", sp->infor[i].R_Sex);
SaveReaderInfor(sp);
return;
}
}
printf("查无此人!\n");
}
void LokBook(struct ManageBooks*sp)
{
printf("请输入图书的编号或者名字!\n");
char name[16] = { 0 };
scanf("%s", name);
for (int i = 0; i < sp->cursize; i++)
{
if (!strcmp(sp->data[i].B_Name, name) || !strcmp(sp->data[i].B_Num, name))
{
printf("\t%-6s %-10s %-10s %-12s %-4s\n", "编号", "书名", "作者", "出版社", "库存");
printf("\t%-6s %-10s %-10s %-12s %-5d\n", BOOK_DATA);
return;
}
}
printf("查无此书!\n");
}
void LokReaders(struct ManageBooks*sp)
{
printf("请输入查询读者的学号!\n");
char stunam[11] = { 0 };
scanf("%s", stunam);
for (int i = 0; i < sp->CurReader; i++)
{
if (!strcmp(stunam, sp->infor[i].R_StuNum))
{
printf("\t%-8s %-11s %-6s %-16s\n", "姓名", "学号", "性别", "所借书籍");
printf("\t%-8s %-11s %-6s", READ_DATA);
if (sp->infor[i].CurBorrow == 0)
{
printf(" %d\n", 0);
}
if (sp->infor[i].CurBorrow == 1)
{
printf(" %-8s\n", sp->infor[i].BorrowBookName[0]);
}
if (sp->infor[i].CurBorrow == 2)
{
printf("%-8s %8s\n", sp->infor[i].BorrowBookName[0], sp->infor[i].BorrowBookName[1]);
}
}
return;
}
printf("查无此人!\n");
}
void Managereaders(struct ManageBooks*sp)
{
ShwReadsMenu();
printf("请输入你的选择!\n");
int choince = 0;
scanf("%d", &choince);
while (choince != 0)
{
switch (choince)
{
case 1:
NewReaders(sp);
break;
case 2:
DelReaders(sp);
break;
case 3:
ModReaders(sp);
break;
case 4:
LokReaders(sp);
break;
case 5:
ShwReaders(sp);
break;
default:
printf("亲,你输入有误啦,请重新输入!\n");
Sleep(1000);
}
ShwReadsMenu();
scanf("%d", &choince);
}
}
void BorrowBook(struct ManageBooks*sp)
{
printf("请输入读者的学号!\n");
began:char stunum[11] = { 0 };
scanf("%s", stunum);
for (int i = 0; i < sp->CurReader; i++)
{
if (!strcmp(stunum,sp->infor[i].R_StuNum))
{
book:if (sp->infor[i].CurBorrow >= sp->infor[i].R_Max)
{
printf("已达最大借阅量!\n");
printf("请重新输入读者学号!\n");
goto began;
}
printf("请输入所借书籍的名字!\n");
char bookname[17] = { 0 };
scanf("%s", bookname);
for (int j = 0; j < sp->cursize; j++)
{
if (!strcmp(bookname, sp->data[j].B_Name))
{
if (sp->data[j].B_Stock <= 0)
{
printf("库存不足!,请您重新输入\n");
goto book;
}
if (sp->infor[i].CurBorrow == 0)
{
strcpy(sp->infor[i].BorrowBookName[0], bookname);
}
else
{
strcpy(sp->infor[i].BorrowBookName[1], bookname);
}
printf("借阅成功,是否继续借阅?输入Y继续借阅,输入其他退出借阅\n");
sp->data[j].B_Stock--;
sp->infor[i].CurBorrow++;
char choice = 0;
fflush(stdin);
scanf("%c", &choice);
if (choice == 'Y')
{
goto book;
}
else
{
SaveReaderInfor(sp);
return;
}
}
}
printf("书库内没有此类书籍!\n");
printf("请重新输入!\n");
goto book;
}
}
printf("您输入的学号有误,请重新输入!\n");
goto began;
}
void ReturnBook(struct ManageBooks*sp)
{
began:printf("请输入读者的学号!\n");
char stunum[11] = { 0 };
scanf("%s", stunum);
for (int i = 0; i < sp->CurReader; i++)
{
if (!strcmp(stunum, sp->infor[i].R_StuNum))
{
Rrtu2:if (sp->infor[i].CurBorrow == 0)
{
printf("抱歉此读者没有借书!\n");
printf("是否更换读者还书? 输入Y继续,其他退出\n");
char choice = 0;
fflush(stdin);
scanf("%c", &choice);
if (choice == 'Y')
{
goto began;
}
else
{
SaveReaderInfor(sp);
return;
}
}
else
{
Retu:printf("请输入所还书籍名称\n");
char bookname[17] = { 0 };
scanf("%s", bookname);
for (int j = 0; j < sp->infor[i].CurBorrow; j++)
{
if (!strcmp(bookname, sp->infor[i].BorrowBookName[j]))
{
sp->infor[i].CurBorrow--;
for (int m = 0; m < sp->cursize; m++)
{
if (!strcmp(bookname, sp->data[m].B_Name))
{
sp->data[m].B_Stock++;
}
}
printf("换书成功是否继续还书?输入Y继续,其他退出\n");
char tmp = 0;
fflush(stdin);
scanf("%c", &tmp);
if (tmp == 'Y')
{
goto Rrtu2;
}
else
{
SaveReaderInfor(sp);
return;
}
}
else
{
printf("您没有借阅此书,是否继续还书,输入Y继续,其他退出\n");
fflush(stdin);
char selc = 0;
scanf("%c", &selc);
if (selc == 'Y')
{
goto Retu;
}
else
{
SaveReaderInfor(sp);
return;
}
}
}
}
}
}
printf("输入学号有误!\n");
goto began;
}
void ShwReadsMenu()
{
printf("***************** 1. 新增读者 ********************\n");
printf("***************** 2. 删除读者 ********************\n");
printf("***************** 3. 修改读者 ********************\n");
printf("***************** 4. 查询读者 ********************\n");
printf("***************** 5. 显示读者 ********************\n");
printf("***************** 0.返回主菜单 ********************\n");
}
void BorrowReturnMenu()
{
printf("***************** 1. 借书登记 ********************\n");
printf("***************** 2. 还书登记 ********************\n");
printf("***************** 0.返回主菜单 ********************\n");
}
void Borrow_Return(struct ManageBooks*sp)
{
BorrowReturnMenu();
int choince = 0;
scanf("%d", &choince);
while (choince != 0)
{
switch (choince)
{
case 1:
BorrowBook(sp);
break;
case 2:
ReturnBook(sp);
break;
default:
printf("亲,您输入有误哦,请重新输入!\n");
Sleep(1000);
}
BorrowReturnMenu();
scanf("%d", &choince);
}
}
void FreeManageBooks(struct ManageBooks*sp)
{
free(sp->data);
free(sp->infor);
sp->data = NULL;
sp->infor = NULL;
}
main.h
#include "head.h"
struct ManageBooks Lhy;
int main()
{
InitBooks(&Lhy);
int choice = 0;
ReadBookInfor(&Lhy);
ReadReaderInfor(&Lhy);
ShowMainMenu();
scanf("%d", &choice);
while (choice != 0)
{
switch (choice)
{
case 1:
ManageBooks(&Lhy);
break;
case 2:
Managereaders(&Lhy);
break;
case 3:
Borrow_Return(&Lhy);
break;
default:
printf("亲,没有这个选线哦,2秒后将跳转到主菜单,请您重新选择\n");
Sleep(2000);
}
system("cls");
ShowMainMenu();
scanf("%d", &choice);
}
SaveBookInfor(&Lhy);
FreeManageBooks(&Lhy);
system("pause");
return 0;
}
标签:struct,管理系统,ManageBooks,sp,---,printf,infor,void,图书 来源: https://blog.csdn.net/LoveHoneyYou/article/details/123171329