这篇“怎么用C++代码实现图书馆管理系统”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么用C++代码实现图书馆管理系统”文章吧。
#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表示已经续借过,0时还未续借 int sy;//1时已经还书,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();//用于续借时修改应该还书的时间,即将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<<" "<<"是否续借过:"<<xj<<" "<<"是否已还书:"<<sy<<endl; } class Book { int id;//书号 string name;//书名 string press;//出版社 Date pd;//出版日期 int sum,ln;//总册数,已借出 int i; public: vector<Record>v1; vector<Record>::iterator it1; multimap<int,int>m1;//根据借阅人的学号建立 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);//根据借阅人的学号查询 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;//返回的是这本书存储的关于借阅人x的借阅记录的下标值 } else return -1; } void Book::display() { cout<<"书号:"<<id<<" "<<"书名:"<<name<<" "<<"出版社:"<<press<<" "<<"出版日期:"<<pd<<" "<<"总册数:"<<sum<<" "<<"已借出:"<<ln<<endl; if(v1.size()!=0) { dispRecord(); } } void Book::display1() { cout<<"书号:"<<id<<" "<<"书名:"<<name<<" "<<"出版社:"<<press<<" "<<"出版日期:"<<pd<<" "<<"总册数:"<<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;//根据所借书的书号建立 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<<" "<<"专业班级:"<<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变为1 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); //还书 }
以上就是关于“怎么用C++代码实现图书馆管理系统”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。