?? 圖書管.cpp
字號:
//圖書館借書和還書
#include <iostream>
#include <fstream>
#include <string>
#include <utility> // for pair class
#include "d_stree.h" // stree class
#include "d_book.h" // video class
#include "d_util.h" // for writeSTree()
using namespace std;
//書庫
void setupInventory(stree<book>& inventory);
//根據ISBN還書
void returnTransaction(stree<book>& inventory,
stree<book>& rentals, const string& ISBN);
//根據ISBN借書
void rentalTransaction(stree<book>& inventory,
stree<book>& rentals, const string& ISBN);
//根據輸入的書名檢索
void search(stree<book>& inventory, const string& name);
//書本入庫(寫到樹中,同時修改books.dat的數據)
void store(stree<book>& inventory);
//**************************main**********************************
int main()
{
stree<book> inventory, rentals;
stree<book>::iterator bookter;
char transactionType;
string ISBN;
string name;
char yesorno=true;
setupInventory(inventory);
cout<<left<<setw(15)<<"ISBN"
<<left<<setw(15)<<"書名"
<<left<<setw(15)<<"作者"
<<left<<setw(15)<<"出版日期"
<<left<<setw(15)<<"庫存量"<<endl;
writeSTree(inventory, "\n");
cout << endl;
do
{
cout << "Transactions: Enter type (A:借書,B: 還書,C:檢索 ,D:存儲)" << endl;
cout << "followed by ISBN or space if done" << endl << endl;
cout << "Transaction: ";
cin>>transactionType;
switch(transactionType)
{
case 'A':
cout<<"請輸入ISBN號: ";
cin>>ISBN;
rentalTransaction(inventory, rentals, ISBN);break;
case 'B':
cout<<"請輸入ISBN號: ";
cin>>ISBN;
returnTransaction(inventory, rentals, ISBN);break;
case 'C':
cout<<"請輸入您所查詢的書名: ";
cin>>name;
search(inventory,name);break;
case 'D':
store(inventory);break;
}
/*cout<<"圖書管的收藏情況:"<<endl;
cout<<left<<setw(15)<<"ISBN"
<<left<<setw(15)<<"書名"
<<left<<setw(15)<<"作者"
<<left<<setw(15)<<"出版日期"
<<left<<setw(15)<<"庫存量"<<endl;
writeSTree(inventory, "\n");*/
cout<<"**************************************************************"<<endl;
cout<<"是否繼續:是(Y),否(N)"<<endl;
cin>>yesorno;
}while(yesorno!='N');
cout << endl;
return 0;
}
//*************************建立書庫************************************
void setupInventory(stree<book>& inventory)
{
ifstream bookFile; // input stream
string author;
string name;
string ISBN;
string pudate;
string fenge=" ";
// use with stree insert()
pair<stree<book>::iterator, bool> p;
// open the file "films.dat"
bookFile.open("books.dat");
if (!bookFile)
{
cerr << "File 'books.dat' not found!" << endl;
exit(1);
}//end for if
// read lines until EOF; insert names in inventory list
while(true)
{
getline(bookFile,author,'\n');
getline(bookFile,name,'\n');
getline(bookFile,ISBN,'\n');
getline(bookFile,pudate,'\n');
if (!bookFile)
break;
p = inventory.insert(book(author,name,ISBN,pudate));
if (p.second == false)//如果有相同了,就加1
(*(p.first)).updateCopies(1);
}//end for while
}
//*****************************************還書操縱***********************************
void returnTransaction(stree<book>& inventory,stree<book>& rentals, const string& ISBN)
{
stree<book>::iterator bookIter,con;
string author;
string name;
string pudate;
for(con=rentals.begin();con!=rentals.end();con++)
{
if((*con).getISBN()==ISBN)
{
//如果借出去的樹中只有一本書時,直接將它刪除,否則減1
if ((*con).getCopies() == 1)
{
rentals.erase(con);
}//end for if
else
(*con).updateCopies(-1);
break;
}//end for if
}//end for for
stree<book>::iterator coniter;
for(coniter=inventory.begin();coniter!=inventory.end();coniter++)
if((*coniter).getISBN()==ISBN)
(*coniter).updateCopies(1);
cout<<"還書成功!"<<endl;
}
//*********************************借書操縱********************************
void rentalTransaction(stree<book>& inventory, stree<book>& rentals, const string& ISBN)
{
stree<book>::iterator bookIter,con;
pair<stree<book>::iterator,bool> pObj;
string name;
string author;
string pudate;
string findBookName;
for(bookIter=inventory.begin();bookIter!=inventory.end();bookIter++)
{
if((*bookIter).getISBN()==ISBN)
{
findBookName=(*bookIter).getName();
if((*bookIter).getCopies() == 0)
cout <<(*bookIter).getName()<< "都已經借完"<< endl;
else
{ (*bookIter).updateCopies(-1);
author=(*bookIter).getAuthor();
name=(*bookIter).getName();
pudate=(*bookIter).getPudate();
pObj =rentals.insert(book(author,name,ISBN,pudate));
if (pObj.second == false)
(*(pObj.first)).updateCopies(1);
cout<<"借書成功!"
<<"\n書名:"<<name
<<" ISBN:"<<ISBN
<<" 作者:"<<author<<endl;
}//end for else
break;
}//end for if
}//end for for
if(bookIter==inventory.end())
cout<<"對不起!您找的"<<findBookName<<"本書庫還沒有"<<endl;
}
//******************************search*********************************
void search(stree<book>& inventory, const string& name)
{
stree<book>::iterator con;
int copies;
bool isexit=false;
for(con=inventory.begin();con!=inventory.end();con++)
{
if(name==(*con).getName())
{
isexit=true;
break;
}//end for if
}
if(isexit)
{
copies=(*con).getCopies();
cout<<"您所查詢的書"<<name<<"目前還存:"<<copies<<endl;
}
else
cout<<"對不起!目前圖書管沒有你所找的書"<<endl;
}
//**********************************store*****************************
void store(stree<book>& inventory)
{
string ISBN ;
string name;
string author;
string pudate;
int copies;
stree<book>::iterator con;
bool isexit=false;
pair<stree<book>::iterator,bool> pObj;
cout<<"請輸入ISBN:";
cin>>ISBN;
for(con=inventory.begin();con!=inventory.end();con++)
{
//如果要輸入的書已存在于圖書館,則只需要輸入數量即可
if(ISBN==(*con).getISBN())
{
isexit=true;
break;
}
}
if(isexit)
{
cout<<"請輸入該書的數量:";
cin>>copies;
name=(*con).getName();
author=(*con).getAuthor();
pudate=(*con).getPudate();
for(int i=1;i<=copies;i++)
{
pObj =inventory.insert(book(author,name,ISBN,pudate));
if (pObj.second == false)
(*(pObj.first)).updateCopies(1);
}
}
else
{
//如果要存入的書目前圖書館還未收藏,則需要輸入全部的信息
cout<<"該書為新書,請輸入該書的全部信息:"<<endl;
cout<<"書名:";
cin>>name;
cout<<"作者:";
cin>>author;
cout<<"出版日期";
cin>>pudate;
cout<<"數量:";
cin>>copies;
for(int i=1;i<=copies;i++)
{
pObj =inventory.insert(book(author,name,ISBN,pudate));
if (pObj.second == false)
(*(pObj.first)).updateCopies(1);
}
//inventory.insert(book(author,name,ISBN,pudate,copies));
}
//插入到樹的同時把書的信息寫入到books.dat的文件中
fstream file1;
file1.open("books.dat",ios::out|ios::app);
if(!file1)
{
cout<<"文件不存在!"<<endl;
}else
{
for(int i=1;i<=copies;i++)
{
file1<<author<<"\n";
file1<<name<<"\n";
file1<<ISBN<<"\n";
file1<<pudate<<"\n";
}
}
file1.close();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -