#include<bits/stdc++.h>
using namespace std;
class Time//时间类,已重载运算符
{
int year;
int month;
int day;
public:
Time()
{
year=0;
month=0;
day=0;
}
Time(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void setYear(int y){year=y;}
int getYear(){return year;}
void setMonth(int m){month=m;}
int getMonth(){return month;}
void setDay(int d){day=d;}
int getDay(){return day;}
friend istream&operator>>(istream&input,Time&t);
friend ostream&operator<<(ostream&output,Time&t);
bool operator<(const Time&t1)const
{
return year!=t1.year?year<t1.year:month!=t1.month?month<t1.month:day<t1.day;
}
};
ostream&operator<<(ostream&output,Time&t)
{
output<<t.year<<" ";
output<<t.month<<" ";
output<<t.day;
return output;
}
istream&operator>>(istream&input,Time&t)
{
input>>t.year>>t.month>>t.day;
return input;
}
class Record//借书记录类 已重载运算符
{
int id;
Time tim;
int shuhao;
Time daoqi;
int xj;
int state;
public:
Record(){}
Record(int i,Time s,int h)
{
id=i;
tim=s;
shuhao=h;
/*
if(s.getMonth()==11||s.getMonth()==12)
{
Time e(s.getYear()+1,s.getMonth()-10,s.getDay());
daoqi=e;
}
else
{
Time e(s.getYear(),s.getMonth(),s.getDay());
}
*/
xj=0;
state=0;
}
int getId(){return id;}
Time getTim(){return tim;}
int getShuhaouhao(){return shuhao;}
Time getDaoqi(){return daoqi;}
void setDaoqi();
void setXj(){xj=1;}
int getXj(){return xj;}
void setState(int n){state=n;}
int getState(){return state;}
friend ostream&operator<<(ostream&output,Record&r);
friend istream&operator>>(istream&input,Record&r);
};
void Record::setDaoqi()
{
Time e;
if(daoqi.getMonth()==11||daoqi.getMonth()==12)
e=Time(daoqi.getYear()+1,daoqi.getMonth()+1,daoqi.getDay());
else
e=Time(daoqi.getYear()+1,daoqi.getMonth(),daoqi.getDay());
daoqi=e;
}
istream&operator>>(istream&input,Record&r)
{
if(r.id==0)
return input;
input>>r.id;
input>>r.tim;
input>>r.shuhao;
input>>r.daoqi;
input>>r.xj;
input>>r.state;
return input;
}
ostream&operator<<(ostream&output,Record&r)
{
output<<r.id<<" ";
output<<r.tim<<" ";
output<<r.shuhao<<" ";
output<<r.daoqi<<" ";
output<<r.xj<<" ";
output<<r.state<<endl;
return output;
}
class Book
{
int shuhao;
int date;
string name;
string press;
int num;
int jc;
vector<Record>v1;
vector<Record>::iterator it1;
multimap<int,int>m1;
multimap<int,int>::iterator mit1;
public:
Book()
{
}
Book(int h,string nam,string p,int c,int n,int j)
{
shuhao=h;
name=nam;
press=p;
date=c;
num=n;
jc=j;
}
vector<Record>getRecord()
{
return v1;
}
int getShuhao(){return shuhao;}
string getName(){return name;}
string getPress(){return press;}
int getDate(){return date;}
void setNum(int n){num=n;}
int getNum(){return num;}
void setJc(int j){jc=j;}
int getJc(){return jc;}
void addRecord1(Record r1);
void dispRecord1();
void deleteRecord1(int no);
friend istream&operator>>(istream&input,Book&b);
friend ostream&operator<<(ostream&output,Book&b);
int search1(int no);
void display();
void modify(int no);
void putRecord();
};
void Book::display()
{
cout<<shuhao<<endl;
cout<<name<<endl;
cout<<press<<endl;
cout<<date<<endl;
}
istream&operator>>(istream&input,Book&b)
{
input>>b.shuhao;
if(b.shuhao==-1)
return input;
input>>b.name;
input>>b.press;
input>>b.date;
input>>b.num;
input>>b.jc;
}
ostream&operator<<(ostream&output,Book&b)
{
output<<b.shuhao<<endl;
output<<b.name<<endl;
output<<b.press<<endl;
output<<b.date<<endl;
output<<b.num<<endl;
output<<b.jc<<endl;
/*for(int i=0;i<b.v1.size();i++)
output<<b.v1[i];
return output;*/
}
void Book::addRecord1(Record r1)
{
r1.setState(1);
v1.push_back(r1);
int i=v1.size();
m1.insert(make_pair(r1.getId(),i-1));
}
void Book::dispRecord1()
{
if(v1.size()!=0)
for(it1=v1.begin();it1!=v1.end();it1++)
cout<<*it1;
else
cout<<"No Record!"<<endl;
}
int Book::search1(int no)
{
mit1=m1.find(no);
if(mit1!=m1.end())
return mit1->second;
else
return -1;
}
void Book::modify(int no)
{
int i=search1(no);
if(v1[i].getXj()==0)
{
v1[i].setXj();
v1[i].getDaoqi();
}
}
void Book::deleteRecord1(int no)
{
int i;
i=search1(no);
if(i!=-1)
{
v1[i].setState(0);
}
}
void Book::putRecord()
{
Record r;
while(cin>>r)
{if(r.getId()==0)
break;
v1.push_back(r);}
}
class User
{
int id;
string name;
int max;
int now;
int wj;
vector<Record>v2;
vector<Record>::iterator it2;
multimap<int,int>m2;
multimap<int,int>::iterator mit2;
public:
User()
{
}
User(int i,string nam,int n)
{
id=i;
name=nam;
max=10;
now=n;
}
int getId(){return id;}
void setName(string nam){name=nam;}
string getName(){return name;}
void setMax(int m){max=m;}
int getMax(){return max;}
void setNow(int n){now=n;}
int getNow(){return now;}
void setWj(int n){wj=n;}
int getWj(){return wj;}
vector<Record>getRecord(){return v2;}
void addRecord2(Record r2);
void dispRecord2();
int search2(int h);
void deleteRecord2(int h);
void modify(int h);
friend istream&operator>>(istream&input,User&u);
friend ostream&operator<<(ostream&output,User&u);
void pd(Time t);
void operator=(User&u)
{
id=u.getId();
name=u.getName();
max=u.getMax();
now=u.getNow();
v2=u.getRecord();
}
bool operator==(User u)
{
return this->id==u.getId()?1:0;
}
};
istream&operator>>(istream&input,User&u)
{
input>>u.id;
if(u.id==-1)
return input;
input>>u.name;
input>>u.max;
input>>u.now;
input>>u.wj;
return input;
}
ostream&operator<<(ostream&output,User&u)
{
output<<u.id<<endl;
output<<u.name<<endl;
output<<u.max<<endl;
output<<u.now<<endl;
output<<u.wj<<endl;
/* for(int i=0;i<u.v2.size();i++)
output<<u.v2[i];
return output;*/
}
void User::addRecord2(Record r2)
{
r2.setState(1);
v2.push_back(r2);
int i=v2.size();
m2.insert(make_pair(r2.getShuhaouhao(),i-1));
}
void User::dispRecord2()
{
if(v2.size()!=0)
for(it2=v2.begin();it2!=v2.end();it2++)
cout<<*it2;
else
cout<<"No Record!"<<endl;
}
int User::search2(int h)
{
mit2=m2.find(h);
if(mit2!=m2.end())
return mit2->second;
else
return -1;
}
void User::deleteRecord2(int h)
{
int i;
i=search2(h);
if(i!=-1)
{
v2[i].setState(0);
}
}
void User::pd(Time t)
{
setWj(0);
for(int i=0;i<v2.size();i++)
if(v2[i].getDaoqi()<t&&v2[i].getState()==1)
{
setWj(1);
break;
}
}
class Manage
{
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:
Manage()
{
}
~Manage()
{
save1();
save2();
}
void addBook();
void dispBook();
int search3(int h);
void chaxun();
void deleteBook();//*
void xiugai();//修改信息
void addUser();
void dispUser();
int search4(int h);
void queryById();
void deleteUser();//*没实现
void modifyUser();
void load1();
void load2();
void save1();
void save2();
};
void Manage::load1()
{
Book b;
int i;
ifstream infile("20171791Book.txt",ios::in);
if(!infile)
return;
v3.clear();
m3.clear();
i=0;
while(infile>>b)
{v3.push_back(b);
m3.insert(make_pair(b.getShuhao(),i-1));
i++;
}
infile.close();
}
void Manage::save1()
{
ofstream outfile("20171791Book.txt",ios::out);
if(!outfile)
return ;
for(it3=v3.begin();it3!=v3.end();it3++)
{
outfile<<*it3;
}
outfile.close();
}//删除与装载
void Manage::load2()
{
User u;
int i;
ifstream infile("20171791user.txt",ios::in);
if(!infile)
return;
v4.clear();
m4.clear();
i=0;
while(infile>>u)
{v4.push_back(u);
m4.insert(make_pair(u.getId(),i));
i++;
}
infile.close();
}
void Manage::save2()
{
ofstream outfile("20171791user.txt",ios::out);
if(!outfile)
return ;
for(it4=v4.begin();it4!=v4.end();it4++)
{
outfile<<*it4;
}
outfile.close();
}
void Manage::addBook()//增加图书
{
Book b;
int i;
while(1)
{
cin>>b;
if(b.getShuhao()==-1)
break;
v3.push_back(b);
i=v3.size();
m3.insert(make_pair(b.getShuhao(),i-1));
}
}
void Manage::addUser()//增加用户
{
User u;
int i;
while(1)
{
cin>>u;
if(u.getId()==-1)
break;
v4.push_back(u);
i=v4.size();
m4.insert(make_pair(u.getId(),i-1));
}
}
void Manage::chaxun()//查询图书
{
int h;
cin>>h;
int i=search3(h);
cout<<v3[i];
}
void Manage::xiugai()
{
int h;
cin>>h;
int i=search3(h);
int n;
cin>>n;
v3[i].setNum(n);
}
void Manage::dispBook()
{
for( it3=v3.begin();it3!=v3.end();++it3)
cout<<*it3;
}
int Manage::search3(int h)
{
mit3=m3.find(h);
if(mit3!=m3.end())
{
return mit3->second;
}
else
return -1;
}
int Manage::search4(int no)
{
mit4=m4.find(no);
if(mit4!=m4.end())
{
return mit4->second;
}
else
return -1;
}
void Manage::queryById()
{
int no;
cin>>no;
int i=search4(no);
cout<<v4[i];
}
void Manage::modifyUser()
{
int no;
cin>>no;
int i=search4(no);
int m;
cin>>m;
v4[i].setMax(m);
}
void Manage::dispUser()
{
for( it4=v4.begin();it4!=v4.end();++it4)
cout<<*it4;
}
void User::modify(int h)
{
int i=search2(h);
if(v2[i].getXj()==0)
{
v2[i].setXj();
v2[i].setDaoqi();
}
}
class Userop
{
vector<Book>v5;
vector<Book>::iterator it5;
multimap<int,int>m5;
multimap<int,int>::iterator mit5;
User u;
public:
Userop()
{
load3();
load4();
}
~Userop()
{
save3();
save4();
}
void load3();
void display();
void save3();
void load4();
void save4();
int search5(int h);
void queryBook();
void borrow();
void back();
};
void Userop::display()
{
for( int i=0;i<v5.size();i++)
cout<<v5[i];
}
void Userop::save3()
{
ofstream outfile("20171791Book.txt",ios::out);
if(!outfile)
{
return ;
}
for(it5=v5.begin();it5!=v5.end();it5++)
{
outfile<<*it5;
}
outfile.close();
}
void Userop::load3()
{
Book b;
int i;
ifstream infile("20171791Book.txt",ios::in);
if(!infile)
{
return;
}
v5.clear();
m5.clear();
while(infile>>b)
{
v5.push_back(b);
int i=v5.size();
m5.insert(make_pair(b.getShuhao(),i-1));
}
infile.close();
}
void Userop::load4()
{
int id;
cin>>id;
User u;
int i;
ifstream infile("20171791user.txt",ios::in);
if(!infile)
return;
while(infile>>u)
{
if(u.getId()==id)
this->u=u;
}
infile.close();
}
void Userop::borrow()
{
int h,no;
Time t;
cin>>h;
cin>>t;
int a=search5(h);
u.pd(t);
if(u.getWj()==0&&u.getNow()<u.getMax())
{v5[a].setJc(v5[a].getJc()+1);
u.setNow(u.getNow()+1);
Record r(u.getId(),t,h);
v5[a].addRecord1(r);
u.addRecord2(r);
}
}
void Userop::save4()
{
vector<User>v;
vector<User>::iterator it;
User u1;
ifstream infile("20171791user.txt",ios::in);
if(!infile)
return ;
while(infile>>u1)
{
if(u1==this->u)
u1=this->u;
v.push_back(u1);
}
infile.close();
ofstream outfile("20171791user.txt",ios::out);
if(!outfile)
return;
for(it=v.begin();it!=v.end();it++)
outfile<<*it;
outfile.close();
}
int Userop::search5(int h)
{
mit5=m5.find(h);
if(mit5!=m5.end())
{
return mit5->second;
}
else
return -1;
}
void Userop::queryBook()
{
int h;
cin>>h;
int i=search5(h);
if(i!=-1)
v5[i].display();void Userop::borrow()
{
int h,no;
Time t;
cin>>h;
cin>>t;
int a=search5(h);
u.pd(t);
if(u.getWj()==0&&u.getNow()<u.getMax())
{v5[a].setJc(v5[a].getJc()+1);
u.setNow(u.getNow()+1);
Record r(u.getId(),t,h);
v5[a].addRecord1(r);
u.addRecord2(r);
//v5[a].dispRecord1();
//v6[b].dispRecord2();
}
}
}
int main()
{
Manage m;
m.addBook();
m.chaxun();
m.xiugai();
//m.deleteBook();//并没有实现
m.addUser();
m.queryById();
m.modifyUser();
//m.deleteUser();//没实现
/*Userop c1;
c1.queryBook();
c1.borrow()//;
c1.back();*/
}
简单的图书管理系统
最新推荐文章于 2019-12-25 15:27:39 发布