图书管理系统课程设计及代码(共16页).doc
精选优质文档-倾情为你奉上#include"iostream.h"#include"conio.h"#include"fstream.h"#include"iomanip.h" #include"string.h"const int Maxr=100 ;/最多的读者数 const int Maxb=100; /最多的图书数const int Maxbor=5; /每位读者最多借的书class Book /图书类,实现对图书的描述,图书的编号,书名,借出,还入等功能private: int tag; /删除标记 1:已删 0:未删int no; /图书编号char name20; /书名 char author20;/作者char cbs20; /出版社int onshelf; /是否再架 1:再架 2:已借 public: Book(); char *getname()/获取书名 return name; char *getauthorname() /获取作者名 return author; char *getcbsname()/获取出版社名 return cbs; int getno()/获取图书编号 return no; int gettag()/获取删除标记 return tag; void setname(char na) /设置书名 strcpy(name,na); void setonshelf(int oa) onshelf=oa; void delbook()/删除图书 tag=1; void addbook(int n,char *na,char *aa,char *ca,int oa) /增加图书 tag=0; no=n; strcpy(name,na); strcpy(author,aa); strcpy(cbs,ca); onshelf=oa; int borrowbook() /借书操作 if (onshelf>0) onshelf-; return 1; return 0; void retbook()/还书操作 onshelf+; void disp() /输出图书 cout<<setw(6)<<no<<setw(18)<<name<<setw(10)<<author<<setw(10)<<cbs<<setw(10)<<onshelf<<endl; ; class BDatabase /图书库类,实现对图书的维护,查找,删除等 private: int top; /图书记录指针Book bookMaxb; /图书记录public: BDatabase() /构造函数,将book.txt读到book中 Book b; top=-1; fstream file("book.txt",ios:in); while (1) file.read(char *)&b,sizeof(b); if (!file) break; top+; booktop=b; file.close(); void clear() /全删 top=-1; int addbook(int n,char *na,char *aa,char *ca,int oa) /增加图书 Book *p=query1(n); if (NULL=p) top+; booktop.addbook(n,na,aa,ca,oa); return 1; return 0; Book *query1(int bookid) /按编号查找图书 for(int i=0;i<=top;i+) if(booki.getno()=bookid &&booki.gettag()=0) return &booki; return NULL; Book *query2(char a) /按书名查找图书 Book *e; int r=0; for(int i=0;i<=top;i+) if(strcmp(booki.getname(),a)=0 &&booki.gettag()=0) if(r=0) cout<<setw(6)<<"编号"<<setw(18)<<"书名"<<setw(10)<<"作者"<<setw(10)<<"出版社"<<setw(10)<<"存量"<<endl; e=&booki; e->disp(); r+; if(r=0) cout<<"找不到该书!"<<endl; return NULL; Book *query3(char a) /按作者查找图书 Book *e; int r=0; for(int i=0;i<=top;i+) if(strcmp(booki.getauthorname(),a)=0 &&booki.gettag()=0) if(r=0) cout<<setw(6)<<"编号"<<setw(18)<<"书名"<<setw(10)<<"作者"<<setw(10)<<"出版社"<< setw(10)<<"存量"<<endl; e=&booki; e->disp(); r+; if(r=0) cout<<"找不到该书!"<<endl; return NULL; Book *query4(char a) /按出版社查找图书 Book *e; int r=0; for (int i=0;i<=top;i+) if (strcmp(booki.getcbsname(),a)=0 &&booki.gettag()=0) if(r=0)cout<<setw(6)<<"编号"<<setw(18)<<"书名"<<setw(10)<<"作者"<<setw(10)<<"出版社"<<setw(10)<<"存量"<<endl; e=&booki; e->disp(); r+; if(r=0) cout<<"找不到该书!"<<endl; return NULL; void bookdata(); /图书库信息void disp() for(int i=0;i<=top;i+) if(booki.gettag()=0) booki.disp(); BDatabase() /析构函数,将book写到book.txt文件中 fstream file("book.txt",ios:out); for(int i=0;i<=top;i+) if(booki.gettag()=0) file.write(char *)&booki,sizeof(booki); file.close(); ; void BDatabase:bookdata() char choice3; char bname40;char auname40;char cname40;char ch;int choice4;int bookid;int bookod;Book *b; while (choice3!='0') cout<<"n*图 书 信 息*"<<endl; cout<<"* 1.新 增 "<<" 4.查 找 *"<<endl; cout<<"* 2.更 改 "<<" 5.显 示 *"<<endl; cout<<"* 3.删 除 "<<" 0.返 回 *"<<endl; cout<<"*"<<endl; cout<<" 请选择:" cin>>choice3; switch(choice3) case '1': cout<<"请输入新书编号:" cin>>bookid; b=query1(bookid); if(b!=NULL) cout<<"该编号已经存在,不能添加!"<<endl; break; cout<<"请输入新书书名:" cin>>bname; cout<<"请输入新书作者名:" cin>>auname; cout<<"请输入新书出版社:" cin>>cname; cout<<"请输入新书数量:" cin>>bookod; addbook(bookid,bname,auname,cname,bookod); cout<<"添加图书成功!" getch(); break; case '2': cout<<"请输入图书编号:" cin>>bookid; b=query1(bookid); if(b=NULL) cout<<"该图书不存在! "<<endl; break; cout<<"该图书的信息是:"<<endl; cout<<setw(6)<<"编号"<<setw(18)<<"书名"<<setw(10)<<"作者"<<setw(10)<<"出版社"<< setw(10)<<"存量"<<endl; b->disp(); cout<<"是否修改?( y/n ):" cin>>ch; if(ch='y') cout<<"请输入新的书名:" cin>>bname; cout<<"请输入新的作者:" cin>>auname; cout<<"请输入新书出版社:" cin>>cname; cout<<"请输入新的存量:" cin>>bookod; b->setname(bname); b->setonshelf(bookod); cout<<"修改图书成功!" getch(); break; case '3': cout<<"请输入图书编号:" cin>>bookid; b=query1(bookid); if(b=NULL) cout<<"该图书不存在,无法删除!"<<endl; break; b->delbook(); cout<<"删除成功!" getch(); break; case '4': cout<<"n*查找图书*"<<endl; cout<<"* 1.按图书编号查找 *"<<endl; cout<<"* 2.按图书书名查找 *"<<endl; cout<<"* 3.按图书作者查找 *"<<endl; cout<<"* 4.按图书出版社查找*"<<endl; cout<<"* 0. 返回 *"<<endl;cout<<"*"<<endl; cout<<" 请选择:" cin>>choice4; switch(choice4) case 1: cout<<"请输入图书编号:" cin>>bookid; b=query1(bookid); if(b=NULL) cout<<"该图书不存在!" break; cout<<setw(6)<<"编号"<<setw(18)<<"书名"<<setw(10)<<"作者"<<setw(10)<<"出版社"<<setw(10)<<"存量"<<endl; b->disp(); break; case 2: cout<<"请输入图书书名:" cin>>bname; b=query2(bname); break; case 3: cout<<"请输入图书作者:" cin>>auname; b=query3(auname); break; case 4: cout<<"请输入图书出版社:" cin>>cname; b=query4(cname); break; case 0: break; break; case '5': cout<<setw(6)<<"编号"<<setw(18)<<"书名"<<setw(10)<<"作者"<<setw(10)<<"出版社"<< setw(10)<<"存量"<<endl; disp(); getch(); break; case '6': clear(); break; default: break; class Reader/读者的信息描述 private: int tag; /删除标记 1表示已删 0表示未删 int no; /读者编号char name10; /读者姓名 int borbookMaxbor; /所借图书 public: Reader() ; char *getname() /获取姓名 return name; int gettag() /获取删除标记 return tag; int getno() /获取读者编号 return no; void setname(char na) /设置姓名 strcpy(name,na); void delbook() /设置删除标记 1:已删 0:未删 tag=1; void addreader(int n,char *na) /增加读者 tag=0; no=n; strcpy(name,na); for(int i=0;i<Maxbor;i+) borbooki=0; void borrowbook(int bookid) /借书操作 for(int i=0;i<Maxbor;i+) if (borbooki=0) borbooki=bookid; return ; int retbook(int bookid) /还书操作 for(int i=0;i<Maxbor;i+) if(borbooki=bookid) borbooki=0; cout<<"还书成功!"<<endl; return 1; cout<<"未借该书,还书失败!"<<endl; return 0; void disp() /读出读者信息 int have=0; int bz=0; cout<<setw(5)<<no<<setw(21)<<name<<setw(15); for(int i=0;i<Maxbor;i+) if(borbooki!=0) if(bz=0) have=1; cout<<""<<borbooki<<"tt"<<endl; bz+; else cout<<"rttttt"""<<borbooki<<"tt"<<setw(15)<<endl; if(have=0) cout<<"t 还未借书"<<endl;class RDatabase /读者类库,实现建立读者的个人资料 private: int top; /读者记录指针 Reader readMaxr; /读者记录 public: RDatabase() /构造函数,将reader.txt读到read中 Reader s; top=-1; fstream file("reader.txt",ios:in); /打开一个输入文件 while (1) file.read(char *)&s,sizeof(s); if (!file) break; top+; readtop=s; file.close(); /关闭 reader.txt 文件 void clear() /删除所有读者信息 top=-1; int addreader(int n,char *na) /添加读者时先查找是否存在 Reader *p=queryid(n); if (p=NULL) top+; readtop.addreader(n,na); return 1; else cout<<"该编号已经存在!" return 0; Reader *queryid(int readerid) /按读者编号查找 for (int i=0;i<=top;i+) if (readi.getno()=readerid&&readi.gettag()=0) return &readi; return NULL;Reader *queryname(char readername10) /按读者姓名查找 for (int i=0;i<=top;i+) if (strcmp(readi.getname(),readername)=0 && readi.gettag()=0) return &readi; return NULL; void disp() /输出所有读者信息 for(int i=0;i<=top;i+) readi.disp(); void readerdata(); /读者库信息RDatabase() /析构函数,将read写到reader.txt文件中 fstream file("reader.txt",ios:out); for (int i=0;i<=top;i+) if (readi.gettag()=0) file.write(char *)&readi,sizeof(readi); file.close(); ;void RDatabase:readerdata() char choice1; char rname20; int readerid;char readername10;int choice2;Reader *r; while(choice1!='0') cout<<"n*读 者 信 息 *"<<endl; cout<<"* 1.新 增 "<<" 4.查 找 *"<<endl; cout<<"* 2.更 改 "<<" 5.显 示 *"<<endl; cout<<"* 3.删 除 "<<" 0.返 回 *"<<endl; cout<<"*"<<endl; cout<<" 请选择:" cin>>choice1; switch(choice1) case '1': cout<<"请输入读者编号:" cin>>readerid; cout<<"请输入读者姓名:" cin>>rname; addreader(readerid,rname); cout<<"添加读者成功!"<<endl; getch(); break; case '2': cout<<"请输入读者编号:" cin>>readerid; r=queryid(readerid); if(r=NULL) cout<<"该读者不存在! "<<endl; break; cout<<"请输入新的姓名:" cin>>rname; r->setname(rname); cout<<"修改读者成功!"<<endl; getch(); break; case '3': cout<<"请输入读者编号:" cin>>readerid; r=queryid(readerid); if(r=NULL) cout<<"该读者不存在!" << endl; break; r->delbook(); cout<<"删除成功!"<<endl; getch(); break; case '4':cout<<"n*"<<endl; cout<<"* 1.按读者编号查找 *"<<endl; cout<<"* 2.按读者姓名查找 *"<<endl; cout<<"* 0.返 回 *"<<endl;cout<<"*"<<endl; cout<<" 请选择:" cin>>choice2; switch(choice2) case 1: cout<<"请输入读者编号:" cin>>readerid; r=queryid(readerid); if(r=NULL) cout<<"该读者不存在!"<< endl; break; cout<<setw(10)<<"读者编号"<<setw(17)<<"读者姓名"<<setw(20)<<"已借书编号"<<endl; r->disp(); break; case 2: cout<<"请输入读者姓名:" cin>>readername; r=queryname(readername); if(r=NULL) cout<<"该读者不存在!"<<endl; break; cout<<setw(10)<<"读者编号"<<setw(17)<<"读者姓名"<<setw(20)<<"已借书编号"<<endl; r->disp(); break; case 0: break; break; case '5': cout<<setw(10)<<"读者编号"<<setw(17)<<"读者姓名"<<setw(20)<<"已借书编号"<<endl; disp(); getch(); break; case '6': clear(); break; default: break; void main() /实现程序的主界面 char choice5; int bookid,readerid; RDatabase ReaderDB; Reader *r; BDatabase BookDB; Book *b;while(choice5!='0') cout<<"n*图 书 管 理 系 统*"<<endl; cout<<"* 1.图 书 信 息 *"<<endl; cout<<"* 2.读 者 信 息 *"<<endl; cout<<"* 3.借 阅 图 书 *"<<endl; cout<<"* 4.归 还 图 书 *"<<endl; cout<<"* 0.退 出 *"<<endl; cout<<"*"<<endl; cout<<" 请选择:" cin>>choice5; switch (choice5) case '1': BookDB.bookdata(); break; case '2': ReaderDB.readerdata(); break; case '3': cout<<"借书操作"<<endl; cout<<"请输入借书读者编号:" cin>>readerid; r=ReaderDB.queryid(readerid); if(NULL=r) /按编号查找是否有该读者 cout<<"不存在该读者,不能借书!"<<endl; break; cout<<"请输入要借图书编号:" cin>>bookid; b=BookDB.query1(bookid); if(b=NULL) /按编号查找是否有该图书 cout<<"不存在该图书,不能借书!"<<endl; break; if(b->borrowbook()=0) cout<<"该图书已借完,不能借书!"<<endl; break; cout<<"读者借书成功!"<<endl; r->borrowbook(b->getno(); break; case '4': cout<<"还书操作"<<endl; cout<<"请输入还书读者编号:" cin>>readerid; r=ReaderDB.queryid(readerid); if(r=NULL) cout<<"不存在该读者,不能还书"<<endl; break; cout<<"请输入要归还图书编号:" cin>>bookid; b=BookDB.query1(bookid); if(b=NULL) cout<<"不存在该图书,不能还书"<<endl; break; b->retbook(); r->retbook(b->getno(); break; default: break; 专心-专注-专业