下面介绍的是简易图书馆管理系统。实现的功能有:
- 管理端:增加图书,增加用户;
- 客户端:借书,还书
程序结构层次概括为四个层次
- 基本成员层:该层次包括两个类:Date日期类和Record记录类。日期类定义的对象可用于图书出版日期,借书还书的日期;记录类用于客户端(学生端)借阅图书和还书的时候增加相应的记录。
- 基础信息层:该层次包括两个类:student类和book类。用于管理端的操作。
- 操作层:该层次包括两个类:管理端Adminop和用户端User,完成增加图书,增加用户(学生)和借书还书的操作
- 主函数:通过类定义对象完成操作和对文件的修改
- 基本成员层:
- class Date
- {
- int year,month,day;
- public:
- Date(int y,int m,int d){year=y;month=m;day=d;}
- Date(){}
- void setYear(int y){year=y;}
- void setMonth(int m){month=m;}
- void setDay(int d){day=d;}
- int getYear(){return year;}
- int getMonth(){return month;}
- int getDay(){return day;}
- bool operator<(const Date&d)const
- {
- return year!=d.year?year<d.year:month!=d.month?month<d.month:day<d.day;
- }
- friend istream&operator>>(istream&is,Date&d);
- friend ostream&operator<<(ostream&os,Date&d);
- };
- istream&operator>>(istream&is,Date&d)
- {
- is>>d.year>>d.month>>d.day;
- return is;
- }
- ostream&operator<<(ostream&os,Date&d)
- {
- os<<d.year<<" ";
- os<<d.month<<" ";
- os<<d.day;
- return os;
- }
- class Record
- {
- int id; //学号
- Date jieshudate; //借书时间
- int bookno; //书号
- Date huanshudate; //还书时间
- int xujie; //续借
- int state;
- public:
- Record(int a,Date d,int b)
- {
- id=a;
- jieshudate=d;
- bookno=b;
- if(d.getMonth()+2>12)
- {
- Date d1(d.getYear()+1,d.getMonth()-10,d.getDay());
- huanshudate=d1;
- }
- else
- {
- Date d1(d.getYear(),d.getMonth()+2,d.getDay());
- huanshudate=d1;
- }
- xujie=0;
- state=0;
- }
- Record(){}
- int getId(){return id;}
- Date getJieshudate(){return jieshudate;}
- int getBookno(){return bookno;}
- int getXujie(){return xujie;}
- Date getHuanshudate(){return huanshudate;}
- void setHuanshudate();
- void setXujie(){xujie=1;}
- void setState(int n){state=n;}
- int getState(){return state;}
- friend istream&operator>>(istream&is,Record&r);
- friend ostream&operator<<(ostream&os,Record&r);
- };
- void Record::setHuanshudate()
- {
- Date d;
- if(huanshudate.getMonth()+1>12)
- d=Date(huanshudate.getYear()+1,huanshudate.getMonth()+1,huanshudate.getDay());
- else
- d=Date(huanshudate.getYear()+1,huanshudate.getMonth(),huanshudate.getDay());
- huanshudate;
- }
- istream&operator>>(istream&is,Record&r)
- {
- is>>r.id;
- is>>r.jieshudate;
- is>>r.bookno;
- is>>r.huanshudate;
- is>>r.xujie;
- is>>r.state;
- return is;
- }
- ostream&operator<<(ostream&os,Record&r)
- {
- os<<r.id<<" ";
- os<<r.jieshudate<<" ";
- os<<r.bookno<<" ";
- os<<r.huanshudate<<" ";
- os<<r.xujie<<" ";
- os<<r.state<<endl;
- return os;
- }
- 基础信息层:
- class Book
- {
- int bookno; //书号
- string bookname; //书名
- string pub; //出版社
- Date pubdate; //出版时间
- int num; //书的总数
- int leadnum; //借出数目
- int jilunum; //借阅记录数
- vector<Record>v1;
- vector<Record>::iterator it1;
- multimap<int,int>m1;
- multimap<int,int>::iterator mit1;
- public:
- Book(){jilunum=0;}
- vector<Record>getRecord(){return v1;}
- int getBookno(){return bookno;}
- string getBookname(){return bookname;}
- string getPub(){return pub;}
- Date getDate(){return pubdate;}
- void setNum(int n){num=n;}
- int getNum(){return num;}
- void setLeadnum(int x){leadnum=x;}
- int getLeadnum(){return leadnum;}
- void setJilunum(int n){jilunum=n;}
- int getJilunum(){return jilunum;}
- void addRecord1(Record r1)
- {
- r1.setState(1);
- v1.push_back(r1);
- int i=v1.size();
- m1.insert(make_pair(r1.getId(),i-1));
- }
- void addR1(Record r1)
- {
- v1.push_back(r1);
- int i=v1.size();
- m1.insert(make_pair(r1.getId(),i-1));
- }
- void deleteRecord1(int no)
- {
- int i;
- i=search1(no);
- if(i!=-1)
- {
- v1[i].setState(0);
- }
- }
- int search1(int no)
- {
- mit1=m1.find(no);
- if(mit1!=m1.end())
- return mit1->second;
- else
- return -1;
- }
- void clear1(){v1.clear();}
- friend istream&operator>>(istream&is,Book&b);
- friend ostream&operator<<(ostream&os,Book&b);
- };
- istream&operator>>(istream&is,Book&b)
- {
- is>>b.bookno;
- if(b.bookno==-1)
- return is;
- is>>b.bookname;
- is>>b.pub;
- is>>b.pubdate;
- is>>b.num;
- is>>b.leadnum;
- is>>b.jilunum;
- return is;
- }
- ostream&operator<<(ostream&os,Book&b)
- {
- os<<b.bookno<<" "<<b.bookname<<" "<<b.pub<<" "<<b.pubdate<<" "<<b.num<<" "<<b.leadnum<<endl;
- os<<b.jilunum<<endl;
- if(b.getJilunum()!=0)
- for(int i=0;i<b.getJilunum();i++)
- os<<b.v1[i];
- return os;
- }
- class student
- {
- int xuehao; //学号
- string name; //姓名
- int maxnum; //最大借阅数
- int nnum; //目前借阅数
- int weiji; //是否违纪
- int rnum; //当前借阅数
- vector<Record>v2;
- vector<Record>::iterator it2;
- multimap<int,int>m2;
- multimap<int,int>::iterator mit2;
- public:
- student(){weiji=0;rnum=0;}
- int getXuehao(){return xuehao;}
- void setName(string nam){name=nam;}
- string getName(){return name;}
- void setMaxnum(int x){maxnum=x;}
- int getMaxnum(){return maxnum;}
- void setNnum(int x){nnum=x;}
- int getNnum(){return nnum;}
- void setWeiji(int x){weiji=x;}
- int getWeiji(){return weiji;}
- void setRnum(int x){rnum=x;}
- int getRnum(){return rnum;}
- vector<Record>getRecord(){return v2;}
- multimap<int,int>getMap(){return m2;}
- void clear2();
- void addRecord2(Record r2);
- int search2(int h);
- void deleteRecord2(int h);
- friend istream&operator>>(istream&is,student &s);
- friend ostream&operator<<(ostream&os,student&s);
- void huanshudat(Date d);
- void addR2(Record r);
- void operator=(student&s)
- {
- xuehao=s.getXuehao();
- name=s.getName();
- maxnum=s.getMaxnum();
- nnum=s.getNnum();
- weiji=s.getWeiji();
- rnum=s.getRnum();
- v2=s.getRecord();
- m2=s.getMap();
- }
- bool operator==(student s)
- {
- return this->xuehao==s.getXuehao()?1:0;
- }
- };
- istream&operator>>(istream&is,student&s)
- {
- is>>s.xuehao;
- if(s.xuehao==-1)
- return is;
- is>>s.name;
- is>>s.maxnum;
- is>>s.nnum;
- is>>s.weiji;
- is>>s.rnum;
- return is;
- }
- ostream&operator<<(ostream&os,student&s)
- {
- os<<s.xuehao<<" "<<s.name<<" "<<s.maxnum<<" "<<s.nnum<<" "<<s.weiji<<" "<<endl;
- os<<s.rnum<<endl;
- if(s.getRnum()!=0)
- for(int i=0;i<s.getRnum();i++)
- os<<s.v2[i];
- return os;
- }
- void student::addR2(Record r)
- {
- v2.push_back(r);
- int i=v2.size();
- m2.insert(make_pair(r.getBookno(),i-1));
- }
- void student::addRecord2(Record r)
- {
- r.setState(1);
- v2.push_back(r);
- int i=v2.size();
- m2.insert(make_pair(r.getBookno(),i-1));
- }
- int student::search2(int h)
- {
- mit2=m2.find(h);
- if(mit2!=m2.end())
- return mit2->second;
- else
- return -1;
- }
- void student::deleteRecord2(int h)
- {
- int i;
- i=search2(h);
- if(i!=-1)
- {
- v2[i].setState(0);
- }
- }
- void student::huanshudat(Date d)
- {
- setWeiji(0);
- for(int i=0;i<v2.size();i++)
- if(v2[i].getHuanshudate()<d&&v2[i].getState()==1){setWeiji(1);break;}
- }
- void student::clear2()
- {
- v2.clear();
- }
- 操作层:
- class Adminop
- {
- vector<Book>v3;
- vector<Book>::iterator it3;
- multimap<int,int>m3;
- multimap<int,int>::iterator mit3;
- vector<student>v4;
- vector<student>::iterator it4;
- multimap<int,int>m4;
- multimap<int,int>::iterator mit4;
- public:
- Adminop(){}
- ~Adminop()
- {
- save1();
- save2();
- }
- void load1();
- void load2();
- void save1();
- void save2();
- void addBook()
- {
- cout<<"请输入图书信息"<<endl;
- cout<<"格式:书号 书名 出版社 出版日期 馆藏数目 借出数目 借阅记录数目"<<endl;
- Book b;
- int i;
- while(cin>>b)
- {
- if(b.getBookno()==-1)
- break;
- v3.push_back(b);
- i=v3.size();
- m3.insert(make_pair(b.getBookno(),i-1));
- b.clear1();
- }
- }
- int search3(int x)
- {
- mit3=m3.find(x);
- if(mit3!=m3.end())
- {
- return mit3->second;
- }
- else
- return -1;
- }
- void addstudent()
- {
- cout<<"请输入学生信息"<<endl;
- cout<<"格式:学号 姓名 最大借阅数目 目前借阅数目 是否违纪 借阅记录数目"<<endl;
- student s;
- int i;
- while(cin>>s)
- {
- if(s.getXuehao()==-1)
- break;
- v4.push_back(s);
- i=v4.size();
- m4.insert(make_pair(s.getXuehao(),i-1));
- s.clear2();
- }
- }
- int search4(int x)
- {
- mit4=m4.find(x);
- if(mit4!=m4.end())
- {
- return mit4->second;
- }
- else
- return -1;
- }
- };
- void Adminop::save1()
- {
- ofstream outfile("d:\\20171808book.txt",ios::out);
- if(!outfile)
- return ;
- for(it3=v3.begin();it3!=v3.end();it3++)
- {
- outfile<<*it3;
- }
- outfile.close();
- }
- void Adminop::save2()
- {
- ofstream outfile("d:\\20171808stu.txt",ios::out);
- if(!outfile)
- return ;
- for(it4=v4.begin();it4!=v4.end();it4++)
- {
- outfile<<*it4;
- }
- outfile.close();
- }
- class User
- {
- vector<Book>v5;
- vector<Book>::iterator it5;
- multimap<int,int>m5;
- multimap<int,int>::iterator mit5;
- student s;
- Date t;
- public:
- User(int x)
- {
- cout<<"当前日期,如2017 1 1 ;图书书号,如1001"<<endl;
- load3();
- load4(x);
- }
- ~User()
- {
- save3();
- save4();
- }
- void load3();
- void save3();
- void load4(int x);
- void save4();
- int search5(int x)
- {
- mit5=m5.find(x);
- if(mit5!=m5.end())
- {
- return mit5->second;
- }
- else
- return -1;
- }
- void borrow()
- {
- int x;
- cin>>x;
- int a=search5(x);
- s.huanshudat(t);
- if(s.getWeiji()==0&&s.getNnum()<s.getMaxnum())
- {
- v5[a].setLeadnum(v5[a].getLeadnum()+1);
- v5[a].setJilunum(v5[a].getJilunum()+1);
- s.setNnum(s.getNnum()+1);
- s.setRnum(s.getRnum()+1);
- Record r(s.getXuehao(),t,x);
- v5[a].addRecord1(r);
- s.addRecord2(r);
- cout<<"Borrow book successfully"<<endl;
- }
- }
- void back()
- {
- int x;
- cin>>x;
- int a=search5(x);
- v5[a].setLeadnum(v5[a].getLeadnum()-1);
- s.setNnum(s.getNnum()-1);
- v5[a].deleteRecord1(s.getXuehao());
- s.deleteRecord2(x);
- cout<<"return book successfully"<<endl;
- }
- };
- void User::save3()
- {
- ofstream outfile("d:\\20171808book.txt",ios::out);
- if(!outfile)
- {
- return;
- }
- for(it5=v5.begin();it5!=v5.end();it5++)
- {
- outfile<<*it5;
- }
- outfile.close();
- }
- void User::load3()
- {
- Book b;
- int i;
- ifstream infile("d:\\20171808book.txt",ios::in);
- if(!infile)return;
- v5.clear();
- m5.clear();
- while(infile>>b)
- {
- if(b.getJilunum()!=0)
- for(int i=0;i<b.getJilunum();i++)
- {
- Record r3;
- infile>>r3;
- b.addR1(r3);
- }
- v5.push_back(b);
- int i=v5.size();
- m5.insert(make_pair(b.getBookno(),i-1));
- b.clear1();
- }
- infile.close();
- }
- void User::load4(int x)
- {
- student s1;
- int i;
- cin>>t;
- ifstream infile("d:\\20171808stu.txt",ios::in);
- if(!infile)
- return;
- while(infile>>s1)
- {
- if(s1.getRnum()!=0)
- for(int i=0;i<s1.getRnum();i++)
- {
- Record r4;
- infile>>r4;
- s1.addR2(r4);
- }
- if(s1.getXuehao()==x)
- this->s=s1;
- s1.clear2();
- }
- infile.close();
- }
- void User::save4()
- {
- vector<student>v;
- vector<student>::iterator it;
- student s1;
- ifstream infile("d:\\20171808stu.txt",ios::in);
- if(!infile)return ;
- while(infile>>s1)
- {
- if(s1.getRnum()!=0)
- for(int i=0;i<s1.getRnum();i++)
- {
- Record r5;
- infile>>r5;
- s1.addR2(r5);
- }
- if(s1==this->s)
- s1=this->s;
- v.push_back(s1);
- s1.clear2();
- }
- infile.close();
- ofstream outfile("d:\\20171808stu.txt",ios::out);
- if(!outfile)return;
- for(it=v.begin();it!=v.end();it++)
- outfile<<*it;
- outfile.close();
- }
- 程序的主函数:
- int main()
- {
- /*
- Adminop a; //管理端定义对象
- a.addBook(); //增加图书,调用函数时会提示输入的信息,输入-1终止
- a.addstudent(); //增加学生用户,调用函数时会提示输入的信息,输入-1终止
- */
- /*
- int xuehao;
- cout<<"请输入学号";
- cin>>xuehao ;
- User h(xuehao); //定义学生用户
- //h.borrow(); //借书,函数调用时会提示输入借书日期、书号
- h.back(); //还书,函数调用时会提示输入还书日期、书号,如果逾期还书或者不还书,会把违纪记录变为1,将不能再借书
- */
- }
完整代码链接:https://paste.ubuntu.com/p/qzvvFbpnBm/