感悟
首先做任何一个程序,首先必须要有一个总体的规划,整体思路必须在程序开始写之前完成,剩下的细节在打程序后再进行逐步添加。
比如这次的图书管理系统需要差不多5个类 时间类Time 纪录类 Record 学生类Student 图书类 Book 客户端类 Client 管理类 Manage
之后使用继承加入了类Records
每个数据类规划好基本的数据
通过最近打代码各种类,类其实都是由基本模板构成
基本格式:由数据类 操作类 登陆类构成
数据类1{
public:
带参构造函数;
无参构造函数;
get/set函数;
一些必要的处理数据函数;
运算符重载函数;
private:
基本数据成员;
数据对象成员;
};
...
操作类1{
public:
带参/无参构造函数;//完成必要的初始化操作(例如从文件读数据初始化相关的集合对象及其他数据成员,调用菜单函数)
功能实现函数;
void menu();
辅助函数;
void load();//读文件中的相关数据到集合数据成员
void save();//将更新的数据成员信息写到文件中
析构函数
private:
由数据类构成的集合对象;
其他数据成员;
}
....
登录验证类{
public:
构造函数;//从密码文件读取信息并初始化集合对象
//1.对用户输入的登录信息结合密码对象进行合法验证
//2.对合法用户:确定用户具体角色,并生成对应的操作类对象,执行对应操作类功能;
void login();
private:
含密码信息的集合对象;
}
int main(){
定义验证类对象;
调用验证类的login函数;
return 0;
}
代码如下
#include<bits/stdc++.h>
using namespace std;
class Time
{
int year,month,day;
public:
Time(int x,int y,int z):year(x),month(y),day(z){}
Time(){year=month=day=0;}
int getYear(){return year;}
void setYear(int y){year=y;}
int getMonth(){return month;}
void setMonth(int m){month=m;}
int getDay(){return day;}
void setDay(int d){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 &t);
friend istream &operator>>(istream &in,Time &t);
};
ostream &operator<<(ostream& out,Time &t)
{
out<<t.year<<" "<<t.month<<" "<<t.day;
return out;
}
istream &operator>>(istream &in,Time &t)
{
in>>t.year>>t.month>>t.day;
return in;
}
class Record
{
int id;//学号
string name;
int sh;//书号
Time start;
Time end;
int xj;//0的时候可续借一次,续借后为1
int bz;//记录标志,借出书时为1,归还后为0
public:
Record(int a,string na,int s,Time t1,Time t2):start(t1),end(t2)
{
id=a;
name=na;
sh=s;
xj=0;
bz=1;
}
Record(){}
int getId(){return id;}
void setId(int i){id=i;}
string getName(){return name;}
void setName(string na){name=na;}
int getSh(){return sh;}
Time getS(){return start;}
Time getE(){return end;}
void setE(Time e){end=e;}
int getXj(){return xj;}
void setXj(int x){xj=x;}
int getBz(){return bz;}
void setBz(int b){bz=b;}
friend ostream &operator<<(ostream &out,Record &r);
friend istream &operator>>(istream &in,Record &r);
};
ostream &operator<<(ostream &out,Record &r)
{
out<<r.id<<" "<<r.name<<" "<<r.sh<<" ";
out<<r.start<<" ";
out<<r.end<<" ";
out<<r.xj<<" "<<r.bz<<endl;
return out;
}
istream &operator>>(istream &in,Record &r)
{
in>>r.id;
if(r.id==-1) return in;
in>>r.name>>r.sh>>r.start>>r.end>>r.xj>>r.bz;
return in;
}
class User
{
int id;
string name;
int max;
int now;
int wj;//违纪时为1
int num;//记录条数
vector<Record>v1;
vector<Record>::iterator it1;
multimap<int,int>m1;
multimap<int,int>::iterator mit1;
multimap<Time,int>m2;
multimap<Time,int>::iterator mit2;
public:
User(int i,string s,int n,int w)
{
id=i;
name=s;
max=10;
now=n;
wj=w;
}
User()
{
id=2017;
name="null";
max=10;
now=0;
wj=0;
}
int getId(){return id;}
void setId(int i){id=i;}
string getName(){return name;}
void setName(string na){name=na;}
int getMax(){return max;}
void setMax(int m){max=m;}
int getNow(){return now;}
void setNow(int n){now=n;}
int getWj(){return wj;}
void setWj()
{
wj=1;
}
int getNum(){return num;}
void setNum(int n){num=n;}
void add(Record r1);
int search1(int n);
int search2(Time t);
void dispAllRecord();
vector<Record>getRecord(){return v1;}
multimap<int,int>getM1(){return m1;}
multimap<Time,int>getM2(){return m2;}
void clear1(){v1.clear();}
void setJs(int s,Time t);
bool operator ==(User &u1)
{
if(this->id==u1.getId())
return 1;
else
return 0;
}
void operator =(User &u)
{
id=u.getId();
name=u.getName();
max=u.getMax();
now=u.getNow();
wj=u.getWj();
num=u.getNum();
v1=u.getRecord();
m1=u.getM1();
m2=u.getM2();
}
friend istream &operator>>(istream &in,User &u);
friend ostream &operator<<(ostream &out,User &u);
};
void User::add(Record r1)
{
int i;
v1.push_back(r1);
i=v1.size();
m1.insert(make_pair(r1.getSh(),i-1));
m2.insert(make_pair(r1.getS(),i-1));
}
int User::search1(int n)
{
mit1=m1.find(n);
if(mit1!=m1.end())
{
return mit1->second;
}
else
return -1;
}
void User::setJs(int s,Time t)
{
int i;
i=search1(s);
if(i!=-1)
{
v1[i].setBz(0);
v1[i].setE(t);
}
}
int User::search2(Time t)
{
mit2=m2.find(t);
if(mit2!=m2.end())
{
return mit2->second;
}
else return -1;
}
void User::dispAllRecord()
{
for(it1=v1.begin();it1!=v1.end();it1++)
cout<<*it1;
}
istream &operator>>(istream &in,User &u)
{
in>>u.id;
if(u.id==-1) return in;
in>>u.name>>u.max>>u.now>>u.wj;
in>>u.num;
return in;
}
ostream &operator<<(ostream &out,User &u)
{
out<<u.id<<" "<<u.name<<" "<<u.max<<" "<<u.now<<" "<<u.wj<<endl;
out<<u.num<<endl;
if(u.getNum()!=0)
for(int i=0;i<u.getNum();i++)
out<<u.v1[i];
return out;
}
class Book
{
int sh;//书号
string name;//书名
string writer;//作者
string publish;//出版日期
int jc;//借出数量
int sy;//剩余数量
int sum;//每类书的总数
int num;//记录条数
vector<Record>v3;
vector<Record>::iterator it3;
multimap<int,int>m3;
multimap<int,int>::iterator mit3;
multimap<Time,int>m4;
multimap<Time,int>::iterator mit4;
public:
Book(int s,string na,string w,string p,int a,int b,int c,int n):sh(s),name(na),writer(w),publish(p),jc(a),sy(b),sum(c),num(n){}
Book(){}
int getSh(){return sh;}
void setSh(int s){sh=s;}
string getName(){return name;}
void setName(string na){name=na;}
string getWriter(){return writer;}
void setWriter(string w){writer=w;}
string getPublish(){return publish;}
void setPublish(string p){publish=p;}
int getJc(){return jc;}
void setJc(int j){jc=j;}
int getSy(){return sy;}
void setSy(int s){sy=s;}
int getSum(){return sum;}
void setSum(int s){sum=s;}
int getNum(){return num;}
void setNum(int n){num=n;}
vector<Record>getRecord()
{
return v3;
}
void add(Record r2);
int search3(int n1);
int search4(Time t1);
void dispAllRecord();
void clear2(){v3.clear();}
void setJs(int n,Time t);
friend ostream&operator<<(ostream &out,Book &b);
friend istream&operator>>(istream &in,Book &b);
};
void Book::add(Record r2)
{
int i;
v3.push_back(r2);
i=v3.size();
m3.insert(make_pair(r2.getId(),i-1));
m4.insert(make_pair(r2.getS(),i-1));
}
int Book::search3(int n)
{
mit3=m3.find(n);
if(mit3!=m3.end())
{
return mit3->second;
}
else
return -1;
}
int Book::search4(Time t)
{
mit4=m4.find(t);
if(mit4!=m4.end())
{
return mit4->second;
}
else return -1;
}
void Book::dispAllRecord()
{
for(it3=v3.begin();it3!=v3.end();it3++)
cout<<*it3;
}
void Book::setJs(int n,Time t)
{
int i;
i=search3(n);
if(i!=-1)
{
v3[i].setBz(0);
v3[i].setE(t);
}
}
istream&operator>>(istream &in,Book &b)
{
in>>b.sh;
if(b.sh==-1) return in;
in>>b.name>>b.writer>>b.publish>>b.jc>>b.sy>>b.sum;
in>>b.num;
return in;
}
ostream&operator<<(ostream &out,Book &b)
{ out<<b.sh<<" "<<b.name<<" "<<b.writer<<" "<<b.publish<<" "<<b.jc<<" "<<b.sy<<" "<<b.sum<<endl;
out<<b.num<<endl;
if(b.getNum()!=0)
{
for(b.it3=b.v3.begin();b.it3!=b.v3.end();b.it3++)
out<<*b.it3;
}
return out;
}
class Client
{
vector<Book>v5;
vector<Book>::iterator it5;
multimap<int,int>m5;
multimap<int,int>::iterator mit5;
User u;
public:
Client(int id)
{
load1(id);
load2();
}
~Client()
{
save1();
save2();
}
void load1(int n);
void load2();
void save1();
void save2();
int search5(int s);
void borrow();//借书
void back();//还书
void display1()
{
cout<<u;
}
void display2()
{
for(it5=v5.begin();it5!=v5.end();it5++)
{
cout<<*it5;
}
}
};
void Client::load1(int n)
{
User u;
ifstream infile("20171814Stu.txt",ios::in);
if(!infile)
return ;
while(infile>>u)
{
if(u.getNum()!=0)
{
for(int i=0;i<u.getNum();i++)
{
Record r;
infile>>r;
u.add(r);
}
}
if(u.getId()==n)
{
this->u=u;
}
u.clear1();
}
infile.close();
}
void Client::load2()
{
Book b;
int i;
ifstream infile("20171814Book.txt",ios::in);
if(!infile)
return;
v5.clear();
m5.clear();
i=0;
while(infile>>b)
{
if(b.getNum()!=0)
{
for(int j=0;j<b.getNum();j++)
{
Record r;
infile>>r;
b.add(r);
}
}
v5.push_back(b);
m5.insert(make_pair(b.getSh(),i));
i++;
b.clear2();
}
infile.close();
}
void Client::save1()
{
User u1;
vector<User>v;
vector<User>::iterator it;
ifstream infile("20171814Stu.txt",ios::in);
if(!infile)
return;
while(infile>>u1)
{
if(u1.getNum()!=0)
{
for(int i=0;i<u1.getNum();i++)
{
Record r;
infile>>r;
u1.add(r);
}
}
if(u1==this->u)
{
u1=this->u;
}
v.push_back(u1);
u1.clear1();
}
infile.close();
ofstream outfile("20171814Stu.txt",ios::out);
if(!outfile)
return ;
for(it=v.begin();it!=v.end();it++)
{
outfile<<*it;
}
outfile.close();
}
void Client::save2()
{
ofstream outfile("20171814Book.txt",ios::out);
if(!outfile)
return ;
for(it5=v5.begin();it5!=v5.end();it5++)
{
outfile<<*it5;
}
outfile.close();
}
int Client::search5(int s)
{
mit5=m5.find(s);
if(mit5!=m5.end())
{
return mit5->second;
}
else
return -1;
}
void Client::borrow()
{
if(u.getWj()!=1)
{
int s;//书号
Time t1,t2;
cin>>s>>t1;
if(t1.getMonth()+2>12)
{
t2.setYear(t1.getYear()+1);
t2.setMonth(t1.getMonth()-10);
t2.setDay(t1.getDay());
}
else
{
t2.setYear(t1.getYear());
t2.setMonth(t1.getMonth()+2);
t2.setDay(t1.getDay());
}
int a=search5(s);
if(u.getNow()<u.getMax())
{
v5[a].setJc(v5[a].getJc()+1);
v5[a].setSy(v5[a].getSy()-1);
v5[a].setNum(v5[a].getNum()+1);
u.setNow(u.getNow()+1);
u.setNum(u.getNum()+1);
Record r(u.getId(),u.getName(),s,t1,t2);
v5[a].add(r);
u.add(r);
}
}
else
cout<<"该生有违纪现象不能借书";
}
void Client::back()
{
int s;
Time e;
cin>>s>>e;
int a=search5(s);
v5[a].setJc(v5[a].getJc()-1);
v5[a].setSy(v5[a].getSy()+1);
v5[a].setJs(u.getId(),e);
u.setNow(u.getNow()-1);
u.setJs(s,e);
}
class Administrator
{
vector<User>v6;
vector<User>::iterator it6;
multimap<int,int>m6;
multimap<int,int>::iterator mit6;
vector<Book>v7;
vector<Book>::iterator it7;
multimap<int,int>m7;
multimap<int,int>::iterator mit7;
public:
Administrator()
{
load3();
load4();
}
~Administrator()
{
save3();
save4();
}
void load3();
void load4();
void save3();
void save4();
int search6(int n);
int search7(int s);
void addUser();
void deleteUser();
void addBook();
void deleteBook();
};
void Administrator::load3()
{ User u;
int i;
ifstream infile("20171814Stu.txt",ios::in);
if(!infile)
return;
v6.clear();
m6.clear();
i=0;
while(infile>>u)
{
if(u.getNum()!=0)
{
for(int i=0;i<u.getNum();i++)
{
Record r;
infile>>r;
u.add(r);
}
}
v6.push_back(u);
m6.insert(make_pair(u.getId(),i));
i++;
u.clear1();
}
infile.close();
}
void Administrator::load4()
{
Book b;
int i;
ifstream infile("20171814Book.txt",ios::in);
if(!infile)
return;
v7.clear();
m7.clear();
i=0;
while(infile>>b)
{
if(b.getNum()!=0)
{
for(int j=0;j<b.getNum();j++)
{
Record r;
infile>>r;
b.add(r);
}
}
v7.push_back(b);
m7.insert(make_pair(b.getSh(),i));
i++;
b.clear2();
}
infile.close();
}
void Administrator::save3()
{
ofstream outfile("20171814Stu.txt",ios::out);
if(!outfile)
return ;
for(it6=v6.begin();it6!=v6.end();it6++)
{
outfile<<*it6;
}
outfile.close();
}
void Administrator::save4()
{
ofstream outfile("20171814Book.txt",ios::out);
if(!outfile)
return ;
for(it7=v7.begin();it7!=v7.end();it7++)
{
outfile<<*it7;
}
outfile.close();
}
int Administrator::search6(int n)
{
mit6=m6.find(n);
if(mit6!=m6.end())
{
return mit6->second;
}
else
return -1;
}
int Administrator::search7(int s)
{
mit7=m7.find(s);
if(mit7!=m7.end())
{
return mit7->second;
}
else
return -1;
}
void Administrator::addUser()
{
User u;
int i;
while(1)
{
cin>>u;
if(u.getId()==-1) break;
if(u.getNum()!=0)
{
for(int i=0;i<u.getNum();i++)
{
Record r;
cin>>r;
u.add(r);
}
}
v6.push_back(u);
i=v6.size();
m6.insert(make_pair(u.getId(),i-1));
u.clear1();
}
}
void Administrator::deleteUser()
{
int n;
cin>>n;
int i;
i=search6(n);
//cout<<i<<endl;
if(i!=-1)
{
it6=v6.begin()+i;
m6.erase(m6.find(n));
v6.erase(it6);
}
}
void Administrator::addBook()
{
Book b;
int i;
while(1)
{
cin>>b;
if(b.getSh()==-1) break;
if(b.getNum()!=0)
{
for(int i=0;i<b.getNum();i++)
{
Record r;
cin>>r;
b.add(r);
}
}
v7.push_back(b);
i=v7.size();
m7.insert(make_pair(b.getSh(),i-1));
b.clear2();
}
}
void Administrator::deleteBook()
{
int s;
cin>>s;
int i;
i=search7(s);
//cout<<i<<endl;
if(i!=-1)
{
it7=v7.begin()+i;
m7.erase(m7.find(s));
v7.erase(it7);
}
}
int main()
{
/*
Administrator A;
A.addUser();
//A.deleteUser();
A.addBook();
//A.deleteBook();
*/
int id;
cin>>id;
Client c(id);
//c.display1();
//c.display2();
//c.borrow();
c.back();
return 0;
}
总结
此图书管理系统可以简单实现书籍的添加删除,学生信息的添加删除,以及学生借书、还书等功能。(后续如果有时间会增加续借,多种方式查找信息的功能)。目前此系统运用了“类与对象”“运算符重载”“STL”等知识,后续还会加入“继承”“多态”(记录类可作为基类,由图书和用户来继承,应该可以缩短不少代码;然后用户登录与管理员使用多态来处理)。