C 图书管理系统代码.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<<"nttt*ͼ Êé ЊϢ*n"<<endl; cout<<"ttt* 1.РÔö *"<<endl; cout<<"ttt* 2.¸ü ¸Ä *"<<endl; cout<<"ttt* 3.ɾ ³ý *"<<endl; cout<<"ttt* 4.²é ÕÒ *"<<endl; cout<<"ttt* 5.ÏÔ Ê¾ *"<<endl; cout<<"ttt* 6.È« ɾ *"<<endl; cout<<"ttt* 0.·µ »Ø *"<<endl; cout<<"ttt ÇëÑ¡Ôñ£º" 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<<"ttt* 1.°´Í¼Êé±àºÅ²éÕÒ *"<<endl; cout<<"ttt* 2.°´Í¼ÊéÊéÃû²éÕÒ *"<<endl; cout<<"ttt* 3.°´Í¼Êé×÷Õß²éÕÒ *"<<endl; cout<<"ttt* 4.°´Í¼Êé³ö°æÉç²éÕÒ*"<<endl; cout<<"ttt* 0.·µ »Ø *"<<endl; cout<<"ttt ÇëÑ¡Ôñ:" 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<<"nttt* ¶Á Õß ÐŠϢ *n"<<endl; cout<<"ttt* 1.РÔö *"<<endl; cout<<"ttt* 2.¸ü ¸Ä *"<<endl; cout<<"ttt* 3.ɾ ³ý *"<<endl; cout<<"ttt* 4.²é ÕÒ *"<<endl; cout<<"ttt* 5.ÏÔ Ê¾ *"<<endl; cout<<"ttt* 6.È« ɾ *"<<endl; cout<<"ttt* 0.·µ »Ø *"<<endl; cout<<"ttt ÇëÑ¡Ôñ£º" 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<<"ttt* 1.°´¶ÁÕß±àºÅ²éÕÒ *"<<endl; cout<<"ttt* 2.°´¶ÁÕßÐÕÃû²éÕÒ *"<<endl; cout<<"ttt* 0.·µ »Ø *"<<endl; cout<<"ttt ÇëÑ¡Ôñ:" 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;cout<<"ttt »¶Ó¹âÁÙͼÊé¹Ý¹ÜÀíϵͳ!"<<endl;while(choice5!='0') cout<<"nttt* ͼ Êé ¹Ü Àí ϵ ͳ*nn" cout<<"ttt* 1.ͼ Êé ЊϢ *"<<endl; cout<<"ttt* 2.¶Á Õß ÐŠϢ *"<<endl; cout<<"ttt* 3.½è ÔÄ Í¼ Êé *"<<endl; cout<<"ttt* 4.¹é »¹ ͼ Êé *"<<endl; cout<<"ttt* 0.ÍË ³ö *"<<endl; cout<<"ttt ÇëÑ¡Ôñ£º" cin>>choice5; switch (choice5) case '1': BookDB.bookdata(); break; case '2': ReaderDB.readerdata(); break; case '3': cout<<"tttt½èÊé²Ù×÷"<<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<<"tttt»¹Êé²Ù×÷"<<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(); br