// SimpleLibrary.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<string>
#include <typeinfo.h>
using namespace std;
#define MAXSIZE 5
typedef struct BNode {//存储书的一系列信息
string bid;
string bname;
string bwriter;
int num_current;
int num_total;
}BNode;
typedef struct TNode{//存储图书信息的二叉树的二叉树结点
BNode *data;
struct TNode *lchild;
struct TNode *rchild;
}TNode,*BookStorage;
typedef struct RBNode{//存储借阅书籍的信息
BNode *data;
struct RBNode *nextbook;
}RBNode,*RBList;
/*typedef struct RNode{//存储读者信息,以及借的书
int borrower_id;
string borrower_name;
RBNode *borrow_book;
}RNode;*/
typedef struct Borrowers{
// struct Borrower *base;
int borrower_id;
string borrower_name;
RBList borrow_book;
}Borrowers,Borrower[MAXSIZE];
typedef struct {
Borrower borrowers;
BookStorage books;
}Library;
void CreateBookStorage(BookStorage &books);
void CreateLiBrary(Library &library){//图书馆系统初始化
CreateBookStorage(library.books);
for(int i=0;i<MAXSIZE;i++){
library.borrowers[i].borrow_book=new RBNode;
library.borrowers[i].borrow_book->nextbook=NULL;
}
library.borrowers[0].borrower_id=1;
}
//-------------------------------------------------------------------------------------
BNode* SearchBook(BookStorage books,string bookid,TNode *&tag){//查找书,并返回该书的信息(非递归的方式查找)
TNode *p=books,*q;
BNode *b=NULL;
while(p){
if(p->data->bid>bookid){
q=p;
p=p->lchild;
}
else if(p->data->bid<bookid){
tag=p;
p=p->rchild;
}
else return p->data;
}
return b;
}
void CreateBookStorage(BookStorage &books){//创建存储图书信息的二叉树(非递归的方式创建)
books=NULL;
string id,s;
int booknum;
TNode *p,*tag;
tag=NULL;
cout<<"****************图书馆录入系统*****************"<<endl;
cout<<"你要录入几本书?"<<endl;
cin>>booknum;
for(int i=0;i<booknum;i++){
cout<<"请输入书号:";
cin>>id;
if(!SearchBook(books,id,tag)){
p=new TNode;//输入书的相关信息
p->data=new BNode;
p->data->bid=id;
cout<<"请输入书名:";
cin>>p->data->bname;
cout<<"请输入作者:";
cin>>p->data->bwriter;
cout<<"请输入本书的总量:";
cin>>p->data->num_total;
p->data->num_current=p->data->num_total;
p->lchild=p->rchild=NULL;
if(tag==NULL) books=p;//将点插入
else if(p->data->bid<tag->data->bid) tag->lchild=p;
else tag->rchild=p;
cout<<endl;
}
else cout<<"该书已录入!"<<endl;
}
cout<<"书已全部录入"<<endl;
}
void InsertBook(BookStorage &books){//添加新的书籍
cout<<"----------------添加新书----------------"<<endl;
string id;
TNode *p,*tag;
tag=NULL;
cout<<"请输入书号:"<<endl;
cin>>id;
if(!SearchBook(books,id,tag)){
p=new TNode;//输入书的相关信息
p->data=new BNode;
p->data->bid=id;
cout<<"请输入书名:";
cin>>p->data->bname;
cout<<"请输入作者:";
cin>>p->data->bwriter;
cout<<"请输入本书的总量:";
cin>>p->data->num_total;
p->data->num_current=p->data->num_total;
p->lchild=p->rchild=NULL;
if(tag==NULL) books=p;//将点插入
else if(p->data->bid<tag->data->bid) tag->lchild=p;
else tag->rchild=p;
}
else cout<<"该书已录入!"<<endl;
cout<<"添加成功"<<endl;
cout<<"----------------------------------------"<<endl;
}
void DeleteBook(BookStorage &books){//删除某本书籍
cout<<"----------------删除旧书----------------"<<endl;
string id;
cout<<"请输入要删除的书的书号:"<<endl;
cin>>id;
TNode *tag=NULL,*that,*temp;
that=new TNode;
that->data=SearchBook(books,id,tag);
cout<<that->data->bid<<" "<<that->data->bname<<endl;;
cout<<that->lchild<<that->rchild;
TNode *p=that->lchild,*p_parent=that;
if(that==NULL){//删除的书不存在
cout<<"这本书不存在"<<endl;
return;
}
if(that->rchild&&that->lchild){//删除度为2的节点
while(p){
p_parent=p;
p=p->rchild;
}
temp=that;
that=p;
p_parent=p->lchild;
temp->data=NULL;
free(temp);
}
else{//删除度为1、0的节点
p=that->rchild ? that->rchild : that->lchild;
if(!tag) books=p;
else if(that==tag->lchild) tag->lchild=p;
else tag->rchild=p;
free(p);
}
cout<<"删除成功"<<endl;
cout<<"----------------------------------------"<<endl;
}
int SearchBorrower(Borrower borrower,int borrower_id);
void ReturnBook(Library &library){//归还书(借书人懒得删除了)
cout<<"----------------还书--------------------"<<endl;
int borrower_id,i;
string book_id;
cout<<"请输入借书证号:";
cin>>borrower_id;
i=SearchBorrower(library.borrowers,borrower_id);
cout<<"请输入书号:";
RBNode *temp,*temp_p;
temp_p=temp=library.borrowers[i].borrow_book->nextbook;
while(cin>>book_id,book_id!="#"){
while(temp->data->bid!=book_id){
temp_p=temp;
temp=temp->nextbook;
}
temp_p->nextbook=temp->nextbook;
++temp->data->num_current;
free(temp);
cout<<"请输入书号(或输入#退出):"<<endl;
}
cout<<"还书成功"<<endl;
cout<<"----------------------------------------"<<endl;
}
//----------------------------------------------------------------
void AddBorrower(Borrower &borrower,int borrower_id){//查找借书人
string s;
int num=borrower[0].borrower_id;
borrower[num].borrower_id=borrower_id;
cout<<"请输入借书人的名字:";
cin>>s;
borrower[num].borrower_name=s;
++borrower[0].borrower_id;
cout<<"创建借书人成功"<<endl;
}
int SearchBorrower(Borrower borrower,int borrower_id){
int i=1,num=borrower[0].borrower_id;
for(i;borrower[i].borrower_id!=borrower_id&&i<=num;i++){}
if(i>num) return -1;
return i;
}
void BorrowBook(Library &library){//借书(要查看他是否有某本书)还没写完
cout<<"----------------借书--------------------"<<endl;
// BNode *bn;
RBNode *rbn;
TNode *a;
string bookid;
int borrower_id,i;
cout<<"请输入借书证号:";
cin>>borrower_id;
i=SearchBorrower(library.borrowers,borrower_id);//查看此人是否曾经借过书
if(i==-1) AddBorrower(library.borrowers,borrower_id);
i=SearchBorrower(library.borrowers,borrower_id);
cout<<"请输入书号:";
while(cin>>bookid,bookid!="#"){
rbn=new RBNode;
rbn->data=SearchBook(library.books,bookid,a);
if(rbn->data->num_current<=0){
cout<<"抱歉!此书库存不足。请继续输入其他书"<<endl;
continue;
}
--rbn->data->num_current;
rbn->nextbook=library.borrowers[i].borrow_book->nextbook;//头插法建立链表
library.borrowers[i].borrow_book->nextbook=rbn;
cout<<"请输入书号(或输入#退出):";
}
cout<<"----------------------------------------"<<endl;
}
void ShowBookInfo(BNode *r){//展示某本书的信息
if(r==NULL)cout<<"此书不存在"<<endl;
else{
cout<<"----------------------------------------"<<endl;
cout<<"| 书号:"<<r->bid<<endl;;
cout<<"| 书名:"<<r->bname<<endl;
cout<<"| 作者:"<<r->bwriter<<endl;
cout<<"| 总库存:"<<r->num_total<<endl;
cout<<"| 目前库存:"<<r->num_current<<endl;
cout<<"----------------------------------------"<<endl;
}
}
void ShowBorrower(Borrower borrowers,int borrower_id){//查阅借书者的资料
int i=SearchBorrower(borrowers,borrower_id);
RBNode *r=borrowers[i].borrow_book;
cout<<"----------------------------------------"<<endl;
cout<<"| 姓名:"<<borrowers[i].borrower_name<<endl;
cout<<"| 借书证号:"<<borrowers[i].borrower_id<<endl;
cout<<"----------------------------------------"<<endl;
while(r->nextbook){
ShowBookInfo(r->nextbook->data);
r=r->nextbook;
}
}
void Print(){
cout<<"----------------全部功能----------------"<<endl;
cout<<"|1.借书 |"<<endl;
cout<<"|2.还书 |"<<endl;
cout<<"|3.查书 |"<<endl;
cout<<"|4.添加新书 |"<<endl;
cout<<"|5.删除旧书 |"<<endl;
cout<<"|6.查阅借书人信息 |"<<endl;
cout<<"|0.退出 |"<<endl;
cout<<"----------------------------------------"<<endl;
cout<<endl;
cout<<"请输入要执行功能的序号:";
}
void ShowLibrary(Library library){
int choice;
while(true){
Print();
cin>>choice;
switch (choice){
case 1:BorrowBook(library);break;//借书ok
case 2:ReturnBook(library);break;//还书ok
case 3:{//查书ok
cout<<"请输入书号:";
string book_id;
TNode *a;
cin>>book_id;
ShowBookInfo(SearchBook(library.books,book_id,a));
break;
}
case 4:InsertBook(library.books);break;//添加新书OK
case 5:DeleteBook(library.books);break;//删除旧书
case 6:{
int borrower_id;
cout<<"请输入借书证号:";
cin>>borrower_id;
ShowBorrower(library.borrowers,borrower_id);//查阅借书人信息
break;
}
default:exit(0);
}
}
}
int main(int argc, char* argv[])
{
Library library;
CreateLiBrary(library);//这一步就已经录入书的相关信息了
ShowLibrary(library);
return 0;
}