图书管理系统是自己写的第二个系统,之前ATM没有钻研细致,到了写这个的时候可算是苦坏了自己,对vetcor,multimap,load,save的使用自己也不熟悉,刚上手的时候真的是很蒙圈,虽然不至于一点也不会写,但写出来的实在不敢恭维,纯粹的是代码的堆积,没有实现什么实际的功能。于是挤牙膏一样一点点的改,从最简单的数据类开始,添上重载运算符,再到book,student里加入借阅记录,再到用文件读入,虽然自己还是没有大彻大悟,但起码算是明了了那么一点,虽然最后自己写的不好,但是能够运行了,从刚开始的无从下手到增加图书增加用户可以实现,对于自己来说算是很大的进步了。当然要学的东西还很多,要写的系统也要更好才行。自己这个小虾米还会继续努力的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<fstream>
using namespace std;
class Time
{
int year;
int month;
int day;
public:
Time(){}
Time(int y,int m,int d)
{
year=y;month=m;day=d;
}
int getYear(){return year;}
int getMonth(){return month;}
int getDay(){return day;}
void setYear(int y){year=y;}
void setMonth(int m){month=m;}
void setDay(int d){day=d;}
void setTime(int y,int m,int d)
{
year=y,month=m,day=d;
}
bool operator<(const Time&t)const
{
return year!=t.year?year<t.year:month!=t.month?month<t.month:day<t.day;
}
friend ostream&operator<<(ostream&out,Time&a)
{
out<<a.year<<"/"<<a.month<<"/"<<a.day;
return out;
}
friend istream&operator>>(istream&in,Time&a)
{
in>>a.year>>a.month>>a.day;
return in;
}
};
class Record
{
int no;//学号
Time lenddate;//借出日期
int num;//书号
string xujie;//是否续借
Time returndate;//归还日期
int state;//现有状态
public:
Record(){}
Record(int n,Time l,int nu)
{
no=n,lenddate=l,num=nu;state=0;xujie="不续借";
if(l.getMonth()+2>12)
{
Time r(l.getYear()+1,l.getMonth()-10,l.getDay());
returndate=r;
}
else
{
Time r(l.getYear(),l.getMonth()+2,l.getDay());
returndate=r;
}
}
int getNo(){return no;}
int getNum(){return num;}
Time getLenddate(){return lenddate;}
string getXujie(){return xujie;}
Time getReturndate(){return returndate;}
int getState(){return state;}
void setNo(int n){no=n;}
void setNum(int nu){num=nu;}
void setLenddate(Time l){lenddate=l;}
void setXujie(){xujie="续借";}
void setReturndate()
{
Time r;
if(returndate.getMonth()+1>12)
r=Time(returndate.getYear()+1,returndate.getMonth()-11,returndate.getDay());
else
r=Time(returndate.getYear(),returndate.getMonth()+1,returndate.getDay());
returndate=r;
}
void setState(int s){state=s;}
friend ostream&operator<<(ostream&out,Record&a)
{
out<<a.no<<" "<<a.lenddate<<" "<<a.num<<" "<<a.xujie<<" "<<a.returndate<<" "<<a.state<<endl;
return out;
}
friend istream&operator>>(istream&in,Record&a)
{
in>>a.no;
if(a.no==-1) return in;
in>>a.lenddate>>a.num>>a.xujie>>a.returndate>>a.state;
return in;
}
};
class Book
{
int num;//书号
string title;//书名
string press;//出版社
int allnum;//总册数
int lendnum;//借出册数
vector<Record>v1;
vector<Record>::iterator it1;
multimap<int,int>m1;
multimap<int,int>::iterator mit1;
public:
Book(){}
Book(int nu,string t,string p,int a,int l)
{
num=nu;title=t;press=p;allnum=a;lendnum=l;
}
vector<Record>getRecord()
{
return v1;
}
int getNum(){return num;}
string getTitle(){return title;}
string getPress(){return press;}
int getAllnum(){return allnum;}
int getLendnum(){return lendnum;}
void setNum(int nu){num=nu;}
void setAllnum(int a){allnum=a;}
void setLendnum(int l){lendnum=l;}
void addRecord1(Record r1);
void dispRecord1();
friend istream&operator>>(istream&in,Book&b)
{
in>>b.num;
if(b.num==-1) return in;
in>>b.title>>b.press>>b.allnum>>b.lendnum;
}
friend ostream&operator<<(ostream&out,Book&b)
{
out<<b.num<<" "<<b.title<<" "<<b.press<<" "<<b.allnum<<" "<<b.lendnum<<" "<<endl;
return out;
}
void display();
};
void Book::display()
{
cout<<num<<" ";
cout<<title<<" ";
cout<<press<<endl;
}
void Book::addRecord1(Record r1)
{
r1.setState(1);
v1.push_back(r1);
int i=v1.size();
m1.insert(make_pair(r1.getNo(),i-1));
}
void Book::dispRecord1()
{
if(v1.size()!=0)
for(it1=v1.begin();it1!=v1.end();it1++)
cout<<*it1;
else
cout<<"No Record!"<<endl;
}
class Student
{
int no;//学号
string name;//姓名
int max;//最大借书数
int yj;//已借数
vector<Record>v2;
vector<Record>::iterator it2;
multimap<int,int>m2;
multimap<int,int>::iterator mit2;
public:
Student(){}
Student(int n,string na,int y)
{
no=n;name=na;max=10;yj=y;
}
int getNo(){return no;}
string getName(){return name;}
int getMax(){return max;}
int getYj(){return yj;}
void setNo(int n){no=n;}
void setName(int na){name=na;}
void setMax(int m){max=m;}
void setYj(int y){yj=y;}
vector<Record>getRecord()
{
return v2;
}
void addRecord2(Record r2);
void dispRecord2();
friend istream&operator>>(istream&in,Student&a)
{
in>>a.no;
if(a.no==-1)
return in;
in>>a.name;
in>>a.max;
in>>a.yj;
return in;
}
friend ostream&operator<<(ostream&out,Student&a)
{
out<<a.no<<" ";
out<<a.name<<" ";
out<<a.max<<" ";
out<<a.yj<<" ";
}
void operator=(Student&a)
{
no=a.getNo();
name=a.getName();
max=a.getMax();
yj=a.getYj();
v2=a.getRecord();
}
bool operator==(Student a)
{
return this->no==a.getNo()?1:0;
}
};
void Student::addRecord2(Record r2)
{
r2.setState(1);
v2.push_back(r2);
int i=v2.size();
m2.insert(make_pair(r2.getNum(),i-1));
}
void Student::dispRecord2()
{
if(v2.size()!=0)
for(it2=v2.begin();it2!=v2.end();it2++)
cout<<*it2;
else
cout<<"No Record!"<<endl;
}
class Manage
{
vector<Book>v3;
vector<Book>::iterator it3;
multimap<int,int>m3;
multimap<int,int>::iterator mit3;
vector<Student>v4;
vector<Student>::iterator it4;
multimap<int,int>m4;
multimap<int,int>::iterator mit4;
public:
Manage()
{
load1();
load2();
}
~Manage()
{
save1();
save2();
}
void addBook();
void dispBook();
int search3(int h);
void queryByNum();
void modifyBook();
void addStudent();
void dispStudent();
int search4(int h);
void queryByNo();
void modifyStudent();
void load1();
void load2();
void save1();
void save2();
};
void Manage::load1()
{
Book b;
int i;
ifstream infile("d:\\Book.txt",ios::in);
if(!infile)
return;
v3.clear();
m3.clear();
i=0;
while(infile>>b)
{
v3.push_back(b);
m3.insert(make_pair(b.getNum(),i-1));
i++;
}
infile.close();
}
void Manage::save1()
{
ofstream outfile("d:\\book.txt",ios::out);
if(!outfile)
return ;
for(it3=v3.begin();it3!=v3.end();it3++)
{
outfile<<*it3;
}
outfile.close();
}
void Manage::load2()
{
Student u;
int i;
ifstream infile("d:\\Student.txt",ios::in);
if(!infile)
return;
v4.clear();
m4.clear();
i=0;
while(infile>>u)
{
v4.push_back(u);
m4.insert(make_pair(u.getNo(),i));
i++;
}
infile.close();
}
void Manage::save2()
{
ofstream outfile("d:\\student.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.getNum()==-1)
break;
v3.push_back(b);
i=v3.size();
m3.insert(make_pair(b.getNum(),i-1));
}
}
void Manage::dispBook()
{
for( it3=v3.begin();it3!=v3.end();++it3)
cout<<*it3;
}
void Manage::addStudent()
{
Student u;
int i;
while(1)
{
cin>>u;
if(u.getNo()==-1)
break;
v4.push_back(u);
i=v4.size();
m4.insert(make_pair(u.getNo(),i-1));
}
}
void Manage::dispStudent()
{
for( it4=v4.begin();it4!=v4.end();++it4)
cout<<*it4;
}
class Client
{
vector<Book>v5;
vector<Book>::iterator it5;
multimap<int,int>m5;
multimap<int,int>::iterator mit5;
Student u;
public:
Client()
{
load3();
load4();
}
~Client()
{
save3();
save4();
}
void load3();
void save3();
void load4();
void save4();
int search5(int h);
void queryBook();
void borrow();
};
void Client::save3()
{
ofstream outfile("d:\\Book.txt",ios::out);
if(!outfile)
{
return ;
}
for(it5=v5.begin();it5!=v5.end();it5++)
{
outfile<<*it5;
}
outfile.close();
}
void Client::load3()
{
Book b;
int i;
ifstream infile("d:\\Book.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.getNum(),i-1));
}
infile.close();
}
void Client::load4()
{
int no;
cin>>no;
Student u;
int i;
ifstream infile("d:\\Student.txt",ios::in);
if(!infile)
{
return;
}
while(infile>>u)
{
if(u.getNo()==no)
this->u=u;
}
infile.close();
}
void Client::save4()
{
vector<Student>v;
vector<Student>::iterator it;
Student u1;
ifstream infile("d:\\Student.txt",ios::in);
if(!infile)
return ;
while(infile>>u1)
{
if(u1==this->u)
u1=this->u;
v.push_back(u1);
}
infile.close();
ofstream outfile("d:\\Student.txt",ios::out);
if(!outfile)
return;
for(it=v.begin();it!=v.end();it++)
outfile<<*it;
outfile.close();
}
int Client::search5(int h)
{
mit5=m5.find(h);
if(mit5!=m5.end())
{
return mit5->second;
}
else
return -1;
}
void Client::queryBook()
{
int h;
cin>>h;
int i=search5(h);
if(i!=-1)
v5[i].display();
}
void Client::borrow()
{
int h,no;
Time t;
cin>>h;
cin>>t;
int a=search5(h);
if(u.getYj()<u.getMax())
{
v5[a].setLendnum(v5[a].getLendnum()+1);
u.setYj(u.getYj()+1);
Record r(u.getNo(),t,h);
v5[a].addRecord1(r);
u.addRecord2(r);
//v5[a].dispRecord1();
//v6[b].dispRecord2();
}
}
int main()
{
/*Book b1(1,"laojie","bianjiang",8,4);
cout<<b1;
b1.display();
Time t1(2018,4,5);
Record r1;(20171812,t1,1);
r1.setXujie();
r1.setReturndate();
r1.setState(1);
b1.addRecord1(r1);
b1.dispRecord1();*/
Manage m1;
//m1.addBook();
//m1.dispBook();
m1.addStudent();
return 0;
}