这几天没学啥新鲜的,今天就分享一下图书馆代码吧!这个代码比较繁琐,大家可以在里面找找自己需要的信息,希望有所帮助。与君共勉!
#include<bits/stdc++.h> using namespace std; class Book//在这里定义图书类,存书籍信息 { string shuming,bianhao,zuozhe; int money,allshu,outshu=0; public: void setoutshu(int); void setallshu() { cout<<"newnumber:"; int n; cin>>n; allshu=n; } string outshuming(){return shuming;} string outzuozhe(){return zuozhe;} friend ostream& operator<<(ostream& os,const Book& c);//重新定义输入输出流 friend istream& operator>>(istream& is,Book& c); }; ostream& operator<<(ostream& os,const Book& c) { cout<<"bookname/author/note/number/outnumber/herenumber/money"<<endl; os<<c.shuming<<" "<<c.zuozhe<<" "<<c.bianhao<<" "<<c.allshu<<" "<<c.outshu<<" "<<(c.allshu-c.outshu)<<" "<<c.money<<endl; } istream& operator>>(istream& is,Book& c) { cout<<"bookname/author/note/number/money"<<endl; is>>c.shuming>>c.zuozhe>>c.bianhao>>c.allshu>>c.money; } void Book::setoutshu(int b) { outshu=outshu+b; } class Student//定义一个学生类 { string stuname,xuehao,nianji,zhuanye,banji; int canjie=10,hadjie=0,cun=1; public: Student(string n,string x,string nian,string zhu,string ban):stuname(n),xuehao(x),nianji(nian),zhuanye(zhu),banji(ban){} Student(){} string outxuehao(){return xuehao;} string outnianji(){return nianji;} void sethadjie(int); int outcun(){return cun;} void setcun(){cun=0;} void fixstudent(); friend ostream& operator<<(ostream& os,const Student& c); friend istream& operator>>(istream& is,Student& c); }; ostream& operator<<(ostream& os,const Student& c) { cout<<"name/xuehao/nianji/zhuanye/banji/yijie/kejie"<<endl; os<<c.stuname<<" "<<c.xuehao<<" "<<c.nianji<<" "<<c.zhuanye<<" "<<c.banji<<" "<<c.hadjie<<" "<<(c.canjie-c.hadjie)<<endl; } istream& operator>>(istream& is,Student& c) { cout<<"name/xuehao/nianji/zhuanye/banji"<<endl; is>>c.stuname>>c.xuehao>>c.nianji>>c.zhuanye>>c.banji; } void Student::fixstudent() { string newnianji,newzhuanye,newbanji; cout<<"newnianji/newzhuanye/newbanji"<<endl; cin>>newnianji>>newzhuanye>>newbanji; nianji=newnianji; zhuanye=newzhuanye; banji=newbanji; } void Student::sethadjie(int b) { if(b==1) { hadjie++; } else if(b==0) { hadjie--; } else { cout<<"intput error"<<endl; } }//以上两个类作为数据类,下面就是操作类 class Manage//定义管理员类对图书信息与学生信息进行整理 { friend class Record; //vector<Record>allrecord; vector<Student>stuall; map<string,int>findstu; multimap<string,int>byyear; vector<Book>all; multimap<string,int>findbook; public: void addstudent(); void fixstudent(); void offstudent(); void findstudent(); void addbook(); void fixbook(); void obbook(); void findbooks(); void readstudent(); void writestudent(); void readbook(); void writebook(); }; void Manage::readbook()//从文件中读取图示信息 { ifstream in; in.open("C:\\GameDownload\\book.txt"); while(!in.eof()) { string one; getline(in,one); if(one.empty()) continue; istringstream is(one); Book two; is>>two; all.push_back(two); findbook.insert(make_pair(two.outshuming(),all.size()-1)); } in.close(); } void Manage::writebook()//将图书信息写到文件中 { ofstream out; out.open("C:\\GameDownload\\book1.txt"); for(auto it1=all.begin();it1!=all.end();it1++) { out<<(*it1); } out.close(); } void Manage::readstudent() { ifstream in; in.open("C:\\GameDownload\\student.txt"); while(!in.eof()) { string one; getline(in,one); if(one.empty()) continue; istringstream is(one); Student two; is>>two; stuall.push_back(two); findstu.insert(make_pair(two.outxuehao(),stuall.size()-1)); byyear.insert(make_pair(two.outnianji(),stuall.size()-1)); } in.close(); } void Manage::writestudent() { ofstream out; out.open("C:\\GameDownload\\student1.txt"); for(auto it1=stuall.begin();it1!=stuall.end();it1++) { if((*it1).outcun()) out<<(*it1); } out.close(); } void Manage::addstudent() { Student three; cin>>three; map<string,int>::iterator it1; it1=findstu.find(three.outxuehao()); if(it1==findstu.end()) { stuall.push_back(three); findstu.insert(make_pair(three.outxuehao(),stuall.size()-1)); byyear.insert(make_pair(three.outnianji(),stuall.size()-1)); } else { cout<<"already have this note,add defeat"<<endl; } } void Manage::findstudent() { string findxue; cout<<"xuehao:"; cin>>findxue; map<string,int>::iterator it1; it1=findstu.find(findxue); if(it1!=findstu.end()) { if(stuall[it1->second].outcun()) { cout<<stuall[it1->second]; } else { cout<<"not have student"<<endl; } } else { cout<<"not have student"<<endl; } } void Manage::fixstudent() { string findxue; cout<<"xuehao:"; cin>>findxue; map<string,int>::iterator it1; it1=findstu.find(findxue); if(it1!=findstu.end()) { stuall[it1->second].fixstudent(); } else { cout<<"not have student"<<endl; } } void Manage::offstudent() { cout<<"offbyyear/0 offbyone/1"<<endl; int n; cin>>n; if(n==0) { string bynian; cin>>bynian; multimap<string,int>::iterator it1; it1=byyear.lower_bound(bynian); if(it1!=byyear.end()) { for(;it1!=byyear.upper_bound(bynian);it1++) { stuall[it1->second].setcun(); } } } else { string findxue; cout<<"xuehao:"; cin>>findxue; map<string,int>::iterator it1; it1=findstu.find(findxue); if(it1!=findstu.end()) { stuall[it1->second].setcun(); } else { cout<<"not have student"<<endl; } } } void Manage::addbook() { Book one; cin>>one; all.push_back(one); findbook.insert(make_pair(one.outshuming(),all.size()-1)); } void Manage::findbooks() { string zhaoshu,author; cout<<"bookname/author"<<endl; cin>>zhaoshu>>author; multimap<string,int>::iterator it1; it1=findbook.lower_bound(zhaoshu); if(it1!=findbook.end()) { for(;it1!=findbook.upper_bound(zhaoshu);it1++) { if(all[it1->second].outzuozhe()==author) { cout<<all[it1->second]; } } } else { cout<<"not found"<<endl; } } void Manage::fixbook() { string zhaoshu,author; cout<<"bookname/author"<<endl; cin>>zhaoshu>>author; multimap<string,int>::iterator it1; it1=findbook.lower_bound(zhaoshu); if(it1!=findbook.end()) { for(;it1!=findbook.upper_bound(zhaoshu);it1++) { if(all[it1->second].outzuozhe()==author) { all[it1->second].setallshu(); } } } else { cout<<"not found"<<endl; } } void Manage::obbook() { string zhaoshu,author; cout<<"bookname/author"<<endl; cin>>zhaoshu>>author; multimap<string,int>::iterator it1; it1=findbook.lower_bound(zhaoshu); if(it1!=findbook.end()) { for(;it1!=findbook.upper_bound(zhaoshu);it1++) { if(all[it1->second].outzuozhe()==author) { cout<<"outbook/1 backbook/0"<<endl; int n; cin>>n; if(n==1) { all[it1->second].setoutshu(1); } else if(n==0) { all[it1->second].setoutshu(-1); } else { cout<<"intput error"<<endl; } } } } } int main() { int s; Manage two; two.readstudent(); two.readbook(); while(cin>>s) { if(s==1) { two.addstudent(); } else if(s==2) { two.findstudent(); } else if(s==3) { two.fixstudent(); } else if(s==4) { two.offstudent(); } else if(s==5) { two.addbook(); } else if(s==6) { two.findbooks(); } else if(s==7) { two.fixbook(); } else if(s==8) { two.obbook(); } else if(s==9) { return 0; } } two.writestudent(); two.writebook(); }以上代码只实现了对图书和学生的增删查改,图书借阅记录没有完成,过两天再继续完善此代码。