C++ 实现小型图书管理系统

因为课程设计的原因,需要实现一个小型图书管理系统

包含功能:


问题描述:
设计一个系统,对图书信息进行管理,信息描述:有关该系统基本信息的描述,如:图书名称、图书编号、单价、作者、存在状态、借书人姓名、性别、学号等。
基本要求:
基本功能:
1、新进图书基本信息的输入。
2、图书基本信息的查询。
3、对撤消图书信息的删除。
4、为借书人办理注册。
5、办理借书手续(非注册会员不能借书)。
6、办理还书手续。
7、统计图书库存、已借出图书数量。


需要创建三个文本文件:record.txt  book.txt reader.txt

operating.h的头文件:

#include <iostream>
#include <fstream>
#include <string>
#include <time.h> 
#include<sstream>
#include<vector>
#include <iomanip>


using namespace std;


int all_stock = 0;
int out_stock = 0;
int times=0;
void outData(vector<string> res,int n)  // n为txt中 每行数据个数
{
	for(int i=0;i<res.size();i+=n){
		for(int j=0;j<n;j++)
			cout<<setw(12)<<res[i+j]<<" ";
		
		cout<<endl;
    }
}








void BookEntry()
{
	double price;
	string bookname,writer;
	fstream out;


	out.open("book.txt",ios::app);
	if(!out)
	{
		cerr<<"打开文件失败!"<<endl;
	}




	time_t tt = time(NULL);//这句返回的只是一个时间cuo
	
	cout<<"请输入书籍名称"<<endl;
	cin>>bookname;
	cout<<"请输入书籍作者"<<endl;
	cin>>writer;
	cout<<"请输入书籍价格"<<endl;


	while(! (cin>>price)  || price <= 0 )
	{
		cin.clear();
		cin.ignore(100,'\n');
		cout<<"请输入正确的价格"<<endl;
	}
	
	out<<tt<<" "<<bookname<<" "<<writer<<" "<<price<<" "<<"0"<<"\n";
	
	out.close();
	
	


}


void BookMes()
{
	fstream in;
	string line;
	//用于存放分割后的字符串 
    vector<string> res;
	string temp; //暂存字符串
    


	in.open("book.txt",ios::in);
	if(!in)
	{
		cerr<<"打开文件失败!"<<endl;
	}
	all_stock = 0;
	while(getline(in,line))
	{
		all_stock++;
		//cout<<line<<endl;
		//将字符串读到input中 
		stringstream input(line); //将line切割 通过input存入temp,然后存入res中
		while(input>>temp)
		{
			
			res.push_back(temp);
		}
	}
	// 0 书籍编号 1 书籍名称 2作者 3价格 4书籍状态
    //输出res 
	cout<<endl<<setw(12)<<"书籍编号"<<" "<<setw(12)<<"书籍名称"<<" "<<setw(12)<<"作者"<<" "<<setw(12)<<"价格"<<" "<<setw(12)<<"在馆0,不在1"<<"\n";
    outData(res,5);
	in.close();
}


void DelBook()
{
	string del_book;
	string line;
	vector<string>res;
	string temp;
	bool flag=false;


	fstream in;
	in.open("book.txt",ios::in);
	if(!in)
	{
		cerr<<"打开错误文件"<<endl;
	}


	cout<<"请输入需要删除的图书ID"<<endl;
	cin>>del_book;
	
	
	
	while(getline(in,line))
	{
		//cout<<line<<endl;
		//将字符串读到input中 
		stringstream input(line); //将line切割 通过input存入temp,然后存入res中
		times=0;
		while(input>>temp)
		{
			if(del_book == temp && times==0)
			{
				
				for(int i=0;i<3;i++)  //因为一共五个 第一个temp已经是del_book 所以这里取得是四个
				{
					input>>temp;
				}
				input>>temp;
				if(temp != "0")
				{
					cout<<"书籍状态不对";
					in.close();
					return ;
				}
				flag=true;
				cout<<"\n找到了喔,应该删除成功了\n";
				continue;
			}
			res.push_back(temp);
			times++;
			
		}
	}
	
	//outData(res,5);
	in.close();
	
	if(!flag)
	{
		cout<<"\n错误的书籍ID\n";
		return ;
	}
	fstream out;


	out.open("book.txt",ios::out);
	if(!out)
	{
		cerr<<"打开文件失败!"<<endl;
	}
	
	for(int j=0;j<res.size();j+=5)
	{
		line = res[j] + " " + res[j+1] + " " + res[j+2] + " " + res[j+3] + " " + res[j+4] + "\n";
		out<<line;
	}
	out.close();
	
}








void ReaderEntry()
{
	
	string readername,sex_str;
	int sex;
	fstream out;


	out.open("reader.txt",ios::app);
	if(!out)
	{
		cerr<<"打开文件失败!"<<endl;
	}




	time_t readerid = time(NULL);//这句返回的只是一个时间cuo
	
	cout<<"请输入读者姓名"<<endl;
	cin>>readername;
	
	do
	{
		cout<<"请输入读者性别:0为女,1为男"<<endl;
		while(! (cin>>sex) )
		{
			cin.clear();
			cin.ignore(100,'\n');
			cout<<"请输入正确的0或1"<<endl;
		}
	}while(sex != 0 && sex!=1);


	if(sex == 1)
	{
		sex_str = "男";
	}else if (sex == 0){
		sex_str = "女";
	}else{
		out.close();
		return ;
	}


	
	
	out<<readerid<<" "<<readername<<" "<<sex_str<<"\n";
	
	out.close();
	
	


}
/*读者信息*/
void ReaderMes()
{
	fstream in;
	string line;
	//用于存放分割后的字符串 
    vector<string> res;
	string temp; //暂存字符串
    


	in.open("reader.txt",ios::in);
	if(!in)
	{
		cerr<<"打开文件失败!"<<endl;
	}
	
	while(getline(in,line))
	{
		//cout<<line<<endl;
		//将字符串读到input中 
		stringstream input(line); //将line切割 通过input存入temp,然后存入res中
		while(input>>temp)
        res.push_back(temp);
	}
	// 0读者学号 1读者姓名 2读者性别
    //输出res 
	cout<<endl<<setw(12)<<"读者编号"<<" "<<setw(12)<<"读者"<<" "<<setw(12)<<"性别"<<"\n";
    outData(res,3);
	in.close();
}




/* 借阅书籍 */
void BorrowBook()
{
	


	string book[5];
	string readerid;
	string readername;
	string line;
	vector<string>res; //取书籍状况,并且更新


	string temp;
	bool flag_book = false; //用于判断书籍是否存在  读者是否存在
	bool flag_reader = false;


	/* 取book的图书情况,并判断是否在馆*/
	fstream in;
	in.open("book.txt",ios::in);
	if(!in)
	{
		cerr<<"打开错误文件"<<endl;
	}


	cout<<"请输入需要借的图书ID"<<endl;
	cin>>book[0];
	
	
	
	while(getline(in,line))
	{
		//cout<<line<<endl;
		//将字符串读到input中 
		stringstream input(line); //将line切割 通过input存入temp,然后存入res中
		times=0;
		while(input>>temp)
		{
			if(book[0] == temp && times ==0)
			{
				res.push_back(temp);
				for(int i=0;i<3;i++)  //从书籍名称开始取,一直取到价钱
				{
					input>>temp; //读取了书籍编号,要及时写入res,以后要写进文本
					book[1+i]=temp;
					res.push_back(temp);
				}
				input>>temp;  //取书籍状态,如果0在馆 如果1不在馆
				if(temp == "0")
				{
					book[4]="1";
					temp="1";
					res.push_back(temp);
					flag_book=true;
				}else{
					cout<<"\n书籍不在馆\n";
					in.close();
					return ;
				}
				continue;  //继续取
			}
			res.push_back(temp);
			times++;
			
		}
	}


	
	in.close();
	if(!flag_book)
	{
		cout<<"错误的书籍ID"<<endl;
		return ;
	}
	
	in.open("reader.txt",ios::in);
	if(!in)
	{
		cerr<<"打开错误文件"<<endl;
	}
	cout<<"\n请输入读者ID\n";
	cin>>readerid;


	while(getline(in,line))
	{
		//cout<<line<<endl;
		//将字符串读到input中 
		stringstream input(line); //将line切割 通过input存入temp,然后存入res中
		times=0;
		while(input>>temp)
		{
			if(readerid == temp && times==0)
			{
				input>>temp;
				readername=temp;
				flag_reader=true;
				break;


			}
			times++;
			
		}
	}
	if(!flag_reader)
	{
		cout<<"错误的读者ID"<<endl;
		in.close();
		return ;
	}


	in.close();
	
	
	fstream out;
	out.open("record.txt",ios::app);
	if(!out)
	{
		cerr<<"打开错误文件"<<endl;
	}
	line = book[0] + " " + book[1] + " " + readername + '\n';
	out<<line;
	cout<<"\n办理借书成功\n";
	out.close();






	


	out.open("book.txt",ios::out);
	if(!out)
	{
		cerr<<"打开文件失败!"<<endl;
	}
	
	for(int j=0;j<res.size();j+=5)
	{
		line = res[j] + " " + res[j+1] + " " + res[j+2] + " " + res[j+3] + " " + res[j+4] + "\n";
		out<<line;
	}
	out.close();
}


void BorrowMes()
{
	fstream in;
	string line;
	//用于存放分割后的字符串 
    vector<string> res;
	string temp; //暂存字符串
    


	in.open("record.txt",ios::in);
	if(!in)
	{
		cerr<<"打开文件失败!"<<endl;
	}
	out_stock=0;
	while(getline(in,line))
	{
		out_stock++;
		//cout<<line<<endl;
		//将字符串读到input中 
		stringstream input(line); //将line切割 通过input存入temp,然后存入res中
		while(input>>temp)
        res.push_back(temp);
	}
	// 0书籍编号 1书籍名称 2读者姓名
    //输出res 
	cout<<endl<<setw(12)<<"书籍编号"<<" "<<setw(12)<<"书籍名称"<<" "<<setw(12)<<"读者"<<"\n";
    outData(res,3);
	
	in.close();
}


void RtnBook()
{
	string rtn_book;
	string line;
	vector<string>res;
	string temp;
	bool flag=false;


	fstream in;
	in.open("record.txt",ios::in);  //先打开record 查看是否有借这本书
	if(!in)
	{
		cerr<<"打开错误文件"<<endl;
	}


	cout<<"请输入需要归还的书籍ID"<<endl;
	cin>> rtn_book;
	
	
	
	while(getline(in,line))
	{
		//cout<<line<<endl;
		//将字符串读到input中 
		stringstream input(line); //将line切割 通过input存入temp,然后存入res中
		times=0;
		while(input>>temp)
		{
			if(rtn_book == temp && times==0) //如果有的话
			{
				flag=true;
				
				for(int i=0;i<2;i++)  //因为一共三个 第一个temp已经是del_book 所以这里取得是两个
				{
					input>>temp;// 将删除的东西不输出到向量中
				}
				continue;
			}
			res.push_back(temp);
			times++;
			
		}
	}
	
	//outData(res,3);
	in.close();
	if(!flag)
	{
		cout<<"该图书不存在或者没有被外借"<<endl;
		return ;
	}




	fstream out;


	out.open("record.txt",ios::out); //record已经删除成功
	if(!out)
	{
		cerr<<"打开文件失败!"<<endl;
	}
	
	for(int j=0;j<res.size();j+=3)
	{
		line = res[j] + " " + res[j+1] + " " + res[j+2]  + "\n";
		out<<line;
	}
	out.close();




	vector<string>res_book;


	
	in.open("book.txt",ios::in); //开始取 被修改的书籍
	if(!in)
	{
		cerr<<"打开错误文件"<<endl;
	}	
	
	while(getline(in,line))
	{
		//cout<<line<<endl;
		//将字符串读到input中 
		stringstream input(line); //将line切割 通过input存入temp,然后存入res中
		times=0;
		while(input>>temp)
		{
			if(rtn_book == temp && times==0)
			{
				res_book.push_back(temp);
				for(int i=0;i<3;i++)  //因为一共五个 第一个temp已经是rtn_book 所以这里取得是四个
				{
					input>>temp;
					res_book.push_back(temp);
				}
				input>>temp;//最后一个取得是书籍状态,需要修改书籍状态
				temp = "0";
				res_book.push_back(temp);
				continue;
			}
			res_book.push_back(temp);
			times++;
		}
	}
	
	//outData(res,5);
	in.close();
	
	out.open("book.txt",ios::out); //再存入文本中;
	if(!out)
	{
		cerr<<"打开文件失败!"<<endl;
	}
	
	for(int j=0;j<res_book.size();j+=5)
	{
		line = res_book[j] + " " + res_book[j+1] + " " + res_book[j+2] + " " + res_book[j+3] + " " + res_book[j+4] + "\n";
		out<<line;
	}
	out.close();


	cout<<"\n找到了喔,应该还书成功了\n";
}


void CountBook()
{	
	cout<<"\n图书馆书籍情况";
	BookMes();
	cout<<"图书馆一共有:"<<all_stock<<" 本书\n\n\n";
	cout<<"\n图书馆书籍外借情况";
	BorrowMes();
	cout<<"图书馆目前外借:"<<out_stock<<" 本书\n\n";
	cout<<"\n\n图书馆当前在馆书籍还有:"<<all_stock - out_stock<<" 本书\n";
}



main.cpp的主函数

#include "operating.h"


int main()
{
	int order;
	do
	{
		order = -1;
		cout<<"\n";
		cout<<"----------------------------------------------------------\n";
		cout<<"| 1. 图书信息录入    2. 图书信息查询    3. 图书信息删除  |\n";
		cout<<"| 4. 读者办理注册    5. 读者信息查询    6. 办理借书手续  |\n";
		cout<<"| 7. 办理还书手续    8  已借出图书      9.统计图书库存  |\n";
		cout<<"|                                    	按 \"0\"退出    |\n";
		cout<<"----------------------------------------------------------\n";
		cout<<"  请输入相应序号进行相应操作:";
		cin>>order;
		cin.clear();//清除缓冲区中后面的字符
		cin.ignore(100,'\n');

		switch(order)
		{
		case 1:
			BookEntry();
			break;
		case 2:
			BookMes();
			break;
		case 3:
			DelBook();
			break;
		case 4:
			ReaderEntry();
			break;
		case 5:
			ReaderMes();
			break;
		case 6:
			BorrowBook();
			break;
		case 7:
			RtnBook();
			break;
		case 8:
			BorrowMes();
			break;
		case 9:
			CountBook();
			break;
		case 0:
			break;
		default:
			cout<<"错误的命令行"<<endl;
			break;
		}
		
	}while(order != 0);

	system("pause");
	return 0;

	
}

  • 31
    点赞
  • 273
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值