图书管理系统(并不完善)
#include<bits/stdc++.h>
using namespace std;class Time
{
int month,day;
public:
Time (int a,int b)
{
month=a;day=b;
}
Time ()
{
month=0;day=0;
}
void settime(int a,int b){month=a;day=b;}
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;
public:
record (string a,Time b,string c,string d)
{
no=a;t1=b;bname=c;autor=d;t2.settime(t1.getmonth()+1,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<<r.no<<" "<<r.bname<<" "<<r.autor<<" "<<r.t1<<" "<<r.t2<<" "<<r.num<<endl;
return os;
}
istream &operator>>(istream &is,record &r)
{
is>>r.no;
if(r.no=="-1")return is;
is>>r.bname>>r.autor>>r.t1>>r.t2>>r.num;
return is;
}
class book
{
string name;
string autor;
string publish;
int num;
vector<record> r;
vector<record>::iterator it;
public:
book (string b,string c,string d,int a)
{
name=b;autor=c;publish=d;
num=a;
}
book (){;}
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.name<<" "<<b.autor<<" "<<b.publish<<" "<<b.num<<endl;
return os;
}
istream &operator>>(istream &is,book &b)
{
record a;
is>>b.name>>b.autor>>b.publish>>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)
{
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)
{
is>>s.no>>s.name>>s.num;
return is;
}
ostream &operator<<(ostream &os,const student &s)
{
os<<s.no<<" "<<s.name<<" "<<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<Time,int> m2;
multimap<Time,int>::iterator mit2;
multimap<string,int> m3;
multimap<string,int>::iterator mit3;
public:
manageop()
{
loads();loadb();
}
~manageop()
{
saves();saveb();
}
int searchs(string a);
int searchb(string a);
int searcht(Time a);
void adds();
void addb();
void loads();
void loadb();
void saves();
void saveb();
};
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()
{
string a,d,c;
int e,o;
cout<<"请输入书名"<<endl;
cin>>a;
cout<<"请输入作者"<<endl;
cin>>d;
cout<<"请输入出版社"<<endl;
cin>>c;
cout<<"请输入书的数量"<<endl;
cin>>e;
book p(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::searcht(Time a)
{
mit2=m2.find(a);
if(mit2!=m2.end())
return mit2->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("d:\\20171751stu.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("d:\\20171751book.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("d:\\20171751stu.txt",ios::in);
if(!infile)
return;
s.clear();
m1.clear();
m2.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);
p=s[o].r.size()-1;
m2.insert(make_pair(f.gettime(),p));
}
else
break;}
infile.close();
}
void manageop::loadb()
{
book a;
record f;
int o,p;
ifstream infile("d:\\20171751book.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()
{
loadb();
loads();
}
~studentop()
{
saveb();
saves();
}
void loadb();
void saveb();
void loads();
void saves();
void lend();
int searchb(string a);
void repay();
};
void studentop::loads()
{
string a;
student c;
record d;
cout<<"请输入您的学号"<<endl;
cin>>a;
int i=0;
ifstream infile("d:\\20171751stu.txt",ios::in);
if(!infile)
return;
while(infile>>c)
{
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("d:\\20171751stu.txt",ios::in);
if(!infile)
return;
while(infile>>c)
{
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("d:\\20171751stu.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("d:\\20171751book.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("d:\\20171751book.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()<5)
{
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();
studentop s1;
s1.lend();
s1.repay();
}