您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > 图书管理系统C++代码
#includeiostream#includeiomanip#includestring#includefstreamusingnamespacestd;constintReader=100;constintMaxb=100;constintBor=5;classStudent{private:inttag;intnumber;charname[10];intborbook[Bor];public:Student(){}char*getname(){returnname;}intgettag(){returntag;}intgetnumber(){returnnumber;}voidsetname(char*na){strcpy(name,na);}voiddelbook(){tag=1;}voidaddStudent(intn,char*na){tag=0;number=n;strcpy(name,na);for(inti=0;iBor;i++)borbook[i]=0;}voidborrowbook(intbookid)//借书操作{for(inti=0;iBor;i++){if(borbook[i]==0){borbook[i]=bookid;return;}}}intretbook(intbookid)//还书操作{for(inti=0;iBor;i++){if(borbook[i]==bookid){borbook[i]=0;return1;}}return0;}voidoutput()//读出读者信息{coutsetw(5)numbersetw(10)name借书编号:[;for(inti=0;iBor;i++)if(borbook[i]!=0)coutborbook[i]|;cout]endl;}};classRData{private:inttop;//读者记录指针Studentread[Reader];public:RData()//构造函数,将Student.txt读到read[]中{Students;top=-1;fstreamfile(Student.txt,ios::in);//打开一个输入文件while(1){file.read((char*)&s,sizeof(s));if(!file)break;top++;read[top]=s;}file.close();}voidclear(){top=-1;}intaddStudent(intn,char*na)//查找是否存在{Student*p=equal(n);if(p==NULL){top++;read[top].addStudent(n,na);return1;}return0;}Student*equal(intStudentid)//按编号查找{for(inti=0;i=top;i++)if(read[i].getnumber()==Studentid&&read[i].gettag()==0){return&read[i];}returnNULL;}voidoutput(){for(inti=0;i=top;i++)read[i].output();}voidStudentdata();//读者库维护~RData()//析构函数,将read[]写到Student.txt文件中{fstreamfile(Student.txt,ios::out);for(inti=0;i=top;i++)if(read[i].gettag()==0)file.write((char*)&read[i],sizeof(read[i]));file.close();}};voidRData::Studentdata(){charchoice;charrname[20];intStudentid;Student*r;while(choice!='0'){cout┏━━━━━━━━━━━━━┓\n;cout┃读者维护┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃1.新增┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃2.更改┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃3.删除┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃4.查找┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃5.显示┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃6.全删┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃0.退出┃\n;cout┗━━━━━━━━━━━━━┛\n;//coutcinchoice;switch(choice){case'1':cout输入读者编号:;cinStudentid;cout输入读者姓名:;cinrname;addStudent(Studentid,rname);break;case'2':cout输入读者编号:;cinStudentid;r=equal(Studentid);if(r==NULL){cout该读者不存在endl;break;}cout输入新的姓名:;cinrname;r-setname(rname);break;case'3':cout输入读者编号:;cinStudentid;r=equal(Studentid);if(r==NULL){cout该读者不存在endl;break;}r-delbook();break;case'4':cout读入读者编号:;cinStudentid;r=equal(Studentid);if(r==NULL){cout该读者不存在endl;break;}r-output();break;case'5':output();break;case'6':clear();break;default:cout退出:\n;system(cls);break;}}}classBook{private:inttag;intnumber;charname[20];intonshelf;public:Book(){}char*getname(){returnname;}intgetnumber(){returnnumber;}intgettag(){returntag;}voidsetname(charna[]){strcpy(name,na);}voiddelbook(){tag=1;}voidaddbook(intn,char*na){tag=0;number=n;strcpy(name,na);onshelf=1;}intborrowbook()//借书操作{if(onshelf==1){onshelf=0;return1;}return0;}voidretbook()//还书操作{onshelf=1;}voidoutput()//输出图书{coutsetw(6)numbersetw(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=equal(n);if(NULL==p){top++;book[top].addbook(n,na);return1;}return0;}Book*equal(intbookid){for(inti=0;i=top;i++)if(book[i].getnumber()==bookid&&book[i].gettag()==0){return&book[i];}returnNULL;}voidbookdata();voidoutput(){for(inti=0;i=top;i++)if(book[i].gettag()==0)book[i].output();}~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;cout┃图书维护┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃1.新增┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃2.更改┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃3.删除┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃4.查找┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃5.显示┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃6.全删┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃0.退出┃\n;cout┗━━━━━━━━━━━━━┛\n;cinchoice;switch(choice){case'1':cout输入图书编号:endl;cinbookid;cout输入图书书名:endl;cinbname;addbook(bookid,bname);break;case'2':cout输入图书编号:endl;cinbookid;b=equal(bookid);if(b==NULL){cout该图书不存在endl;break;}cout输入新的书名:endl;cinbname;b-setname(bname);break;case'3':cout读入图书编号:endl;cinbookid;b=equal(bookid);if(b==NULL){cout该图书不存在endl;break;}b-delbook();break;case'4':cout读入图书编号:endl;cinbookid;b=equal(bookid);if(b==NULL){cout该图书不存在endl;break;}b-output();break;case'5':output();break;case'6':clear();break;default:cout退出\n;system(cls);break;}}}voidmain(){charchoice;intbookid,Studentid;RDataStudentDB;Student*r;BDatabaseBookDB;Book*b;while(choice!='0'){cout┏━━━━━━━━━━━━━┓\n;cout┃图书管理系统┃\n;cout┃━━━━━━━━━━━━━┃\n;cout┃1.借书┃\
本文标题:图书管理系统C++代码
链接地址:https://www.777doc.com/doc-3967301 .html