圖書館簡易系統心得總結
最近幾周在製作圖書館簡易系統時,實現了最基本的幾個功能,包括增加學生和圖書的操作、借書和還書的功能。編寫程式碼時必然會遇到諸多的問題。而反思和問題的解決比草率地寫程式碼更重要。以下藉助程式碼來總結一下心得。
時間類及其過載輸出和輸入運載符。
class Time
{int year;
int month;
int day;
int hour;
int minute;
public:
Time(int x,int y,int z,int v,int f):year(x),month(y),day(z),hour(v),minute(f){}
Time(){}
int getYear(){return year;};
int getMonth(){return month;};
int getDay(){return day;};
int getHour(){return hour;};
int getMinute(){return minute;}
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?hour<d.hour:minute<d.minute;
}
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<<" "<<d.minute;
return os;
}
istream&operator>>(istream &in,Time &d)
{
in>>d.year;
in>>d.month;
in>>d.day;
in>>d.hour;
in>>d.minute;
return in;
}
資料類及其過載輸入和輸出運算子
class Record
{
string name;
int no;
int id;
int major;
int classes;
int nowBorrows;
int breaks;
string type;
Time start;
Time end;
bool either;
Time changeEnd;
public:
Record(int no1,int id1,string type1,Time start1,Time end1):no(no1),id(id1),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 no;};
int getId(){return id;};
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.no;
if(r.no==0)return in;
in>>r.id;
in>>r.type;
in>>r.start;
in>>r.end;
r.changeEnd=r.end;
return in;
}
ostream&operator<<(ostream&os,const Record&r)
{
os<<r.no<<" ";
os<<r.id<<" ";
os<<r.type<<" ";
os<<r.start<<" ";
os<<r.changeEnd<<" "<<endl;
return os;
}本類中包含書籍和學生的部分資訊
書籍類及其過載輸入輸出運算子
class Book
{
string name;
string writer;
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.writer>>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.writer<<" "<<b.press<<" "<<b.number<<" "<<b.num<<endl;
for(int i=0;i<b.num;++i)
{
os<<b.r[i];
}
return os;
}
本類中提供書的資訊和提供這本書一些人借閱記錄查閱以及為下面操作做鋪墊。
使用者學生類
class User
{
string name;
string major;
string classes;
int id;
int num;
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();}
int getNowBorrows(){return nowBorrows;};
int getNowReborrow(){return canReborrow;};
int getBreaks(){return breaks;};
int getId(){return id;}
string getName(){return name;}
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);
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;
name=u.name;
major=u.major;
classes=u.classes;
nowBorrows=u.nowBorrows;
breaks=u.breaks;
num=u.num;
r=u.getRecord();
}
void User::show()
{
for(it=r.begin();it!=r.end();it++)
{
cout<<*it;
}
}
istream&operator>>(istream&in,User &u)
{
in>>u.id;
if(u.id==0)return in;
in>>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.name<<" "<<u.major<<" "<<u.classes<<" "<<u.nowBorrows<<" "<<u.breaks<<" "<<u.num<<endl;
for(int i=0;i<u.num;++i)
{
os<<u.r[i];
}
}
在本類,提供大多數資料,包含一部分小函式。
操作類
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();
b.clear();
b1.clear();
}
void load1();
void load2(int id);
void load1p();
void load2p();
void borrowBook();
void returnBook();
};
void Operate::borrowBook()
{
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(),"jieshu",t1,t2);
u.addRecord(r);
b[b1p->second].addRecord(r);
u.addNowBorrowsJ(1);
b[b1p->second].addBorrowsJ(1);
}
void Operate::returnBook()
{
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(),"huanshu",t1,t2);
u.addRecord(r);
b[b1p->second].addRecord(r);
u.addNowBorrowsH(1);
b[b1p->second].addBorrowsH(1);
}
void Operate::load1p()
{
ofstream outfileB("e:\\20171769book.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:\\20171769stu.txt",ios::in);
if(!infile)
return ;
while(infile>>u1)
{
if(u1==this->u)
{
u1=this->u;
}
v1.push_back(u1);
u1.de1();
u1.de2();
}
infile.close();
ofstream outfile("e:\\20171769stu.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:\\20171769book.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)
{
User uu;
int i;
ifstream infile("e:\\20171769stu.txt",ios::in);
if(!infile)
return;
while(infile>>uu)
{
if(uu.getId()==id)
{
u=uu;
break;
}
else
{
uu.de1();
uu.de2();
}
}
infile.close();
}
本類中,實現借書和還書的功能。
void load1()的目的是把檔案中的資料讀到vector裡。 void load1p()是解構函式。 infile.close()先把新的vector構建出來,在檔案中替換這個學生的資訊,再把這組資料寫到檔案中。
管理員類
class Adm
{
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();
void load2();
void load1p();
void load2p();
void addUser();
void addBook();
};
void Adm::load1p()
{
ofstream outfileB("e:\\20171769book.txt",ios::out);
if(!outfileB)
return;
for(it1=b.begin();it1!=b.end();it1++)
{
outfileB<<*it1;
}
outfileB.close();
}
void Adm::load2p()
{
ofstream outfileS("e:\\20171769stu.txt",ios::out);
if(!outfileS)
return;
for(it=u.begin();it!=u.end();it++)
{
outfileS<<*it;
}
outfileS.close();
}
void Adm::addUser()
{
User uu;
while(cin>>uu)
{
if(uu.getId()==0)break;
u.push_back(uu);
}
};
void Adm::addBook()
{
Book bb;
while(cin>>bb)
{
if(bb.getName()=="E")break;
b.push_back(bb);
}
}
void Adm::load1()
{
int i;
Book bb;
ifstream infile("e:\\20171769book.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:\\20171769stu.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.borrowBook();
//o.returnBook();
//Adm a;
//a.addUser();
//a.addBook();
return 0;
}
這個簡易系統使用STL,使程式變得簡單。若再使用繼承相關,可以簡化程式。由於書籍和學生類都包含資料,所以可以在此基礎上,讓書籍和學生類繼承資料類。
由於之前未使用過STL,在編寫程式碼時會遇到很多錯誤,此時我會反覆查閱資料、詢問同學,直到可以調通程式,過程雖然可能很艱難,但成功後很有成就感,從而給我更多繼續寫程式的動力。知識是死的,如何運用才是活的,只記住知識是沒用的,因此在今後學習任何程式語言以及程式設計的路上必定是學習知識和實踐相結合,才能更好地學會程式設計。