【C++初阶】:简单的图书管理系统(可保存,完整源代码)

图书管理系统

【C++初阶】:简单的图书管理系统(可保存,完整源代码)_第1张图片

library.h

#include
#include
#include
using namespace std;

/****************************************************************公共类*****************************************************************************/

struct Book
{
	char _name[30] = {0};
	char _author[20] = {0};
	char _price[10] = {0};
	char _type[20] = {0};
	int _condition=0;//0表示未被借出去,1表示已被借出去
};

vector<Book>_bookshelf;

class Bookshelf
{
public:
	void Init()
	{
		FILE* fp = fopen("library.txt", "rb");//将图书储存在library.txt文件里
		if (fp != nullptr)
		{
			struct Book tmp;
			while (fread(&tmp,sizeof(Book),1,fp))
			{
				_bookshelf.push_back(tmp);
			}
			if (fp != nullptr) fclose(fp);
			fp = nullptr;
		}
	}

	void Show()
	{
		if (_bookshelf.size() == 0)
		{
			cout << "暂无图书" << endl;
			return;
		}
		for (auto book : _bookshelf)
		{
			printf("Book{name='%s',author='%s',price=%s,type='%s',", book._name, book._author, book._price, book._type);
			if (book._condition == 0) cout << "该书未被借出}" << endl;
			else cout << "该书已被借出}" << endl;
		}
	}

	void Save()//保存
	{
		FILE* fp = fopen("library.txt", "wb");
		for (size_t i=0;i<_bookshelf.size();i++)
		{
			fwrite(&_bookshelf[i], sizeof(Book), 1, fp);
		}
		fclose(fp);
		fp = nullptr;
	}

	int Find(char name[])
	{
		for (size_t i = 0; i < _bookshelf.size(); i++)
		{
			if (strcmp(_bookshelf[i]._name,name)==0)
			{
				return i;
			}
		}
		cout << "未找到此书!" << endl;
		return -1;
	}
};



/************************************************************管理者和普通用户***************************************************************************/

class Administrator:public Bookshelf
{
public:
	Administrator(string name)
		:_name(name)
	{}

	void Menu()
	{
		cout << "************************************" << endl;
		cout << "hello " << _name << "欢迎来到图书管理系统!" << endl;
		cout << "1.查找图书" << endl;
		cout << "2.新增图书" << endl;
		cout << "3.删除图书" << endl;
		cout << "4.显示图书" << endl;
		cout << "0.退出系统" << endl;
		cout << "************************************" << endl;
	}
	void Choice(int choice)
	{
		switch (choice)
		{
		case 0:
			cout << "退出成功!" << endl;
			break;
		case 1:
		{
			cout << "请输入查找图书名称>";
			char name[30];
			scanf("%s", name);
			int cur = Find(name);
			if (cur >= 0)
			{
				struct Book book = _bookshelf[cur];
				printf("Book{name='%s',author='%s',price=%s,type='%s',", book._name, book._author, book._price, book._type);
				if (book._condition == 0) cout << "该书未被借出}" << endl;
				else cout << "该书已被借出}" << endl;
			}
			break;
		}
		case 2:
			Add();
			break;
		case 3:
			Delete();
			break;
		case 4:
			Show();
			break;
		default:
			cout << "无效输入,请重新选择" << endl;
			break;
		}

	}

private:
	void Add()
	{
		_bookshelf.push_back(Book{});
		int n = _bookshelf.size() - 1;
		cout << "请输入书名>";
		scanf("%s", _bookshelf[n]._name);
		cout << "请输入作者名>";
		scanf("%s", _bookshelf[n]._author);
		cout << "请输入价格>";
		scanf("%s", _bookshelf[n]._price);
		cout << "请输入类型>";
		scanf("%s", _bookshelf[n]._type);

		cout << "增添成功!" << endl;
	}

	void Delete()
	{
		cout << "请输入删除图书的名称>";
		char name[30];
		scanf("%s", name);
		int cur=Find(name);
		if (cur == -1) return;
		if (_bookshelf[cur]._condition == 1)
		{
			cout << "已被借阅,无法删除!" << endl;
		}
		else
		{
			_bookshelf.erase(_bookshelf.begin() + cur);
			cout << "删除成功!" << endl;
		}
	}
	string _name;
};







class User:public Bookshelf
{
public:
	User(string name)
		:_name(name)
	{}

	void Menu()
	{
		cout << "************************************" << endl;
		cout << "hello " << _name << "欢迎来到图书管理系统!" << endl;
		cout << "1.查找图书" << endl;
		cout << "2.借阅图书" << endl;
		cout << "3.归还图书" << endl;
		cout << "4.展示图书" << endl;
		cout << "0.退出系统" << endl;
		cout << "************************************" << endl;
	}

	void Choice(int choice)
	{
		switch (choice)
		{
		case 0:
			cout << "退出成功!" << endl;
			break;
		case 1:
		{
			cout << "请输入查找图书名称>";
			char name[30];
			scanf("%s", name);
			int cur = Find(name);
			if (cur >= 0)
			{
				struct Book book = _bookshelf[cur];
				printf("Book{name='%s',author='%s',price=%s,type='%s',", book._name, book._author, book._price, book._type);
				if (book._condition == 0) cout << "该书未被借出}" << endl;
				else cout << "该书已被借出}" << endl;
			}
			break;
		}
		case 2:
			Lend();
			break;
		case 3:
			Reback();
			break;
		case 4:
			Show();
			break;
		default:
			cout << "无效输入,请重新选择" << endl;;
			break;
		}
	}
private:
	void Lend()
	{
		cout << "请输入要借阅的图书名称>>";
		char name[30];
		scanf("%s", name);
		int cur = Find(name);
		if (cur == -1) return;
		_bookshelf[cur]._condition = 1;
		cout << "借阅成功!" << endl;
	}
	void Reback()
	{
		cout << "请输入归还图书的名称>";
		char name[30];
		scanf("%s", name);
		int cur = Find(name);
		if (cur == -1) return;
		_bookshelf[cur]._condition = 0;
		cout << "归还成功!" << endl;
	}
	string _name;
};

library.cpp

#include"library.h"

void menu()
{
	cout << "************************************" << endl;
	cout << "请选择身份:" ;
	cout << "  1->管理员  2->普通用户" << endl;
	cout << "请选择:";
}



void Adfunc(Administrator*person)
{
	int choice = 1;
	while (choice)
	{
		person->Menu();
		cout << "请选择功能>>";
		cin >> choice;
		person->Choice(choice);
	}
}

void Usfunc(User*person)
{
	int choice = 1;
	while (choice)
	{
		person->Menu();
		cout << "请选择功能>>";
		cin >> choice;
		person->Choice(choice);
	}
}

int main()
{
	string name;
	cout << "请输入您的姓名:";
	cin >> name;
	Bookshelf book;
	book.Init();//初始化
	while (1)
	{
		menu();
		int n = 0;
		cin >> n;
		if (n == 1)
		{
			Administrator* person = new Administrator(name);
			Adfunc(person);
			delete person;
			break;
		}
		else if (n == 2)
		{
			User* person = new User(name);
			Usfunc(person);
			delete person;
			break;
		}
		else cout << "无效选择,请重新选择"<<endl;
	}
	cout << "是否选择保存本次操作(1.是 0.否)>>";
	int save = 0;
	cin >> save;
	if (save == 1)
	{
		book.Save();
		cout << "保存成功" << endl;
	}
	cout << "欢迎再次光临" << endl;

	return 0;
}

你可能感兴趣的:(C++初阶,c++,开发语言)