图书借阅管理系统——C++版

文章目录

前言

一、总体结构

二、详细代码

前言

图书借阅管理系统的总体功能要求

图书借阅管理系统的总体目标:在Visual Studio 2022的开发环境下,利用所学C++语言和数据结构的相关知识,编写程序系统主要实现图书馆的增加图书、查询图书、删除图书、借阅图书、还书;增加会员、查询会员、删除会员、借书信息等智能化管理过程。

主要分为两大功能:

  1. 图书管理(增加图书、查询图书、删除图书、图书借阅、还书);
  2. 会员管理(增加会员、查询会员、删除会员、借书信息);

提示:以下是本篇文章正文内容,下面案例可供参考

一、总体结构

主函数、图书管理类、会员管理类

二、详细代码

1.主函数

constexpr auto PASSWORD = "password.txt";
#include<fstream>
#include<iostream>
using namespace std;
#include<string>
#include"BookManage.h"
#include"MemberManage.h"

//菜单界面
void menu() {
	cout << "\t 欢迎使用图书借阅系统" << endl;
	cout << "\t   1、图书管理" << endl;
	cout << "\t   2、会员管理" << endl;
	cout << "\t   3、修改密码" << endl;
	cout << "\t   0、退出系统" << endl;
	cout << endl;
}

//获取密码
string getpassword() {
	ifstream ifs;
	ifs.open(PASSWORD, ios::in);
	string val;
	string password;

	while (ifs>>val)
	{
		password = val;
	}

	ifs.close();
	return password;
}

//保存密码
void savepassword(string mima ) {
	ofstream ofs;
	ofs.open(PASSWORD, ios::out);

	ofs << mima;
	ofs.close();
}
//系统登录
bool password(string mima) {
	
	static string MiMa = getpassword();

	if (MiMa == mima)
	{
		return true;
	}
	else
	{
		cout << "密码输入错误!" << endl;
		system("pause");
		system("cls");
		return false;
	}
}



int main() 
{
	//登陆界面
	do
	{
		string mima;
		cout << "\n\n\n\n\t\t\t\t请输入登录密码:";
		cin >> mima;
		bool temp = password(mima);
		if (temp)
		{
			break;
		}
	} while (true);
	system("cls");

	//功能选择
	menu();

	do
	{
		int choice = 0;
		cout << "请输入功能号:";
		cin >> choice;

		static bool fanhui = false;
		system("pause");
		system("cls");

		switch (choice)
		{
		case 1: {
			BookManage book;
			do
			{
				int option;
				cout << "请输入功能号:";
				cin >> option;

				switch (option)
				{
				case 1:
					book.addbook();
					break;
				case 2:
					book.showbook();
					break;
				case 3:
					book.delete_book();
					break;
				case 4:
					book.modify_condition();
					break;
				case 5:
					book.search_book();
					break;
				case 6:
					fanhui = true;
					break;
				default:
					cout << "请输入正确的功能号!" << endl;
					break;
				}
			} while (!fanhui);
			system("cls");
			menu();
            fanhui = false;
			break;
		}
		case 2: 
		{
			MemberManage member;
			do
			{
				int option;
				cout << "请输入功能号:";
				cin >> option;

				switch (option)
				{
				case 1:
					member.add();
					break;
				case 2:
					member.show();
					break;
				case 3:
					member.delete_mem();
					break;
				case 4:
					member.modify();
					break;
				case 5:
					member.search();
					break;
				case 6:
					fanhui = true;
					break;
				default:
					cout << "请输入正确的功能号!" << endl;
					break;
				}
			} while (!fanhui);
			system("cls");
			menu();
            fanhui = false;
			break;
		}
		case 3:
		{
			do
			{
				string mima;
				cout << "\n\n\n\n\t\t\t\t请输入登录密码:";
				cin >> mima;
				bool temp = password(mima);
				if (temp)
				{
					cout << "请输入新密码:";
					string xin_Mima;
					cin >> xin_Mima;

					static string MiMa = xin_Mima;
					savepassword(xin_Mima);

					cout << "修改成功!新密码:" << MiMa << endl;

					break;
				}
			} while (true);
			menu();
			break;
		}
		case 0:
			system("pause");
			return 0;
		default:
			cout << "请输入正确的功能号!" << endl;
			break;
		}
	} while (true);

	return 0;
}

注释:程序包含密码登录功能。主函数写了获取密码、保存密码、密码核对、修改密码等功能函数。其中密码保存至项目文件夹下的password.txt中。

 

2.图书管理类

constexpr auto FILENAME ="bookfile.txt";
#include"BookManage.h"
#include<fstream>

BookManage::BookManage() {
	
	menu();
	
	//文件不存在
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	if (!ifs.is_open())
	{
		this->bookarray = NULL;
		this->arr_number = 0;
		this->file_empty = true;

		ifs.close();
		return;
	}

	//文件为空
	char c;
	ifs >> c;
	if (ifs.eof())
	{
		this->bookarray = NULL;
		this->arr_number = 0;
		this->file_empty = true;

		ifs.close();
		return;
	}

	//文件不为空
	this->arr_number = this->getbook_num();
	init_array();
	this->file_empty = false;
	
}

int BookManage::getbook_num() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int number;
	string name;
	int price;
	int condition;

	int num = 0;
	while (ifs>>number && ifs>>name && ifs>>price &&ifs>>condition)
	{
		num++;
	}

	ifs.close();
	return num;
}

void BookManage::init_array() {
	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int number;
	string name;
	int price;
	int condition;

	int num = this->getbook_num();

	struct m_book* p = new m_book[num];
	int i = 0;
	while (ifs >> number && ifs >> name && ifs >> price && ifs >> condition)
	{
		p[i].m_number = number;
		p[i].m_name = name;
		p[i].m_price = price;
		p[i].m_condition = condition;

		i++;
	}
	this->bookarray = p;
	ifs.close();
}

BookManage::~BookManage() {
	if (this->bookarray != NULL)
	{
		delete[]this->bookarray;
		this->bookarray = NULL;
	}
}

void BookManage::menu() {
	cout << "\t   1、增添图书信息" << endl;
	cout << "\t   2、显示图书信息" << endl;
	cout << "\t   3、删除图书信息" << endl;
	cout << "\t   4、修改图书状态" << endl;
	cout << "\t   5、查找图书信息" << endl;
	cout << "\t   6、返回上一级界面" << endl;
	cout << endl;
}

void BookManage::addbook() {
	int val;
	cout << "请输入你要录入几本书的信息:";
	cin >> val;

	struct m_book* p;
	p = new m_book[val + arr_number];

	if (this->bookarray != NULL)
	{
		for(int i = 0; i<this->arr_number; ++i)
		{
			p[i].m_number = this->bookarray[i].m_number;
			p[i].m_name = this->bookarray[i].m_name;
			p[i].m_price = this->bookarray[i].m_price;
			p[i].m_condition = this->bookarray[i].m_condition;
		}
	}
	for (int i = this->arr_number ; i < val + this->arr_number; i++)
	{
		int number;
		string name;
		int price;
		bool condition;

		cout << "请输入书的编号:" << endl;
		cin >> number;
		p[i].m_number = number;

		cout << "请输入书的书名:" << endl;
		cin >> name;
		p[i].m_name = name;

		cout << "请输入书的价格:" << endl;
		cin >> price;
		p[i].m_price = price;

		cout << "请输入书的状态:  0、在馆   1、借出" << endl;
		cin >> condition;
		p[i].m_condition = condition;
	}

	delete[]this->bookarray;

	this->arr_number += val;
	this->bookarray = p;

	savefile();
	this->file_empty = false;

	cout << "录入成功!" << endl;

}

void BookManage::showbook() {
	for (int i = 0; i < this->arr_number; i++)
	{
		cout << "编号:" << this->bookarray[i].m_number << "\t";
		cout << "书名:" << this->bookarray[i].m_name << "\t";
		cout << "价格:" << this->bookarray[i].m_price << "\t";
		cout << "状态:" <<( this->bookarray[i].m_condition ? "借出" : "在馆") << endl;
	}
}

void BookManage::savefile() {
	ofstream ofs;
	ofs.open(FILENAME, ios::out);

	for (int i = 0; i < this->arr_number; i++)
	{
		ofs << this->bookarray[i].m_number << " "
			<< this->bookarray[i].m_name << " "
			<< this->bookarray[i].m_price << " "
			<< this->bookarray[i].m_condition << endl;
	}

	ofs.close();
	
}

int BookManage::index(int number) {
	for (int i = 0; i < this->arr_number; i++)
	{
		if (this->bookarray[i].m_number == number)
		{
			return i;
		}
	}
	return -1;
}

void BookManage::delete_book() {
	cout << "请输入所要删除的图书的编号:";
	int number;
	cin >> number;

	int val = index(number);
	if (val == -1)
	{
		cout << "馆内无该编号图书信息,删除失败!" << endl;
	}
	else
	{
		for (int i = val; i < this->arr_number; i++)
		{
			this->bookarray[i].m_number = this->bookarray[i + 1].m_number;
			this->bookarray[i].m_name = this->bookarray[i + 1].m_name;
			this->bookarray[i].m_price = this->bookarray[i + 1].m_price;
			this->bookarray[i].m_condition = this->bookarray[i + 1].m_condition;
		}

		this->arr_number--;
		savefile();
		cout << "删除成功!" << endl;
	}
}

void BookManage::modify_condition() {
	cout << "请输入所要修改的图书的编号:";
	int number;
	cin >> number;

	int val = index(number);
	if (val == -1)
	{
		cout << "馆内无该编号图书信息,修改失败!" << endl;
	}
	else
	{
		cout << "请选择该书的状态: 0、在馆  1、借出" << endl;
		bool m;
		cin >> m;

		this->bookarray[val].m_condition = m;
		savefile();
		cout << "修改成功!" << endl;
	}
}

void BookManage::search_book() {
	cout << "请选择查询方式: 1、按图书编号查询  2、按图书书名查询" << endl;
	int n;
	cin >> n;

	if (n == 1)
	{
		cout << "请输入图书的编号:";
		int number;
		cin >> number;

		int val = index(number);
		if (val == -1)
		{
			cout << "馆内无该编号图书信息,查找失败!" << endl;
		}
		else
		{
			cout << "编号:" << this->bookarray[val].m_number << "\t"
				<< "书名:" << this->bookarray[val].m_name << "\t"
				<< "价格:" << this->bookarray[val].m_price << "\t"
				<< "状态:" << (this->bookarray[val].m_condition ? "借出" : "在馆") << endl;
		}
	}
	if (n == 2)
	{
		cout << "请输入图书名:";
		string name;
		cin >> name;

		int sign = 0;
		for (int i = 0; i < this->arr_number; i++)
		{
			if(this->bookarray[i].m_name == name)
			{
				cout << "编号:" << this->bookarray[i].m_number << "\t"
					<< "书名:" << this->bookarray[i].m_name << "\t"
					<< "价格:" << this->bookarray[i].m_price << "\t"
					<< "状态:" << (this->bookarray[i].m_condition ? "借出" : "在馆") << endl;
				++sign;
			}
		}
		if (sign == 0)
		{
			cout << "馆内无该书名的图书,查找失败!" << endl;
		}
	}
}

3.会员管理类

#include"MemberManage.h"
#include<fstream>


MemberManage::MemberManage() {
	menu();

	//文件不存在
	ifstream ifs;
	ifs.open("member.txt", ios::in);
	if (!ifs.is_open())
	{
		this->mem_array = NULL;
		this->array_num = 0;
		this->file_empty = true;

		ifs.close();
		return;
	}

	//文件为空
	char c;
	ifs >> c;
	if (ifs.eof())
	{
		this->mem_array = NULL;
		this->array_num = 0;
		this->file_empty = true;

		ifs.close();
		return;
	}

	//文件不为空
	this->array_num = this->get_num();
	init_array();
	this->file_empty = false;

}

MemberManage::~MemberManage() {
	if (this->mem_array != NULL)
	{
		delete[]this->mem_array;
		this->mem_array = NULL;
	}
}


void MemberManage::menu() {
	cout << "\t   1、增添会员信息" << endl;
	cout << "\t   2、显示会员信息" << endl;
	cout << "\t   3、删除会员信息" << endl;
	cout << "\t   4、修改借书信息" << endl;
	cout << "\t   5、查找会员信息" << endl;
	cout << "\t   6、返回上一级界面" << endl;
	cout << endl;
}

int MemberManage::get_num() {
	ifstream ifs;
	ifs.open("member.txt", ios::in);

	int id;
	string name;
	int sex;
	string imfo;

	int num = 0;
	while (ifs >> id && ifs >> name && ifs >> sex && ifs >> imfo)
	{
		num++;
	}

	ifs.close();
	return num;
}

void MemberManage::init_array() {
	ifstream ifs;
	ifs.open("member.txt", ios::in);

	int id;
	string name;
	int sex;
	string imfo;

	int num = this->get_num();

	struct m_member* p = new m_member[num];
	int i = 0;
	while (ifs>>id && ifs>>name && ifs>>sex && ifs>>imfo)
	{
		p[i].id = id;
		p[i].name = name;
		p[i].sex = sex;
		p[i].imfo = imfo;

		i++;
	}

	this->mem_array = p;
	ifs.close();
}

void MemberManage::save() {
	ofstream ofs;
	ofs.open("member.txt", ios::out);

	for (int i = 0; i < this->array_num; i++)
	{
		ofs << this->mem_array[i].id << " "
			<< this->mem_array[i].name << " "
			<< this->mem_array[i].sex << " "
			<< this->mem_array[i].imfo << endl;
	}

	ofs.close();
}

void MemberManage::add(){
	int id;
	string name;
	int sex;
	string imfo;

	cout << "请输入会员卡号:" ;
	cin >> id;
	cout << "请输入会员姓名:";
	cin >> name;
	cout << "请选择会员性别: 1、男   2女    ";
	cin >> sex;
	cout << "请输入会员的借阅信息:";
	cin >> imfo;

	if (this->mem_array == NULL)
	{
		this->mem_array = new m_member;
	}
	int num = this->get_num();
	
	struct m_member* p = new m_member[num + 1];
	for (int i = 0; i < num; i++)
	{
		p[i].id = this->mem_array[i].id;
		p[i].name = this->mem_array[i].name;
		p[i].sex = this->mem_array[i].sex;
		p[i].imfo = this->mem_array[i].imfo;
	}
	p[num].id = id;
	p[num].name = name;
	p[num].sex = sex;
	p[num].imfo = imfo;

	delete[]this->mem_array;

	this->mem_array = p;

	this->array_num++;

	
	save();

	cout << "录入成功!" << endl;
}

void MemberManage::show() {
	for (int i = 0; i < this->array_num; i++)
	{
		cout << "卡号:" << this->mem_array[i].id << "\t";
		cout << "姓名:" << this->mem_array[i].name << "\t";
		cout << "性别:" << (this->mem_array[i].sex == 1 ? "男" : "女") << "\t";
		cout << "借阅信息:" << this->mem_array[i].imfo << endl;
	}
}

int MemberManage::index(int id) {
	for (int i = 0; i < this->array_num; i++)
	{
		if (this->mem_array[i].id == id)
		{
			return i;
		}
	}
	return -1;
}

void MemberManage::delete_mem() {
	cout << "请输入会员的卡号:" << endl;
	int id;
	cin >> id;

	int m = index(id);
	if (m == -1)
	{
		cout << "系统内无相应卡号持有者的信息,删除失败!" << endl ;
	}
	else
	{
		for (int i = m; i < this->array_num; i++)
		{
			this->mem_array[i].id = this->mem_array[i + 1].id;
			this->mem_array[i].name = this->mem_array[i + 1].name;
			this->mem_array[i].sex = this->mem_array[i + 1].sex;
			this->mem_array[i].imfo = this->mem_array[i + 1].imfo;
		}

		this->array_num--;
		save();
		cout << "删除成功!" << endl;
	}
}

void MemberManage::search() {
	cout << "请输入会员的卡号:" << endl;
	int id;
	cin >> id;

	int m = index(id);
	if (m == -1)
	{
		cout << "系统内无相应卡号持有者的信息,搜索失败!" << endl;
	}
	else
	{
		cout << "卡号:" << this->mem_array[m].id << "\t"
			<< "姓名:" << this->mem_array[m].name << "\t"
			<< "性别:" << (this->mem_array[m].sex == 1 ? "男" : "女") << "\t"
			<< "借阅信息:" << this->mem_array[m].imfo;

		this->array_num--;
	}
}

void MemberManage::modify() {
	cout << "请输入会员的卡号:" << endl;
	int id;
	cin >> id;

	int m = index(id);
	if (m == -1)
	{
		cout << "系统内无相应卡号持有者的信息,修改失败!" << endl;
	}
	else
	{
		cout << "会员为姓名:" << this->mem_array[m].name << endl;
		cout << "请输入该会员的新的借阅信息:" << endl;
		string imfo;
		cin >> imfo;
		
		this->mem_array[m].imfo = imfo;
		save();
		cout << "修改成功!" << endl;
	}
}

 

版权声明:本文为博主作者:靓靓不亮原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/wild_idea/article/details/128881956

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2024年1月11日
下一篇 2024年1月11日

相关推荐