图书馆的管理系统
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
class Time
{
int year;
int month;
int day;
public:
Time(){};
Time(int x,int y,int z):year(x),month(y),day(z){}
int setyear(int y){year=y;}
int setmonth(int m){month=m;}
int setday(int d){day=d;}
int getyear(){return year;}
int getmonth(){return month;}
int getday(){return day;}
friend ostream&operator<<(ostream &os,Time&d);
friend istream&operator>>(istream &is,Time&d);
};
ostream&operator<<(ostream &os,Time&d)
{
os<<d.year<<" ";
os<<d.month<<" ";
os<<d.day<<endl;
return os;
}
istream&operator>>(istream &is,Time&d)
{
is>>d.year>>d.month>>d.day;
return is;
}
/*int main()
{
Time t;
cin>>t;
cout<<t;
}*/
class Record
{
Time start;
Time ending;
string shuhao;
string xuehao;
int state;
int jorh;
public:
Record(){};
Record(string x,string sh,Time s)
{
xuehao=x;
shuhao=sh;
start=s;
if(s.getmonth()+2>12)
{
Time e(s.getyear()+1,s.getmonth()-10,s.getday());
ending=e;
}
else
{
Time e(s.getyear(),s.getmonth()+2,s.getday());
ending=e;
}
state=0;
jorh=0;
}
int setjorh(){jorh=1;}
Time getstart(){return start;}
Time getending(){return ending;}
Time setending(Time tt)
{
//ending=tt;
if(tt.getmonth()+2>12)
{
Time e(tt.getyear()+1,tt.getmonth()-10,tt.getday());
ending=e;
}
else
{
Time e(tt.getyear(),tt.getmonth()+2,tt.getday());
ending=e;
}
}
string getshuhao(){return shuhao;}
string getxuehao(){return xuehao;}
int getstate(){return state;}
int setstate()
{
/*int s;
cin>>s;
if(s==0) state="归还";
else if(s==1) state=="续借";
else cout<<"Error"<<endl;*/
state=1;
}
friend ostream&operator<<(ostream &os,Record&a);
friend istream&operator>>(istream &is,Record&a);
};
ostream&operator<<(ostream &os,Record&a)
{
os<<a.xuehao<<" ";
os<<a.shuhao<<" ";
os<<a.start<<" ";
os<<a.ending<<" ";
os<<a.jorh<<" ";
os<<a.state<<endl;
return os;
}
istream&operator>>(istream &is,Record&a)
{
is>>a.xuehao>>a.shuhao>>a.start>>a.ending>>a.jorh>>a.state;
return is;
}
/*int main()
{
Record r;
cin>>r;
cout<<r;
}*/
class Recordn
{
protected:
vector<Record>v;
vector<Record>::iterator itn;
public:
Recordn(){};
};
class Xuesheng
{
string name;
string ID;
int year1;
string bj;//班级
public:
Xuesheng(int r,string i,string m):year1(r),name(m),ID(i){}
Xuesheng(){};
string getname(){return name;}
string getID(){return ID;}
int getyear1(){return year1;}
void setname(string n){name=n;}
void setID(string m){ID=m;}
string getbj(){return bj;}
friend ostream&operator<<(ostream &os,Xuesheng&x);
friend istream&operator>>(istream &is,Xuesheng&x);
};
ostream&operator<<(ostream &os,Xuesheng&x)
{
os<<x.year1<<" "<<x.bj<<" "<<x.ID<<" "<<x.name<<endl;
}
istream&operator>>(istream &is,Xuesheng&x)
{
is>>x.year1>>x.bj>>x.ID>>x.name;
}
class Shuju:public Recordn
{
Record rs;
string shuhao;
string shuming;
string zuozhe;
string cbs;//出版社
string cbsj;//出版时间
int zgcs;//在馆册数
int zcs;//总册数
int zxx;//总借阅信息
//vector<Record>vs;
//vector<Record>::iterator vs1;
map<string,int>sh2;
map<string,int>::iterator sh22;
map<string,int>xh2;
map<string,int>::iterator xh22;
public:
Shuju(string sh,string sm,string zz,int zc,int cs):shuhao(sh),shuming(sm),zuozhe(zz),zcs(zc),zgcs(cs)
{
zxx=0;
}
Shuju(){};
string getcbs()
{
return cbs;
}
string getcbsj()
{
return cbsj;
}
string getshuhao()
{
return shuhao;
}
string getshuming()
{
return shuming;
}
string getzuozhe()
{
return zuozhe;
}
int getzcs()
{
return zcs;
}
int getzgcs()
{
return zgcs;
}
void setzxx(){zxx=zxx+1;}
void setzgcs1(){zgcs=zgcs+1;}//比上一次代码改变的地方
void setzgcs2(){zgcs=zgcs-1;}
int getzxx(){return zxx;}
void setshuhao(string s){shuhao=s;}
void setshuming(string nn){shuming=nn;}
void addRecord1(Record r);
void huansj(string s);//还书的时候
void xujiesj(string sss);
void shanchu1();
friend ostream&operator<<(ostream &os,Shuju&s);
friend istream&operator>>(istream &is,Shuju&s);
};
void Shuju::shanchu1()
{
vector<Record>().swap(v);
}
void Shuju::xujiesj(string sss)
{
sh22=sh2.find(sss);
if(sh22!=sh2.end())
{
int a=sh22->second;
v[a].setstate();
}
}
void Shuju::huansj(string s)//s为书号
{
sh22=sh2.find(s);
if(sh22!=sh2.end())
{
int l=sh22->second;
v[l].setjorh();
}
else cout<<"Error!"<<endl;
}
void Shuju::addRecord1(Record r)
{
v.push_back(r);
sh2.insert(make_pair(r.getshuhao(),v.size()-1));
xh2.insert(make_pair(r.getxuehao(),v.size()-1));
}
ostream&operator<<(ostream &os,Shuju&s)
{
vector<Record>::iterator itn;
os<<s.shuhao<<" "<<s.shuming<<" "<<s.zuozhe<<" "<<s.cbs<<" "<<s.cbsj<<" "<<s.zcs<<" "<<s.zgcs<<" "<<s.zxx<<endl;
int n=s.zxx;
itn=s.v.begin();
for(int i=0;i<n;i++)
{
os<<*itn++;
}
return os;
}
istream&operator>>(istream &is,Shuju&s)
{
is>>s.shuhao>>s.shuming>>s.zuozhe>>s.cbs>>s.cbsj>>s.zcs>>s.zgcs>>s.zxx;
}
class chazhao
{
protected:
Shuju sj1;
vector<Shuju>sj;
vector<Shuju>::iterator it;
map<string,int>gs;//更改在馆册数
map<string,int>::iterator gs1;
map<string,int>sm;
map<string,int>::iterator sm1;
map<string,int>sh;
map<string,int>::iterator sh1;
map<string,int>zz;//作者
//map<string,int>::iterator zz1;
map<string,int>sjian;//出版时间
//map<string,int>::iterator sjian1;
map<string,int>cbshe;//出版社
//map<string,int>::iterator cbshe1;
public:
chazhao(){};
void load();
void chasm();
void chash();
void chazz();
void chacbs();
void chacbsj();
};
void chazhao::chacbsj()
{
map<string,int>::iterator a,b,c;
string cbsj;
while(1)
{
cin>>cbsj;
if(cbsj=="0") break;
else
{
b=sjian.lower_bound(cbsj);
c=sjian.upper_bound(cbsj);
if(b==c) cout<<"No book!"<<endl;
else
{
for(a=b;a!=c;a++)
{
cout<<sj[a->second]<<endl;
}
}
}
}
}
void chazhao::chacbs()
{
map<string,int>::iterator a,b,c;
string cbs;
while(1)
{
cin>>cbs;
if(cbs=="0") break;
else
{
b=cbshe.lower_bound(cbs);
c=cbshe.upper_bound(cbs);
if(b==c) cout<<"No book!"<<endl;
else
{
for(a=b;a!=c;a++)
{
cout<<sj[a->second]<<endl;
}
}
}
}
}
void chazhao::chazz()
{
map<string,int>::iterator a,b,c;
string zz1;
while(1)
{
cin>>zz1;
if(zz1=="0") break;
else
{
b=zz.lower_bound(zz1);
c=zz.upper_bound(zz1);
if(b==c) cout<<"No book!"<<endl;
else
{
for(a=b;a!=c;a++)
{
cout<<sj[a->second]<<endl;
}
}
}
}
}
void chazhao::chash()
{
string num;
while(1)
{
cin>>num;
if(num=="0") break;
else
{
sh1=sh.find(num);
if(sh1!=sh.end())
{
cout<<sj[sh1->second];
}
else cout<<"No book!"<<endl;
}
}
}
void chazhao::chasm()
{
string name;
while(1)
{
cin>>name;
if(name=="0") break;
else
{
sm1=sm.find(name);
if(sm1!=sm.end())
{
cout<<sj[sm1->second];
}
else cout<<"No book!"<<endl;
}
}
}
void chazhao::load()
{
Record r1;
Shuju s1;
int i=0;
int n;
ifstream infile("2018212524邱枫-b图书.txt",ios::in);
if(!infile) return;
while(infile>>s1)
{
//infile>>sj1;
n=s1.getzxx();
for(int j=0;j<n;j++)
{
infile>>r1;
s1.addRecord1(r1);
}
sj.push_back(s1);
sm.insert(make_pair(s1.getshuming(),i));
sh.insert(make_pair(s1.getshuhao(),i));
gs.insert(make_pair(s1.getshuming(),s1.getzgcs()));
zz.insert(make_pair(s1.getzuozhe(),i));
sjian.insert(make_pair(s1.getcbsj(),i));
cbshe.insert(make_pair(s1.getcbs(),i));
i=i+1;
s1.shanchu1();
}
infile.close();
}
/*int main()
{
Record r;
cin>>r;
cout<<r;
}*/
class user:public Recordn
{
string name;
string ID;
int zong;
int zjsm;//借书的数目
Record r1;
//vector<Record>v;
//vector<Record>::iterator it;
map<string,int>sh1;
map<string,int>::iterator sh11;
map<string,int>xh1;
map<string,int>::iterator xh11;
public:
user(){};
user(string na,string i,int z1,int z2,Record rr):name(na),ID(i),zong(z1),zjsm(z2),r1(rr){}
string getname(){return name;}
string getID(){return ID;}
int getzong(){return zong;}
int getzjsm(){return zjsm;}
string setname(string n){name=n;}
string setID(string nn){ID=nn;}
int setzong(int z){zong=z;}
int setzjsm(){zjsm=zjsm+1;}
//Record getRecord(int i){return v[i];}
void addRecord(Record r);//添加记录
void chaRecord();
void xjRecord(string sh);
void chashRecord(string nn);//查这个人啥时候还这本书
void shanchu();
friend ostream&operator<<(ostream &os,user&d);
friend istream&operator>>(istream &is,user&d);
};
void user::shanchu()
{
vector<Record>().swap(v);
}
void user::xjRecord(string sh)
{
Time t1;
sh11=sh1.find(sh);
if(sh11!=sh1.end())
{
int j=sh11->second;
if(v[j].getstate()==0)
{
v[j].setstate();
t1=v[j].getending();
v[j].setending(t1);
cout<<"续借成功!"<<endl;
}
else cout<<"已经续借过,不可复借!"<<endl;
}
else cout<<"Error!"<<endl;
}
void user::addRecord(Record r3)
{
v.push_back(r3);
sh1.insert(make_pair(r3.getshuhao(),v.size()-1));
xh1.insert(make_pair(r3.getxuehao(),v.size()-1));
}
void user::chashRecord(string nn)
{
sh11=sh1.find(nn);
if(sh11!=sh1.end())
{
Time tt;
//int i=xh11->second;
tt=v[sh11->second].getending();
v[sh11->second].setjorh();
//v[sh11->second].setstate();
cout<<tt;
}
else cout<<"No record!"<<endl;
}
void user::chaRecord()
{
if(v.size()!=0)
{
for(itn=v.begin();itn!=v.end();itn++)
{
cout<<*itn;
}
}
else cout<<"No record!"<<endl;
}
ostream&operator<<(ostream &os,user&d)
{
int n;
vector<Record>::iterator itn;
os<<d.ID<<" ";
os<<d.name<<" ";
os<<d.zong<<" ";
os<<d.zjsm<<endl;
n=d.zjsm;
itn=d.v.begin();
for(int i=0;i<n;i++)
{
os<<*itn++;
}
return os;
}
istream&operator>>(istream &is,user&d)
{
is>>d.ID;
if(d.ID=="0") return is;
is>>d.name;
is>>d.zong;
is>>d.zjsm;
//is>>d.r1;
return is;
}
/*int main()
{
Record r;
string sh;
cin>>r;
user u;
u.addRecord(r);
cin>>sh;
u.chashRecord(sh);
//u.xjRecord(sh);
cout<<u;
}*/
class Guanli:public chazhao
{
user u1;
Record r2;
vector<user>u;
vector<user>::iterator it2;
map<string,int>m;//按名字查找
map<string,int>::iterator m1;
map<string,int>i;//按ID查找
map<string,int>::iterator i1;
//Shuju sj1;
Xuesheng xue;
vector<Xuesheng>x;
vector<Xuesheng>::iterator x1;
map<string,int>srm;
map<string,int>::iterator sr1;
map<string,int>sri;
map<string,int>::iterator sr2;
public:
Guanli(){};
Guanli(user u,Record r):u1(u),r2(r){}
void addjl1();
void addjl2();//第一次借书
void loadjl();
void savejl();
void chajl();
void huanshu();
void jieshu();
void xujie();
void all();//测验
void loadSj();
void saveSj();
//void loadSj();
void zengshuSj();
void jianshuSj();
void chashuSj();//查书
//void saveSj();
void gaiSj();//更改在馆册数
void loadxs();
void savexs();
void shanxs();
void addxs();
void shanxs1();
void gaimxs();
};
void Guanli::loadxs()
{
int m=0;
ifstream infile("xue.txt",ios::in);
while(infile>>xue)
{
x.push_back(xue);
srm.insert(make_pair(xue.getname(),m));
sri.insert(make_pair(xue.getID(),m));
m++;
}
infile.close();
}
void Guanli::savexs()
{
ofstream outfile("xue.txt",ios::out);
if(!outfile) return;
for(x1=x.begin();x1!=x.end();x1++)
{
outfile<<*x1;
}
outfile.close();
}
void Guanli::shanxs1()
{
string ss;
cin>>ss;
sr1=srm.find(ss);
sr2=sri.find(ss);
if(sr1!=srm.end())
{
x1=x.begin()+sr1->second;
x.erase(x1);
}
else if(sr1==srm.end()&&sr2!=sri.end())
{
x1=x.begin()+sr2->second;
x.erase(x1);
}
else cout<<"No find!!"<<endl;
}
void Guanli::addxs()
{
while(cin>>xue)
{
if(xue.getyear1()==0) break;
x.push_back(xue);
srm.insert(make_pair(xue.getname(),x.size()-1));
sri.insert(make_pair(xue.getID(),x.size()-1));
}
}
void Guanli::gaimxs()
{
string ss,nn;
cin>>ss>>nn;//名+号
sr1=srm.find(ss);
if(sr1!=srm.end())
{
//x1=x.begin()+sr1->second;
x[sr1->second].setID(nn);
}
else cout<<"No find!!"<<endl;
}
void Guanli::zengshuSj()
{
while(cin>>sj1)
{
if(sj1.getshuhao()!="0")
{
sj.push_back(sj1);
sm.insert(make_pair(sj1.getshuming(),sj.size()-1));
sh.insert(make_pair(sj1.getshuhao(),sj.size()-1));
gs.insert(make_pair(sj1.getshuming(),sj1.getzgcs()));
}
else break;
}
}
void Guanli::jianshuSj()
{
string ss;
cin>>ss;//书名
sm1=sm.find(ss);
if(sm1!=sm.end())
{
it=sj.begin()+sm1->second;
sj.erase(it);
sm.erase(ss);
gs.erase(ss);
}
else cout<<"Don't have this book!"<<endl;
}
void Guanli::chashuSj()//查书
{
int n;
cin>>n;
if(n==1) chasm();
else if(n==2) chash();
else if(n==3) chazz();
else if(n==4) chacbs();
else if(n==5) chacbsj();
else cout<<"Error operation!"<<endl;
}
void Guanli::gaiSj()
{
string mh1,mh2;
cin>>mh1>>mh2;
sm1=sm.find(mh1);
sh1=sh.find(mh2);
if(sm1!=sm.end()&&sh1==sh.end())
{
it=sj.begin()+sm1->second;
sj[sm1->second].setshuhao(mh2);
}
else if(sm1==sm.end()&&sh1!=sh.end())
{
it=sj.begin()+sh1->second;
sj[sm1->second].setshuhao(mh2);
}
else cout<<"Don't have this book!"<<endl;
}
void Guanli::all()
{
for(it2=u.begin();it2!=u.end();it2++)
{
cout<<*it2;
}
}
void Guanli::addjl2()
{
cin>>u1;
u.push_back(u1);
m.insert(make_pair(u1.getname(),u.size()-1));
i.insert(make_pair(u1.getID(),u.size()-1));
}
void Guanli::xujie()
{
string ss;
string sss;//书号
cin>>ss;//姓名或学号
m1=m.find(ss);
i1=i.find(ss);
if(m1!=m.end()&&i1==i.end())
{
int s=m1->second;
cin>>sss;//书号
u[s].xjRecord(sss);
sh1=sh.find(sss);
if(sh1!=sh.end())
{
int t=sh1->second;
sj[t].xujiesj(sss);
}
else cout<<"错误输入!"<<endl;
}
else if(m1==m.end()&&i1!=i.end())
{
int s=i1->second;
cin>>sss;
u[s].xjRecord(sss);
sh1=sh.find(sss);
if(sh1!=sh.end())
{
int t=sh1->second;
sj[t].xujiesj(sss);
}
else cout<<"错误输入!"<<endl;
}
else cout<<"Error!"<<endl;
}
void Guanli::huanshu()
{
string ss;
string sss;
string pd;//自己判断是否超期
Time ttn;
cin>>ss;//姓名或学号
m1=m.find(ss);
i1=i.find(ss);
if(m1!=m.end()&&i1==i.end())
{
int s=m1->second;
cin>>sss;//书号
sh1=sh.find(sss);
if(sh1!=sh.end())
{
int f=sh1->second;
sj[f].setzgcs1();
sj[f].huansj(sss);
}
else cout<<"输入有误!"<<endl;
u[s].chashRecord(sss);
cin>>ttn;
cin>>pd;
if(pd=="按期")
{
cout<<"还书成功!"<<endl;
}
else if(pd=="超期")
{
cout<<"需缴纳罚款!"<<endl;
}
else cout<<"Error!"<<endl;
}
else if(m1==m.end()&&i1!=i.end())
{
int s=i1->second;
cin>>sss;//书号
sh1=sh.find(sss);
if(sh1!=sh.end())
{
int f=sh1->second;
sj[f].setzgcs1();
sj[f].huansj(sss);
}
else cout<<"输入有误!"<<endl;
u[s].chashRecord(sss);
cin>>ttn;
cin>>pd;
if(pd=="按期")
{
cout<<"还书成功!"<<endl;
}
else if(pd=="超期")
{
cout<<"需缴纳罚款!"<<endl;
}
else cout<<"Error!"<<endl;
}
else cout<<"Error!"<<endl;
}
void Guanli::jieshu()//如果这个姓名或号码存在,那就是已经借过书的。
{//直接添加记录就可以,如果是不存在的,直接添加新的记录。
string ss;
cin>>ss;
Record rs;
m1=m.find(ss);
i1=i.find(ss);
if(m1!=m.end()&&i1==i.end())
{
cin>>rs;
string ss;//存记录中的书号
ss=rs.getshuhao();
sh1=sh.find(ss);
int m=sh1->second;
sj[m].addRecord1(rs);
sj[m].getzxx();
sj[m].setzgcs2();
int i=m1->second;
u[i].setzjsm();
u[i].addRecord(rs);
}
if(m1==m.end()&&i1!=i.end())
{
cin>>rs;
string ss;//存记录中的书号
ss=rs.getshuhao();
sh1=sh.find(ss);
int m=sh1->second;
sj[m].addRecord1(rs);
sj[m].getzxx();
sj[m].setzgcs2();
int i=i1->second;
u[i].setzjsm();
u[i].addRecord(rs);
}
else cout<<"No Record!"<<endl;
}
void Guanli::chajl()
{
string s;
cin>>s;//根据用户查记录或者根据书名查记录
m1=m.find(s);
sh1=sh.find(s);
if(m1!=m.end()&&sh1==sh.end())
{
cout<<u[m1->second];
}
else if(m1==m.end()&&sh1!=sh.end())
{
cout<<sj[sh1->second];
}
else cout<<"No find!"<<endl;
}
void Guanli::loadjl()
{
user u1;
Record r1;
int n;
int s=0;
ifstream infile("2018212524邱枫-a记录.txt",ios::in);
//i=0;
if(!infile) return;
u.clear();
m.clear();
while(infile>>u1)
{
//infile>>n;
n=u1.getzjsm();
for(int j=0;j<n;j++)
{
infile>>r1;
u1.addRecord(r1);
//continue;
}
//u1.addRecord(r1);
u.push_back(u1);
m.insert(make_pair(u1.getname(),s));
i.insert(make_pair(u1.getID(),s));
s++;
u1.shanchu();
}
infile.close();
}
void Guanli::savejl()
{
ofstream outfile("2018212524邱枫-a记录.txt",ios::out);
if(!outfile) return;
for(it2=u.begin();it2!=u.end();it2++)
{
outfile<<*it2;
}
outfile.close();
}
void Guanli::loadSj()
{
load();
}
void Guanli::saveSj()
{
ofstream outfile("2018212524邱枫-b图书.txt");
if(!outfile) return;
for(it=sj.begin();it!=sj.end();it++)
{
outfile<<*it;
}
outfile.close();
}
int main()
{
Guanli gl;
//int b;
//cin>>b;
//if(b=1)//用户记录
//{
gl.loadjl();
gl.loadSj();
gl.huanshu();
gl.xujie();
gl.addjl2();
gl.jieshu();//已经借过书
gl.chajl();
gl.all();//检验输入输出的错误
gl.savejl();
//gl.saveSj();
//}
//if(b=2)//管理员
//{
//gl.loadSj();
gl.zengshuSj();
gl.jianshuSj();
gl.chashuSj();
gl.gaiSj();
gl.loadxs();//取用户
gl.addxs();//添用户
//gl.shanxs();//批量删用户
gl.shanxs1();//单独删除单个用户
gl.gaimxs();//改学号
gl.savexs();//存入用户
gl.saveSj();
//}
//else cout<<"Error!"<<endl;
}