⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 图书管.cpp

📁 本软件利用VC++树结构
💻 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 + -