老師規定的不用選單寫的簡單圖書管理系統
阿新 • • 發佈:2018-11-19
#include<bits/stdc++.h> using namespace std; class Time { int month,day; public: Time (int b,int c) { month=b;day=c; } Time () { month=0;day=0; } void settime(int b,int c){month=b;day=c;} int getmonth(){return month;} int getday(){return day;} bool operator<(const Time &a)const { if(month<a.month) return true; else if ((month==a.month)&&day<a.day) return true; return false; } bool operator==(const Time &a)const { if (month==a.month&&day==a.day) return true; return false; } bool operator>(const Time &a)const { if(month>a.month) return true; else if ((month==a.month)&&day>a.day) return true; return false; } friend ostream &operator<<(ostream &os,const Time &); friend istream &operator>>(istream &is,Time &); }; ostream &operator<<(ostream &os,const Time &t) { os<<t.month<<" "<<t.day; return os; } istream &operator>>(istream &is,Time &t) { is>>t.month>>t.day; return is; } class record { string no; Time t1; Time t2; string bname; string autor; int num;//1 正在借閱中 0 不在借閱中 public: record (string a,Time b,string c,string d) { no=a;t1=b;bname=c;autor=d;t2.settime(t1.getmonth()+3,t1.getday());num=0; } record(){}; friend ostream &operator<<(ostream &os,const record &); friend istream &operator>>(istream &os,record &); string getno(){return no;} Time gettime(){return t1;} void setnum(){num=1;} string getbname(){return bname;} }; ostream &operator<<(ostream &os,const record &r) { os<<"借閱記錄:"<<endl; os<<"學號:"<<" "<<r.no<<endl; os<<"書名:"<<" "<<r.bname<<endl; os<<"作者:"<<" "<<r.autor<<endl; os<<"借書日期:"<<" "<<r.t1<<endl; os<<"還書日期:"<<" "<<r.t2<<endl; os<<"是否借閱中:"<<" "<<r.num<<endl; return os; //os<<r.no<<" "<<r.bname<<" "<<r.autor<<" "<<r.t1<<" "<<r.t2<<" "<<r.num<<endl; //return os; } istream &operator>>(istream &is,record &r) { string q; is>>q; is>>q>>r.no; if(r.no=="-1") return is; is>>q>>r.bname>>q>>r.autor>>q>>r.t1>>q>>r.t2>>q>>r.num; return is; } class book { int id; string name; string autor; string publish; int num; vector<record> r; vector<record>::iterator it; public: book (int f,string b,string c,string d,int a) { id=f;name=b;autor=c;publish=d; num=a; } book (){} int getid() {return id;} void setnum(int a){num=a;} string getname(){return name;} string getautor(){return autor;} string getpublish(){return publish;} void ser(string a); void add(record a){r.push_back(a);} int getnum(){return num;} friend ostream &operator<<(ostream &os,const book &); friend istream &operator>>(istream &is,book &); friend class manageop; friend class studentop; }; void book::ser(string a)//修改借閱記錄 { for(int i=0;i<r.size();i++) if(r[i].getno()==a) {r[i].setnum(); setnum(getnum()+1); break;} } ostream &operator<<(ostream &os,const book &b) { os<<"書號:"<<" "<<b.id<<endl; os<<"書名:"<<" "<<b.name<<endl; os<<"作者:"<<" "<<b.autor<<endl; os<<"出版社:"<<" "<<b.publish<<endl; os<<"庫存:"<<" "<<b.num<<endl; return os; } istream &operator>>(istream &is,book &b) { record a; string q; is>>q>>b.id>>q>>b.name>>q>>b.autor>>q>>b.publish>>q>>b.num; return is; } class student { string no; string name; int num; vector<record> r; vector<record>::iterator it; public: student (string a,string b) { no=a;name=b;num=0; } student (){}; string getno(){return no;} string getname(){return name;} void ser(string a);//修改學生借閱記錄 int getnum(){return num;} void setnum(int a){num=a;} void add(record a){r.push_back(a);}//增加記錄 friend istream &operator>>(istream &is,student &); friend ostream &operator<<(ostream &os,const student &); friend class manageop; friend class studentop; }; void student::ser(string a)//修改學生借閱記錄,a書名 { for(int i=0;i<r.size();i++) if(r[i].getbname()==a) {r[i].setnum(); setnum(getnum()-1); break;} } istream &operator>>(istream &is,student &s) { string q; is>>q>>s.no>>q>>s.name>>q>>s.num; return is; } ostream &operator<<(ostream &os,const student &s) { os<<"學號:"<<" "<<s.no<<endl<<"學生姓名:"<<" "<<s.name<<endl<<"借閱量:"<<" "<<s.num<<endl; return os; } class manageop { vector<book> b; vector<student> s; vector<student>::iterator pos; vector<book>::iterator ra; multimap<string,int> m1;//學號查詢 multimap<string,int>::iterator mit1; multimap<string,int> m3;//書籍查詢 multimap<string,int>::iterator mit3; public: manageop() { loads();loadb(); } ~manageop() { saves();saveb(); } int searchs(string a);//通過學號查 int searchb(string a);//通過書名查 void adds();//學生加記錄 void addb();//書加記錄 void addbook();//增加多本圖書 void addstu();//增加多名學生使用者 void loads(); void loadb(); void saves(); void saveb(); }; /*void manageop::addbook() { book o; int i; cin>>o; if(o.getid==-1) break; else b.push_back(o); }*/ void manageop::adds() { string a,d; int o; cout<<"請輸入你的學號: "<<endl; cin>>a; cout<<"請輸入你的姓名: "<<endl; cin>>d; student c(a,d); if(searchs(a)==-1) {s.push_back(c); o=s.size()-1; m1.insert(make_pair(c.getno(),o));}//存 else {cout<<"該學號已被註冊,請重新輸入:"<<endl; adds();} } void manageop::addb()//增加書的記錄 { int f; string a,d,c; int e,o; cout<<"請輸入書號: "<<endl; cin>>f; cout<<"請輸入書名: "<<endl; cin>>a; cout<<"請輸入作者: "<<endl; cin>>d; cout<<"請輸入出版社: "<<endl; cin>>c; cout<<"請輸入書的數量: "<<endl; cin>>e; book p(f,a,d,c,e); b.push_back(p); o=b.size()-1; m3.insert(make_pair(p.getname(),o)); } int manageop::searchs(string a)//學號 { mit1=m1.find(a); if(mit1!=m1.end()) return mit1->second; else return -1; } int manageop::searchb(string a)//查書 { mit3=m3.find(a); if(mit3!=m3.end()) return mit3->second; else return -1; } void manageop::saves() { ofstream outfile("c:\\20171819\\20171819stu.txt",ios::out); if(!outfile) return; for(int i=0;i<s.size();++i) { outfile<<s[i]; for(int j=0;j<s[i].r.size();j++) outfile<<s[i].r[j]; outfile<<"-1"<<endl; } outfile.close(); } void manageop::saveb() { ofstream outfile("c:\\20171819\\20171819book.txt",ios::out); if(!outfile) return; for(int i=0;i<b.size();++i) { outfile<<b[i]; for(int j=0;j<b[i].r.size();j++) outfile<<b[i].r[j]; outfile<<"-1"<<endl; } outfile.close(); } void manageop::loads() { student a; record f; int o,p; ifstream infile("c:\\20171819\\20171819stu.txt",ios::in); if(!infile) return; s.clear(); m1.clear(); while(infile>>a) {s.push_back(a); o=s.size()-1; m1.insert(make_pair(a.getno(),o)); while(infile>>f) if(f.getno()!="-1") {s[o].r.push_back(f); } else break;} infile.close(); } void manageop::loadb() { book a; record f; int o,p; ifstream infile("c:\\20171819\\20171819book.txt",ios::in); if(!infile) return; b.clear(); m3.clear(); while(infile>>a) {b.push_back(a); o=b.size()-1; m3.insert(make_pair(a.getname(),o)); while(infile>>f) if(f.getno()!="-1") b[o].r.push_back(f); else break;} infile.close(); } class studentop { student s; vector<book> b; vector<book>::iterator ra; multimap<string,int> m3; multimap<string,int>::iterator mit3; public: studentop(string a) { loadb(); loads(a); } ~studentop() { saveb(); saves(); } void loadb(); void saveb(); void loads(string a); void saves(); void lend();//借書 int searchb(string a); void repay();//還書 }; void studentop::loads(string a) { student c; record d; int i=0; ifstream infile("c:\\20171819\\20171819stu.txt",ios::in); if(!infile) return; while(infile>>c) { c.r.clear(); while(infile>>d) if(d.getno()!="-1") {c.r.push_back(d);} else break; if(c.getno()==a) {s=c;i=1;break;} } if(i!=1) cout<<"您輸入的學號不正確"<<endl; infile.close(); } void studentop::saves() { vector<student> v; student c; record d; int i=0; ifstream infile("c:\\20171819\\20171819stu.txt",ios::in); if(!infile) return; while(infile>>c) { c.r.clear(); v.push_back(c); while(infile>>d) if(d.getno()!="-1") {c.r.push_back(d);} else break; if(c.getno()==s.getno()) v[i]=s; else v[i]=c; i++; } infile.close(); ofstream outfile("c:\\20171819\\20171819stu.txt",ios::out); if(!outfile) return; for(int j=0;j<v.size();++j) { outfile<<v[j]; for(int h=0;h<v[j].r.size();h++) {outfile<<v[j].r[h];} outfile<<"-1"<<endl; } outfile.close(); } void studentop::loadb() { book a; record f; int o; ifstream infile("c:\\20171819\\20171819book.txt",ios::in); if(!infile) return; b.clear(); m3.clear(); while(infile>>a) {b.push_back(a); o=b.size()-1; m3.insert(make_pair(a.getname(),o)); while(infile>>f) if(f.getno()!="-1") b[o].r.push_back(f); else break;} infile.close(); } void studentop::saveb() { ofstream outfile("c:\\20171819\\20171819book.txt",ios::out); if(!outfile) return; for(int i=0;i<b.size();++i) { outfile<<b[i]; for(int j=0;j<b[i].r.size();j++) {outfile<<b[i].r[j];} outfile<<"-1"<<endl; } outfile.close(); } void studentop::lend() { string e,f,a; a=s.getno(); int h,j; Time o; cout<<"請輸入書籍名"<<endl; cin>>e; j=searchb(e); if(j!=-1) {if(b[j].getnum()!=0) {if(s.getnum()<10) { cout<<"請輸入借書日期"<<endl; cin>>o; record r(a,o,e,b[j].getautor()); s.add(r); b[j].add(r); b[j].setnum(b[j].getnum()-1);//修改書借閱記錄 s.setnum(s.getnum()+1);//修改學生借閱記錄 cout<<"借書成功"<<endl; } else cout<<"對不起您的借閱量已達上限"<<endl; } else cout<<"對不起,目前該書籍的庫存不足"<<endl; } else cout<<"未查詢到該書籍"<<endl; } void studentop::repay() { string e,a; a=s.getno(); cout<<"請輸入書籍名"<<endl; cin>>e; int j; j=searchb(e); s.ser(e);//呼叫上面函式 b[j].ser(a);//呼叫上面函式 cout<<"還書成功"<<endl; } int studentop::searchb(string a) { mit3=m3.find(a); if(mit3!=m3.end()) return mit3->second; else return -1; } int main() { manageop m; m.adds(); m.addb(); string a; cout<<"請輸入學號:"<<endl; cin>>a; studentop s1(a); s1.lend(); s1.repay(); }
最後的主函式應該先實行增加記錄操作,完成後註釋掉。 第二步完成借書操作,生成借書記錄。第三步完成還書操作,生成記錄