图书管理系统
https://blog.csdn.net/qq_43305922/article/details/102026377
#include <stdlib.h>
#include <iostream>
#include <conio.h>
#include <string.h>
#define MAXREADER 20
using namespace std;
struct Date /*日期结构*/
{
int Year; /*年*/
int Month; /*月*/
int Day; /*日*/
};
struct Reader /*读者结构*/
{
char num[20]; /*借书证号*/
struct Date bro; /*借出时间*/
struct Date back; /*归还时间*/
};
struct Book /*图书结构*/
{
int BookNumber; /*对应书号*/
char strTitle[150]; /*书名*/
char strWroter[150]; /*作者*/
int RemNum; /*可借阅册数*/
int AllNum; /*馆藏总册数*/
char strComment[300]; /*图书简介*/
struct Reader reader[MAXREADER];
};
struct Info /*借书信息结构*/
{
struct Info *pParentPoint; /*前驱结点*/
struct Book *pBookInfo; /*对应图书的信息*/
struct Info *pSon; /*后继结点*/
};
struct Book *InputNode();
struct Info *Search(struct Info *bth,int x,int *flag); /*查找图书*/
struct Info *InsertBookInfo(struct Info *bth);/*增加图书*/
struct Info *DeleteBookInfo(struct Info *bth);/*删除图书*/
void OutputBookInfo(struct Info *bth);/*输出图书信息*/
void BorrowBook(struct Info *bth);/*图书借阅*/
void ReturnBook(struct Info *bth);/*图书归还*/
void SaveInfo(struct Info *head);/*保存图书信息*/
struct Info *ReadInfo();/*读取图书信息*/
char ListMenu();/*图书馆管理系统主菜单*/
struct Info *Search(struct Info *head,int x,int *flag)
{
struct Info *p=NULL;
p=head; /*每次查询前,将工作指针指向双向链表头部结点*/
*flag=0; /*是否查找到指定书号的标志*/
while(p)
{
if(p->pBookInfo->BookNumber==x)
{
*flag = 1;/*找到相同的书号,置找到的标志*/
return p;
}
if(p->pSon!=NULL)
p = p->pSon;/*没到双向链表的尾部时,向后移动当前指针*/
else
break;/*到达链表尾部,跳出循环*/
}
return head;
}
struct Book *InputNode()
{
struct Book *p=NULL;
int i;
p=(struct Book *)malloc(sizeof(struct Book)); /*分配内存*/
fflush(stdin); /*清除以前的输入*/
cout << endl;
cout << "请输入书名:";
cin >> p->strTitle;/*从键盘取得书名*/
cout << endl;
cout << "请输入作者:";
cin >> p->strWroter;/*从键盘取得作者名*/
cout << endl;
cout << "请输入可借阅册数:";
cin >> p->RemNum;/*从键盘取得当前可借阅册数*/
cout << endl;
cout << "请输入馆藏总册数:";
cin >> p->AllNum;/*从键盘取得当前馆藏总册数*/
fflush(stdin);
cout << endl;
cout << "请输入本书简介:";
cin >> p->strComment;
/*初始化图书结构成员的相关读者指针内容为空*/
for(i=0;i<MAXREADER;i++)
(p->reader[i]).num[0]='\0';
return p; /*返回成功插入的一本书信息*/
}
struct Info *InsertBookInfo(struct Info *head)
{
int flag;
int x,z;
struct Info *p=NULL,*q=NULL;
struct Book *bookinfo=NULL;
cout << endl;
cout << "请输入增加的书号:";
cin >> x;
q=Search(head,x,&flag);/*查找借阅的书是否有馆藏*/
if(flag==1)
{
/*查找成功,存在此书*/
cout << endl;
cout << "当前馆藏<<" << q->pBookInfo->strTitle << ">>书共有" << q->pBookInfo->AllNum << "本,您想新增一本吗?(y/n)" << endl;
z=getch();
if(z=='y'||z=='Y')
{
q->pBookInfo->AllNum++;
q->pBookInfo->RemNum++;
cout << endl;
cout << "新增后共有" << q->pBookInfo->AllNum << "本, " << q->pBookInfo->RemNum << "本在馆。" << endl;
}
return head;
}
bookinfo=InputNode(); /*成功插入图书信息*/
if(head==NULL)
{
/*指针head为空时,表示当前链表为空,此时需要单独处理,即链表头内存分配*/
p=(struct Info *)malloc(sizeof(struct Info));/*申请内存*/
head=p;
bookinfo->BookNumber = x;/*将书号存入图书信息结构体*/
p->pParentPoint= NULL;/*前驱结点置空*/
p->pSon=NULL;/*后继结点置空*/
p->pBookInfo=bookinfo;
}
else
{
/*说明已经有头指针,则在此开始处理新插入的结构体指针*/
p=head;
while(p->pSon!=NULL)/*当循环结束后,p指向的就是最后一个结点*/
p = p->pSon;/*当后继结点不为空时,表示还未到链表尾部*/
q=(struct Info *)malloc(sizeof(struct Info));/*申请内存*/
bookinfo->BookNumber = x;/*将书号存入图书信息结构体*/
p->pSon = q; /*双向链表的前驱与后继结点链接*/
q->pParentPoint= p;/*前驱结点置为p*/
q->pSon=NULL;/*后继结点置空*/
q->pBookInfo=bookinfo;/*指向新插入的图书信息结构体*/
}
return head;
}
struct Info *DeleteBookInfo(struct Info *head)
{
int flag;
int x;
struct Info *p=NULL,*q=NULL;
struct Book *bookinfo=NULL;
struct Info *BookParentPoint=NULL;/*前驱结点*/
struct Info *BookSonPoint = NULL;/*后继结点*/
cout << endl;
cout << "请输入删除的书号:";
cin >> x;
q=Search(head,x,&flag);/*查找指定的书是否存在*/
if(flag==0)
{
cout << endl;
cout << "这本书不存在!" << endl; /*图书不存在是,直接输出消息并返回*/
}
else
{
bookinfo=q->pBookInfo;
cout << endl;
cout << "借阅的图书信息:";
cout << endl;
cout << "书名:" << bookinfo->strTitle;
cout << endl;
cout << "作者:" << bookinfo->strWroter;
cout << endl;
cout << "当前可借阅册数:" << bookinfo->RemNum;
cout << endl;
cout << "馆藏总册数:" << bookinfo->AllNum;
cout << endl;
cout << "本书简介:" << bookinfo->strComment << endl;
if(q->pParentPoint!=NULL && q->pSon!=NULL)
{
/*夹在链表中间的结点*/
/*链表非空*/
BookParentPoint = q->pParentPoint;/*要删除的结点的前驱指向赋给前驱结点指针变量*/
BookSonPoint = q->pSon;/*要删除的结点的后继指向赋给后继结点的指针变量*/
BookParentPoint->pSon = BookSonPoint;/*将后继结点指针指向的地址作为前驱结点中新后继域*/
BookSonPoint->pParentPoint = BookParentPoint;/*将后继结点指向的前驱域设定为要删除的结点的前驱结点*/
q->pParentPoint = NULL;
q->pSon = NULL;
free(q->pBookInfo);/*先将图书信息结构体的内存释放*/
q->pBookInfo = NULL;/*使指针指向安全地址*/
free(q);/*将要删除的借还书信息结构体指针使用的内存空间释放*/
q = NULL;/*要习惯将动态分配的内存在使用后动态释放,并将指针域指向空*/
}
else if(q->pParentPoint==NULL)
{
/*首结点的前驱为空,这里处理首结点删除操作*/
if(q->pSon==NULL)
{
/*前后指针域均为空时,说明只有一个结点,此时只需要将此结点删除*/
free(q->pBookInfo);
q->pBookInfo = NULL;
free(q);
q = NULL;
return NULL;
}
head = q->pSon;
head->pParentPoint = NULL;
/*要删除的结点的后继指向赋给后继结点的指针变量*/
q->pParentPoint = NULL;/*将链表中指向首结点的后继指针的前驱指针域置为空,表明是新的首结点*/
q->pSon = NULL;/*将链表中指向首结点的后继指针的前驱指针域置为空,表明是新的首结点*/
free(q->pBookInfo);/*先将图书信息结构体的内存释放*/
q->pBookInfo = NULL;/*使指针指向安全地址*/
free(q);/*将要删除的借还书信息结构体指针使用的内存空间释放*/
q = NULL;/*要习惯将动态分配的内存在使用后动态释放,并将指针域指向空*/
}
else if (q->pSon==NULL)
{
BookParentPoint=q->pParentPoint;
BookParentPoint->pSon=NULL;
/*尾结点的后继结点为空,这里处理尾结点的删除操作*/
q->pParentPoint = NULL;
free(q->pBookInfo);/*先将图书信息结构体的内存释放*/
q->pBookInfo = NULL;/*使指针指向安全地址*/
free(q);/*将要删除的借还书信息结构体指针使用的内存空间释放*/
q = NULL;/*要习惯将动态分配的内存在使用后动态释放,并将指针域指向空*/
}
}
return head;/*返回首结点*/
}
void OutputBookInfo(struct Info *head)
{
struct Info *q=NULL;
struct Book *p=NULL;
int k=0;
int x=0;
int flag=0;
cout << endl;
cout << "请输入你想查找的书号:";
cin >> x;
q=Search(head,x,&flag);
if(flag==1)
{
p=q->pBookInfo;
cout << endl;
cout << "借阅的图书信息:";
cout << endl;
cout << "书名:" << p->strTitle;
cout << endl;
cout << "作者:" << p->strWroter;
cout << endl;
cout << "当前可借阅册数:" << p->RemNum;
cout << endl;
cout << "馆藏总册数:" << p->AllNum;
cout << endl;
cout << "本书简介:" << p->strComment << endl;
}
else
{
cout << endl;
cout << "这本书不存在!" << endl;
}
}
void BorrowBook(struct Info *head)
{
struct Info *q=NULL;
struct Book *p=NULL;
int i,x, flag,t;
cout << endl;
cout << "请输入你想借的书号:";
cin >> x;
q=Search(head,x,&flag); /*查找图书*/
if(flag==1)
{
p=q->pBookInfo;
cout << endl;
cout << "借阅的图书信息:";
cout << endl;
cout << "书名:" << p->strTitle;
cout << endl;
cout << "作者:" << p->strWroter;
cout << endl;
cout << "当前可借阅册数:" << p->RemNum;
cout << endl;
cout << "馆藏总册数:" << p->AllNum;
cout << endl;
cout << "本书简介:" << p->strComment << endl;
cout << endl;
cout << "借出这本书?(y/n)";
t=getch();
if(t=='y'||t=='Y')
{
/*确认借这本书*/
if( (p->RemNum)==0)
{
cout << endl;
cout << "对不起,本书已经全部借出。";
}
else
{
for(i=0;i<MAXREADER;i++)
{
if( (p->reader[i]).num[0]=='\0')
break;
}
cout << endl;
cout << "请输入借书证号:";
cin >> p->reader[i].num;
cout << endl;
cout << "请分别输入借出的年-月-日:";
cout << endl;
cout << "年:";
cin >> p->reader[i].bro.Year;
cout << endl;
cout << "月:";
cin >> p->reader[i].bro.Month;
cout << endl;
cout << "日:";
cin >> p->reader[i].bro.Day;
cout << endl;
cout << "请分别输入归还的年-月-日:";
cout << endl;
cout << "年:";
cin >> p->reader[i].back.Year;
cout << endl;
cout << "月:";
cin >> p->reader[i].back.Month;
cout << endl;
cout << "日:";
cin >> p->reader[i].back.Day;
p->RemNum--;
cout << endl;
cout << "成功借到本书。";
}
}
}
else
{
cout << endl;
cout << "这本书不存在!";
}
}
void ReturnBook(struct Info *head)
{
struct Info *q=NULL;
struct Book *p=NULL;
int i,x,flag,t,j;
int year,month,day,d;
char temp[20];
cout << endl;
cout << "请输入归还的书号:";
cin >> x;
q=Search(head,x,&flag); /*图书查找*/
if(flag==1)
{
/*找到本书*/
p=q->pBookInfo;
cout << endl;
cout << "借阅的图书信息:";
cout << endl;
cout << "书名:" << p->strTitle;
cout << endl;
cout << "作者:" << p->strWroter;
cout << endl;
cout << "当前可借阅册数:" << p->RemNum;
cout << endl;
cout << "馆藏总册数:" << p->AllNum;
cout << endl;
cout << "本书简介:" << p->strComment << endl;
cout << endl;
cout << "你想归还这本书?(y/n)";
t=getch();
if(t=='y'||t=='Y')
{
/*确认归还这本书*/
system("cls");
cout << endl;
cout << "请输入借书证号:";
cin >> temp;
j=0;
for(i=0;i<MAXREADER;i++)
{
if(! (strcmp(temp,(p->reader[i]).num)))
{
j=1;
break;
}
}
if(j==0)
{
cout << endl;
cout << "你没有借阅这本书。";
return;
}
cout << endl;
cout << "今天是:";
cout << endl;
cout << "年:";
cin >> year;
cout << "月:";
cin >> month;
cout << "日:";
cin >> day;
d=0;
if(year<(p->reader[i]).back.Year)
d=1;
if(year<=(p->reader[i]).back.Year && month<(p->reader[i]).back.Month)
d=1;
if(year<=(p->reader[i]).back.Year && month<=(p->reader[i]).back.Month && day<(p->reader[i]).back.Day)
d=1;
if(d==0)
{
/*超过还书日期,在此还书*/
system("cls");
cout << endl;
cout << "你的借书日期是:" << p->reader[i].bro.Year << '-' << p->reader[i].bro.Month << '-' << p->reader[i].bro.Day;
cout << endl;
cout << "你的还书日期是:" << p->reader[i].back.Year << '-' << p->reader[i].back.Month << '-' << p->reader[i].back.Day;
cout << endl;
cout << "今天是:" << year << '-' << month << '-' << day;
cout << endl;
cout << "超出借期,罚款,归还了这本书!";
}
else
{
cout << endl;
cout << "已经归还了这本书。";
}
(p->reader[i]).num[0]='\0';
p->RemNum++;
}
}
else
{
cout << endl;
cout << "输入书号有误!";
}
}
struct Info *ReadInfo()
{
FILE *fp;
struct Info *head,*q,*p;
struct Book *bookinfo;
if((fp=fopen("data.bin","rb"))==0)
{
cout << "读取文件失败!" << endl;
exit(-1);
}
head=NULL;
while(!feof(fp))
{
bookinfo=(struct Book *)malloc(sizeof(struct Book));
fread(bookinfo,sizeof(struct Book),1,fp);
if(head==NULL)
{
head=(struct Info *)malloc(sizeof(struct Info));
head->pParentPoint=NULL;
head->pSon=NULL;
head->pBookInfo=bookinfo;
p=head;
}
else
{
q=(struct Info *)malloc(sizeof(struct Info));
q->pParentPoint=p;
q->pSon=NULL;
q->pBookInfo=bookinfo;
p->pSon=q;
p=p->pSon;
}
}
fclose(fp);
return head;
}
void SaveInfo(struct Info *head)
{
FILE *fp;
struct Info *p=NULL;
p=head;
if((fp=fopen("data.bin","wb"))==0)
{
cout << "保存文件失败!" << endl;
exit(-1);
}
while(p)
{
fwrite(p->pBookInfo,sizeof(struct Book),1,fp);
if(p->pSon!=NULL)
p = p->pSon;/*没到双向链表的尾部时,向后移动当前指针*/
else
break;/*到达链表尾部,跳出循环*/
}
fclose(fp);
}
char ListMenu()
{
/*主菜单显示函数*/
char c;
system("cls");
cout << "********************************************" << endl;
cout << "* 图书馆管理系统 *" << endl;
cout << "* 1. 增加图书 *" << endl;
cout << "* 2. 删除图书 *" << endl;
cout << "* 3. 查找图书 *" << endl;
cout << "* 4. 图书借阅 *" << endl;
cout << "* 5. 图书归还 *" << endl;
cout << "* 6. 读取数据 *" << endl;
cout << "* 7. 保存数据 *" << endl;
cout << "* 8. 退出系统 *" << endl;
cout << "********************************************" << endl;
cout << "请选择菜单项(1~8):";
fflush(stdin);
c=getchar();
return c;
}
int main()
{
char c,t;
struct Info *head=NULL;
while(1)
{
c=ListMenu();
cout << "您选择了:" << c << endl;
switch(c)
{
case '1':
head=InsertBookInfo(head); /*增加图书*/
break;
case '2':
head=DeleteBookInfo(head); /*删除图书*/
break;
case '3':
OutputBookInfo(head); /*查找图书*/
break;
case '4':
BorrowBook(head); /*图书借阅*/
break;
case '5':
ReturnBook(head); /*图书归还*/
break;
case '6':
head=ReadInfo();
break;
case '7':
SaveInfo(head);
break;
case '8':
cout << endl;
cout << "你想退出系统?(y/n)";
t=getch();
if(t=='y'||t=='Y')
exit(0);
break;
}
cout << endl;
cout << "按任意键返回主菜单......";
getch();
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <string>
#include<stdio.h>
using namespace std;
void display(int);//输出信息
int size=0;
class Book
{
public:
void welcome();//欢迎界面
void welcome1();
void addbook();//1.增加图书
void findbook();
void findbookbyNo();//2.查找图书
void findbookbyName();//2.查找图书
void findbookbyBName();//2.查找图书
void findbookbyAddress();//2.查找图书
void show();//3.查找全部
double setNo()
{
return No;
}
string setaccName()
{
return accName;
}
string setbookName()
{
return bookName;
}
string setaddress()
{
return address;
}
double setprice()
{
return price;
}
private:
int No;
string accName;
string bookName;
string address;
double price;
};
Book book[10000];
int main()
{
Book b;
b.welcome();//欢迎界面
return 0;
}
void Book::welcome()
{
int order;
cout<<"欢迎使用图书馆管理系统"<<endl;
cout<<"|--------------------------------------------|"<<endl;
cout<<"| |"<<endl;
cout<<"|----- 图书管理系统 ------|"<<endl;
cout<<"| |"<<endl;
cout<<"|--------------------------------------------|"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"|--------------------------------------------|"<<endl;
cout<<"|----------------1.增加图书-------------------|"<<endl;
cout<<"|----------------2.查找图书-------------------|"<<endl;
cout<<"|----------------3.查找全部-------------------|"<<endl;
cout<<"|----------------0.退出系统-------------------|"<<endl;
cout<<"|--------------------------------------------|"<<endl;
cout<<"请选择业务:";
cin>>order;
switch(order)
{
case 0:
exit (0);
break;
case 1:
Book::addbook();
break;
case 2:
Book:
findbook();
break;
case 3:
Book::show();
break;
}
}
void display(int i)//输出信息
{
cout<<"_____________________________________________________________________"<<endl;
cout<<setiosflags(ios::left)<<setw(12)<<"图书编号"<<setw(20)<<"图书名称"<<setw(12)<<"作者"<<setw(20)<<"出版社"<<setw(20)<<"价格"<<endl;
cout<<setiosflags(ios::left)<<setw(12)<<book[i].setNo()<<setw(20)<<book[i].setbookName()<<setw(12)<<book[i].setaccName()<<setw(20)<<book[i].setaddress()<<setw(20)<<book[i].setprice()<<endl;
cout<<"_____________________________________________________________________"<<endl;
}
void Book::addbook()
{
char c;
cout<<"图书编号:";
cin>>book[size].No;
cout<<"图书名称:";
cin>>book[size].bookName;
cout<<"作者:";
cin>>book[size].accName;
cout<<"出版社:";
cin>>book[size].address;
cout<<"价格:";
cin>>book[size].price;
cout<<"添加图书成功。"<<endl;
display(size);
size++;
cout<<"是否继续添加图书?是(y)否(n)"<<endl;
cin>>c;
if(c=='y')
Book::addbook();
if(c=='n')
Book::welcome();
}
void Book::findbook()
{
int order,c;
cout<<"|------------------------------------------------|"<<endl;
cout<<"|-----------------1.根据图书编号-------------------|"<<endl;
cout<<"|-----------------2.根据图书名称-------------------|"<<endl;
cout<<"|-----------------3.根 据 作 者-------------------|"<<endl;
cout<<"|-----------------4.根 据出版 社-------------------|"<<endl;
cout<<"|-----------------5.返 回主菜 单-------------------|"<<endl;
cout<<"|------------------------------------------------|"<<endl;
cout<<"请选择业务:";
cin>>order;
switch(order)
{
case 1:
cout<<"请输入图书编号:"<<endl;
Book::findbookbyNo();//2.查找图书
fflush(stdin);
getchar();
Book::findbook();
break;
case 2:
cout<<"请输入图书名称:"<<endl;
Book::findbookbyBName();
fflush(stdin);
getchar();
Book::findbook();
break;
case 3:
cout<<"请输入作者姓名:"<<endl;
Book::findbookbyName();//2.查找图书
fflush(stdin);
getchar();
Book::findbook();
break;
case 4:
cout<<"请输入出版社:"<<endl;
Book::findbookbyAddress();//2.查找图书
fflush(stdin);
getchar();
Book::findbook();
break;
case 5:
Book::welcome();
break;
}
}
void Book::findbookbyNo()
{
int No;
cin>>No;
bool flag=false;
int n;
for(n=0; n<=size; n++)
if(No==book[n].No)
{
flag=true;
break;
}
if(flag)
display(n);
else
cout<<"查无此书!"<<endl;
}
void Book::findbookbyBName()
{
string bname;
cin>>bname;
bool flag=false;
int n;
for(n=0; n<=size; n++)
if(bname==book[n].bookName)
{
flag=true;
break;
}
if(flag)
display(n);
else
cout<<"查无此书!"<<endl;
}
void Book::findbookbyName()
{
string name;
cin>>name;
bool flag=false;
int n;
for(n=0; n<=size; n++)
if(name==book[n].accName)
{
flag=true;
break;
}
if(flag)
display(n);
else
cout<<"查无此书"<<endl;
}
void Book::findbookbyAddress()
{
string address;
cin>>address;
bool flag;
int n;
for(n=0; n<=size; n++)
if(address==book[n].address)
{
flag=true;
break;
}
if(flag)
display(n);
else
cout<<"查无此书"<<endl;
}
void Book::show()
{
for(int n=0; n<size; n++)
{
display(n);
Book::welcome();
}
}