#include<bits/stdc++.h>
using namespace std;
class Time
{
int year;
int month;
int day;
int hour;
public:
Time(int x,int y,int z,int v):year(x),month(y),day(z),hour(v){}
Time(){}
int getYear(){return year;};
int getMonth(){return month;};
int getDay(){return day;};
int getHour(){return hour;};
bool operator<(const Time&d)const
{
return year!=d.year?year<d.year:month!=d.month?month<d.month:day!=d.day?day<d.day:hour<d.hour;
}
friend ostream&operator<<(ostream &os, const Time &d);
friend istream&operator>>(istream &in, Time &d);
};
ostream&operator<<(ostream &os,const Time &d)
{
os<<d.year<<" "<<d.month<<" "<<d.day<<" "<<d.hour;
return os;
}
istream&operator>>(istream &in,Time &d)
{
in>>d.year;
in>>d.month;
in>>d.day;
in>>d.hour;
/*while(1)
{
in>>d.year;
if(d.year>1900&&d.year<2019)
break;
else cout<<"请重输\n";
}
while(1)
{
in>>d.month;
if(d.month>0&&d.month<13)
break;
else cout<<"请重输\n";
}
while(1)
{
in>>d.day;
if(d.day>0&&d.day<32)
break;
else cout<<"请重输\n";
}
while(1)
{
in>>d.hour;
if(d.hour>0&&d.hour<24)
break;
else cout<<"请重输\n";
}
*/
return in;
}
/*
记录类
1.包含借阅人一部分信息
2.借阅图书序号及少部分信息
3.简易为下面服务的函数及运算符重载;
*/
class Record
{
string name;
int id;
int id1;
int code;
int major;
int classes;
int nowBorrows;
int breaks;
string type;
Time start;
Time end;
bool either;
Time changeEnd;
public:
Record(int id1,int id11,string type1,Time start1,Time end1):id(id1),id1(id11),type(type1),start(start1),end(end1){changeEnd=end;};
Record(){changeEnd=end;}
Time getStart(){return start;};
Time getEnd(){return changeEnd;};
Time setChange(Time t){changeEnd=t;}
int getNo(){return id;};
int getId(){return id1;};
string getType(){return type;};
string getName(){return name;};
friend ostream&operator<<(ostream&os,const Record&r);
friend istream&operator>>(istream&in,Record&r);
};
istream&operator>>(istream&in,Record&r)
{
in>>r.id;
if(r.id==0)return in;
in>>r.id1;
in>>r.type;
in>>r.start;
in>>r.end;
r.changeEnd=r.end;
return in;
}
ostream&operator<<(ostream&os,const Record&r)
{
os<<r.id<<" ";
os<<r.id1<<" ";
os<<r.type<<" ";
os<<r.start<<" ";
os<<r.changeEnd<<" "<<endl;
return os;
}
/*
书数据类
1.提供书的信息
2.提供这本书一些人借阅记录查阅
3.可借数量,判断函数为下面操作类服务
*/
class Book//书籍类
{
string name;
string name1;
string press;
int no;
int number;//每类书的总数
int borrow;//借出数量
int left;
vector<Record>r;
int num;
public:
Book(string name1,int no1,string press1,int number1,int num1):name(name1),no(no1),press(press1),number(number1),num(num1){}
~Book(){r.clear();}
Book(){number=0;borrow=0;num=0;}
string getName(){return name;};
string getPress(){return press;};
Record getRecord(int i){return r[i];};
void addBorrowsJ(int n){borrow+=n;}
void addBorrowsH(int n){borrow-=n;};
int getNo(){return no;};
int getNum(){return num;};
void setNum(int i){num+=i;};
void addRecord(Record rr){num++;r.push_back(rr);};
int getNowNumber(){return (number-borrow);}//可借数量
int getNumber(){return number;};
int getBorrows(){return borrow;};
int getLeft(){return left;};
vector<Record>getRecord(){return r;};
friend ostream&operator<<(ostream&os,const Book&b);
friend istream&operator>>(istream&in,Book&b);
void de(){r.clear();}
};
istream&operator>>(istream&in,Book&b)
{
in>>b.name;
if(b.name=="E")return in;
in>>b.no>>b.name1>>b.press>>b.number>>b.num;
for(int i=0;i<b.num;++i)
{
Record rr;
in>>rr;
b.r.push_back(rr);
}
return in;
}
ostream&operator<<(ostream&os,const Book&b)
{
os<<b.name<<" "<<b.no<<" "<<b.name1<<" "<<b.press<<" "<<b.number<<" "<<b.num<<endl;
for(int i=0;i<b.num;++i)
{
os<<b.r[i];
}
return os;
}
/*
用户数据类
1.提供大多数数据
2.包含一部分小函数
3.查询函数,显示函数,修改函数,为操作类服务;
4.一部分注释,修改版
*/
class User
{
string name;
string major;
string classes;
int id;
int num;//记录条数
int code;
int borrows;
int nowBorrows;
int canReborrow;
bool breaks;
vector<Book>b;
multimap<int,int>b1;
multimap<int,int>::iterator b1p;
vector<Record>r;
vector<Record>::iterator it;
multimap<int,int>m1;
multimap<int,int>::iterator m1p;
public:
User()
{borrows=10;breaks=0;nowBorrows=0;canReborrow=1;num=0;}
~User(){b.clear();r.clear();m1.clear();}
void addOne();
void addSome();
int getNowBorrows(){return nowBorrows;};
int getNowReborrow(){return canReborrow;};
int getBreaks(){return breaks;};
int getId(){return id;}
string getName(){return name;}
void addReborrow(int i){canReborrow-=i;};
void addBreaks(int m){breaks+=m;};
void addNowBorrowsJ(int m){nowBorrows+=m;};
void addNowBorrowsH(int m){nowBorrows-=m;};
void addRecord(Record rr){num++;r.push_back(rr);}
void show();
void queryTime(Time t);
void changeTime(int no,Time t);
void queryNow();
Time search1(int no);
//void reBorrow();
friend istream&operator>>(istream&in,User &u);
friend ostream&operator<<(ostream&os,User &u);
void operator=(User u);
bool operator==(User u);
vector<Record>getRecord(){return r;};
vector<Book>getBook(){return b;};
void de1(){r.clear();}
void de2(){b.clear();}
};
Time User::search1(int no)
{
for(it=r.begin();it!=r.end();it++)
{
if(it->getId()==no)//->
{
return it->getEnd();
break;
}
}
}
bool User::operator==(User u)
{
return this->id==u.getId()?1:0;
}
void User::operator=(User u)
{
id=u.id;
code=u.code;
name=u.name;
major=u.major;
classes=u.classes;
nowBorrows=u.nowBorrows;
breaks=u.breaks;
num=u.num;
r=u.getRecord();
}
void User::changeTime(int no,Time t)//具体哪一位学生
{
for(it=r.begin();it!=r.end();it++)
{
if(it->getNo()==no)
{
r[it-r.begin()].setChange(t);
}
}
}
void User::queryTime(Time t)
{
for(it=r.begin();it!=r.end();it++)
{
if(t<it->getEnd())
cout<<*it;
}
}
void User::show()
{
for(it=r.begin();it!=r.end();it++)
{
cout<<*it;
}
}
void User::queryNow()
{
cout<<"输入当前时间\n";
Time t;
cin>>t;
for(it=r.begin();it!=r.end();it++)
{
if(t<r[it-r.begin()].getEnd())
cout<<r[it-r.begin()];
}
}
istream&operator>>(istream&in,User &u)
{
in>>u.id;
if(u.id==0)return in;
in>>u.code>>u.name>>u.major>>u.classes>>u.nowBorrows>>u.breaks>>u.num;
for(int i=0;i<u.num;++i)
{
Record rr;
in>>rr;
u.r.push_back(rr);
}
return in;
}
ostream&operator<<(ostream&os,User&u)
{
os<<u.id<<" "<<u.code<<" "<<u.name<<" "<<u.major<<" "<<u.classes<<" "<<u.nowBorrows<<" "<<u.breaks<<" "<<u.num<<endl;
for(int i=0;i<u.num;++i)
{
os<<u.r[i];
}
}
/*
操作类
1.查询当前信息
2.查询记录
2.借还书
3.续借
4.添加记录
*/
class Operate
{
vector<Book>b;
vector<Book>::iterator it1;
multimap<int,int>b1;
multimap<int,int>::iterator b1p;
User u;
public:
Operate(int id)
{
load1();
load2(id);
}
~Operate()
{
load1p();
load2p();
u.getRecord().clear();
u.getBook().clear();
//u.m1.clear();
b.clear();
b1.clear();
}
void load1();//把文件中的数据读到vector里
void load2(int id);
void load1p();//析构函数
void load2p();
int search2(int no);//查书
void borrowBook();//借书
void returnBook();//还书
void reBorrow();//修改时间续借
void queryNumber();//查询当前是否可借
void queryNow();//查询当前
void queryHistory();//查询历史
};
int Operate::search2(int no)
{
b1p=b1.find(no);
if(b1p!=b1.end())
{
int i=b1p->second;
return i;
}
else return -1;
}
void Operate::queryNumber()
{
cout<<"您想查询的书的序号为\n";
int no;
cin>>no;
if(b[search2(no)].getNowNumber()>0)
cout<<"可借\n";
else
cout<<"不可借\n";
}
void Operate::reBorrow()
{
cout<<"您想续借的书的序号为\n";
int no;
cin>>no;
cout<<"您想更改的日期为\n";
Time t;
cin>>t;
u.changeTime(no,t);
}
void Operate::queryNow()
{
u.queryNow();
}
void Operate::queryHistory()
{
cout<<"您的记录为\n";
u.show();
}
void Operate::borrowBook()
{
cout<<"你想借的书的编号及目前和借到的时间\n";
int no;
Time t1,t2;
cin>>no>>t1>>t2;
b1p=b1.find(no);
if(u.getBreaks()>=3||u.getNowBorrows()>10||b[b1p->second].getNowNumber()<=0)
{
return;
}
Record r(no,u.getId(),"J",t1,t2);
u.addRecord(r);
b[b1p->second].addRecord(r);
u.addNowBorrowsJ(1);//该学生可借数量减1
b[b1p->second].addBorrowsJ(1);
}
void Operate::returnBook()
{
cout<<"你借的书的编号及借书和目前时间\n";
int no;
Time t1,t2;
cin>>no>>t1>>t2;
b1p=b1.find(no);
if(t2<u.search1(no))
{
u.addBreaks(1);//判断是否超期
}
Record r(no,u.getId(),"H",t1,t2);
u.addRecord(r);
b[b1p->second].addRecord(r);
u.addNowBorrowsH(1);
b[b1p->second].addBorrowsH(1);//书的数量变化
}
void Operate::load1p()
{
ofstream outfileB("e:\\20171789book.txt",ios::out);
if(!outfileB)
return;
for(it1=b.begin();it1!=b.end();it1++)
{
outfileB<<*it1;
}
outfileB.close();
}
void Operate::load2p()//
{
vector<User>v1;
vector<User>::iterator it;
User u1;
ifstream infile("e:\\20171789stu.txt",ios::in);
if(!infile)
return ;
while(infile>>u1)
{
if(u1==this->u)//this->u为当前对象
{
u1=this->u;
}
v1.push_back(u1);////?
u1.de1();
u1.de2();///////////////////
}
infile.close();
/*
先把新的vector构建出来,在文件中替换这个学生的信息
再把这组数据写到文件中;
*/
ofstream outfile("e:\\20171789stu.txt",ios::out);
if(!outfile)
return;
for(it=v1.begin();it!=v1.end();it++)
outfile<<*it;//写回文件
outfile.close();
}
void Operate::load1()
{
int i;
Book bb;
ifstream infile("e:\\20171789book.txt",ios::in);
if(!infile)
return;
b.clear();
b1.clear();
i=0;
while(infile>>bb)
{
b.push_back(bb);
b1.insert(make_pair(bb.getNo(),i));
bb.de();////////////
i++;
}
infile.close();
}
void Operate::load2(int id)
{
//int id;
//cin>>id;
User uu;
int i;
ifstream infile("e:\\20171789stu.txt",ios::in);
if(!infile)
return;
while(infile>>uu)
{
if(uu.getId()==id)
{
u=uu;
break;
}
else
{
uu.de1();//////////////////
uu.de2();
}
}
infile.close();
}
/*
管理员类
1.增加学生图书信息
2.删除学生图书信息
3.修改学生图书信息
4.查询学生图书信息
*/
class Adm//更新为STL
{
vector<Book>b;
vector<Book>::iterator it1;
multimap<int,int>b1;
multimap<int,int>::iterator b1p;
vector<User>u;
vector<User>::iterator it;
multimap<int,int>u1;
multimap<int,int>::iterator u1p;
//对书集合和学生集合操作
public:
Adm()
{
load1();
load2();
}
~Adm()
{
load1p();
load2p();
}
void load1();//把文件中的数据读到vector里
void load2();
void load1p();//析构函数
void load2p();
void addUser();//增加一个用户
void addBook();
void deletesU();//删除一个用户
void deletesB();
void searchS();
void searchB();
void queryIdS();//查询
void queryIdB();
void changeU();//修改
void changeB();
};
void Adm::load1p()
{
ofstream outfileB("e:\\20171789book.txt",ios::out);
if(!outfileB)
return;
for(it1=b.begin();it1!=b.end();it1++)
{
outfileB<<*it1;
}
outfileB.close();
}
void Adm::load2p()
{
ofstream outfileS("e:\\20171789stu.txt",ios::out);
if(!outfileS)
return;
for(it=u.begin();it!=u.end();it++)
{
outfileS<<*it;
}
outfileS.close();
}
void Adm::queryIdS()//STL
{
cout<<"你想查询的学生学号为\n";
User uu;
int id;
cin>>id;
u1p=u1.find(id);
cout<<u[u1p->second];
}
void Adm::queryIdB()
{
cout<<"你想查询的书号为\n";
Book bb;
int id;
cin>>id;
b1p=b1.find(id);
cout<<b[b1p->second];
}
void Adm::changeU()//STL
{
cout<<"您想修改信息的学生的学号为\n";
User uu;
int id;
cin>>id;
u1p=u1.find(id);//it??
it=u.begin()+u1p->second;
u.erase(it);
u1.erase(id);//删除map内指定元素;
cout<<"请重输\n";
cin>>uu;
u.insert(it,uu);
//u1.insert(u1p,uu);
cout<<"修改信息成功\n";
}
void Adm::changeB()//STL
{
cout<<"您想修改信息的书的编号为\n";
Book bb;
int id;
cin>>id;
b1p=b1.find(id);
it1=b.begin()+b1p->second;
b.erase(it1);
b1.erase(id);
cout<<"请重输\n";
cin>>bb;
b.insert(it1,bb);
cout<<"修改信息成功\n";
}
void Adm::deletesU()
{
cout<<"你想删除的学生学号为\n";
int id;
User uu;
cin>>id;
u1p=u1.find(id);//it??
it=u.begin()+u1p->second;
it=u.erase(it);
cout<<"删除成功\n";
}
void Adm::deletesB()
{
cout<<"您想删除的书的编号是\n";
int id;
Book bb;
cin>>id;
b1p=b1.find(id);
it1=b.begin()+b1p->second;
it1=b.erase(it1);
cout<<"删除成功\n";
}
void Adm::addUser()
{
cout<<"您想增加的用户信息为,学号输入为0结束\n";
User uu;
while(cin>>uu)
{
if(uu.getId()==0)break;
u.push_back(uu);
cout<<"增加用户信息成功\n";
}
};
void Adm::addBook()
{
cout<<"您想增加的图书信息为:书名输入为E结束\n";
Book bb;
while(cin>>bb)
{
if(bb.getName()=="E")break;
b.push_back(bb);
cout<<"增加图书信息成功\n";
}
}
void Adm::load1()
{
int i;
Book bb;
ifstream infile("e:\\20171789book.txt",ios::in);
if(!infile)
return;
b.clear();
b1.clear();
i=0;
while(infile>>bb)
{
b.push_back(bb);
b1.insert(make_pair(bb.getNo(),i));
i++;
}
infile.close();
}
void Adm::load2()
{
int i;
User uu;
ifstream infile("e:\\20171789stu.txt",ios::in);
if(!infile)
return;
u.clear();
u1.clear();
i=0;
while(infile>>uu)
{
u.push_back(uu);
u1.insert(make_pair(uu.getId(),i));
i++;
}
infile.close();
}
int main()
{
int id;
cin>>id;
Operate o(id);
//o.addOne();
//o.queryHistory();
//o.queryNow();
o.borrowBook();
//o.returnBook();
//Adm a;
//a.deletesB();
//a.queryIdB();
//a.queryIdS();
//a.deletesU();
//a.addUser();
//a.addBook();
//a.deletesB();
return 0;
}
#include<bits/stdc++.h>
using namespace std;
class Time
{
int year;
int month;
int day;
int hour;
public:
Time(int x,int y,int z,int v):year(x),month(y),day(z),hour(v){}
Time(){}
int getYear(){return year;};
int getMonth(){return month;};
int getDay(){return day;};
int getHour(){return hour;};
bool operator<(const Time&d)const
{
return year!=d.year?year<d.year:month!=d.month?month<d.month:day!=d.day?day<d.day:hour<d.hour;
}
friend ostream&operator<<(ostream &os, const Time &d);
friend istream&operator>>(istream &in, Time &d);
};
ostream&operator<<(ostream &os,const Time &d)
{
os<<d.year<<" "<<d.month<<" "<<d.day<<" "<<d.hour;
return os;
}
istream&operator>>(istream &in,Time &d)
{
in>>d.year;
in>>d.month;
in>>d.day;
in>>d.hour;
/*while(1)
{
in>>d.year;
if(d.year>1900&&d.year<2019)
break;
else cout<<"请重输\n";
}
while(1)
{
in>>d.month;
if(d.month>0&&d.month<13)
break;
else cout<<"请重输\n";
}
while(1)
{
in>>d.day;
if(d.day>0&&d.day<32)
break;
else cout<<"请重输\n";
}
while(1)
{
in>>d.hour;
if(d.hour>0&&d.hour<24)
break;
else cout<<"请重输\n";
}
*/
return in;
}
/*
记录类
1.包含借阅人一部分信息
2.借阅图书序号及少部分信息
3.简易为下面服务的函数及运算符重载;
*/
class Record
{
string name;
int id;
int id1;
int code;
int major;
int classes;
int nowBorrows;
int breaks;
string type;
Time start;
Time end;
bool either;
Time changeEnd;
public:
Record(int id1,int id11,string type1,Time start1,Time end1):id(id1),id1(id11),type(type1),start(start1),end(end1){changeEnd=end;};
Record(){changeEnd=end;}
Time getStart(){return start;};
Time getEnd(){return changeEnd;};
Time setChange(Time t){changeEnd=t;}
int getNo(){return id;};
int getId(){return id1;};
string getType(){return type;};
string getName(){return name;};
friend ostream&operator<<(ostream&os,const Record&r);
friend istream&operator>>(istream&in,Record&r);
};
istream&operator>>(istream&in,Record&r)
{
in>>r.id;
if(r.id==0)return in;
in>>r.id1;
in>>r.type;
in>>r.start;
in>>r.end;
r.changeEnd=r.end;
return in;
}
ostream&operator<<(ostream&os,const Record&r)
{
os<<r.id<<" ";
os<<r.id1<<" ";
os<<r.type<<" ";
os<<r.start<<" ";
os<<r.changeEnd<<" "<<endl;
return os;
}
/*
书数据类
1.提供书的信息
2.提供这本书一些人借阅记录查阅
3.可借数量,判断函数为下面操作类服务
*/
class Book//书籍类
{
string name;
string name1;
string press;
int no;
int number;//每类书的总数
int borrow;//借出数量
int left;
vector<Record>r;
int num;
public:
Book(string name1,int no1,string press1,int number1,int num1):name(name1),no(no1),press(press1),number(number1),num(num1){}
~Book(){r.clear();}
Book(){number=0;borrow=0;num=0;}
string getName(){return name;};
string getPress(){return press;};
Record getRecord(int i){return r[i];};
void addBorrowsJ(int n){borrow+=n;}
void addBorrowsH(int n){borrow-=n;};
int getNo(){return no;};
int getNum(){return num;};
void setNum(int i){num+=i;};
void addRecord(Record rr){num++;r.push_back(rr);};
int getNowNumber(){return (number-borrow);}//可借数量
int getNumber(){return number;};
int getBorrows(){return borrow;};
int getLeft(){return left;};
vector<Record>getRecord(){return r;};
friend ostream&operator<<(ostream&os,const Book&b);
friend istream&operator>>(istream&in,Book&b);
void de(){r.clear();}
};
istream&operator>>(istream&in,Book&b)
{
in>>b.name;
if(b.name=="E")return in;
in>>b.no>>b.name1>>b.press>>b.number>>b.num;
for(int i=0;i<b.num;++i)
{
Record rr;
in>>rr;
b.r.push_back(rr);
}
return in;
}
ostream&operator<<(ostream&os,const Book&b)
{
os<<b.name<<" "<<b.no<<" "<<b.name1<<" "<<b.press<<" "<<b.number<<" "<<b.num<<endl;
for(int i=0;i<b.num;++i)
{
os<<b.r[i];
}
return os;
}
/*
用户数据类
1.提供大多数数据
2.包含一部分小函数
3.查询函数,显示函数,修改函数,为操作类服务;
4.一部分注释,修改版
*/
class User
{
string name;
string major;
string classes;
int id;
int num;//记录条数
int code;
int borrows;
int nowBorrows;
int canReborrow;
bool breaks;
vector<Book>b;
multimap<int,int>b1;
multimap<int,int>::iterator b1p;
vector<Record>r;
vector<Record>::iterator it;
multimap<int,int>m1;
multimap<int,int>::iterator m1p;
public:
User()
{borrows=10;breaks=0;nowBorrows=0;canReborrow=1;num=0;}
~User(){b.clear();r.clear();m1.clear();}
void addOne();
void addSome();
int getNowBorrows(){return nowBorrows;};
int getNowReborrow(){return canReborrow;};
int getBreaks(){return breaks;};
int getId(){return id;}
string getName(){return name;}
void addReborrow(int i){canReborrow-=i;};
void addBreaks(int m){breaks+=m;};
void addNowBorrowsJ(int m){nowBorrows+=m;};
void addNowBorrowsH(int m){nowBorrows-=m;};
void addRecord(Record rr){num++;r.push_back(rr);}
void show();
void queryTime(Time t);
void changeTime(int no,Time t);
void queryNow();
Time search1(int no);
//void reBorrow();
friend istream&operator>>(istream&in,User &u);
friend ostream&operator<<(ostream&os,User &u);
void operator=(User u);
bool operator==(User u);
vector<Record>getRecord(){return r;};
vector<Book>getBook(){return b;};
void de1(){r.clear();}
void de2(){b.clear();}
};
Time User::search1(int no)
{
for(it=r.begin();it!=r.end();it++)
{
if(it->getId()==no)//->
return it->getEnd();
}
}
bool User::operator==(User u)
{
return this->id==u.getId()?1:0;
}
void User::operator=(User u)
{
id=u.id;
code=u.code;
name=u.name;
major=u.major;
classes=u.classes;
nowBorrows=u.nowBorrows;
breaks=u.breaks;
num=u.num;
r=u.getRecord();
}
void User::changeTime(int no,Time t)//具体哪一位学生
{
for(it=r.begin();it!=r.end();it++)
{
if(it->getNo()==no)
{
r[it-r.begin()].setChange(t);
}
}
}
void User::queryTime(Time t)
{
for(it=r.begin();it!=r.end();it++)
{
if(t<it->getEnd())
cout<<*it;
}
}
void User::show()
{
for(it=r.begin();it!=r.end();it++)
{
cout<<*it;
}
}
void User::queryNow()
{
cout<<"输入当前时间\n";
Time t;
cin>>t;
for(it=r.begin();it!=r.end();it++)
{
if(t<r[it-r.begin()].getEnd())
cout<<r[it-r.begin()];
}
}
istream&operator>>(istream&in,User &u)
{
in>>u.id;
if(u.id==0)return in;
in>>u.code>>u.name>>u.major>>u.classes>>u.nowBorrows>>u.breaks>>u.num;
for(int i=0;i<u.num;++i)
{
Record rr;
in>>rr;
u.r.push_back(rr);
}
return in;
}
ostream&operator<<(ostream&os,User&u)
{
os<<u.id<<" "<<u.code<<" "<<u.name<<" "<<u.major<<" "<<u.classes<<" "<<u.nowBorrows<<" "<<u.breaks<<" "<<u.num<<endl;
for(int i=0;i<u.num;++i)
{
os<<u.r[i];
}
}
/*
操作类
1.查询当前信息
2.查询记录
2.借还书
3.续借
4.添加记录
*/
class Operate
{
vector<Book>b;
vector<Book>::iterator it1;
multimap<int,int>b1;
multimap<int,int>::iterator b1p;
User u;
public:
Operate()
{
load1();
load2();
}
~Operate()
{
load1p();
load2p();
u.getRecord().clear();
u.getBook().clear();
//u.m1.clear();
b.clear();
b1.clear();
}
void load1();//把文件中的数据读到vector里
void load2();
void load1p();//析构函数
void load2p();
int search2(int no);//查书
void borrowBook();//借书
void returnBook();//还书
void reBorrow();//修改时间续借
void queryNumber();//查询当前是否可借
void queryNow();//查询当前
void queryHistory();//查询历史
};
int Operate::search2(int no)
{
b1p=b1.find(no);
if(b1p!=b1.end())
{
int i=b1p->second;
return i;
}
else return -1;
}
void Operate::queryNumber()
{
cout<<"您想查询的书的序号为\n";
int no;
cin>>no;
if(b[search2(no)].getNowNumber()>0)
cout<<"可借\n";
else
cout<<"不可借\n";
}
void Operate::reBorrow()
{
cout<<"您想续借的书的序号为\n";
int no;
cin>>no;
cout<<"您想更改的日期为\n";
Time t;
cin>>t;
u.changeTime(no,t);
}
void Operate::queryNow()
{
u.queryNow();
}
void Operate::queryHistory()
{
cout<<"您的记录为\n";
u.show();
}
void Operate::borrowBook()
{
cout<<"你想借的书的编号及目前和借到的时间\n";
int no;
Time t1,t2;
cin>>no>>t1>>t2;
b1p=b1.find(no);
if(u.getBreaks()>=3||u.getNowBorrows()>10||b[b1p->second].getNowNumber()<=0)
{
return;
}
Record r(no,u.getId(),"J",t1,t2);
u.addRecord(r);
b[b1p->second].addRecord(r);
u.addNowBorrowsJ(1);//该学生可借数量减1
b[b1p->second].addBorrowsJ(1);
}
void Operate::returnBook()
{
cout<<"你借的书的编号及借书和目前时间\n";
int no;
Time t1,t2;
cin>>no>>t1>>t2;
b1p=b1.find(no);
if(u.search1(no)<t2)
{
u.addBreaks(1);//判断是否超期
}
Record r(no,u.getId(),"H",t1,t2);
u.addRecord(r);
b[b1p->second].addRecord(r);
u.addNowBorrowsH(1);
b[b1p->second].addBorrowsH(1);//书的数量变化
}
void Operate::load1p()
{
ofstream outfileB("e:\\20171789book.txt",ios::out);
if(!outfileB)
return;
for(it1=b.begin();it1!=b.end();it1++)
{
outfileB<<*it1;
}
outfileB.close();
}
void Operate::load2p()//
{
vector<User>v1;
vector<User>::iterator it;
User u1;
ifstream infile("e:\\20171789stu.txt",ios::in);
if(!infile)
return ;
while(infile>>u1)
{
if(u1==this->u)//this->u为当前对象
{
u1=this->u;
}
v1.push_back(u1);////?
u1.de1();
u1.de2();///////////////////
}
infile.close();
/*
先把新的vector构建出来,在文件中替换这个学生的信息
再把这组数据写到文件中;
*/
ofstream outfile("e:\\20171789stu.txt",ios::out);
if(!outfile)
return;
for(it=v1.begin();it!=v1.end();it++)
outfile<<*it;//写回文件
outfile.close();
}
void Operate::load1()
{
int i;
Book bb;
ifstream infile("e:\\20171789book.txt",ios::in);
if(!infile)
return;
b.clear();
b1.clear();
i=0;
while(infile>>bb)
{
b.push_back(bb);
b1.insert(make_pair(bb.getNo(),i));
bb.de();////////////
i++;
}
infile.close();
}
void Operate::load2()
{
int id;
cout<<"输入学号\n";
cin>>id;
User uu;
int i;
ifstream infile("e:\\20171789stu.txt",ios::in);
if(!infile)
return;
while(infile>>uu)
{
if(uu.getId()==id)
{
u=uu;
break;
}
else
{
uu.de1();//////////////////
uu.de2();
}
}
infile.close();
}
/*
管理员类
1.增加学生图书信息
2.删除学生图书信息
3.修改学生图书信息
4.查询学生图书信息
*/
class Adm//更新为STL
{
vector<Book>b;
vector<Book>::iterator it1;
multimap<int,int>b1;
multimap<int,int>::iterator b1p;
vector<User>u;
vector<User>::iterator it;
multimap<int,int>u1;
multimap<int,int>::iterator u1p;
//对书集合和学生集合操作
public:
Adm()
{
load1();
load2();
}
~Adm()
{
load1p();
load2p();
}
void load1();//把文件中的数据读到vector里
void load2();
void load1p();//析构函数
void load2p();
void addUser();//增加一个用户
void addBook();
void deletesU();//删除一个用户
void deletesB();
void searchS();
void searchB();
void queryIdS();//查询
void queryIdB();
void changeU();//修改
void changeB();
};
void Adm::load1p()
{
ofstream outfileB("e:\\20171789book.txt",ios::out);
if(!outfileB)
return;
for(it1=b.begin();it1!=b.end();it1++)
{
outfileB<<*it1;
}
outfileB.close();
}
void Adm::load2p()
{
ofstream outfileS("e:\\20171789stu.txt",ios::out);
if(!outfileS)
return;
for(it=u.begin();it!=u.end();it++)
{
outfileS<<*it;
}
outfileS.close();
}
void Adm::queryIdS()//STL
{
cout<<"你想查询的学生学号为\n";
User uu;
int id;
cin>>id;
u1p=u1.find(id);
cout<<u[u1p->second];
}
void Adm::queryIdB()
{
cout<<"你想查询的书号为\n";
Book bb;
int id;
cin>>id;
b1p=b1.find(id);
cout<<b[b1p->second];
}
void Adm::changeU()//STL
{
cout<<"您想修改信息的学生的学号为\n";
User uu;
int id;
cin>>id;
u1p=u1.find(id);//it??
it=u.begin()+u1p->second;
u.erase(it);
u1.erase(id);//删除map内指定元素;
cout<<"请重输\n";
cin>>uu;
u.insert(it,uu);
//u1.insert(u1p,uu);
cout<<"修改信息成功\n";
}
void Adm::changeB()//STL
{
cout<<"您想修改信息的书的编号为\n";
Book bb;
int id;
cin>>id;
b1p=b1.find(id);
it1=b.begin()+b1p->second;
b.erase(it1);
b1.erase(id);
cout<<"请重输\n";
cin>>bb;
b.insert(it1,bb);
cout<<"修改信息成功\n";
}
void Adm::deletesU()
{
cout<<"你想删除的学生学号为\n";
int id;
User uu;
cin>>id;
u1p=u1.find(id);//it??
it=u.begin()+u1p->second;
it=u.erase(it);
cout<<"删除成功\n";
}
void Adm::deletesB()
{
cout<<"您想删除的书的编号是\n";
int id;
Book bb;
cin>>id;
b1p=b1.find(id);
it1=b.begin()+b1p->second;
it1=b.erase(it1);
cout<<"删除成功\n";
}
void Adm::addUser()
{
cout<<"您想增加的用户信息为,学号输入为0结束\n";
User uu;
while(cin>>uu)
{
if(uu.getId()==0)break;
u.push_back(uu);
cout<<"增加用户信息成功\n";
}
};
void Adm::addBook()
{
cout<<"您想增加的图书信息为:书名输入为E结束\n";
Book bb;
while(cin>>bb)
{
if(bb.getName()=="E")break;
b.push_back(bb);
cout<<"增加图书信息成功\n";
}
}
void Adm::load1()
{
int i;
Book bb;
ifstream infile("e:\\20171789book.txt",ios::in);
if(!infile)
return;
b.clear();
b1.clear();
i=0;
while(infile>>bb)
{
b.push_back(bb);
b1.insert(make_pair(bb.getNo(),i));
i++;
}
infile.close();
}
void Adm::load2()
{
int i;
User uu;
ifstream infile("e:\\20171789stu.txt",ios::in);
if(!infile)
return;
u.clear();
u1.clear();
i=0;
while(infile>>uu)
{
u.push_back(uu);
u1.insert(make_pair(uu.getId(),i));
i++;
}
infile.close();
}
int main()
{
Operate o;
//o.addOne();
//o.queryHistory();
//o.queryNow();
o.borrowBook();
o.returnBook();
//Adm a;
//a.deletesB();
//a.queryIdB();
//a.queryIdS();
//a.deletesU();
//a.addUser();
//a.addBook();
//a.deletesB();
return 0;
}
或者 111 余华 山东 10 2
111 20171789 J 2018 1 1 1 2018 6 6 6
111 20171789 H 2018 1 1 1 2018 5 5 5
傲娇与偏见 112 奥斯汀 青岛 10 4
112 20171791 J 2019 9 9 9 2019 10 10 10
112 20171791 H 2019 9 9 9 2019 9 10 10
112 20171790 J 2019 1 1 1 2019 2 2 2
112 20171790 H 2019 1 1 1 2019 1 2 2
童年 113 高尔基 上海 10 0
20171789 666 小王 计算机 2 0 1 2
111 20171789 J 2018 1 1 1 2018 6 6 6
111 20171789 H 2018 1 1 1 2018 5 5 5
20171790 666 小李 计算机 3 0 1 2
112 20171790 J 2019 1 1 1 2019 2 2 2
112 20171790 H 2019 1 1 1 2019 1 2 2
20171791 666 小菲 计算机 1 0 1 2
112 20171791 J 2019 9 9 9 2019 10 10 10
112 20171791 H 2019 9 9 9 2019 9 10 10