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

图书管理系统

在这里插入图片描述

library.h

#include<iostream>
#include<string>
#include<vector>
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;
}
  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

咸蛋挞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值