C++課程設計之圖書館管理系統(tǒng)
本文實例為大家分享了C++課程設計之圖書館管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一.代碼
#include<bits/stdc++.h> using namespace std; class Date { ? ? int year,month,day; public: ? ? Date(int x,int y,int z):year(x),month(y),day(z){} ? ? Date(){year=month=day=0;} ? ? void setDate(int x,int y,int z){year=x;month=y;day=z;} ? ? void setYear(int x){year=x;} ? ? void setMonth(int x){month=x;} ? ? void setDay(int x){day=x;} ? ? ? ? int getYear(){return year;} ? ? int getMonth(){return month;} ? ? int getDay(){return day;} ? ? friend ostream& operator<<(ostream &out,Date &t ); ? ? friend istream& operator>>(istream &in,Date &t); ? ? bool operator<(const Date &t); }; ostream& operator<<(ostream &out,Date &t ) { ? ? out<<t.year<<" "; ? ? out<<t.month<<" "; ? ? out<<t.day; ? ? return out; } istream& operator>>(istream &in,Date &t) { ? ? in>>t.year>>t.month>>t.day; ? ? return in; } bool Date::operator<(const Date &t) { ? ? return year!=t.year?year<t.year:month!=t.month?month<t.month:day<t.day; } class Record { ? ? int uid; ? ? int bid; ? ? Date start; ? ? Date end;//默認end為應該還書的時間 ? ? int xj;//1表示已經(jīng)續(xù)借過,0時還未續(xù)借 ? ? int sy;//1時已經(jīng)還書,0時未還書 public: ? ? Record(int a,int b,Date c):uid(a),bid(b),start(c) ? ? { ? ? ? ?int i,j,k; ? ? ? ?i=start.getYear(); ? ? ? ?j=start.getMonth(); ? ? ? ?k=start.getDay(); ? ? ? ?if(j+2<=12) ? ? ? ?{ ? ? ? ? ? ?end.setDate(i,j+2,k); ? ? ? ?} ? ? ? ?else ?end.setDate(i+1,j+2-12,k); ? ? ? ?xj=0;sy=0; ? ? } ? ? Record(){} ? ? int getUid(){return uid;} ? ? int getBid(){return bid;} ? ? Date getS(){return start;} ? ? ? ? Date getE(){return end;} ? ? int getxj(){return xj;} ? ? int getsy(){return sy;} ? ? ? ? void setE();//用于續(xù)借時修改應該還書的時間,即將end再加兩個月 ? ? void setxj(int x){xj=x;} ? ? void setsy(int x){sy=x;} ? ? ? ? friend ostream& operator<<(ostream &out,Record &r ); ? ? friend istream& operator>>(istream &in,Record &r); ? ? void display(); }; void Record::setE() { ? ? int i,j,k; ? ? i=end.getYear(); ? ? j=end.getMonth(); ? ? k=end.getDay(); ? ? if(j+2<=12) ? ? { ? ? ? ? end.setDate(i,j+2,k); ? ? } ? ? else ?end.setDate(i+1,j+2-12,k); } ostream& operator<<(ostream &out,Record &r) { ? ? out<<r.uid<<" "; ? ? out<<r.bid<<" "; ? ? out<<r.start<<" "; ? ? out<<r.end<<" "; ? ? out<<r.xj<<" "; ? ? out<<r.sy<<" "; ? ? return out; } istream& operator>>(istream &in,Record &r) { ? ? in>>r.uid>>r.bid>>r.start>>r.end>>r.xj>>r.sy; ? ? return in; } void Record::display() { ? ? cout<<"借閱人:"<<uid<<" "<<"書號:"<<bid<<" "<<"借書日期:"<<start<<" "<<"應還書日期:"<<end<<" "<<"是否續(xù)借過:"<<xj<<" "<<"是否已還書:"<<sy<<endl; } class Book { ? ? int id;//書號 ? ? string name;//書名 ? ? string press;//出版社 ? ? Date pd;//出版日期 ? ? int sum,ln;//總冊數(shù),已借出 ? ? int i; public: ? ? vector<Record>v1; ? ? vector<Record>::iterator it1; ? ? multimap<int,int>m1;//根據(jù)借閱人的學號建立 ? ? multimap<int,int>::iterator mit1; ? ? Book(int a,string b,string c,Date d,int e,int f):id(a),name(b),press(c),pd(d),sum(e),ln(f){i=v1.size();} ? ? Book(){} ? ? int getId(){return id;} ? ? string getName(){return name;} ? ? string getPress(){return press;} ? ? Date getPd(){return pd;} ? ? ? ? int getSum(){return sum;} ? ? int getLn(){return ln;} ? ? ? ? void setSum(int x){sum=x;} ? ? void setLn(int x){ln=x;} ? ? ? ? void addRecord(Record r); ? ? void dispRecord(); ? ? int search(int x);//根據(jù)借閱人的學號查詢 ? ? void display(); ? ? void display1(); ? ? friend istream& operator>>(istream &in,Book &b); ? ? friend ostream& operator<<(ostream &out,Book &b); }; istream& operator>>(istream &in,Book &b) { ? ? in>>b.id>>b.name>>b.press>>b.pd>>b.sum>>b.ln>>b.i; ? ? int s=0; ? ? if(b.i!=0) ? ? { ? ? ? ? while(s!=b.i) ? ? ? ? { ? ? ? ? ? ? in>>b.v1[s];s++; ? ? ? ? } ? ? } ? ? return in; } ostream& operator<<(ostream &out,Book &b) { ? ? out<<b.id<<" "<<b.name<<" "<<b.press<<" "<<b.pd<<" "<<b.sum<<" "<<b.ln<<" "<<endl; ? ? if(b.v1.size()!=0) ? ? { ? ? ? ?out<<b.v1.size()<<endl; ? ? ? ?for(int i=0;i<b.v1.size();i++) ? ? ? ?{ ? ? ? ? ? ?out<<b.v1[i]; ? ? ? ? ? ?if(i!=b.v1.size()-1) ? ? ? ? ? ? ? out<<endl; ? ? ? ?} ? ? } ? ? else ? ? ? ? out<<b.v1.size(); ? ? return out; } void Book::addRecord(Record r) { ? ? int i; ? ? v1.push_back(r); ? ? i=v1.size(); ? ? m1.insert(make_pair(r.getUid(),i-1)); } void Book::dispRecord() { ? ? for(int i=0;i<v1.size();i++) ? ? ? ? v1[i].display(); } int Book::search(int x) { ? ? mit1=m1.find(x); ? ? if(mit1!=m1.end()) ? ? { ? ? ? ? return mit1->second;//返回的是這本書存儲的關(guān)于借閱人x的借閱記錄的下標值 ? ? } ? ? else return -1; } void Book::display() { ? ? cout<<"書號:"<<id<<" "<<"書名:"<<name<<" "<<"出版社:"<<press<<" "<<"出版日期:"<<pd<<" "<<"總冊數(shù):"<<sum<<" "<<"已借出:"<<ln<<endl; ? ? if(v1.size()!=0) ? ? { ? ? ? ? dispRecord(); ? ? } } void Book::display1() { ? ? cout<<"書號:"<<id<<" "<<"書名:"<<name<<" "<<"出版社:"<<press<<" "<<"出版日期:"<<pd<<" "<<"總冊數(shù):"<<sum<<" "<<"已借出:"<<ln<<endl; } class User { ? ? int id; ? ? string name; ? ? string major; ? ? int mb,nb;//mb為最大借閱量,nb為當前借閱量 ? ? int wj;//1為違紀過,此時無法借書; ? ? int i; public: ? ? vector<Record>v2; ? ? vector<Record>::iterator it2; ? ? multimap<int,int>m2;//根據(jù)所借書的書號建立 ? ? multimap<int,int>::iterator mit2; ? ? User(int a,string b,string c):id(a),name(b),major(c) ? ? { ? ? ? ? mb=10;nb=0;wj=0;i=v2.size(); ? ? } ? ? User(){i=v2.size();} ? ? int getId(){return id;} ? ? string getName(){return name;} ? ? string getMajor(){return major;} ? ? ? ? int getmb(){return mb;} ? ? int getnb(){return nb;} ? ? int getwj(){return wj;} ? ? ? ? void setmb(int x){mb=x;} ? ? void setnb(int x){nb=x;} ? ? void setwj(){wj=1;mb=0;} ? ? ? ? void addRecord(Record r); ? ? void dispRecord(); ? ? int search(int no);//通過書號查找 ? ? void display(); ? ? ? ? bool operator ==( User &u) ? ? { ? ? ? ? if(this->id==u.getId()) ? ? ? ? return 1; ? ? ? ? else return 0; ? ? } ? ? User & operator =(User &u) ? ? { ? ? ? ? id=u.id; ? ? ? ? name=u.name; ? ? ? ? major=u.major; ? ? ? ? mb=u.mb; ? ? ? ? nb=u.nb; ? ? ? ? wj=u.wj; ? ? ? ? v2=u.v2; ? ? ? ? m2=u.m2; ? ? } ? ? friend ostream& operator<<(ostream &out,User &u); ? ? friend istream& operator>>(istream &in,User &u); }; ostream& operator<<(ostream &out,User &u) { ? ? out<<u.id<<" "<<u.name<<" "<<u.major<<" "<<u.mb<<" "<<u.nb<<" "<<u.wj<<" "<<endl; ? ? if(u.v2.size()!=0) ? ? { ? ? ? ? out<<u.v2.size()<<endl; ? ? ? ? for(int i=0;i<u.v2.size();++i) ? ? ? ? { ? ? ? ? ? ? out<<u.v2[i]; ? ? ? ? ? ? if(i!=u.v2.size()-1) ? ? ? ? ? ? ? ?out<<endl; ? ? ? ? } ? ? } ? ? else ? ? ? ? out<<u.v2.size(); ? ? return out; } istream& operator>>(istream &in,User &u) { ? ? ? ? in>>u.id>>u.name>>u.major>>u.mb>>u.nb>>u.wj>>u.i; ? ? int s=0; ? ? if(u.i!=0) ? ? { ? ? ? ? while(s!=u.i) ? ? ? ? { ? ? ? ? ? ? in>>u.v2[s];s++; ? ? ? ? } ? ? } ? ? return in; } void User::addRecord(Record r) { ? ? int i; ? ? v2.push_back(r); ? ? i=v2.size(); ? ? m2.insert(make_pair(r.getBid(),i-1)); } void User::dispRecord() { ? ? for(int i=0;i<v2.size();i++) ? ? { ? ? ? ? v2[i].display(); ? ? } } int User::search(int no) { ? ? mit2=m2.find(no); ? ? if(mit2!=m2.end()) ? ? { ? ? ? ? return mit2->second; ? ? } ? ? else return -1; } void User::display() { ? ? cout<<"學號:"<<id<<" "<<"姓名:"<<name<<" "<<"專業(yè)班級:"<<major<<" "<<"最大借閱量:"<<mb<<" "<<"當前借閱量:"<<nb<<" "<<"有無違紀:"<<wj<<endl; ? ? if(v2.size()!=0) ? ? { ? ? ? ? dispRecord(); ? ? } } class Client { ? ? User u; ? ? vector<Book>v3; ? ? vector<Book>::iterator it3; ? ? multimap<int,int>m3; ? ? multimap<int,int>::iterator mit3; ? ? ? ? vector<User>v; ? ? vector<User>::iterator it; ? ? multimap<int,int>m; ? ? multimap<int,int>::iterator mit; public: ? ? Client(int x) ? ? { ? ? ? ? load1(); ? ? ? ? ?load2(x); ? ? } ? ? ~Client() ? ? { ? ? ? ? save1(); ? ? ? ? ?save2(); ? ? } ? ? void load1(); ? ? void save1(); ? ? void load2(int x); ? ? void save2(); ? ? void queryBook(int x); ? ? void borrow(int x,Date t); ? ? void back(int x); ? ? void displayu(){u.display();} ? ? void displayb(int x){mit3=m3.find(x); v3[mit3->second].display();} }; void Client::load1() { ? ? Book b; ? ? int i; ? ? ifstream infile("d:\\20171750book.txt",ios::in); ? ? if(!infile) return; ? ? while(infile>>b) ? ? { ? ? ? ? v3.push_back(b); ? ? ? ? i=v3.size(); ? ? ? ? m3.insert(make_pair(b.getId(),i-1)); ? ? } ? ? infile.close(); } void Client::save1() { ? ? ofstream outfile("d:\\20171750book.txt",ios::out); ? ? if(!outfile) return; ? ? for(int i=0;i<v3.size();i++) ? ? { ? ? ? ? outfile<<v3[i]; ? ? ? ? outfile<<endl; ? ? } ? ? outfile.close(); } void Client::load2(int x) { ? ? User u; ? ? int i; ? ? ifstream infile("d:\\20171750stu.txt",ios::in); ? ? if(!infile) return; ? ? while(infile>>u) ? ? { ? ? ? ? v.push_back(u); ? ? ? ? i=v.size(); ? ? ? ? m.insert(make_pair(u.getId(),i-1)); ? ? ? ? if(x==u.getId()) ? ? ? ? { ? ? ? ? ? ? this->u=u; ? ? ? ? } ? ? } ? ? infile.close(); } void Client::save2() { ? ? ofstream outfile("d:\\20171750stu.txt",ios::out); ? ? if(!outfile) return ; ? ? for(it=v.begin();it!=v.end();it++) ? ? { ? ? ? ? outfile<<*it; ? ? ? ? outfile<<endl; ? ? } ? ? outfile.close(); } void Client::queryBook(int x) { ? ? mit3=m3.find(x); ? ? if(mit3!=m3.end()) ? ? { ? ? ? ? v3[mit3->second].display1(); ? ? } ? ? else ? ? ? ? cout<<"未找到該書!"<<endl; } void Client::borrow(int x,Date t) { ? ? mit3=m3.find(x); ? ? int i,j,k; ? ? i=mit3->second; ? ? j=v3[i].getLn(); ? ? k=v3[i].getSum(); ? ? if(u.getwj()==0&&j<k&&u.getnb()<u.getmb()&&mit3!=m3.end()) ? ? { ? ? ? ? cout<<"借書成功!"<<endl; ? ? ? ? Record r(u.getId(),v3[i].getId(),t); ? ? ? ? u.addRecord(r); ? ? ?mit=m.find(u.getId()); v[mit->second].addRecord(r); ? ? ? ? v3[i].addRecord(r); ? ? ? ? u.setnb(u.getnb()+1); ? ? ? ? ? ? ? ? ? ? ? v[mit->second].setnb(u.getnb()); ? ? ? ? v3[i].setLn(j+1); ? ? } ? ? else ? ? ? ?cout<<"借書失敗!"<<endl; } void Client::back(int x) { ? ? mit3=m3.find(x); ? ? int s=u.v2.size(); ? ? int flag=0; ? ? int i=mit3->second; ? ? int j=v3[i].getLn(); ? ? if(s!=0) ? ? { ? ? ? ? for(int i=0;i<s;++i) ? ? ? ? { ? ? ? ? ? ? if(x==u.v2[i].getBid()) ? ? ? ? ? ? ? flag=1; ? ? ? ? } ? ? } ? ? if(mit3!=m3.end()&&flag==1) ? ? { ? ? ? ? cout<<"還書成功!"<<endl;//用戶的nb-1√ ? 書的ln-1√ ?用戶和書的借閱記錄的sy變?yōu)? ? ? ? ? u.setnb(u.getnb()-1); ? mit=m.find(u.getId()); ?v[mit->second].setnb(u.getnb()); ? ? ? ? v3[i].setLn(j-1); ? ? ? ? v3[i].mit1=v3[i].m1.find(u.getId()); ?v3[i].v1[v3[i].mit1->second].setsy(1); ? ? ? ? u.mit2=u.m2.find(x); ? ? ? ? ? ? ? ? ?u.v2[u.mit2->second].setsy(1); ? ? ? ? int k=mit->second; ? ? ? ? v[k].mit2=v[k].m2.find(x); ?v[k].v2[v[k].mit2->second].setsy(1); ? ? } ? ? else ? ? ? ? cout<<"還書失敗!"<<endl; } class Manger { ? ?vector<User>v4; ? ?vector<User>::iterator it4; ? ?multimap<int,int>m4; ? ?multimap<int,int>::iterator mit4; ? ? ? ?vector<Book>v5; ? ?vector<Book>::iterator it5; ? ?multimap<int,int>m5; ? ?multimap<int,int>::iterator mit5; public: ? ?Manger() ? ?{ ? ? ? ?load1(); ? ? ? ?load2(); ? ?} ? ?~Manger() ? ?{ ? ? ? ?save1(); ? ? ? ?save2(); ? ?} ? ?void load1(); ? ?void load2(); ? ?void save1(); ? ?void save2(); ? ?void addUser(User u); ? ?void addBook(Book b); ? ?void displayU(int x); ? ?void displayB(int x); }; void Manger::load1() { ? ? User u; ? ? int i; ? ? ifstream infile("d:\\20171750stu.txt",ios::in); ? ? if(!infile) return; ? ? v4.clear(); m4.clear(); ? ? i=0; ? ? while(infile>>u) ? ? { ? ? ? ? v4.push_back(u); ? ? ? ? m4.insert(make_pair(u.getId(),i)); ? ? ? ? i++; ? ? } ? ? infile.close(); } void Manger::load2() { ? ? Book b; ? ? int i; ? ? ifstream infile("d:\\20171750book.txt",ios::in); ? ? if(!infile) ?return; ? ? v5.clear();m5.clear(); ? ? i=0; ? ? while(infile>>b) ? ? { ? ? ? ? v5.push_back(b); ? ? ? ? m5.insert(make_pair(b.getId(),i)); ? ? ? ? i++; ? ? } ? ? infile.close(); } void Manger::save1() { ? ? ofstream outfile("d:\\20171750stu.txt",ios::out); ? ? if(!outfile) ?return; ? ? for(it4=v4.begin();it4!=v4.end();it4++) ? ? { ? ? ? ? outfile<<*it4; ? ? ? ? outfile<<endl; ? ? } ? ? outfile.close(); } void Manger::save2() { ? ? ofstream outfile("d:\\20171750book.txt",ios::out); ? ? if(!outfile) ?return; ? ? for(it5=v5.begin();it5!=v5.end();it5++) ? ? { ? ? ? ? outfile<<*it5; ? ? ? ? outfile<<endl; ? ? } ? ? outfile.close(); } void Manger::addUser(User u) { ? ? int x; ? ? x=u.getId(); ? ? mit4=m4.find(x); ? ? if(mit4!=m4.end()) ? ? { ? ? ? ? cout<<"添加用戶失敗,該用戶已存在!"<<endl; ? ? } ? ? else ? ? { ? ? ? ? cout<<"添加用戶成功!"<<endl; ? ? ? ? int i; ? ? ? ? v4.push_back(u); ? ? ? ? i=v4.size(); ? ? ? ? m4.insert(make_pair(u.getId(),i-1)); ? ? } } void Manger::addBook(Book b) { ? ? int x; ? ? x=b.getId(); ? ? mit5=m5.find(x); ? ? if(mit5!=m5.end()) ? ? { ? ? ? ? cout<<"添加圖書失敗,該圖書已存在!"<<endl; ? ? } ? ? else ? ? { ? ? ? ? cout<<"添加圖書成功!"<<endl; ? ? ? ? int i; ? ? ? ? v5.push_back(b); ? ? ? ? i=v5.size(); ? ? ? ? m5.insert(make_pair(b.getId(),i-1)); ? ? } } void Manger::displayU(int x) { ? ? mit4=m4.find(x); ? ? v4[mit4->second].display(); } void Manger::displayB(int x) { ? ? mit5=m5.find(x); ? ? v5[mit5->second].display(); } int main() { ? //時間類測試 /* ? Date a(1999,4,27); ? Date b(2000,4,27); ? cout<<a<<endl; ? cout<<b<<endl; ? if(a<b) cout<<"a小于b"; ? ? ? ? ? ? ? ? ? ? ? ? ?*/ ? //記錄類測試 /* ? Date t(1999,4,27); ? Record r(20171750,1,t); ? r.display(); ? r.setE(); ? r.setxj(1); ? r.setsy(1); ? r.display(); ? ? ? ? ? ? ? */ ? //圖書類測試 ? ? ? /*Date t1(2015,8,6); ? Date t2(2018,6,7); ? Book b(1,"吶喊","人民出版社",t1,10,0); ? b.display(); ? Record r(201717,1,t2); ? b.addRecord(r); ? b.display(); ? //用戶類測試 /* ? User u(20171750,"朱憲棟","計算機3班"); ? u.display(); ? Date t(2018,6,7); ? Record r(20171750,1,t); ? u.addRecord(r); ? u.display(); ? ? ? ? ? ? ? */ ? //管理端測試 ? ? ? /*Manger m;*/ ? ? ? /*User u1(20171750,"朱憲棟","計算機3班"); ? User u2(20176666,"李明","計算機4班");//加用戶 ? m.addUser(u1); ? m.addUser(u2);*/ ? ? ? /*Date t1(2017,1,1); ? Date t2(2014,5,1); ? Book b1(1,"吶喊","人民出版社",t1,10,0); ? Book b2(2,"狂人日記","人民出版社",t2,10,0); ? //加書 ? m.addBook(b1); ? m.addBook(b2);*/ ? ? ? Client c(20171750); ? Date t(2018,6,14); ? c.borrow(1,t); ? ?//借書 ? c.back(1); ? //還書 }
二.總結(jié)
經(jīng)過了三周的探索與嘗試,通過對重載運算符和STL模板庫的實際應用,完成了第一個面對設計對象的程序設計,當然在接下來的完善中,還需使用“繼承”“多態(tài)”進行修改。總之,作為一個完善的圖書館管理系統(tǒng),如今已經(jīng)實現(xiàn)了完整的四個功能:增加用戶,增加書籍,借書和還書以及相應的完整的數(shù)據(jù)記錄與變化,可以說基本具備了一個圖書館管理系統(tǒng)應該具有的功能。
當然,在試驗設計的過程中,出現(xiàn)了諸多問題。例如讀入用戶信息后無法進行操作,借書還書前后的圖書數(shù)據(jù)沒有變化,這些問題的出現(xiàn)大多都是對于重載的認知不夠?qū)е碌?,于是我參考了各類書籍,其中對我?guī)椭畲蟮氖荂++ Premer Plus ,某些例子的使用使我找到了靈感??傊?,解決問題的過程是辛苦的但也是快樂的,試驗設計使我深深地體會到了這一點。
在接下來的學習中,有更多更深入的設計等待著我,最終的目的便是應用程序APP之類的,我相信通過自己的努力,在不久的將來會更加優(yōu)秀。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++基礎(chǔ)入門教程(七):一些比較特別的基礎(chǔ)語法總結(jié)
這篇文章主要介紹了C++基礎(chǔ)入門教程(七):一些比較特別的基礎(chǔ)語法總結(jié),本文總結(jié)的都是一些特殊的語法,需要的朋友可以參考下2014-11-11C++循環(huán)鏈表之約瑟夫環(huán)的實現(xiàn)方法
這篇文章主要介紹了C++循環(huán)鏈表之約瑟夫環(huán)的實現(xiàn)方法,對于學習數(shù)據(jù)結(jié)構(gòu)與算法有一定的借鑒價值,需要的朋友可以參考下2014-09-09