图书馆源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeiostream#includeiomanip#includestring#includefstream//输入/输出文件流类usingnamespacestd;constintMaxr=100;//最多的读者constintMaxb=100;//最多的图书constintMaxbor=5;//每位读者最多借五本书//读者类,实现对读者的信息的描述classReader{private:inttag;//删除标记1:已删0:未删intno;//读者编号charname[10];//读者姓名intborbook[Maxbor];//所借图书public:Reader(){}char*getname(){returnname;}//获取姓名intgettag(){returntag;}//获取删除标记intgetno(){returnno;}//获取读者编号voidsetname(charna[])//设置姓名{strcpy(name,na);}voiddelbook(){tag=1;}//设置删除标记1:已删0:未删voidaddreader(intn,char*na)//增加读者{tag=0;no=n;strcpy(name,na);for(inti=0;iMaxbor;i++)borbook[i]=0;}voidborrowbook(intbookid)//借书操作{for(inti=0;iMaxbor;i++){if(borbook[i]==0){borbook[i]=bookid;return;}}}intretbook(intbookid)//还书操作{for(inti=0;iMaxbor;i++){if(borbook[i]==bookid){borbook[i]=0;return1;}}return0;}voiddisp()//读出读者信息{coutsetw(5)nosetw(10)name借书编号:[;for(inti=0;iMaxbor;i++)if(borbook[i]!=0)coutborbook[i]|;cout]endl;}};//读者类库,实现建立读者的个人资料classRDatabase{private:inttop;//读者记录指针Readerread[Maxr];//读者记录public:RDatabase()//构造函数,将reader.txt读到read[]中{Readers;top=-1;fstreamfile(reader.txt,ios::in);//打开一个输入文件while(1){file.read((char*)&s,sizeof(s));if(!file)break;top++;read[top]=s;}file.close();//关闭reader.txt}voidclear()//删除所有读者信息{top=-1;}intaddreader(intn,char*na)//添加读者时先查找是否存在{Reader*p=query(n);if(p==NULL){top++;read[top].addreader(n,na);return1;}return0;}Reader*query(intreaderid)//按编号查找{for(inti=0;i=top;i++)if(read[i].getno()==readerid&&read[i].gettag()==0){return&read[i];}returnNULL;}voiddisp()//输出所有读者信息{for(inti=0;i=top;i++)read[i].disp();}voidreaderdata();//读者库维护~RDatabase()//析构函数,将read[]写到reader.txt文件中{fstreamfile(reader.txt,ios::out);for(inti=0;i=top;i++)if(read[i].gettag()==0)file.write((char*)&read[i],sizeof(read[i]));file.close();}};voidRDatabase::readerdata(){charchoice;charrname[20];intreaderid;Reader*r;while(choice!='0'){cout\n\n\t\t\t读者维护\n\n\n\t\t1新增\n\n\t\t2更改\n\n\t\t3删除\n\n\t\t4查找\n\n\t\t5显示\n\n\t\t6全删\n\n\t\t0退出endl;cinchoice;switch(choice){case'1':cout输入读者编号:;cinreaderid;cout输入读者姓名:;cinrname;addreader(readerid,rname);break;case'2':cout输入读者编号:;cinreaderid;r=query(readerid);if(r==NULL){cout该读者不存在endl;break;}cout输入新的姓名:;cinrname;r-setname(rname);break;case'3':cout输入读者编号:;cinreaderid;r=query(readerid);if(r==NULL){cout该读者不存在endl;break;}r-delbook();break;case'4':cout读入读者编号:;cinreaderid;r=query(readerid);if(r==NULL){cout该读者不存在endl;break;}r-disp();break;case'5':disp();break;case'6':clear();break;default:cout输入错误,请从新输入:;break;}}}//图书类,实现对图书的描述,图书的编号,书名,借出,还入等classBook{private:inttag;//删除标记1:已删0:未删intno;//图书编号charname[20];//书名intonshelf;//是否再架1:再架2:已借public:Book(){}char*getname(){returnname;}//获取姓名intgetno(){returnno;}//获取图书编号intgettag(){returntag;}//获取删除标记voidsetname(charna[])//设置书名{strcpy(name,na);}voiddelbook(){tag=1;}//删除图书voidaddbook(intn,char*na)//增加图书{tag=0;no=n;strcpy(name,na);onshelf=1;}intborrowbook()//借书操作{if(onshelf==1){onshelf=0;return1;}return0;}voidretbook()//还书操作{onshelf=1;}voiddisp()//输出图书{coutsetw(6)nosetw(18)namesetw(10)(onshelf==1?在架:已借)endl;}};//图书库类,实现对图书的维护,查找,删除等classBDatabase{private:inttop;//图书记录指针Bookbook[Maxb];//图书记录public:BDatabase()//构造函数,将book.txt读到book[]中{Bookb;top=-1;fstreamfile(book.txt,ios::in);while(1){file.read((char*)&b,sizeof(b));if(!file)break;top++;book[top]=b;}file.close();}voidclear()//全删{top=-1;}intaddbook(intn,char*na)//增加图书{Book*p=query(n);if(NULL==p){top++;book[top].addbook(n,na);return1;}return0;}Book*query(intbookid)//查找图书{for(inti=0;i=top;i++)if(book[i].getno()==bookid&&book[i].gettag()==0){return&book[i];}returnNULL;}voidbookdata();//图书库维护voiddisp(){for(inti=0;i=top;i++)if(book[i].gettag()==0)book[i].disp();}~BDatabase()//析构函数,将book[]写到book.txt文件中{fstreamfile(book.txt,ios::out);for(inti=0;i=top;i++)if(book[i].gettag()==0)file.write((char*)&book[i],sizeof(book[i]));file.close();}};voidBDatabase::bookdata(){charchoice;charbname[40];intbookid;Book*b;while(choice!='0'){cout\n\n\n\t\t\t图书维护endlendl;cout\t\t1新增\n\t\t2更改\n\t\t3删除\n\t\t4查找\n\t\t5显示\n\t\t6全删\n\t\t0退出endl;cinchoice;switch(choice){case'1':cout输入图书编号:endl;cinbookid;cout输入图书书名:endl;cinbname;addbook(bookid,bname);break;case'2':cout输入图书编号:endl;cinbookid;b=query(bookid);if(b==NULL){cout该图书不存在endl;break;}cout输入新的书名:endl;cinbname;b-setname(bname);break;case'3':cout读入图书编号:endl;cinbookid;b=query(bookid);if(b==NULL){cout该图书不存在endl;break;}b-delbook();break;case'4':cout读入图书编号:endl;cinbookid;b=query(bookid);if(b==NULL){cout该图书不存在endl;break;}b-disp();break;case'5':disp();break;case'6':clear();break;default:cout输入错误,请从新输入:;}}}//main()函数的实现,程序的主界面的引导voidmain(){charchoice;intbookid,readerid;RDatabaseReaderDB;Reader*r;BDatabaseBookDB;Book*b;while(choice!='0'){coutendlendl\t\t\t图书管理系统\n\n\n;cout\t\t\t1借书\n\n\t\t\t2还书\n\n\t\t\t3图书维护\n\n\t\t\t4读者维护\n\n\t\t\t0离开endl;cinchoice;switch(choice){case'1':cout借书读者编号:;cinreaderid;cout图书编号:;cinbookid;r=ReaderDB.query(readerid);//按编号查找if(N

1 / 25
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功