①时间类:年月日
②数据记录类(Record):学号,书号,书名,结束时间,应还时间
③图书数据类(Book):书号,书名,出版社,总数,借出数量
vector<Record>v1;
vector<Record>::iterator it1;
④用户数据类(User):学号,书名,借书数量,最大借书数量
⑤管理员类(Manager):vector<Record>v1;
vector<Record>::iterator it1;
vector<Book>v3;
vector<Book>::iterator it3;
vector<User>v4;
vector<User>::iterator it4;
void dispBook();
void dispUser();
void addBook();
void addUser();
int search3(int no);
int search4(int xuehao);
void load1();
void load2();
void save1();
void save2();
void addRecord();
void dispRecord1();
void dispRecord2();
⑥客户端(Client):vector<Book>v5;
vector<Book>::iterator it5;
vector<Record>v7;
vector<Record>::iterator it7;
void borrow();//借书
void back();//还书
int search5(int no);
void load3();
void save3();
void load4();
void save4();
图书管理系统分为客户端和管理员两部分。客户端只有借书还书操作。
#include<bits/stdc++.h>
using namespace std;
class Time
{
int year;
int month;
int day;
int hour;
public:
Time(){}
Time(int year1,int month1,int day1){
year=year1;
month=month1;
day=day1;
}
void setYear(int year1){year=year1;}
int getYear(){return year;}
void setMonth(int month1){month1=month;}
int getMonth(){return month;}
void setDay(int day1){day=day1;}
int getDay(){return day;}
void setHour(int hour1){hour=hour1;}
int getHour(){return hour;}
friend ostream&operator<<(ostream&os,const Time&t);
friend istream&operator>>(istream&is,Time&t);
bool operator<(const Time&t)const{
return year!=t.year?year<t.year:month!=t.month?month<t.month:day<t.day;
}
};
ostream&operator<<(ostream&os,const Time&t){
os<<t.year<<" ";
os<<t.month<<" ";
os<<t.day<<" ";
return os;
}
istream&operator>>(istream&is,Time&t){
is>>t.year>>t.month>>t.day;
return is;
}
class Record
{
int xuehao;//学号
int no;//书号
Time start;
string name;
Time end;
int xj;
string cz;
public:
Record(){}
Record(int xuehao1,string a,Time s,int no1,string c)
{
xuehao=xuehao1;
name=a;
cz=c;
start=s;
Time t(s.getYear(),s.getMonth()+2,s.getDay());
end=t;
no=no1;
}
void setXuehao(int xuehao1){xuehao=xuehao1;}
int getXuehao(){return xuehao;}
void setNo(int no1){no=no1;}
int getNo(){return no;}
Time getStart(){return start;}
void setXj(){xj=1;}
int getXj(){return xj;}
friend ostream&operator<<(ostream&os,const Record&r);
friend istream&operator>>(istream&is,Record&r);
};
ostream&operator<<(ostream&os,const Record&r){
os<<r.xuehao<<" ";
os<<r.no<<" ";
os<<r.name<<" ";
if (r.cz=="借书")
{os<<"借书时间: "<<r.start<<" ";
os<<"应还时间: "<<r.end<<" ";}
else os<<"还书时间: "<<r.start;
return os;
}
istream&operator>>(istream&is,Record&r){
is>>r.xuehao>>r.no>>r.name>>r.start>>r.end;
return is;
}
class Book
{
int no;//书号
string name;//书名
string press;//出版社
int num;//数量
int jiechu;
vector<Record>v1;
vector<Record>::iterator it1;
multimap<int,int>m1;
multimap<int,int>::iterator mit1;
public:
Book(){}
Book(int no1,string name1,string press1,int num1,int jiechu1){
no=no1;
name=name1;
press=press1;
num=num1;
jiechu=jiechu1;
}
void setNo(int no1){no=no1;}
int getNo(){return no;}
void setName(string name1){name=name1;}
string getName(){return name;}
void setPress(string press1){press=press1;}
string getPress(){return press;}
void setNum(int num1){num=num1;}
int getNum(){return num;}
void setJiechu(int jiechu1){jiechu=jiechu1;}
int getJiechu(){return jiechu;}
void addRecord(Record r1);//增加图书信息
int search1(int no);//查找
friend ostream&operator<<(ostream&os,const Book&b);
friend istream&operator>>(istream&is,Book&b);
};
ostream&operator<<(ostream&os,const Book&b){
os<<b.no<<" ";
os<<b.name<<" ";
os<<b.press<<" ";
os<<b.num<<" ";
os<<b.jiechu<<endl;
return os;
}
istream&operator>>(istream&is,Book&b){
is>>b.no;
if(b.no==-1)return is;
is>>b.name>>b.press>>b.num>>b.jiechu;
return is;
}
class User
{
string name;
int xuehao;
int max;//最大数目
int num;//借阅数目
public:
User(){}
User(int xuehao1,string name1,int n,int x){
xuehao=xuehao1;
name=name1;
num=n;
max=x;
}
void setXuehao(int xuehao1){xuehao=xuehao1;}
int getXuehao(){return xuehao;}
void setName(string name1){name=name1;}
string getName(){return name;}
void setMax(int x){max=x;}
int getMax(){return max;}
void setNum(int n){num=n;}
int getNum(){return num;}
int serch2(int xuehao);
void operator=(User&u){
xuehao=u.getXuehao();
name=u.getName();
max=u.getMax();
num=u.getNum();
}
bool operator==(User u){
return this->xuehao==u.getXuehao()?1:0;
}
friend istream&operator>>(istream&in,User&u);
friend ostream&operator<<(ostream&out,User&u);
};
istream&operator>>(istream&is,User&u){
is>>u.xuehao;
if (u.xuehao==-1)return is;
is>>u.name;
is>>u.num;
is>>u.max;
}
ostream&operator<<(ostream&os,User&u){
os<<u.xuehao<<" "<<u.name<<" "<<u.num<<" "<<u.max<<endl;
}
class Manager
{
vector<Record>v1;
vector<Record>::iterator it1;
multimap<int,int>m1;
multimap<int,int>::iterator mit1;//存储记录,包括图书和用户
vector<Book>v3;
vector<Book>::iterator it3;
multimap<int,int>m3;
multimap<int,int>::iterator mit3;
vector<User>v4;
vector<User>::iterator it4;
multimap<int,int>m4;
multimap<int,int>::iterator mit4;
public:
Manager(){
load1();
load2();
}
~Manager()
{
save1();
save2();
}
void dispBook();
void dispUser();
void addBook();
void addUser();
int search3(int no);
int search4(int xuehao);
void load1();
void load2();
void save1();
void save2();
void addRecord();
void dispRecord1();
void dispRecord2();
void deleteRecord();
};
void Manager::load1(){
Book b;
int i;
ifstream infile("d:\\20171770book.txt",ios::in);
if(!infile)
return;
v3.clear();
m3.clear();
while(infile>>b)
{
v3.push_back(b);
m3.insert(make_pair(b.getNo(),i-1));
i++;
}
infile.close();
}
void Manager::save1(){
ofstream outfile("d:\\20171770book.txt",ios::out);
if(!outfile)
return;
for(it3=v3.begin();it3!=v3.end();it3++)
outfile<<*it3;
outfile.close();
}
void Manager::load2(){
User u;
int i;
ifstream infile("d:\\20171770stu.txt",ios::in);
if(!infile)
return;
v4.clear();
m4.clear();
while(infile>>u)
{
v4.push_back(u);
m4.insert(make_pair(u.getXuehao(),i-1));
i++;
}
infile.close();
}
void Manager::save2(){
ofstream outfile("d:\\20171770stu.txt",ios::out);
if(!outfile)
return;
for(it4=v4.begin();it4!=v4.end();it4++)
outfile<<*it4;
outfile.close();
}
void Manager::dispBook(){
for(it3=v3.begin();it3!=v3.end();it3++)
cout<<*it3;
}
void Manager::dispUser(){
for(it4=v4.begin();it4!=v4.end();it4++)
cout<<*it4;
}
void Manager::addUser(){
User u;
int i;
while(1)
{
cin>>u;
if(u.getXuehao()==-1)break;
v4.push_back(u);
i=v4.size();
m4.insert(make_pair(u.getXuehao(),i-1));
}
}
void Manager::addBook(){
Book b;
int i;
while(1)
{
cin>>b;
if(b.getNo()==-1)
break;
v3.push_back(b);
i=v3.size();
m3.insert(make_pair(b.getNo(),i-1));
}
}
int Manager::search3(int no){
mit3=m3.find(no);
if(mit3!=m3.end()){return mit3->second;}
else
return -1;
}
int Manager::search4(int xuehao){
mit4=m4.find(xuehao);
if(mit4!=m4.end())
return mit4->second;
else
return -1;
}
void Manager::dispRecord1(){//按照学生学号输出学生的借阅记录
int xuehao;
cout<<"请输入查询学生借阅记录的学号:"<<endl;
cin>>xuehao;
int i;
i=search4(xuehao);
if(v1.size()!=0)
for(it1=v1.begin();it1!=v1.end();it1++)
cout<<*it1;
else cout<<"Not found"<<endl;
}
void Manager::dispRecord2(){//按照书号输出该书的借阅记录
int no;
cout<<"请输入要查询书籍借阅记录的书号:"<<endl;
cin>>no;
int i;
i=search3(no);
if(v1.size()!=0)
for(it1=v1.begin();it1!=v1.end();it1++)
cout<<*it1;
else cout<<"Not found"<<endl;
}
void Manager::addRecord(){
Record r1;
v1.push_back(r1);
int i=v1.size();
m1.insert(make_pair(r1.getNo(),i-1));
}
class Client
{
vector<Book>v5;
vector<Book>::iterator it5;
multimap<int,int>m5;
multimap<int,int>::iterator mit5;
vector<Record>v7;
vector<Record>::iterator it7;
User u;
public:
Client(){
load3();
load4();
}
~Client(){
save3();
save4();
}
void borrow();//借书
void back();//还书
void liulan();
int search5(int no);
void load3();
void save3();
void load4();
void save4();
};
void Client::liulan(){
for (it5=v5.begin();it5!=v5.end();it5++)
cout<<*it5;
}
void Client::load3(){
Book b;
int i;
ifstream infile("d:\\20171770book.txt",ios::in);
if(!infile)
return;
v5.clear();
m5.clear();
i=0;
while(infile>>b)
{
v5.push_back(b);
m5.insert(make_pair(b.getNo(),i-1));
i++;
}
infile.close();
}
void Client::save3(){
ofstream outfile("d:\\20171770book.txt",ios::out);
if(!outfile)
return;
for(it5=v5.begin();it5!=v5.end();it5++)
outfile<<*it5;
for(it7=v7.begin();it7!=v7.end();it7++)
outfile<<*it7;
outfile.close();
}
void Client::load4(){
int xuehao;
cout<<"Xuehao:";
cin>>xuehao;
int i;
ifstream infile("d:\\20171770stu.txt",ios::in);
if(!infile)
return;
while(infile>>u)
{
if(u.getXuehao()==xuehao)
this->u=u;
}
infile.close();
}
void Client::save4(){
vector<User>v;
vector<User>::iterator it;
User u1;
ifstream infile("d:\\20171770Stu.txt",ios::in);
if(!infile)
return;
while(infile>>u1)
{
if(u1==this->u)
u1=this->u;
v.push_back(u1);
}
infile.close();
ofstream outfile("d:\\20171770Stu.txt",ios::out);
if(!outfile)
return;
for(it=v.begin();it!=v.end();it++)
outfile<<*it;
for (it7=v7.begin();it7!=v7.end();it7++)
outfile<<*it7;
outfile.close();
}
int Client::search5(int no){
mit5=m5.find(no);
if(mit5!=m5.end()){return mit5->second;}
else
return -1;
}
void Client::borrow(){
int xuehao,no;
Time t1;
cout<<"no:"<<endl;
cin>>no;
cin>>t1;
int s=search5(no)+1;
v5[s].setNum(v5[s].getNum()-1);
v5[s].setJiechu(v5[s].getJiechu()+1);
u.setNum(u.getNum()+1);//用户借书的数量+1
Record r(u.getXuehao(),v5[s].getName(),t1,no,"借书");
v7.push_back(r);
cout<<"Success";
}
void Client::back(){
int xuehao,no;
Time t1;
cout<<"no:"<<endl;
cin>>no;
cin>>t1;
int a=search5(no)+1;
v5[a].setNum(v5[a].getNum()+1);
v5[a].setJiechu(v5[a].getJiechu()-1);//该本书籍借出数量-1
u.setNum(u.getNum()-1);//该用户借书数量-1
Record r(u.getXuehao(),v5[a].getName(),t1,no,"还书");
v7.push_back(r);
cout<<"back_success";
}
int main()
{
/*
Manager m;
m.load1();
m.load2();
m.addBook();
m.addUser();
m.save1();
m.save2();
*/
Client c;;
c.borrow();
cout<<endl;
c.borrow();
//c.back();
}