歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++實現一個簡單圖書借閱流程

C++實現一個簡單圖書借閱流程

日期:2017/3/1 9:27:30   编辑:Linux編程

總共實現了myDate類,book類,student類,圖書借閱記錄record類

//
#include <iostream>
#include <string>
#include <vector>
#include <list>

#include <ctime>
#include <cstdio>
using namespace std;

//主要就基於C庫封裝了一個獲取時間戳的數據成員和相關方法
class myDate
{
time_t _time;
public:
myDate()
{
time (&this->_time);
cout<<"get time sucessful"<<endl;
}
friend ostream& operator<<(ostream &out,myDate &d);
};
ostream& operator <<(ostream &out,myDate &d)
{
out<<ctime(&(d._time));
return out;
}

//book類主要封裝一個圖書信息(書名和數量)
class book
{
//string bookid;
string bookname;
int bookcount;
public:
explicit book(string bname,int bcount):
bookname(bname),bookcount(bcount)
{

}
int insertBook()
{
if(bookcount < 0)
return -1;
bookcount += 1;
return 0;

}
int removeBook()
{
if(bookcount <= 0)
return -1;
bookcount -= 1;
return 0;
}
string& getname()
{
return bookname;
}
void print()
{
cout<<"bname:"<<bookname<<",count:"<<bookcount<<endl;
}
};

//record類封裝一條借閱記錄<who,do,bookname>
class recordPer
{
bool f ;//true brrow -- false return
string sname;
string bname;
myDate time;
public:
recordPer(string &bn,string &sn,bool &b):
bname(bn),sname(sn),time(),f(b)
{

}
friend ostream& operator <<(ostream &out,recordPer &r);
};
ostream& operator <<(ostream &out,recordPer &r)
{
if(r.f == true)
out<<"at "<<r.time<<" "<<r.sname<<"--brrow->"<<r.bname;
else
out<<"at "<<r.time<<" "<<r.sname<<"--return->"<<r.bname;
return out;
}

//其實這個類封裝的顯得多余,第二版本後面再優化
class bookRecord
{
public:

vector<recordPer > _record;
void print()
{
for(unsigned int i=0;i< _record.size();++i)
{
cout<<_record.at(i)<<endl;
}
}
int insert(recordPer r)
{
_record.push_back(r);
return 0;
}
};

//下面就是整個流程的靈魂,人,借書還書都是人的動作
class student
{
//string stuid;
string stuname;
bookRecord __record;
list<string> needreturn;
public:
student(string &s):stuname(s){}
int borrowBook(string &bookname,vector<book> &v);
int returnBook(string &bookname,vector<book> &v);
string& getname()
{
return stuname;
}
void printAll()
{
this->__record.print();
}
};
int student::borrowBook(string &bookname,vector<book> &v)
{
bool b = true;
for(int i=0;i<v.size();++i)
{
if(v.at(i).getname() == bookname)
{
if(v.at(i).removeBook() != 0)
return -1;
this->__record.insert( recordPer(bookname,this->stuname,b));
this->needreturn.push_back(bookname);
return 0;
}
}
return 1;
}
int student::returnBook(string &bookname,vector<book> &v)
{
bool b = false;
for(int i=0;i<v.size();++i)
{
if(v.at(i).getname() == bookname)
{
this->needreturn.remove(bookname);
if(v.at(i).insertBook() != 0)
return -1;
this->__record.insert( recordPer(bookname,this->stuname,b));
//this->needreturn.remove(bookname);
return 0;
}
}
return 1;
}


int main()
{
vector<book> _book;
vector<student> _stu;
string name;
string id;
int count;


int code;
while(1)
{
cout<<"----------BOOK-------------"<<endl;
cout<<"1 new book"<<endl;
cout<<"2 new user"<<endl;
cout<<"3 borrow book"<<endl;
cout<<"4 return book"<<endl;
cout<<"5 find all record of some student"<<endl;
cout<<"6 find all book"<<endl;
cout<<"----------BOOK-------------"<<endl;

cout<<"input op:";
cin>>code;
//code = getchar();
//code -= 48;
if(code <1 || code >6)
{
cout<<"input error\n";
continue;
}
if(code == 1)
{
cout<<"input book infomation:name(string) count(int)"<<endl;
cin>>name>>count;
_book.push_back(book(name,count));
}
else if(code == 2)
{
cout<<"input student information:name(string)\n";
cin>>name;
_stu.push_back(student(name));
}
else if(code == 3)//brrow
{
cout<<"input student name && book name :";
int flag = 0;
cin>>name>>id;
int i;
for( i=0;i<_stu.size();++i)
{
if(_stu[i].getname() == name)
{
flag = 1;
break;
}
}
if(flag != 1)
cout<<"student "<<name<<"not found\n";
if(0 == _stu.at(i).borrowBook(id,_book))
cout<<"brrowbook sucessful \n";
else
cout<<"brrowbook failed \n";
}
else if(code == 4)//return
{
cout<<"input student name && book name :";
int flag = 0;
cin>>name>>id;
int i;
for( i=0;i<_stu.size();++i)
{
if(_stu[i].getname() == name)
{
flag = 1;
break;
}
}
if(flag != 1)
cout<<"student "<<name<<"not found\n";
if(0 == _stu.at(i).returnBook(id,_book))
cout<<"returnbook sucessful \n";
else
cout<<"returnbook failed \n";
}
else if(code == 5)
{
cout<<"input student name:";
int flag = 0;
cin>>name;
int i;
for( i=0;i<_stu.size();++i)
{
if(_stu[i].getname() == name)
{
_stu.at(i).printAll();
flag = 1;
break;
}
}
if(flag == 0)
cout<<"student "<<name<<"not found"<<endl;
}
else if(code == 6)
{
for(int i=0;i<_book.size();++i)
{
_book.at(i).print();
}
}
}
return 0;
}

不合理的地方在後期需要改進的地方:

取消掉record類,需要加載上bookid和studentid,或者增加繼承的結構

Copyright © Linux教程網 All Rights Reserved