C++实验2:图书管理系统2.0——继承、派生、多态

题目描述

实现一个简单的图书资源管理系统,管理维护已有图书资源,图书资源有不同的类型,有一般图书,杂志,电子资源等。不同资源有不同的属性和操作。

  1. 增加新的图书或资源
  2. 增加已有图书或资源的数量
  3. 显示某一图书或资源情况,根据资源类型不同显示的信息也不一样。
  4. 显示所有图书或资源情况。
  5. 减少已有图书或资源的数量。
  6. 删除某一图书或资源。

目的

(1) 掌握继承与派生逻辑关系,能够判断类之间的关系是继承派生;
(2) 掌握继承派生中基类,子类的设计方法;
(3) 掌握使用基类,子类对象的方法

问题分析

本题的设计思路如下,建立图书资源基类,并派生出一般图书、杂志、电子资源等子类。根据需要定义父类和子类的成员变量和成员函数,其中包括图书或资源的显示信息等函数。若扩展实现可以将图书馆设计为一个组合类,包含图书资源的对象数组,根据自己的掌握情况选择是否扩展。该题目主要涉及到的知识点有:继承、派生。
根据需求分析父类和子类中是否存在一些功能操作可以设计成为虚函数,或者可以进行操作符重载的设计与实现(可选),如图书或资源的显示信息等虚函数。将图书馆设计为一个组合类,图书馆类包含各种资源对象,优化设计,图书馆类包含基类(资源类)对象指针数组或动态数组(vector),将多种子类的对象统一到基类指针数组的数据结构下进行统一处理,利用基类指针调用虚函数,各个子类对象不同的功能仍然能够发挥出来。该题目主要涉及到的知识点有:组合、继承、派生、虚函数多态,操作符重载多态。

要求

  1. 程序至少包括继承、派生、虚函数多态。(如果设计图书馆类可以设计组合关系,图书馆类包含图书资源类对象)。
  2. 可以使用 new 和 delete 建立类对象和释放类对象。
  3. 界面提示友好,给出菜单选项,及输入提示。

程序流程图

在这里插入图片描述

类图

在这里插入图片描述

参考代码

//书本基类 定义 CColective.h

#pragma once
//书本基类
#include <iostream>
#include <string>
using namespace std;
class CColective
{
public:
	int m_Id;//书本编号
	string m_Name;  //书本名字
	string m_Press; //出版社
	string m_Price; //图书价格
	string m_Page;  //图书页数

	virtual void showInfo() = 0;	//显示书本信息,多态
	virtual string getClass() = 0;  //获取书本类型
	virtual void SetOtherProperties(string Other_Properties) = 0;//设置其他属性
	virtual string GetOtherProperties() = 0;//获得其他属性
	virtual bool Judge(string OtherProperties) = 0;
};


//一般图书类 定义 CBooks.h

#pragma once
//一般图书类
#include "CColective.h"
#include <iostream>
#include <string>
using namespace std;

class CBooks : public CColective
{
private:
	string m_Isbn = "ISBN-";  //书本Isbn码
	int njudge;//判断Ibsn码合法性
public:
	CBooks();//无参构造函数
	CBooks(int Id, string Name, string Press, string Price, string Page); //有参构造函数
	~CBooks();
	bool Judge(string OtherProperties);

	//显示书本信息
	virtual void showInfo();
	//获取书本类型
	virtual string getClass();
	//设置其他属性
	virtual void SetOtherProperties(string Isbn);
	virtual string GetOtherProperties();//获得其他属性
};

//一般图书类 实现 CBooks.cpp

#include "CBooks.h"

CBooks::CBooks()
{
	this->m_Id = 0;
	this->m_Name = "";
	this->m_Press = "";
	this->m_Price = "";
	this->m_Page = "";
}

CBooks::CBooks(int Id, string Name, string Press, string Price, string Page)
{
	this->m_Id = Id;
	this->m_Name = Name;
	this->m_Press = Press;
	this->m_Price = Price;
	this->m_Page = Page;
}

CBooks::~CBooks()
{

}

bool CBooks::Judge(string OtherProperties)
{
	int tmp = 0;
	int ss[20] = { 0 };
	for (int i = 0, j = 0; i < OtherProperties.length(); i++) {
		if (OtherProperties.substr(i, 1) >= "0" && OtherProperties.substr(i, 1) <= "9") {
			ss[j] = atoi(OtherProperties.substr(i, 1).c_str());
			j++;
		}
	}
	for (int i = 0; i < 12; i++) {
		if (i % 2 == 0)
		{
			tmp += ss[i] * 1;
		}
		else {
			tmp += ss[i] * 3;
		}
	}
	if (tmp % 10 == 0 && tmp % 10 == ss[12] || 10 - (tmp % 10) == ss[12])
	{
		return true;
	}
	else
	{
		return false;
	}
}

void CBooks::showInfo()
{
	cout << "书本编号:" << this->m_Id
		<< "\t书本类型:" << this->getClass()
		<< "\t书本名称:" << this->m_Name
		<< "\t书本出版社:" << this->m_Press
		<< "\t书本价格:" << this->m_Price
		<< "\t书本页数:" << this->m_Page
		<< "\t其他属性——书本Ibsn号:" << this->m_Isbn;
}
string CBooks::getClass()
{
	return string("一般图书");
}


void CBooks::SetOtherProperties(string Isbn)
{
	this->m_Isbn.append(Isbn);//append(),追加函数,将两个string字符串拼接在一起
}

string CBooks::GetOtherProperties()
{
	return this->m_Isbn;
	return string();
}

//杂志书籍类 定义 CMagazine.h

#pragma once
//杂志书籍类
#include "CColective.h"
#include <iostream>
#include <string>

using namespace std;
class CMagazine : public CColective
{
private:
	string m_Issn = "ISSN-";//杂志期刊号 格式:ISSN XXXX-XXXX
public:
	CMagazine();
	CMagazine(int Id, string Name, string Press, string Price, string Page);
	~CMagazine();

	//显示书本信息
	virtual void showInfo();
	//获取书本类型
	virtual string getClass();
	//设置其他属性
	virtual void SetOtherProperties(string Issn);
	virtual string GetOtherProperties();//获得其他属性

	bool Judge(string OtherProperties);
};


//杂志书籍类 实现 CMagazine.cpp

#include "CMagazine.h"

CMagazine::CMagazine()
{
	this->m_Id = 0;
	this->m_Name = "";
	this->m_Press = "";
	this->m_Price = "";	
	this->m_Page = "";
}

CMagazine::CMagazine(int Id, string Name, string Press, string Price, string Page)
{
	this->m_Id = Id;
	this->m_Name = Name;
	this->m_Press = Press;
	this->m_Price = Price;
	this->m_Page = Page;
}

CMagazine::~CMagazine()
{
}

void CMagazine::showInfo()
{
	cout << "书本编号:" << this->m_Id
		<< "\t书本类型:" << this->getClass()
		<< "\t书本名称:" << this->m_Name
		<< "\t书本出版社:" << this->m_Press
		<< "\t书本价格:" << this->m_Price
		<< "\t书本页数:" << this->m_Page
		<< "\t其他属性——书本Issn号:" << this->m_Issn;
}

string CMagazine::getClass()
{

	return string("杂志书籍");
}

void CMagazine::SetOtherProperties(string Issn)
{
	this->m_Issn.append(Issn);//append(),追加函数,将两个string字符串拼接在一起
}

string CMagazine::GetOtherProperties()
{
	return this->m_Issn;
	return string();
}

bool CMagazine::Judge(string OtherProperties)
{
	return false;
}

//电子书类 定义 CEBook.h

#pragma once
//电子书类
#include "CColective.h"
#include <iostream>
#include <string>
using namespace std;

class CEBook : public CColective
{
private:
	string m_Format; //电子书的格式(txt、pdf、epub、azw3、kindle)
public:
	CEBook();
	CEBook(int Id, string Name, string Press, string Price, string Words);//Words电子书的字数
	~CEBook();

	//显示书本信息
	virtual void showInfo();
	//获取书本类型
	virtual string getClass();
	//设置其他属性
	virtual void SetOtherProperties(string Format);
	virtual string GetOtherProperties();//获得其他属性
	bool Judge(string OtherProperties);
};

//电子书类 实现 CEBook.cpp

#include "CEBook.h"

CEBook::CEBook()
{
	this->m_Id = 0;
	this->m_Name = "";
	this->m_Press = "";
	this->m_Price = "";
	this->m_Page = "";
}

CEBook::CEBook(int Id, string Name, string Press, string Price, string Words)
{
	this->m_Format = "";
	this->m_Id = Id;
	this->m_Name = Name;
	this->m_Press = Press;
	this->m_Price = Price;
	this->m_Page = Words;
}

CEBook::~CEBook()
{
}

void CEBook::showInfo()
{
	cout << "书本编号:" << this->m_Id
		<< "\t书本类型:" << this->getClass()
		<< "\t书本名称:" << this->m_Name
		<< "\t书本出版社:" << this->m_Press
		<< "\t书本价格:" << this->m_Price
		<< "\t书本字数:" << this->m_Page
		<< "\t其他属性——书本格式:格式为 . " << this->m_Format;
}

string CEBook::getClass()
{
	return string("电子书");
}

void CEBook::SetOtherProperties(string Format)
{
	this->m_Format.append(Format);
}

string CEBook::GetOtherProperties()
{
	return this->m_Format;
	return string();
}

bool CEBook::Judge(string OtherProperties)
{
	return false;
}


//管理类 定义 CLIbraryManager.h

#pragma once //防止头文件重复包含
//管理类
//管理内容:
//与用户的沟通菜单界面
//对书本的增、删、查等操作
//与文件的读写交互

#include "CColective.h"
#include "CBooks.h"		//一般图书
#include "CEBook.h"		//电子书
#include "CMagazine.h"	//杂志

#include<cstdlib>		//system 不明确	
#include <fstream>		//文件流头文件
#define FILENAME "BookFile.txt"

using namespace std;

class CLIbraryManager
{
public:
	CLIbraryManager();//构造函数

	//菜单显示
	void Show_Menu();

	//0.退出系统
	void ExitSystem();

	//1.添加实现
	int m_BookNum;//记录书本的数量
	CColective** m_BookArray;//书本数组指针
	void Add_Book();
	
	//保存文件
	void Save();

	//设置标志判断最开始文件是否存在
	bool m_isFile;

	//统计文件中已有书本数量
	int Get_BookNum();

	//初始化文件中已有书本数组、数量
	void init_Book();

	//2.显示书本
	void show_BookArray();

	//4.查找实现
	void find_Book();//函数

	//6.清空txt实现
	void clear_Txt();

	//3.删除书本信息实现
	void delete_Book();//函数
	int isBook(int Id);
	int isBook(string name);
	
	//5.按照书本序号排序
	void sort_BookArray();

	~CLIbraryManager();//析构函数
};


//管理类 实现 CLIbraryManager.cpp

#include "CLIbraryManager.h"
#include<cstdlib>
using namespace std;
CLIbraryManager::CLIbraryManager()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);//打开文件

	//文件不存在
	if (!ifs.is_open())//ifs.is_open()返回值为bool类型,如果能打开返回true
	{
		this->m_isFile = false;//文件不存在,将m_isFile赋值为false
		this->m_BookNum = 0;//初始化书的本数
		this->m_BookArray = NULL;//初始化书数组指针
		ifs.close();
		return;
	}

	//文件存在,但数据为空
	char ch;
	ifs >> ch;//传入空字符
	if (ifs.eof())//如果文件是空,则返回true
	{
		this->m_isFile = false;//文件存在,但无数据,将m_isFile赋值为false
		this->m_BookNum = 0;//初始化书的本数
		this->m_BookArray = NULL;//初始化书数组指针
		ifs.close();
		return;
	}

	//文件存在且不为空
	else if (!ifs.eof())
	{
		this->m_isFile = true;//文件存在且有数据,将m_isFile赋值为true
		int num = this->Get_BookNum();
		this->m_BookNum = num;
		this->m_BookArray = new CColective* [this->m_BookNum];
		//将读取的数据存入数组中
		this->init_Book();
	}
}

//显示菜单函数
void CLIbraryManager::Show_Menu()
{
	cout << "————————————————————————" << endl;
	cout << "——————  欢迎使用图书管理系统!——————" << endl;
	cout << "———————— 0.退出管理程序 ————————" << endl;
	cout << "———————— 1.增加书本信息 ————————" << endl;
	cout << "———————— 2.显示书本信息 ————————" << endl;
	cout << "———————— 3.删除书本信息 ————————" << endl;
	cout << "———————— 4.查找书本信息 ————————" << endl;
	cout << "———————— 5.按照编号排序 ————————" << endl;
	cout << "———————— 6.清空所有文档 ————————" << endl;
	cout << "————————————————————————" << endl;
	cout << endl;

}

//退出程序函数
void CLIbraryManager::ExitSystem()
{
	cout << "欢迎下次使用!" << endl;
	system("pause");
	exit(0);
}

//增加书函数
void CLIbraryManager::Add_Book()
{
	cout << "请输入添加书本的数量:" << endl;
	int add_number = 0;
	cin >> add_number;//接收用户输入的大小
	if (add_number > 0) //输入大于零才执行增加操作
	{
		//执行添加
		int newSize = this->m_BookNum + add_number;//计算需要开辟的空间大小,新的数量 = 原来记录的 + 新输入的
		CColective** newspace = new CColective * [newSize];//分配空间

		//如果原来的数组不为空,则添加到新开辟的数组中
		if (this->m_BookArray != NULL)
		{
			for (int i = 0; i < this->m_BookNum; i++)
			{
				newspace[i] = this->m_BookArray[i];
			}
		}

		//添加新数据
		for (int i = 0; i < add_number; i++)
		{
			int d_Id = 0;//书本编号
			string d_Name;  //书本名字
			string d_Press; //出版社
			string d_Price; //图书价格
			string d_Page_Words;  //图书页数或字数
			string d_OtherProperties;//书的其他属性(Isbn,Issn,Format)

			cout << "请选择要添加的类型:" << endl;
			cout << "1.一般图书" << endl;
			cout << "2.杂志" << endl;
			cout << "3.电子书" << endl;
			int select;//用户的选择
			cin >> select;

			CColective* book = NULL;//创建book形参
			cout << "请输入第" << i + 1 << "本书编号:" << endl;
			cin >> d_Id;
			cout << endl;
			cout << "请输入第" << i + 1 << "本书书名:" << endl;
			cin >> d_Name;
			cout << endl;
			cout << "请输入第" << i + 1 << "本书出版社:" << endl;
			cin >> d_Press;
			cout << endl;
			cout << "请输入第" << i + 1 << "本书价格:" << endl;
			cin >> d_Price;
			cout << endl;
			
			switch (select)
			{
			case 1:
			{
				// 注意: 一下代码是判断ISBN码是否合法,现已将判断部分注释
				cout << "请输入第" << i + 1 << "本书页数:" << endl;//一般图书的页数
				cin >> d_Page_Words;
				cout << endl;
				book = new CBooks(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
				cout << "请输入第" << i + 1 << "本书Ibsn号:" << endl;
				cin >> d_OtherProperties;

				//do
				//{
				//	cout << "请输入第" << i + 1 << "本书页数:" << endl;//一般图书的页数
				//	cin >> d_Page_Words;
				//	cout << endl;
				//	book = new CBooks(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
				//	cout << "请输入第" << i + 1 << "本书Ibsn号:" << endl;
				//	cin >> d_OtherProperties;
				//	if (!book->Judge(d_OtherProperties)) {
				//		cout << "ISBN码不合法!" << endl
				//			<< "请重新输入:" << endl << endl;
				//		cout << "请输入第" << i + 1 << "本书编号:" << endl;
				//		cin >> d_Id;
				//		cout << endl;
				//		cout << "请输入第" << i + 1 << "本书书名:" << endl;
				//		cin >> d_Name;
				//		cout << endl;
				//		cout << "请输入第" << i + 1 << "本书出版社:" << endl;
				//		cin >> d_Press;
				//		cout << endl;
				//		cout << "请输入第" << i + 1 << "本书价格:" << endl;
				//		cin >> d_Price;
				/*	}
				} while (!book->Judge(d_OtherProperties));*/
				cout << endl;
				book->SetOtherProperties(d_OtherProperties);
				break;
			}
				
			case 2:
			{
				cout << "请输入第" << i + 1 << "本书页数:" << endl;
				cin >> d_Page_Words;
				cout << endl;
				book = new CMagazine(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
				cout << "请输入第" << i + 1 << "本书Issn号:" << endl;
				cin >> d_OtherProperties;
				cout << endl;
				book->SetOtherProperties(d_OtherProperties);
				break;
			}
				
			case 3:
			{
				cout << "请输入第" << i + 1 << "本书字数:" << endl;//电子书的字数
				cin >> d_Page_Words;
				cout << endl;
				book = new CEBook(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
				cout << "请输入第" << i + 1 << "本书格式:" << endl;//电子书的格式
				cout << "电子书的格式有:txt、pdf、epub、azw3、kindle" << endl;
				cin >> d_OtherProperties;
				cout << endl;
				book->SetOtherProperties(d_OtherProperties);
				break;
			}
				
			default:
				break;
			}
			//将创建好的书本增加到数组中
			newspace[this->m_BookNum + i] = book;
		}
		delete[]this->m_BookArray;//释放原来的空间
		this->m_BookArray = newspace;//将新的空间指向m_BookArrary
		this->m_BookNum = newSize;//更新新的书本数量

		//保存数据到文件中
		//this->delete_txt();
		this->Save();
		this->m_isFile = true;//添加成功,则说明文件创建成功且数据不为空
		cout << "成功添加" << add_number << "本新书" << endl;

	}
	else
	{
		cout << "输入的数据有误!" << endl;
	}
	system("pause");
	system("cls");
}

//保存书本数据
void CLIbraryManager::Save()
{
	ofstream ofs;//定义
	ofs.open(FILENAME, ios::out);//用输出的方式文件——写文件
	//将每本书的数据写入到文件中
	
	for (int i = 0; i < this->m_BookNum; i++)
	{
		ofs << this->m_BookArray[i]->m_Id << " "
			<< this->m_BookArray[i]->m_Name << " "
			<< this->m_BookArray[i]->m_Press << " "
			<< this->m_BookArray[i]->m_Price << " "
			<< this->m_BookArray[i]->m_Page << " "
			<< this->m_BookArray[i]->GetOtherProperties() << endl;
	}
	//关闭文件
	ofs.close();
}

//获取文件中已有书本的数量
int CLIbraryManager::Get_BookNum()
{
	int d_Id = 0;//书本编号
	string d_Name;  //书本名字
	string d_Press; //出版社
	string d_Price; //图书价格
	string d_Page_Words;  //图书页数或字数
	string d_OtherProperties;//书的其他属性(Isbn,Issn,Format)

	ifstream ifs;
	ifs.open(FILENAME, ios::in);

	int num = 0;
	while (ifs >> d_Id && ifs >> d_Name && ifs >> d_Press && ifs >> d_Price && ifs >> d_Page_Words && ifs >> d_OtherProperties)
	{
		num++;
	}

	return num;
}

//初始化文件中已有书本数组、数量
void CLIbraryManager::init_Book()
{
	ifstream ifs;
	ifs.open(FILENAME, ios::in);
	int d_Id;//书本编号
	string d_Name;  //书本名字
	string d_Press; //出版社
	string d_Price; //图书价格
	string d_Page_Words;  //图书页数或字数
	string d_OtherProperties;//书的其他属性(Isbn,Issn,Format)

	int index = 0;//记录数组位置
	while (ifs >> d_Id && ifs >> d_Name && ifs >> d_Press && ifs >> d_Price && ifs >> d_Page_Words && ifs >> d_OtherProperties)
	{
		CColective* book = NULL;
		
		//截取读取的其他属性的前4个字符串。如果为"ISBN",则说明这一行数据是一般图书
		if (d_OtherProperties.substr(0, 4) == "ISBN")
		{
			book = new CBooks(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
			book->SetOtherProperties(d_OtherProperties.substr(5, d_OtherProperties.size() - 1));
		}

		//截取读取的其他属性的前4个字符串。如果为"ISSN",则说明这一行数据是杂志
		else if (d_OtherProperties.substr(0, 4) == "ISSN")
		{
			book = new CMagazine(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
			book->SetOtherProperties(d_OtherProperties.substr(5, d_OtherProperties.size() - 1));
		}

		//否则就是电子书类
		else
		{
			book = new CEBook(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
			book->SetOtherProperties(d_OtherProperties);
		}

		//将读取的书本信息存入书本数组中
		this->m_BookArray[index] = book;
		index++;
	}

	ifs.close();//关闭文件
}

//显示函数
void CLIbraryManager::show_BookArray()
{
	if (this->m_BookNum == 0)
	{
		cout << "无书本信息!" << endl;
	}
	else
	{
		for (int i = 0; i < this->Get_BookNum(); i++)
		{
			this->m_BookArray[i]->showInfo();
			cout << endl << endl;//利用函数的多态,调用派生类显示函数
		}
	}

	system("pause");
	system("cls");
	
}

//查找函数
void CLIbraryManager::find_Book()
{
	if (this->m_BookNum == 0)
	{
		cout << "无书本信息!" << endl;
	}
	else
	{
		cout << "可通过以下查找:" << endl;
		cout << "1.书本编号" << endl
			<< "2.书本名称" << endl;
		int select;//用户查找选择
		cin >> select;
		int Id = 0;
		string book_name = "";
		switch (select)
		{
		case 1:
		{
			cout << "请输入书本编号:" << endl;
			cin >> Id;
			for (int i = 0; i < this->m_BookNum; i++)
			{
				if (i == this->m_BookNum - 1 && this->m_BookArray[i]->m_Id != Id) {
					cout << "未查询到该书信息!" << endl;
				}
				if (this->m_BookArray[i]->m_Id == Id) {
					cout << "该书本信息为:" << endl;
					this->m_BookArray[i]->showInfo();
					cout << endl;
					break;
				}
			}
			break;
		}
			
		case 2:
		{
			cout << "请输入书本名称:" << endl;
			cin >> book_name;
			for (int i = 0; i < this->m_BookNum; i++)
			{
				if (i == this->m_BookNum - 1 && this->m_BookArray[i]->m_Name != book_name) {
					cout << "未查询到该书信息!" << endl;
				}
				if (this->m_BookArray[i]->m_Name == book_name) {
					cout << "该书本信息为:" << endl;
					this->m_BookArray[i]->showInfo();
					cout << endl;
					break;
				}
			}
			break;
		}
			
		default:
			break;
		}
	}
	system("pause");
	system("cls");
}

//清空文档函数
void CLIbraryManager::clear_Txt()
{
	if (!this->m_isFile)
	{
		cout << "文件不存在!" << endl;
	}
	else
	{
		int select;
		cout << "是否确定清空所有文档?" << endl
			<< "1.是" << endl
			<< "2.否" << endl;
		cin >> select;
		ofstream ofs;
		switch (select)
		{
		case 1:
		{
			ofs.open(FILENAME, ios::out | ios::trunc);
			ofs.close();
			delete[]this->m_BookArray;
			this->m_BookArray = NULL;
			this->m_BookNum = 0;
			this->m_isFile = false;
			cout << "文档已清空!" << endl;
			break;
		}
			
		case 2:
		break;
		default:
			break;
		}
	}

	system("pause");
	system("cls");
}

//通过Id判断该书是否存在,存在则返回数组下标
int CLIbraryManager::isBook(int Id)
{
	int index = -1;
	for (int i = 0; i < this->m_BookNum; i++)
	{
		if (this->m_BookArray[i]->m_Id == Id) {
			index = i;
			break;
		}
	}
	return index;
}
//通过name判断该书是否存在,存在则返回数组下标(重载)
int CLIbraryManager::isBook(string name)
{
	int index = -1;
	for (int i = 0; i < this->m_BookNum; i++)
	{
		if (this->m_BookArray[i]->m_Name == name) {
			index = i;
			break;
		}
	}
	return index;
}

//删除书本信息
void CLIbraryManager::delete_Book()
{
	//判断文件或记录是否存在
	if (!this->m_isFile)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else
	{
		int Id;
		string name;
		int select;//接収用户选择
		int index = -1;//接收返回的数组下标

		cout << "请输入通过什么删除:" << endl
			<< "1.书本编号" << endl
			<< "2.书本名称" << endl;
		cin >> select;

		switch (select)
		{
		case 1:
		{
			cout << "请输入书本编号:" << endl;
			cin >> Id;
			index = this->isBook(Id);
			break;
		}
		
		case 2:
		{
			cout << "请输入书本名称:" << endl;
			cin >> name;
			index = this->isBook(name);
			break;
		}
		default:
			break;
		}
		//若index的值不为-1,则说明该书存在
		if (index != -1)
		{
			cout << "该书本信息为:" << endl;
			this->m_BookArray[index]->showInfo();//显示该书信息
			cout << endl << endl << "是否确认删除该书信息?" << endl
				<< "1.是" << endl
				<< "2.否" << endl;
			cin >> select;

			switch (select)
			{
			case 1:
			{
				for (int i = index; i < this->m_BookNum - 1 ; i++)
				{
					this->m_BookArray[i] = this->m_BookArray[i + 1];
				}
				this->m_BookNum--;
				cout << "删除成功!" << endl;
				this->Save();
				break;
			}
			case 2:
				cout << "已取消删除!" << endl;
				break;
			default:
				break;
			}
		}
		else
		{
			cout << "删除失败,无该书信息!" << endl;
		}
	}
	system("pause");
	system("cls");
}

//排序实现
void CLIbraryManager::sort_BookArray()
{
	if (!this->m_isFile)
	{
		cout << "文件不存在或记录为空!" << endl;
	}
	else {
		for (int i = 0; i < this->m_BookNum; i++)
		{
			for (int j = 0; j < this->m_BookNum; j++)
			{
				if (this->m_BookArray[i]->m_Id < this->m_BookArray[j]->m_Id)
				{
					CColective* Tbook = this->m_BookArray[i];//创建Tbook中间变量
					this->m_BookArray[i] = this->m_BookArray[j];
					this->m_BookArray[j] = Tbook;
				}
			}
		}
		this->Save();
		cout << "排序成功!" << endl;
	}
	system("pause");
	system("cls");
}

//析构函数
CLIbraryManager::~CLIbraryManager()
{
	if (this->m_BookArray != NULL)
	{
		delete[] this->m_BookArray;
		this->m_BookArray = NULL;
	}
}

主函数 Library Management System2.0.cpp

// Library Management System2.0.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//"C/C++程序设计教程——面向对象分册(第三版)", "中国工信出版集团,电子工业出版社", "978-7-121-33047-6", "47.00", "356"
//"概率论与数理统计","中国工信出版集团,人民邮电出版社","978-7-115-53563-4","45","226"
#include <iostream>
#include "CLIbraryManager.h"
#include "CBooks.h"//一般图书类
#include "CColective.h"//管理者类

int main()
{
    //实例化管理这对象
    CLIbraryManager LM;

    int choice = 0;//接收用户的选择
    while (true)
    {
        //调用展示菜单函数
        LM.Show_Menu();

        cout << "情输入您的选择:" << endl;
        cin >> choice;//接收用户的选择

        switch (choice)
        {
        case 0:  //退出系统
            LM.ExitSystem();//退出接口
            break;
        case 1:  //增加书本
            LM.Add_Book();
            break;
        case 2:  //显示书本
            LM.show_BookArray();
            break;
        case 3:  //删除书本
            LM.delete_Book();
            break;
        case 4:  //查找书本
            LM.find_Book();
            break;
        case 5:  //书本排序
            LM.sort_BookArray();
            break;
        case 6:  //清空书本
            LM.clear_Txt();
            break;
        default:
            system("cls");
            break;
        }
    }
    system("pause");
    return 0;
}

运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

说明

个人能力有限,仅供参考,共同学习!

  • 21
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无奈清风吹过

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值