C++图书管理系统源代码

Wdeath 贡献于2012-07-20

作者 番茄花园  创建于2011-12-04 12:59:00   修改者番茄花园  修改于2011-12-04 13:08:00字数13345

文档摘要:图书管理系统.cpp:定义控制台应用程序的入口点
关键词:

//本文章来源于网络。 // 图书管理系统.cpp : 定义控制台应用程序的入口点。 // //#include "stdafx.h" #include "iostream" #include "fstream" #include "string.h" #include "iomanip" using namespace std; /*int _tmain(int argc, _TCHAR* argv[]) { return 0; }*/ /*int mark0=0;//用于标记是否找到要找的信息 int mark1=0;//用于标记是否找到要找的信息*/ struct Reathur{//记录书籍的书名信息 char bookname[20];//书名 struct Reathur* Lnode;//指向下一图书节点 }; struct Reader{//记录读者信息 char Readername[20];//读者名称 struct Reathur* next;//指向该读者借的书 struct Reader* Lreader,*Right,*Lift;//下一个读者节点 }; struct Libray{//记录图书的详细信息 char bookname[20];//书名 char bookno[20];//编号 char bookreathur[20];//作者名 int booknum;//现有册数 struct Libray* Llink,* Rlink,*Lnext;//下一图书的左右节点 struct Reader* read;//指向借阅该书的读者 }; void bsinsert(struct Libray* s,struct Libray* &t)//{将s所指结点插入到根结点指针为t的树中} { if (t==NULL) t=s; else if(strcmp(s->bookname,t->bookname)==1){ bsinsert(s,t->Rlink); } else { bsinsert(s,t->Llink); } } void Rnorder( struct Reader* T){//中序遍历所有的读者链的所有节点 if(T!=NULL){ Rnorder( T->Lift); struct Reathur* u; u=T->next; cout<Readername<<"\n"; while(u!=NULL){ cout<bookname<<"》"<<"\n"; u=u->Lnode; } Rnorder( T->Right); } } ////////////////////////////////////////////////////////// ///////////////按中序遍历遍历二叉排序树进行检索/////////// void Rnorder_name( struct Reader* T,char name[],int &vetex){//中序遍历读者链的所有节点 int i=vetex; if(T!=NULL){ Rnorder_name( T->Lift,name,i); if(strcmp(T->Readername,name)==0){ struct Reathur* u; u=T->next; cout<Readername<<"\n"; while(u!=NULL){ cout<bookname<<"》"<<"\n"; u=u->Lnode; } vetex=1; } Rnorder_name( T->Right,name,i); } } ////////////////////////////////////////////////////////// ///////////////按中序遍历遍历二叉排序树进行检索/////////// void Inorder( struct Libray* T){//中序遍历所有的图书节点 if(T!=NULL){ Inorder( T->Llink); struct Reader* u; u=T->read; cout<bookname<<"》\n"; cout<bookno<<"\n"; cout<booknum<<"\n"; cout<bookreathur<<"\n"; while(u!=NULL){ cout<Readername<<"\n"; u=u->Lreader; } Inorder( T->Rlink); } } void Inorder_search( struct Libray* T,char bookname[20],int &vetex){//按书名检索 int i=vetex; char cname[20]; strcpy(cname,bookname); if(T!=NULL){ Inorder_search( T->Llink ,cname,i); if(strcmp(cname,T->bookname)==0){ struct Reader* u; u=T->read; cout<bookname<<"》\n"; cout<bookno<<"\n"; cout<booknum<<"\n"; cout<bookreathur<<"\n"; while(u!=NULL){ cout<Readername<<"\n"; u=u->Lreader; } vetex=1; } Inorder_search( T->Rlink,cname,i); } } void Inorder_searchNo( struct Libray* T,char bookno[20],int &vetex){//按书目编号检索 int i=vetex; char cno[20]; strcpy(cno,bookno); if(T!=NULL){ Inorder_searchNo( T->Llink ,cno,i); if(strcmp(cno,T->bookno)==0){ struct Reader* u; u=T->read; cout<bookname<<"》\n"; cout<bookno<<"\n"; cout<booknum<<"\n"; cout<bookreathur<<"\n"; while(u!=NULL){ cout<Readername<<"\n"; u=u->Lreader; } vetex=1; } Inorder_searchNo( T->Rlink,cno,i); } } struct Libray* sortree(struct Libray* r)//建立一个含有m 个结点的二叉排序树,p为指向二叉树根结点的指针 { struct Libray* temp=new struct Libray; struct Libray* p=new struct Libray; struct Libray* q=new struct Libray; if(r->Lnext!=NULL){ temp=r->Lnext; strcpy(q->bookname,temp->bookname); strcpy(q->bookno,temp->bookno); strcpy(q->bookreathur,temp->bookreathur); q->booknum=temp->booknum; q->read=temp->read; q->Llink=NULL; q->Rlink=NULL; ; p=q; } temp=temp->Lnext; while(temp!=NULL){ struct Libray* q=new struct Libray; strcpy(q->bookname,temp->bookname); strcpy(q->bookno,temp->bookno); strcpy(q->bookreathur,temp->bookreathur); q->booknum=temp->booknum; q->read=temp->read; q->Llink=NULL; q->Rlink=NULL; bsinsert(q,p); temp=temp->Lnext; } return p; } ////////////////////////////////////////////////////////// ///////////////往二叉排序树中插入记录/////////// void Book_insert(struct Libray* &t,char bookname[],char bookno[],char bookreathur[],int booknum){ struct Libray* j; int B=1; struct Libray* p=NULL; j=t; while((j!=NULL)&&(B==1)){ switch(strcmp(bookname,j->bookname)){ case 1: p=j; j=j->Rlink; break; case -1: p=j; j=j->Llink; break; case 0: cout<<"该记录已经存在,无需新建该记录....\n"; B=0; break; } } if(B==1) { struct Libray* j=new struct Libray; strcpy(j->bookname,bookname); strcpy(j->bookno,bookno); strcpy(j->bookreathur,bookreathur); j->booknum=booknum; j->Llink=NULL; j->Rlink=NULL; j->read=NULL; if(t==NULL) t=j; if(strcmp(bookname,p->bookname)==1) p->Rlink=j; if(strcmp(bookname,p->bookname)==-1) p->Llink=j; } } ////////////////////////////////////////////////////////// ///////////////往二叉排序树中删除记录/////////// void Book_delete(struct Libray* t,char bookname[])//{j指向被删结点,p指向其双亲,假设树不空} { struct Libray* q,*s; struct Libray* j; struct Libray* p=NULL; int B=1; j=t; while((j!=NULL)&&(B==1)){ switch(strcmp(bookname,j->bookname)){ case 1: p=j; j=j->Rlink; break; case -1: p=j; j=j->Llink; break; case 0: B=0; break; } } if(B==0) { if(j->Llink==NULL)//{被删结点无左子树} s=j->Rlink; else if(j->Rlink==NULL) s=j->Llink; else{ q=j; s=q->Llink; while(s->Rlink!=NULL){ q=s; s=s->Rlink; }; s->Rlink=j->Rlink; if (q!=j){ q->Rlink=s->Llink; s->Llink=j->Llink; } } if(p==NULL) t=s; else if (j==p->Llink) p->Llink=s; else p->Rlink=s; } } //////////////////////////////////////////////////// //////////////寻找重复的读者名称//////////////////// void search_name(struct Reader* T,char name[],struct Reader* &p){ if(T!=NULL){ search_name( T->Lift,name,p); if(strcmp(T->Readername,name)==0){ p=T; goto mark; } else p=NULL; search_name( T->Right,name,p); } mark:; } void Rsinsert(struct Reader* s,struct Reader* &t)//{将s所指结点插入到根结点指针为t的树中} { if (t==NULL) t=s; else if(strcmp(s->Readername,t->Readername)==1){ Rsinsert(s,t->Right); } else { Rsinsert(s,t->Lift); } } ////////////////////////////////////////////////////////// ///////////////建立读者链的二叉排序树/////////// struct Reader* Build_reader(struct Libray* book){ struct Reader* vetex=new struct Reader;; struct Reader* q; struct Reader* Rroot=new struct Reader; struct Reader* temp=new struct Reader; struct Reathur* u=new struct Reathur; struct Reathur* v; struct Libray* p=new struct Libray; char bname[20]; p=book->Lnext; strcpy(u->bookname,p->bookname); u->Lnode=NULL; q=p->read; if(q!=NULL){ strcpy(temp->Readername,q->Readername); temp->next=u; temp->Lift=NULL; temp->Right=NULL; } Rroot=temp; q=q->Lreader; while(q!=NULL){ struct Reathur* u=new struct Reathur; struct Reader* temp=new struct Reader; u->Lnode=NULL; strcpy(u->bookname,p->bookname); strcpy(temp->Readername,q->Readername); temp->next=u; temp->Lift=NULL; temp->Right=NULL; Rsinsert(temp,Rroot); q=q->Lreader; } p=p->Lnext; while(p!=NULL){ q=p->read; while(q!=NULL){ search_name(Rroot,q->Readername,vetex); struct Reathur* u=new struct Reathur; strcpy(u->bookname,p->bookname); u->Lnode=NULL; if(vetex!=NULL){ u->Lnode=vetex->next; vetex->next=u; q=q->Lreader; continue; } struct Reader* temp=new struct Reader; strcpy(temp->Readername,q->Readername); temp->next=u; temp->Lift=NULL; temp->Right=NULL; Rsinsert(temp,Rroot); q=q->Lreader; } p=p->Lnext; } return Rroot; } ////////////////////////////////////////////////////////// //////////////////////////寻找读者想借阅的书籍//////////// struct Libray* Book_search(struct Libray* &t,char bookname[]){ struct Libray* j; struct Libray* p=NULL; int B=1; j=t; while((j!=NULL)&&(B==1)){ switch(strcmp(bookname,j->bookname)){ case 1: p=j; j=j->Rlink; break; case -1: p=j; j=j->Llink; break; case 0: B=0; break; } } if(B==0&&j->booknum>0){ cout<<"已经在到你要寻找的书籍,可以借阅....\n"; return j; } if(B==1){ cout<<"Sorroy!没有你要寻找的书籍....\n"; return NULL; } } ////////////////////////////////////////////////////////// //////////////////////////寻找读者信息,若无则建立新节点/// struct Reader* Reader_sea(struct Reader* &t,char bookname[],char readername[]){ struct Reader* j,*q; struct Reathur* p; int B=1; j=t; while((j!=NULL)&&(B==1)){ switch(strcmp(readername,j->Readername)){ case 1: q=j; j=j->Right; break; case -1: q=j; j=j->Lift; break; case 0: B=0; break; } } if(B==0){ p=j->next; while(p!=NULL){ if(strcmp(bookname,p->bookname)==0){ cout<<"Sorroy!你已经借阅了该书籍,不能重复借阅....\n"; return NULL; } p=p->Lnode; } return j; } else{ struct Reader* j=new struct Reader; strcpy(j->Readername,readername); j->Lift=NULL; j->Right=NULL; j->next=NULL; if(t==NULL) t=j; if(strcmp(readername,q->Readername)==1) q->Right=j; if(strcmp(readername,q->Readername)==-1) q->Lift=j; return j; } } ////////////////////////////////////////////////////////// ///////////////读者借书/////////// void Borrow_book(struct Libray* &book,struct Reader* &Readeroot){ char Rname[20],Bname[20]; struct Libray* p=book; struct Reader* q=Readeroot; struct Libray* u; struct Reader* v; cout<<"请输入用户名!\n"; cin>>Rname; cout<<"请输入书籍名!\n"; cin>>Bname; u=Book_search(p,Bname); v=Reader_sea(q,Bname,Rname); if(u==NULL){ return; } else{ if(v==NULL){ return; } else{ struct Reader* temp=new struct Reader; struct Reathur* tp=new struct Reathur; strcpy(temp->Readername,Rname); u->booknum--; temp->Lreader=u->read; u->read=temp; strcpy(tp->bookname,Bname); tp->Lnode=v->next; v->next=tp; } } } ////////////////////////////////////////////////////////// //////////////////////////寻找读者想借阅的书籍//////////// void B_search(struct Libray* &t,char bookname[],char Rname[]){ struct Libray* j; struct Libray* p=NULL; struct Reader* u,*v; int B=1; j=t; while((j!=NULL)&&(B==1)){ switch(strcmp(bookname,j->bookname)){ case 1: p=j; j=j->Rlink; break; case -1: p=j; j=j->Llink; break; case 0: B=0; break; } } if(B==0){ cout<<"已经找到该书籍的有关记录,更新记录....\n"; j->booknum++; u=j->read; if(strcmp(Rname,u->Readername)==0){ j->read=u->Lreader; delete(u); return; } v=u; u=u->Lreader; while(u!=NULL){ if(strcmp(Rname,u->Readername)==0){ v->Lreader=u->Lreader; delete(u); return; } v=u; u=u->Lreader; } if(B==1){ cout<<"Sorroy!没有找到该书籍的有关记录....\n"; return; } } } ////////////////////////////////////////// ////////////////寻找读者信息///////////// void R_sea(struct Reader* &t,char readername[],char bname[]){ struct Reader* j,*s,*q; struct Reathur* v,*u; struct Reader* p=NULL; int B=1; j=t; while((j!=NULL)&&(B==1)){ switch(strcmp(readername,j->Readername)){ case 1: p=j; j=j->Right; break; case -1: p=j; j=j->Lift; break; case 0: B=0; break; } } if(B==0){ cout<<"已经找到该读者的有关记录,更新记录....\n"; u=j->next; if(strcmp(bname,u->bookname)==0){ if(u->Lnode==NULL){ if(j->Lift==NULL)//{被删结点无左子树} s=j->Right; else if(j->Right==NULL) s=j->Lift; else{ q=j; s=q->Lift; while(s->Right!=NULL){ q=s; s=s->Right; }; s->Right=j->Right; if (q!=j){ q->Right=s->Lift; s->Lift=j->Lift; } } if(p==NULL) t=s; else if (j==p->Lift) p->Lift=s; else p->Right=s; } else j->next=u->Lnode; delete(u); return; } else{ v=u; u=u->Lnode; while(u!=NULL){ if(strcmp(bname,u->bookname)==0){ v->Lnode=u->Lnode; delete(u); return; } u=u->Lnode; } } } else{ cout<<"Sorroy!没有找到该读者的有关记录....\n"; return; } } //////////////////////////////////////////// ///////////////读者还书///////////////////////// void Return_book(struct Libray* &book,struct Reader* &Readeroot){ char Rname[20],Bname[20]; struct Libray* p=book; struct Reader* q=Readeroot; cout<<"请输入用户名!\n"; cin>>Rname; cout<<"请输入书籍名!\n"; cin>>Bname; B_search(p,Bname,Rname); R_sea(q,Rname,Bname); } ////////////////////////////////////////////////////////// ///////////////将新纪录写入到文件当中/////////// void write_file(struct Libray* &T){ int i; struct Libray* p=new struct Libray; struct Libray* s[50]; fstream ofile; ofile.open("C:\\Documents and Settings\\dufan\\My Documents\\Visual Studio 2008\\Projects\\mdata.txt",ios::out); if(!ofile){ cout<<"The file can not open!\n"; return; } if(T==NULL) return; else { i=0; p=T; } do{ while(p!=NULL){ i=i+1; s[i]=p; p=p->Llink; } if(i!=0){ p=s[i]; i=i-1; struct Reader* u; u=p->read; ofile<<"书 名:"<bookname<<"\n"; ofile<<"书目编号:"<bookno<<"\n"; ofile<<"作者名称:"<bookreathur<<"\n"; ofile<<"现有册数:"<booknum<<"\n"; while(u!=NULL){ ofile<<"借阅读者:"<Readername<<"\n"; u=u->Lreader; } p=p->Rlink; } }while (i!=0 || p!=NULL); } int main(){ char ch; int cnum,i=0; char temp[5],cname[20],cno[20],creathur[20],cborrow[20]; struct Libray* book=new struct Libray; struct Libray* p; struct Reader* r=new struct Reader;//用于指向读者链 struct Reader* Readeroot=new struct Reader;//用于指向读者链二叉排序树 fstream ifile; ifile.open("C:\\Documents and Settings\\dufan\\My Documents\\Visual Studio 2008\\Projects\\mdata.txt",ios::in); if(!ifile){ cout<<"The file can not open!\n"; return 0; } p=book; while(!ifile.eof()){ struct Libray* booktemp=new struct Libray; struct Reader* rtemp; rtemp=r; ifile.ignore(20,':'); ifile.getline(cname,20); strcpy(booktemp->bookname,cname);//读取书籍名称 ifile.ignore(20,':'); ifile.getline(cno,20); strcpy(booktemp->bookno,cno);//读取书籍编号 ifile.ignore(20,':'); ifile.getline(creathur,20); strcpy(booktemp->bookreathur,creathur);//读取作者名称 ifile.ignore(20,':'); ifile.getline(temp,20); cnum=atol(temp); booktemp->booknum=cnum;//读取现有册数 while(ifile.get(ch)){//读取借阅者名称 if((int)ch==-54) break; ifile.ignore(20,':'); struct Reader* q=new struct Reader;//读者节点 ifile.getline(cborrow,20); strcpy(q->Readername,cborrow);//读取读者姓名 q->Lreader=NULL; rtemp->Lreader=q; rtemp=rtemp->Lreader; } booktemp->read=r->Lreader; booktemp->Lnext=NULL; p->Lnext=booktemp; p=p->Lnext; } ifile.close(); delete(r); Readeroot=Build_reader(book); book=sortree(book); //Inorder(book); //Rnorder(Readeroot); int fun; while(1){ cout<<"/********** 图书管理系统功能说明表**********\n"; cout<<"/** 1添加新书\n"; cout<<"/** 2删除旧书\n"; cout<<"/** 3按书名检索\n"; cout<<"/** 4按书目编号检索\n"; cout<<"/** 5查询读者信息\n"; cout<<"/** 6读者借书\n"; cout<<"/** 7读者还书\n"; cout<<"/** 8更新文件记录\n"; cout<<"/** 0退出图书管理系统\n"; cout<<"/** 请选择要执行的操作\n"; cin>>fun; switch(fun){ case 1: cout<<"/** 请依次输入书名,书目编号,作者名称,现有册数,并用空格隔开!\n"; cin>>cname; cin>>cno; cin>>creathur; cin>>cnum; Book_insert(book,cname,cno,creathur,cnum); break; case 2: cout<<"/** 请输入书名:"; cin>>cname; Book_delete(book,cname); break; case 3: cout<<"/** 请输入书名:"; cin>>cname; //Inorder(book); Inorder_search( book,cname,i); if(i!=1){ cout<<"没有找到符合条件的信息,是否要新建该记录....\n\n"; } break; case 4: cout<<"/** 请输入书目编号:"; cin>>cno; Inorder_searchNo( book,cno,i); //cout<>cname; Rnorder_name(Readeroot,cname,i); if(i!=1){ cout<<"没有找到符合条件的信息,是否要新建该记录....\n\n"; } break; case 6: Borrow_book(book,Readeroot); break; case 7: Return_book(book,Readeroot); break; case 8: write_file(book); cout<<"更新成功!\n"; break; case 0: exit(0); break; } } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 6 人已下载

下载文档