#include<bits/stdc++.h>
using namespace std;
class Time
{
int month,day;
public:
Time (int b,int c)
{
month=b;day=c;
}
Time ()
{
month=0;day=0;
}
void settime(int b,int c){month=b;day=c;}
int getmonth(){return month;}
int getday(){return day;}
bool operator<(const Time &a)const
{
if(month<a.month)
return true;
else if ((month==a.month)&&day<a.day)
return true;
return false;
}
bool operator==(const Time &a)const
{
if (month==a.month&&day==a.day)
return true;
return false;
}
bool operator>(const Time &a)const
{
if(month>a.month)
return true;
else if ((month==a.month)&&day>a.day)
return true;
return false;
}
friend ostream &operator<<(ostream &os,const Time &);
friend istream &operator>>(istream &is,Time &);
};
ostream &operator<<(ostream &os,const Time &t)
{
os<<t.month<<" "<<t.day;
return os;
}
istream &operator>>(istream &is,Time &t)
{
is>>t.month>>t.day;
return is;
}
class record
{
string no;
Time t1;
Time t2;
string bname;
string autor;
int num;//1 正在借阅中 0 不在借阅中
public:
record (string a,Time b,string c,string d)
{
no=a;t1=b;bname=c;autor=d;t2.settime(t1.getmonth()+3,t1.getday());num=0;
}
record(){};
friend ostream &operator<<(ostream &os,const record &);
friend istream &operator>>(istream &os,record &);
string getno(){return no;}
Time gettime(){return t1;}
void setnum(){num=1;}
string getbname(){return bname;}
};
ostream &operator<<(ostream &os,const record &r)
{
os<<"借阅记录:"<<endl;
os<<"学号:"<<" "<<r.no<<endl;
os<<"书名:"<<" "<<r.bname<<endl;
os<<"作者:"<<" "<<r.autor<<endl;
os<<"借书日期:"<<" "<<r.t1<<endl;
os<<"还书日期:"<<" "<<r.t2<<endl;
os<<"是否借阅中:"<<" "<<r.num<<endl;
return os;
//os<<r.no<<" "<<r.bname<<" "<<r.autor<<" "<<r.t1<<" "<<r.t2<<" "<<r.num<<endl;
//return os;
}
istream &operator>>(istream &is,record &r)
{
string q;
is>>q;
is>>q>>r.no;
if(r.no=="-1")
return is;
is>>q>>r.bname>>q>>r.autor>>q>>r.t1>>q>>r.t2>>q>>r.num;
return is;
}
class book
{
int id;
string name;
string autor;
string publish;
int num;
vector<record> r;
vector<record>::iterator it;
public:
book (int f,string b,string c,string d,int a)
{
id=f;name=b;autor=c;publish=d;
num=a;
}
book (){}
int getid() {return id;}
void setnum(int a){num=a;}
string getname(){return name;}
string getautor(){return autor;}
string getpublish(){return publish;}
void ser(string a);
void add(record a){r.push_back(a);}
int getnum(){return num;}
friend ostream &operator<<(ostream &os,const book &);
friend istream &operator>>(istream &is,book &);
friend class manageop;
friend class studentop;
};
void book::ser(string a)//修改借阅记录
{
for(int i=0;i<r.size();i++)
if(r[i].getno()==a)
{r[i].setnum();
setnum(getnum()+1);
break;}
}
ostream &operator<<(ostream &os,const book &b)
{
os<<"书号:"<<" "<<b.id<<endl;
os<<"书名:"<<" "<<b.name<<endl;
os<<"作者:"<<" "<<b.autor<<endl;
os<<"出版社:"<<" "<<b.publish<<endl;
os<<"库存:"<<" "<<b.num<<endl;
return os;
}
istream &operator>>(istream &is,book &b)
{
record a;
string q;
is>>q>>b.id>>q>>b.name>>q>>b.autor>>q>>b.publish>>q>>b.num;
return is;
}
class student
{
string no;
string name;
int num;
vector<record> r;
vector<record>::iterator it;
public:
student (string a,string b)
{
no=a;name=b;num=0;
}
student (){};
string getno(){return no;}
string getname(){return name;}
void ser(string a);//修改学生借阅记录
int getnum(){return num;}
void setnum(int a){num=a;}
void add(record a){r.push_back(a);}//增加记录
friend istream &operator>>(istream &is,student &);
friend ostream &operator<<(ostream &os,const student &);
friend class manageop;
friend class studentop;
};
void student::ser(string a)//修改学生借阅记录,a书名
{
for(int i=0;i<r.size();i++)
if(r[i].getbname()==a)
{r[i].setnum();
setnum(getnum()-1);
break;}
}
istream &operator>>(istream &is,student &s)
{
string q;
is>>q>>s.no>>q>>s.name>>q>>s.num;
return is;
}
ostream &operator<<(ostream &os,const student &s)
{
os<<"学号:"<<" "<<s.no<<endl<<"学生姓名:"<<" "<<s.name<<endl<<"借阅量:"<<" "<<s.num<<endl;
return os;
}
class manageop
{
vector<book> b;
vector<student> s;
vector<student>::iterator pos;
vector<book>::iterator ra;
multimap<string,int> m1;//学号查询
multimap<string,int>::iterator mit1;
multimap<string,int> m3;//书籍查询
multimap<string,int>::iterator mit3;
public:
manageop()
{
loads();loadb();
}
~manageop()
{
saves();saveb();
}
int searchs(string a);//通过学号查
int searchb(string a);//通过书名查
void adds();//学生加记录
void addb();//书加记录
void addbook();//增加多本图书
void addstu();//增加多名学生用户
void loads();
void loadb();
void saves();
void saveb();
};
/*void manageop::addbook()
{
book o;
int i;
cin>>o;
if(o.getid==-1)
break;
else
b.push_back(o);
}*/
void manageop::adds()
{
string a,d;
int o;
cout<<"请输入你的学号: "<<endl;
cin>>a;
cout<<"请输入你的姓名: "<<endl;
cin>>d;
student c(a,d);
if(searchs(a)==-1)
{s.push_back(c);
o=s.size()-1;
m1.insert(make_pair(c.getno(),o));}//存
else
{cout<<"该学号已被注册,请重新输入:"<<endl;
adds();}
}
void manageop::addb()//增加书的记录
{
int f;
string a,d,c;
int e,o;
cout<<"请输入书号: "<<endl;
cin>>f;
cout<<"请输入书名: "<<endl;
cin>>a;
cout<<"请输入作者: "<<endl;
cin>>d;
cout<<"请输入出版社: "<<endl;
cin>>c;
cout<<"请输入书的数量: "<<endl;
cin>>e;
book p(f,a,d,c,e);
b.push_back(p);
o=b.size()-1;
m3.insert(make_pair(p.getname(),o));
}
int manageop::searchs(string a)//学号
{
mit1=m1.find(a);
if(mit1!=m1.end())
return mit1->second;
else return -1;
}
int manageop::searchb(string a)//查书
{
mit3=m3.find(a);
if(mit3!=m3.end())
return mit3->second;
else return -1;
}
void manageop::saves()
{
ofstream outfile("c:\\20171819\\20171819stu.txt",ios::out);
if(!outfile)
return;
for(int i=0;i<s.size();++i)
{
outfile<<s[i];
for(int j=0;j<s[i].r.size();j++)
outfile<<s[i].r[j];
outfile<<"-1"<<endl;
}
outfile.close();
}
void manageop::saveb()
{
ofstream outfile("c:\\20171819\\20171819book.txt",ios::out);
if(!outfile)
return;
for(int i=0;i<b.size();++i)
{
outfile<<b[i];
for(int j=0;j<b[i].r.size();j++)
outfile<<b[i].r[j];
outfile<<"-1"<<endl;
}
outfile.close();
}
void manageop::loads()
{
student a;
record f;
int o,p;
ifstream infile("c:\\20171819\\20171819stu.txt",ios::in);
if(!infile)
return;
s.clear();
m1.clear();
while(infile>>a)
{s.push_back(a);
o=s.size()-1;
m1.insert(make_pair(a.getno(),o));
while(infile>>f)
if(f.getno()!="-1")
{s[o].r.push_back(f);
}
else
break;}
infile.close();
}
void manageop::loadb()
{
book a;
record f;
int o,p;
ifstream infile("c:\\20171819\\20171819book.txt",ios::in);
if(!infile)
return;
b.clear();
m3.clear();
while(infile>>a)
{b.push_back(a);
o=b.size()-1;
m3.insert(make_pair(a.getname(),o));
while(infile>>f)
if(f.getno()!="-1")
b[o].r.push_back(f);
else
break;}
infile.close();
}
class studentop
{
student s;
vector<book> b;
vector<book>::iterator ra;
multimap<string,int> m3;
multimap<string,int>::iterator mit3;
public:
studentop(string a)
{
loadb();
loads(a);
}
~studentop()
{
saveb();
saves();
}
void loadb();
void saveb();
void loads(string a);
void saves();
void lend();//借书
int searchb(string a);
void repay();//还书
};
void studentop::loads(string a)
{
student c;
record d;
int i=0;
ifstream infile("c:\\20171819\\20171819stu.txt",ios::in);
if(!infile)
return;
while(infile>>c)
{
c.r.clear();
while(infile>>d)
if(d.getno()!="-1")
{c.r.push_back(d);}
else
break;
if(c.getno()==a)
{s=c;i=1;break;}
}
if(i!=1)
cout<<"您输入的学号不正确"<<endl;
infile.close();
}
void studentop::saves()
{
vector<student> v;
student c;
record d;
int i=0;
ifstream infile("c:\\20171819\\20171819stu.txt",ios::in);
if(!infile)
return;
while(infile>>c)
{
c.r.clear();
v.push_back(c);
while(infile>>d)
if(d.getno()!="-1")
{c.r.push_back(d);}
else
break;
if(c.getno()==s.getno())
v[i]=s;
else
v[i]=c;
i++;
}
infile.close();
ofstream outfile("c:\\20171819\\20171819stu.txt",ios::out);
if(!outfile)
return;
for(int j=0;j<v.size();++j)
{
outfile<<v[j];
for(int h=0;h<v[j].r.size();h++)
{outfile<<v[j].r[h];}
outfile<<"-1"<<endl;
}
outfile.close();
}
void studentop::loadb()
{
book a;
record f;
int o;
ifstream infile("c:\\20171819\\20171819book.txt",ios::in);
if(!infile)
return;
b.clear();
m3.clear();
while(infile>>a)
{b.push_back(a);
o=b.size()-1;
m3.insert(make_pair(a.getname(),o));
while(infile>>f)
if(f.getno()!="-1")
b[o].r.push_back(f);
else
break;}
infile.close();
}
void studentop::saveb()
{
ofstream outfile("c:\\20171819\\20171819book.txt",ios::out);
if(!outfile)
return;
for(int i=0;i<b.size();++i)
{
outfile<<b[i];
for(int j=0;j<b[i].r.size();j++)
{outfile<<b[i].r[j];}
outfile<<"-1"<<endl;
}
outfile.close();
}
void studentop::lend()
{
string e,f,a;
a=s.getno();
int h,j;
Time o;
cout<<"请输入书籍名"<<endl;
cin>>e;
j=searchb(e);
if(j!=-1)
{if(b[j].getnum()!=0)
{if(s.getnum()<10)
{
cout<<"请输入借书日期"<<endl;
cin>>o;
record r(a,o,e,b[j].getautor());
s.add(r);
b[j].add(r);
b[j].setnum(b[j].getnum()-1);//修改书借阅记录
s.setnum(s.getnum()+1);//修改学生借阅记录
cout<<"借书成功"<<endl;
}
else
cout<<"对不起您的借阅量已达上限"<<endl;
}
else
cout<<"对不起,目前该书籍的库存不足"<<endl;
}
else
cout<<"未查询到该书籍"<<endl;
}
void studentop::repay()
{
string e,a;
a=s.getno();
cout<<"请输入书籍名"<<endl;
cin>>e;
int j;
j=searchb(e);
s.ser(e);//调用上面函数
b[j].ser(a);//调用上面函数
cout<<"还书成功"<<endl;
}
int studentop::searchb(string a)
{
mit3=m3.find(a);
if(mit3!=m3.end())
return mit3->second;
else return -1;
}
int main()
{
manageop m;
m.adds();
m.addb();
string a;
cout<<"请输入学号:"<<endl;
cin>>a;
studentop s1(a);
s1.lend();
s1.repay();
}
最后的主函数应该先实行增加记录操作,完成后注释掉。 第二步完成借书操作,生成借书记录。第三步完成还书操作,生成记录