经过数天的艰苦奋斗,简单的图书管理系统终于能够正常运行
该系统可以实现增加图书 增加用户,借书还书的功能。
增书 addBook()
增用户addUser()
借书borrow()
还书back()
下面附上源代码
#include<bits/stdc++.h>
using namespace std;
class Time
{
int year,month,day;
public:
Time(int y,int m,int d){year=y;month=m;day=d;}
Time(){}
void setYear(int y){year=y;}
void setMonth(int m){month=m;}
void setDay(int d){day=d;}
int getYear(){return year;}
int getMonth(){return month;}
int getDay(){return day;}
bool operator<(const Time&t1)const
{
return year!=t1.year?year<t1.year:month!=t1.month?month<t1.month:day<t1.day;
}
friend istream&operator>>(istream&input,Time&t);
friend ostream&operator<<(ostream&output,Time&t);
};
istream&operator>>(istream&input,Time&t)
{
input>>t.year>>t.month>>t.day;
return input;
}
ostream&operator<<(ostream&output,Time&t)
{
output<<t.year<<" ";
output<<t.month<<" ";
output<<t.day;
return output;
}
class Record
{
int id;
Time start;
int sh;
Time end;
int xj;
int state;
public:
Record(int i,Time s,int h)
{
id=i;
start=s;
sh=h;
if(s.getMonth()+2>12)
{
Time e(s.getYear()+1,s.getMonth()-10,s.getDay());
end=e;
}
else
{
Time e(s.getYear(),s.getMonth()+2,s.getDay());
end=e;
}
xj=0;
state=0;
}
Record(){}
int getId(){return id;}
Time getStart(){return start;}
int getSh(){return sh;}
Time getEnd(){return end;}
void setEnd();
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::setEnd()
{
Time e;
if(end.getMonth()+1>12)
e=Time(end.getYear()+1,end.getMonth()+1,end.getDay());
else
e=Time(end.getYear()+1,end.getMonth(),end.getDay());
end=e;
}
istream&operator>>(istream&input,Record&r)
{
input>>r.id;
input>>r.start;
input>>r.sh;
input>>r.end;
input>>r.xj;
input>>r.state;
return input;
}
ostream&operator<<(ostream&output,Record&r)
{
output<<r.id<<" ";
output<<r.start<<" ";
output<<r.sh<<" ";
output<<r.end<<" ";
output<<r.xj<<" ";
output<<r.state<<endl;
return output;
}
class Book
{
int sh;
string name;
string press;
int date;
int num;
int jc;
int rnum;
vector<Record>v1;
vector<Record>::iterator it1;
multimap<int,int>m1;
multimap<int,int>::iterator mit1;
public:
Book(){rnum=0;}
vector<Record>getRecord(){return v1;}
int getSh(){return sh;}
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 setRnum(int n){rnum=n;}
int getRnum(){return rnum;}
void addRecord1(Record r1);
void addR1(Record r1);
void deleteRecord1(int no);
friend istream&operator>>(istream&input,Book&b);
friend ostream&operator<<(ostream&output,Book&b);
int search1(int no);
void clear1();
};
istream&operator>>(istream&input,Book&b)
{
input>>b.sh;
if(b.sh==-1)
return input;
input>>b.name;
input>>b.press;
input>>b.date;
input>>b.num;
input>>b.jc;
input>>b.rnum;
return input;
}
ostream&operator<<(ostream&output,Book&b)
{
output<<b.sh<<" "<<b.name<<" "<<b.press<<" "<<b.date<<" "<<b.num<<" "<<b.jc<<endl;
output<<b.rnum<<endl;
if(b.getRnum()!=0)
for(int i=0;i<b.getRnum();i++)
output<<b.v1[i];
return output;
}
void Book::addR1(Record r1)
{
v1.push_back(r1);
int i=v1.size();
m1.insert(make_pair(r1.getId(),i-1));
}
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::clear1()
{
v1.clear();
}
int Book::search1(int no)
{
mit1=m1.find(no);
if(mit1!=m1.end())
return mit1->second;
else
return -1;
}
void Book::deleteRecord1(int no)
{
int i;
i=search1(no);
if(i!=-1)
{
v1[i].setState(0);
}
}
class User
{
int id;
string name;
int max;
int now;
int wj;
int rnum;
vector<Record>v2;
vector<Record>::iterator it2;
multimap<int,int>m2;
multimap<int,int>::iterator mit2;
public:
User(){wj=0;rnum=0;}
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;}
void setRnum(int n){rnum=n;}
int getRnum(){return rnum;}
vector<Record>getRecord(){return v2;}
multimap<int,int>getMap(){return m2;}
void clear2();
void addRecord2(Record r2);
int search2(int h);
void deleteRecord2(int h);
friend istream&operator>>(istream&input,User&u);
friend ostream&operator<<(ostream&output,User&u);
void pd(Time t);
void addR2(Record r);
void operator=(User&u)
{
id=u.getId();
name=u.getName();
max=u.getMax();
now=u.getNow();
wj=u.getWj();
rnum=u.getRnum();
v2=u.getRecord();
m2=u.getMap();
}
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;
input>>u.rnum;
return input;
}
ostream&operator<<(ostream&output,User&u)
{
output<<u.id<<" "<<u.name<<" "<<u.max<<" "<<u.now<<" "<<u.wj<<" "<<endl;
output<<u.rnum<<endl;
if(u.getRnum()!=0)
for(int i=0;i<u.getRnum();i++)
output<<u.v2[i];
return output;
}
void User::addR2(Record r2)
{
v2.push_back(r2);
int i=v2.size();
m2.insert(make_pair(r2.getSh(),i-1));
}
void User::addRecord2(Record r2)
{r2.setState(1);
v2.push_back(r2);
int i=v2.size();
m2.insert(make_pair(r2.getSh(),i-1));
}
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].getEnd()<t&&v2[i].getState()==1){setWj(1);break;}
}
void User::clear2()
{
v2.clear();
}
class Op
{
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:
Op(){}
~Op()
{save1();
save2();}
void addBook();
int search3(int h);
void addUser();
int search4(int h);
void load1();
void load2();
void save1();
void save2();
};
void Op::save1()
{
ofstream outfile("d:\\20171790Book.txt",ios::out);
if(!outfile)
return ;
for(it3=v3.begin();it3!=v3.end();it3++)
{
outfile<<*it3;
}
outfile.close();
}
void Op::save2()
{
ofstream outfile("d:\\20171790Stu.txt",ios::out);
if(!outfile)
return ;
for(it4=v4.begin();it4!=v4.end();it4++)
{
outfile<<*it4;
}
outfile.close();
}
void Op::addBook()
{
Book b;
int i;
while(cin>>b)
{
if(b.getSh()==-1)
break;
v3.push_back(b);
i=v3.size();
m3.insert(make_pair(b.getSh(),i-1));
b.clear1();
}
}
int Op::search3(int h)
{
mit3=m3.find(h);
if(mit3!=m3.end())
{
return mit3->second;
}
else
return -1;
}
void Op::addUser()
{
User u;
int i;
while(cin>>u)
{
if(u.getId()==-1)
break;
v4.push_back(u);
i=v4.size();
m4.insert(make_pair(u.getId(),i-1));
u.clear2();
}
}
int Op::search4(int no)
{
mit4=m4.find(no);
if(mit4!=m4.end())
{
return mit4->second;
}
else
return -1;
}
class Client
{
vector<Book>v5;
vector<Book>::iterator it5;
multimap<int,int>m5;
multimap<int,int>::iterator mit5;
User u;
Time t;
public:
Client(int id)
{
cout<<"请依次输入时间,书号"<<endl;
load3();
load4(id);
}
~Client()
{save3();
save4();
}
void load3();
void save3();
void load4(int id);
void save4();
int search5(int h);
void queryBook();
void borrow();
void back();
void renew();
};
void Client::save3()
{
ofstream outfile("d:\\20171790Book.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:\\20171790Book.txt",ios::in);
if(!infile)
{return;
}
v5.clear();
m5.clear();
while(infile>>b)
{
if(b.getRnum()!=0)
for(int i=0;i<b.getRnum();i++)
{
Record r3;
infile>>r3;
b.addR1(r3);
}
v5.push_back(b);
int i=v5.size();
m5.insert(make_pair(b.getSh(),i-1));
b.clear1();
}
infile.close();
}
void Client::load4(int id)
{
User u1;
int i;
cin>>t;
ifstream infile("d:\\20171790Stu.txt",ios::in);
if(!infile)
return;
while(infile>>u1)
{if(u1.getRnum()!=0)
for(int i=0;i<u1.getRnum();i++)
{
Record r4;
infile>>r4;
u1.addR2(r4);
}
if(u1.getId()==id)
this->u=u1;
u1.clear2();
}
infile.close();
}
void Client::save4()
{vector<User>v;
vector<User>::iterator it;
User u2;
ifstream infile("d:\\20171790Stu.txt",ios::in);
if(!infile)
return ;
while(infile>>u2)
{
if(u2.getRnum()!=0)
for(int i=0;i<u2.getRnum();i++)
{
Record r5;
infile>>r5;
u2.addR2(r5);
}
if(u2==this->u)
u2=this->u;
v.push_back(u2);
u2.clear2();
}
infile.close();
ofstream outfile("d:\\20171790Stu.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::borrow()
{
int bid;
cin>>bid;
int a=search5(bid);
u.pd(t);
if(u.getWj()==0&&u.getNow()<u.getMax())
{v5[a].setJc(v5[a].getJc()+1);
v5[a].setRnum(v5[a].getRnum()+1);
u.setNow(u.getNow()+1);
u.setRnum(u.getRnum()+1);
Record r(u.getId(),t,bid);
v5[a].addRecord1(r);
u.addRecord2(r);
cout<<"借阅成功"<<endl;
}
}
void Client::back()
{
int bid;
cin>>bid;
int a=search5(bid);
v5[a].setJc(v5[a].getJc()-1);
u.setNow(u.getNow()-1);
v5[a].deleteRecord1(u.getId());
u.deleteRecord2(bid);
cout<<"成功还书"<<endl;
}
int main()
{
//Op op;
//op.addBook();
//op.addUser();
//Client c(20171790);
//c.borrow();
//c.back();
}
通过这次的实习,我从C++语言这门课程中发现了程序设计的乐趣,在学习C++语言的过程中也学到了许多计算机应用基础知识。这是个艰辛而又漫长的过程,通过这次C++语言课程设计,我受益匪浅。
首先我只是按照课题的要求,编写了一个很简单的程序,以为每个功能都有应该没什么错,一到我输入计算机中来运行,错误好多,无法修改。后来查了些资料,有一个与这个课题差不多的例题,看后真是惊了,程序那么长,我写出的只有那么一点点,我考虑到的,用到的东西实在太少了。我只好按照这个例题的方法重新写程序输进去,运行一下,有几十个错误,那只有自己仔仔细细的进行修改。我发现例题中的某些功能和课题要求的功能不大一样,就必须进行大幅度修改,在修改的过程中,发现读懂程序的报错很重要。在实际操作过程中犯的一些错误还会有意外的收获,这学期所学的C++语言的理论知识得到巩固,也发现自己的不足之处,同时体会到C++语言具有的语句简洁,使用灵活,执行效率高等特点。编程时我体会到变量赋了值不能不使用该变量等许多微小的容易放错误的地方。
还有最重要的一点就是,编写程序是十分需要耐心和毅力的。在编写程序的过程中肯定会遇到许多困难和错误。当你做到中途时你很有可能不想写下去,因为很多错误你根本就无法很容易的发现,当时的心情烦躁是很正常的。这时我们要调整心情,要相信自己一定能做出来,要有坚强的毅力才能最终完成项目。