图书管理系统(链表实现文件的读写)

H大学图书馆
H大学图书馆邀请你建立一个图书馆信息管理系统。请使用面向对象思想完成该问题,具体要求如下:

一、设计一款文字式交互的图书管理系统;

二、图书馆必须支持至少10000册书存储,如果课实现书籍可动态增长,加分

三、图书信息包含:
题名
ISBN/ISSN
作者
分类号(分类规则自定,要求有三级分类,可参考中图分类法)

四、图书馆系统提供两种用户模式,请为他们设计不同的用户类:
1)管理员模式:
系统最初提供一个默认的管理员账户以及默认密码;
管理员具备以下功能:
可以使用管理员账号登录
支持对学校用户的账号进行基本管理,添加、删除学校用户默认账号和密码,默认账号为学号/教师编号,密码为123456;恢复学校用户默认密码;
管理员可以对图书信息进行修改
管理员可以增加、删除、搜索图书

2)学校用户模式(学校用户超过5千人):
学校用户可以通过账号和密码登录,账号为学号/教师编号,密码为123456;
学校用户可以修改自己的密码
学校用户可以搜索图书
学校用户可以借、还图书
学校用户可以查看自己的借阅记录

五、设计图书馆类,包含馆藏图书列表、用户列表等成员、在馆记录、用户借阅记录等。

六、图书馆系统提供根据任一信息的搜索图书功能:
题名,精确查找到书
ISBN/ISSN,精确查找到书
作者,模糊查找到该作者所有书,字典序排序
分类号,三级分类,每一级分类均可模糊查找到书,字典序排序,按页显示;如,N 自然科学总论——TP 自动化技术、计算机技术——TP3 计算机技术。
在以上每一级时,均会出现该级所有数目,字典排序,按页显示;

代码中没有实现的功能的思路:
上下翻页可以借助链表加goto

字典序可以直接利用string的特性来比较汉字可以实现
(该代码中相关功能存在一定瑕疵和问题,仅供参考,此外该代码在vs2019中运行无报错,dev和cb并不能保证可以正常运行)

globalfile.h文件,宏定义文件
#pragma once
#define ADMIN_FILE  "admin.txt"
#define READER_FILE "reader.txt"
#define BOOK_FILE   "books.txt"
#define LENDMESSAGE_FILE "lendmessage.txt"
user.h文件,用户类(当时图方便,实现文件和头文件放在了一起)
#pragma once
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string.h>
#include "book.h" 
#include "globalfile.h"
using namespace std;
struct useru {
	string name;
};


//父类,用户类 
class user {
public:
	virtual void opermenu() = 0;
	string code;
	string key;
};

//子类,读者类 
class reader :public user {
public:
	~reader() {}
	static int book_num;
	reader();
	reader(string code0, string key0);
	virtual void opermenu();
	void changekey();
	void book_lend();
	void book_show();
	void book_find();
	void book_return();
	reader* pnext;
};

reader::reader() {}//无参构造 

reader::reader(string code0, string key0) {
	this->code = code0;
	this->key = key0;
}

int reader::book_num = 0;

class LIST {
private:
	reader* phead;
public:
	LIST();
	reader* gethead1()
	{return phead;}
	int getlen1();
	int modify1();
	int modify2();
	int dele1();
	void read1();
	void save1();
	void displayu();
};


LIST::LIST() {
	phead = new reader();//创建头节点 
	phead->pnext = NULL;
}

int LIST::getlen1() {
	int n = 0;
	reader* r;
	r = phead;
	while (r) {
		n++;
		r = r->pnext;
	}
	return n;
}

void LIST::read1() {
	reader* p, * q;
	ifstream ifs;
	p = this->phead;
	ifs.open(READER_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件不存在!" << endl;
		ifs.close();
		return;
	}
	string code1, key1;
	while (ifs >> code1 && ifs >> key1) {
		q = new reader();
		q->code = code1; q->key = key1;
		p->pnext = q;
		p = q;
	}
	ifs.close();
}

int LIST::dele1() {
	string n;
	reader* temp, * pr;
	temp = phead;
	pr = phead;
	cout << "输入要删除的用户的学号或者教职工编号:" << endl;
	cin >> n;
	while (temp) {
		if (temp->code == n) {//找到该用户后
			if (temp == phead) {//如果该用在头节点
				phead = phead->pnext;//直接将头节点下一个节点变成头节点
				return 1;
			}
			pr->pnext = temp->pnext;//若果不是,将pr指向下一个节点的指针直接连到temp的下一个节点,即跳过了temp,temp的数据消失
			cout << "删除成功!" << endl;
			return 1;
		}
		pr = temp;//pr为上一个节点
		temp = temp->pnext;//temp遍历链表
	}
	cout << "没有查到要删除的书本!" << endl;
	system("pause");
	system("cls");
	return -1;
}

int LIST::modify1() {
	string a, b;
	reader* temp;
	temp = this->phead;
	cout << "请输入你要修改的用户的帐号:" << endl;
	cin >> a;
	while (temp) {
		if (temp->code == a)
		{
			temp->key = "123456";
		}
		temp = temp->pnext;
	}
	system("pause");
	system("cls");
	return -1;
}

int LIST::modify2() {
	string a, b;
	reader* temp;
	temp = this->phead;
	cout << "请输入您的用户的帐号:" << endl;
	cin >> a;
	cout << "请输入您的原密码:" << endl;
	cin >> b;
	while (temp) {
		if (temp->code == a)
		{
			if (temp->key == b) {
				cout << "请输入您的新密码:" << endl;
				string p;
				cin >> p;
				temp->key = p;
				cout << "修改成功!" << endl;
			}
			else {
				cout << "原密码不一致,请重新输入!" << endl;
			}
		}
		temp = temp->pnext;
	}
	system("pause");
	system("cls");
	return -1;
}

void LIST::displayu() {
	int j = 0;
	int k = 0;
	reader * temp = phead;
	int len = getlen1();
	cout << "图书馆注册用户有" << len << "人" << endl;
	int page = 0;//总书页
	int curpage = 0;//当前页数
	if (len % 10 == 0) page = len / 10;
	else page = len / 10 + 1;
	useru * t1 = new useru[len];
	for (int i = 0; i < len; i++) {
		t1[i].name = temp->code;
		temp = temp->pnext;
	}
	for (j; j <= len; j++) {
		cout << t1[j].name << endl;
		if (j % 10 == 0)
		{
			cout << "总页数:" << page << endl;
			cout << "当前页数:" << curpage << endl;
			curpage++;
			cout << "请在按下任意键后,按1后按回车选择下一页,按2后按回车退出查看图书一览" << endl;
			system("pause");
			int n = 0;
			cin >> n;
			if (n == 1)
			{
				system("cls");
				continue;
			}
			else if (n == 2)
			{
				system("cls");
				break;
			}
		}
	}
	system("cls");

}


void LIST::save1() {
	reader* r;
	ofstream ofs;
	r = this->gethead1();
	ofs.open(READER_FILE, ios::out);
	if (!ofs.is_open())
	{
		cout << "文件不存在!" << endl;
		ofs.close();
		return;
	}
	while (r) {
		ofs << r->code << " " << r->key << endl;
		r = r->pnext;
	}
	ofs.close();
}


void reader::opermenu() {
	cout << "欢迎读者:" << this->code << "登录!" << endl;
	cout << "--------------------------------" << endl;
	cout << "--------  1.修改密码 -----------" << endl;
	cout << "--------  2.借阅书本  ----------" << endl;
	cout << "--------  3.归还书本  ----------" << endl;
	cout << "--------  4.借阅情况  ----------" << endl;
	cout << "--------  5.查找图书  ----------" << endl;
	cout << "--------  6.退出登录  ----------" << endl;
	cout << "--------------------------------" << endl;
	cout << "请选择您的操作" << endl;
}

void reader::changekey() {
	LIST i;
	i.read1();
	i.modify2();
	i.save1();
}

void reader::book_lend() {
	cout << "请选择搜索类型" << endl;
	cout << "1.搜索书名" << endl;
	cout << "2.搜索ISBN号" << endl;
	cout << "请输入您的选择:" << endl;
	int a = 0;
	cin >> a;
	List ii;
	ii.readp();
	list oo;
	oo.read();
	if (a == 1) {
		cout << "请输入书库中存在的书本的书名:" << endl;
		string bn;
		cin >> bn;
		if (ii.find1(bn) != 1 && book_num <= 20) {
			ofstream ofs;
			ofs.open(LENDMESSAGE_FILE, ios::out | ios::app);
			ofs << this->code << " " << bn << " " << oo.getcode(bn) << " " << oo.getauthor(bn) << endl;
			cout << "借阅成功!" << endl;
			book_num++;
			ofs.close();
		}
		else cout << "该书本已被借阅!" << endl;
	}
	else if (a == 2) {
		cout << "请输入书库中存在的书本的ISBN号:" << endl;
		string bc;
		cin >> bc;
		if (ii.find2(bc) != 1 && book_num <= 20) {
			ofstream ofs;
			ofs.open(LENDMESSAGE_FILE, ios::out | ios::app);
			ofs << this->code << " " << oo.getname(bc) << " " << bc << " " << oo.getauthor(bc) << endl;
			cout << "借阅成功!" << endl;
			book_num++;
			ofs.close();
		}
		else cout << "该书本已被借阅!" << endl;
	}
	system("pause");
	system("cls");
}

void reader::book_show() {
	string x = this->code;
	List r;
	r.readp();
	r.displayp(x);
	system("pause");
	system("cls");
}

void reader::book_find() {
	cout << "请输入搜索类型" << endl;
	cout << "1.书名或者ISBN号精确搜索" << endl;
	cout << "2.作者搜索" << endl;
	cout << "3.分类搜索" << endl;
	int select = 0;
	cin >> select;
	if (select == 1) {
		cout << "请输入您要寻找的书名或者ISBN号" << endl;
		string x;
		cin >> x;
		ifstream ifs;
		ifs.open(BOOK_FILE, ios::in);
		if (!ifs.is_open())
		{
			cout << "文件不存在!" << endl;
			ifs.close();
			return;
		}
		string name1, ISBN1, author1, classify1, num;
		while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
			if (x == name1 || x == ISBN1) { cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl; }
		}
		system("pause");
		system("cls");
	}
	else if (select == 2) {
		cout << "请输入作者姓名,如果姓名中带有点的外国作者,请不要输入点" << endl;
		string y;
		cin >> y;
		ifstream ifs;
		ifs.open(BOOK_FILE, ios::in);
		if (!ifs.is_open())
		{
			cout << "文件不存在!" << endl;
			ifs.close();
			return;
		}
		string name1, ISBN1, author1, classify1, num;
		while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
			if (author1.find(y) != author1.npos)
			{
				cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl;
			}
		}
		system("pause");
		system("cls");
	}
	else if (select == 3) {
		cout << "按照中图分类法进行搜索" << endl;
		cout << "1.输入基本大类" << endl;
		cout << "2.输入类目" << endl;
		cout << "3.输入完整分类号" << endl;
		cout << "请输入您的选择" << endl;
		int choice;
		cin >> choice;
		if (choice == 1) {
			cout << "请输入基本大类" << endl;
			string q;
			cin >> q;
			ifstream ifs;
			ifs.open(BOOK_FILE, ios::in);
			if (!ifs.is_open())
			{
				cout << "文件不存在!" << endl;
				ifs.close();
				return;
			}
			string  name1, ISBN1, author1, classify1, num;
			while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
				if (classify1.find(q) != classify1.npos)
				{
					cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl;
				}
			}
			system("pause");
			system("cls");
		}
		else if (choice == 2) {
			cout << "请输入类目" << endl;
			string q;
			cin >> q;
			ifstream ifs;
			ifs.open(BOOK_FILE, ios::in);
			if (!ifs.is_open())
			{
				cout << "文件不存在!" << endl;
				ifs.close();
				return;
			}
			string  name1, ISBN1, author1, classify1, num;
			while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
				if (classify1.find(q) != classify1.npos)
				{
					cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl;
				}
			}
			system("pause");
			system("cls");
		}
		else if (choice == 3) {
			cout << "请输入完整分类号" << endl;
			string q;
			cin >> q;
			ifstream ifs;
			ifs.open(BOOK_FILE, ios::in);
			if (!ifs.is_open())
			{
				cout << "文件不存在!" << endl;
				ifs.close();
				return;
			}
			string  name1, ISBN1, author1, classify1, num;
			while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
				if (classify1.find(q) != classify1.npos)
				{
					cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl;
				}
			}
			system("pause");
			system("cls");
		}
	}
	return;
}

void reader::book_return() {
	cout << "请输入您要归还的书籍的名字或者ISBN号:" << endl;
	string x;
	cin >> x;
	List a;
	a.readp();
	a.delep(x);
	a.savep();
	system("pause");
	system("cls");
}


//子类,管理员类 
class admin :public user {
public:
	~admin() {}
	admin();
	admin(string code1, string key1);
	virtual void opermenu();
	void changekey();
	void recoverkey();
	void addreader();
	void delereader();
	void addbook();
	void delebook();
	void findbook();
	void alterbook();
};

admin::admin() {
}

admin::admin(string code1, string key1) {
	this->code = code1;
	this->key = key1;
}
void admin::changekey() {
	cout << "请输入原密码:" << endl;
	string p;
	cin >> p;
	if (p == this->key) {
		cout << "请输入密码:" << endl;
		string x;
		cin >> x;
		string y;
		y = this->code;
		ofstream ofs;
		ofs.open(ADMIN_FILE, ios::out);
		ofs << y << " " << x << endl;
		ofs.close();
	}
	else {
		cout << "与原密码不一致,请重新输入!" << endl;
	}
	system("pause");
	system("cls");
	return;
}

void admin::opermenu() {
	cout << "欢迎管理员:" << this->code << "登录!" << endl;
	cout << "----------------------------------------" << endl;
	cout << "--------  1.添加用户 -------------------" << endl;
	cout << "--------  2.删除用户  ------------------" << endl;
	cout << "--------  3.恢复用户默认密码  ----------" << endl;
	cout << "--------  4.修改图书信息  --------------" << endl;
	cout << "--------  5.增加图书  ------------------" << endl;
	cout << "--------  6.删除图书  ------------------" << endl;
	cout << "--------  7.查找图书  ------------------" << endl;
	cout << "--------  8.退出登录  ------------------" << endl;
	cout << "--------  9.修改密码  ------------------" << endl;
	cout << "----------------------------------------" << endl;
	cout << "请选择您的操作" << endl;
}


void admin::recoverkey() {
	LIST q;
	q.read1();
	q.modify1();
	q.save1();
}

void admin::addreader() {
	ofstream ofs;
	ofs.open(READER_FILE, ios::out | ios::app);
	string code;
	string key;
	cout << "请输入学号或者教职工编号" << endl;
	cin >> code;
	cout << "请输入密码" << endl;
	cin >> key;
	ofs << "\n" << code << " " << key << " " << endl;
	cout << "添加成功" << endl;
	ofs.close();
	system("pause");
	system("cls");
	return;
}


void admin::delereader() {
	LIST w;
	w.read1();
	w.dele1();
	w.save1();
	system("pause");
	system("cls");
}

void admin::addbook() {
	cout << "请输入书的信息" << endl;
	cout << "请输入书的名字:" << endl;
	string a;
	cin >> a;
	cout << "请输入书的ISBN码:" << endl;
	string b;
	cin >> b;
	cout << "请输入书的作者:" << endl;
	string c;
	cin >> c;
	cout << "请输入书的分类码:" << endl;
	string d;
	cin >> d;
	ofstream ofs;
	ofs.open(BOOK_FILE, ios::out | ios::app);
	ofs << "\n" << a << " " << b << " " << c << " " << d << " " << endl;
	cout << "添加成功" << endl;
	ofs.close();
	system("pause");
	system("cls");
}
void admin::delebook() {
	list l3;
	l3.read();
	l3.dele();
	l3.save();
	system("pause");
	system("cls");
}

void admin::findbook() {
	cout << "请输入搜索类型" << endl;
	cout << "1.书名或者ISBN号精确搜索" << endl;
	cout << "2.作者搜索" << endl;
	cout << "3.分类搜索" << endl;
	int select = 0;
	cin >> select;

	if (select == 1) {
		cout << "请输入您要寻找的书名或者ISBN号:" << endl;
		string x;
		cin >> x;
		ifstream ifs;
		ifs.open(BOOK_FILE, ios::in);
		if (!ifs.is_open())
		{
			cout << "文件不存在!" << endl;
			ifs.close();
			return;
		}
		string name1, ISBN1, author1, classify1, num;
		while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
			if (x == name1 || x == ISBN1) { cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl; }
		}
	}

	else if (select == 2) {
		cout << "请输入作者姓名,如果姓名中带有点的外国作者,请不要输入点" << endl;
		string y;
		cin >> y;
		ifstream ifs;
		ifs.open(BOOK_FILE, ios::in);
		if (!ifs.is_open())
		{
			cout << "文件不存在!" << endl;
			ifs.close();
			return;
		}
		string name1, ISBN1, author1, classify1, num;
		while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
			if (author1.find(y) != author1.npos)
			{
				cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl;
			}
		}
		system("pause");
		system("cls");
	}

	else if (select == 3) {
		cout << "按照中图分类法进行搜索" << endl;
		cout << "1.输入基本大类" << endl;
		cout << "2.输入类目" << endl;
		cout << "3.输入完整分类号" << endl;
		cout << "请输入您的选择" << endl;
		int choice;
		cin >> choice;
		if (choice == 1) {
			cout << "请输入基本大类" << endl;
			string q;
			cin >> q;
			ifstream ifs;
			ifs.open(BOOK_FILE, ios::in);
			if (!ifs.is_open())
			{
				cout << "文件不存在!" << endl;
				ifs.close();
				return;
			}
			string  name1, ISBN1, author1, classify1, num;
			while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
				if (classify1.find(q) != classify1.npos)
				{
					cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl;
				}
			}
			system("pause");
			system("cls");
		}
		else if (choice == 2) {
			cout << "请输入类目:" << endl;
			string q;
			cin >> q;
			ifstream ifs;
			ifs.open(BOOK_FILE, ios::in);
			if (!ifs.is_open())
			{
				cout << "文件不存在!" << endl;
				ifs.close();
				return;
			}
			string  name1, ISBN1, author1, classify1, num;
			while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
				if (classify1.find(q) != classify1.npos)
				{
					cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl;
				}
			}
			system("pause");
			system("cls");
		}
		else if (choice == 3) {
			cout << "请输入完整分类号:" << endl;
			string q;
			cin >> q;
			ifstream ifs;
			ifs.open(BOOK_FILE, ios::in);
			if (!ifs.is_open())
			{
				cout << "文件不存在!" << endl;
				ifs.close();
				return;
			}
			string  name1, ISBN1, author1, classify1, num;
			while (ifs >> name1 && ifs >> ISBN1 && ifs >> author1 && ifs >> classify1 && ifs >> num) {
				if (classify1.find(q) != classify1.npos)
				{
					cout << name1 << " " << ISBN1 << " " << author1 << " " << classify1 << " " << num << endl;
				}
			}
			system("pause");
			system("cls");
		}
	}
	return;
}

void admin::alterbook() {
	list l2;
	l2.read();
	l2.modify();
	l2.save();
}

menu.h头文件
#pragma once
#include <iostream>
#include <cstdlib>
using namespace std;

class menu {
public:
	void showmenu();
	void exitsystem();
};

void menu::showmenu() {
	cout << "---------------------------------" << endl;
	cout << "--------H大学图书管理系统--------" << endl;
	cout << "--------  1.读者登录  -----------" << endl;
	cout << "--------  2.管理员登录 ----------" << endl;
	cout << "--------  3.查看书籍  -----------" << endl;
	cout << "--------  4.用户列表  -----------" << endl;
	cout << "--------  5.借阅一览  -----------" << endl;
	cout << "--------  6.退出系统  -----------" << endl;
	cout << "---------------------------------" << endl;
}

void menu::exitsystem() {
	cout << "欢迎下次使用" << endl;
	system("pause");
	exit(0);
}

book.h
#pragma once
#include <string>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <iostream>
#include "globalfile.h" 
#include <vector>
using namespace std;

struct bookt {
	string name1;
	string author1;
	string code1;
	string classify1;
	string islend1;
};

struct lendmt {
	string u1;
	string bookname1;
	string bookcode1;
	string bookauthor1;
};

class book {
public:
	string u;//用户信息
	string name;
	string author;
	string code;//code为ISSN或ISBN
	string classify;
	string islend;//标记书本是否借出
	book* next;//next指针 
};

class list {
private:
	book* head;
public:
	list();
	~list(){
		delete head;
	}
	book* gethead();
	void display();
	int getlen();
	int modify();
	int dele();
	void read();
	void save();
	string getname(string bn);
	string getcode(string bn);
	string getauthor(string bn);
};

string list::getauthor(string bn) {
	book* ba;
	ba = head;
	while (ba) {
		if (ba->code == bn || ba->name == bn) return ba->author;
		ba = ba->next;
	}
}

string list::getname(string bn) {
	book* b1;
	b1 = head;
	while (b1) {
		if (b1->code == bn) return b1->name;
		b1 = b1->next;
	}
}

string list::getcode(string bn) {
	book* b1;
	b1 = head;
	while (b1) {
		if (b1->name == bn) return b1->code;
		b1 = b1->next;
	}
}


int list::getlen() {
	int n = 0;
	book* b;
	b = head;
	while (b) {
		n++;
		b = b->next;
	}
	return n;
}

void list::display(){
	int j = 0;
	int k = 0;
	book* temp = head;
	int len = getlen();
	cout << "图书馆馆藏书本刊物共:" << len << "本";
	int page = 0;//总书页
	int curpage = 0;//当前页数
	if (len % 10 == 0) page = len / 10;
	else page = len / 10 + 1;
	bookt *t1=new bookt[len];
	for (int i = 0; i < len;i++) {
		t1[i].name1=temp->name ;
		t1[i].code1=temp->code;
		t1[i].author1=temp->author;
		t1[i].classify1=temp->classify;
		t1[i].islend1 = temp->islend;
		temp = temp->next;
	}
		for (j; j<=len; j++) {
			cout << t1[j].name1 << " ";
			cout << t1[j].code1 << " ";
			cout << t1[j].author1 << " ";
			cout << t1[j].classify1 << " ";
			cout << t1[j].islend1 <<endl;
			if (j % 10 == 0)
			{cout << "总页数:" << page << endl;
		     cout << "当前页数:" << curpage << endl;
			 curpage++;
			 cout << "请在按下任意键后,按1后按回车选择下一页,按2后按回车退出查看图书一览" << endl;
			 system("pause");
			 int n = 0;
			 cin >> n;
			 if (n == 1)
			 {
				 system("cls");
				 continue;
			 }
			 else if (n == 2)
			 {
				 system("cls");
			     break;
			 }
			}
		}
	system("cls");
}


list::list() {//链表初始化 
	head = new book();//创建头节点 
	head->next = NULL;
}

book* list::gethead() {//返回头节点 
	return head;
}


void list::read() {
	book* p, * q;
	ifstream ifs;
	p = this->head;
	ifs.open(BOOK_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件不存在!" << endl;
		ifs.close();
		return;
	}
	string name1, code1, author1, fenlei, ben;
	while (ifs >> name1 && ifs >> code1 && ifs >> author1 && ifs >> fenlei && ifs >> ben) {
		q = new book();
		q->name = name1; q->code = code1; q->author = author1; q->classify = fenlei; q->islend = ben;
		p->next = q;
		p = q;
	}
	ifs.close();
}

int list::dele() {
	string n;
	book* temp, * pr;
	temp = head;
	pr = head;
	cout << "请输入要删除的书本名字或者ISBN号:" << endl;
	cin >> n;
	while (temp) {
		if (temp->code == n) {
			if (temp == head) {
				head = head->next;
				return 1;
			}
			pr->next = temp->next;
			cout << "删除成功!" << endl;
			return 1;
		}
		else if (temp->name == n) {
			if (temp == head) {
				head = head->next;
				return 1;
			}
			pr->next = temp->next;
			cout << "删除成功!" << endl;
			return 1;
		}
		pr = temp;
		temp = temp->next;
	}
	cout << "没有查到要删除的书本!" << endl;
	return -1;
}


int list::modify() {
	string a;
	int b;
	book* temp;
	temp = this->gethead();
	cout << "请输入要修改的书本的名字或者ISBN码:" << endl;
	cin >> a;
	while (temp){
		if (temp->code == a) {
			cout << "1.修改书本名字" << endl;
			cout << "2.修改书本作者" << endl;
			cout << "3.修改书本的分类号" << endl;
			cout << "4.修改书本ISBN号" << endl;
			cout << "请输入要修改的项目:" << endl;
			cin >> b;
			switch (b) {
			case 1:
				cout << "请输入修改的名字:" << endl;
				cin >> temp->name;
				cout << "修改成功!" << endl;
				system("pause");
				system("cls");
				return 1;
			case 2:
				cout << "请输入修改的作者:" << endl;
				cin >> temp->author;
				cout << "修改成功!" << endl;
				system("pause");
				system("cls");
				return 1;
			case 3:
				cout << "请输入修改的分类号:" << endl;
				cin >> temp->classify;
				cout << "修改成功!" << endl;
				system("pause");
				system("cls");
				return 1;
			case 4:
				cout << "请输入修改的ISBN号:" << endl;
				cout << "修改成功!" << endl;
				cin >> temp->code;
				system("pause");
				system("cls");
				return 1;
			default:
				cout << "退出修改" << endl;
				system("pause");
				system("cls");
				return 1;
			}
		}
		else if (temp->name==a) {
			cout << "1.修改书本名字" << endl;
			cout << "2.修改书本作者" << endl;
			cout << "3.修改书本的分类号" << endl;
			cout << "4.修改书本ISBN号" << endl;
			cout << "请输入要修改的项目:"<< endl;
			cin >> b;
			switch (b) {
			case 1:
				cout << "请输入修改的名字:" << endl;
				cin >> temp->name;
				cout << "修改成功!" << endl;
				system("pause");
				system("cls");
				return 1;
			case 2:
				cout << "请输入修改的作者:" << endl;
				cin >> temp->author;
				cout << "修改成功!" << endl;
				system("pause");
				system("cls");
				return 1;
			case 3:
				cout << "请输入修改的分类号:" << endl;
				cin >> temp->classify;
				cout << "修改成功!" << endl;
				system("pause");
				system("cls");
				return 1;
			case 4:
				cout << "请输入修改的ISBN号:" << endl;
				cin >> temp->code;
				cout << "修改成功!" << endl;
				system("pause");
				system("cls");
				return 1;
			default:
				cout << "退出修改" << endl;
				system("pause");
				system("cls");
				return 1;
			}
		}temp = temp->next;
	}
	return -1;
}

void list::save(){
	book* b;
	ofstream ofs;
	b = this->gethead();
	ofs.open(BOOK_FILE, ios::out);
	if (!ofs.is_open())
	{
		cout << "文件不存在!" << endl;
		ofs.close();
		return;
	}
	while (b) {
		ofs << b->name << " " << b->code << " " << b->author << " " << b->classify << " " << b->islend << endl;
		b = b->next;
	}
	ofs.close();
}


class lendm{
public:string u;
	  string bookname;
	  string bookcode;
	  string bookauthor;
	  lendm* next;
};

class List {
private:
	lendm* qhead;
public:
	List();
	int find1(string bn);
	int find2(string bn);
	void displayp(string n);
	int getlen1();
	void savep();
	void readp();
	void displaypp();
	int delep(string x);
	lendm* geth() { return qhead; };
};

List::List(){
	qhead = new lendm();
	qhead->next = NULL;
}

int List::getlen1() {
	int n = 0;
	lendm* m;
	m = qhead;
	while (m) {
		n++;
		m = m->next;
	}
	return n;

}


int List::find1(string bn) {
	lendm* b1;
	b1 = qhead;
	while (b1) {
		if (b1->bookname == bn) return 1;
		b1 = b1->next;
	}
	return -1;
}

int List::find2(string bn) {
	lendm* b1;
	b1 = qhead;
	while (b1) {
		if (b1->bookcode == bn) return 1;
		b1 = b1->next;
	}
	return -1;
}

void List::displaypp() {
	int j = 0;
	int len1 = getlen1();
	lendm* temp = qhead;
	int len = getlen1();
	int page = 0;//总书页
	int curpage = 0;//当前页数
	if (len1 % 10 == 0) page = len1 / 10;
	else page = len1 / 10 + 1;
	lendmt* t1 = new lendmt[len1];
	for (int i = 0; i < len1; i++){
	t1[i].u1 = temp->u;
	t1[i].bookname1 = temp->bookname;
	t1[i].bookcode1 = temp->bookcode;
	t1[i].bookauthor1 = temp->bookauthor;
    temp = temp->next;}
		for (j; j <= len1; j++) {
			cout << t1[j].u1 << " ";
			cout << t1[j].bookname1 << " ";
			cout << t1[j].bookcode1 << " ";
			cout << t1[j].bookauthor1 << endl;
			if (j % 10 == 0)
			{
				cout << "总页数:" << page << endl;
				cout << "当前页数:" << curpage << endl;
				curpage++;
				cout << "请在按下任意键后,按1后按回车选择下一页,按2后按回车退出查看借阅一览" << endl;
				system("pause");
				int n = 0;
				cin >> n;
				if (n == 1)
				{
					system("cls");
					continue;
				}
				else if (n == 2)
				{
					system("cls");
					break;
				}
			}
		}
	system("cls");
}

void List::displayp(string x) {
	lendm* temp = this->geth();
	while (temp) {
		if (temp->u == x) {
			cout << temp->bookname << " ";
			cout << temp->bookcode << " ";
			cout << temp->bookauthor << endl;
		}
		temp = temp->next;
	}
	return;
}

void List::savep() {
	lendm* b;
	ofstream ofs;
	b = qhead;
	ofs.open(LENDMESSAGE_FILE, ios::out);
	if (!ofs.is_open())
	{
		cout << "文件不存在!" << endl;
		ofs.close();
		return;
	}
	while (b) {
		ofs << b->u << " " << b->bookname << " " << b->bookcode << " " << b->bookauthor << endl;
		b = b->next;
	}
	ofs.close();
}


int List::delep(string n) {
	lendm* temp, * pr;
	temp = qhead;
	pr = qhead;
	while (temp) {
		if (temp->bookcode == n) {
			if (temp == qhead) {
				qhead = qhead->next;
				return 1;
			}
			pr->next = temp->next;
			cout << "归还成功!" << endl;
			return 1;
		}
		else if (temp->bookname == n) {
			if (temp == qhead) {
				qhead = qhead->next;
				return 1;
			}
			pr->next = temp->next;
			cout << "归还成功!" << endl;
			return 1;
		}
		pr = temp;
		temp = temp->next;
	}
	cout << "没有查到要归还的书本!" << endl;
	system("pause");
	system("cls");
	return -1;
}

void List::readp() {
	lendm* p, * q;
	ifstream ifs;
	p = this->qhead;
	ifs.open(LENDMESSAGE_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件不存在!" << endl;
		ifs.close();
		return;
	}
	string u1, name1, code1, author1;
	while (ifs >> u1 && ifs >> name1 && ifs >> code1 && ifs >> author1) {
		q = new lendm();
		q->u = u1; q->bookname = name1; q->bookcode = code1; q->bookauthor = author1;
		p->next = q;
		p = q;
	}
	ifs.close();

	return;
}

main.cpp
#include <iostream>
#include <fstream>
#include<cstdlib>
#include <cstring>
#include "book.h"
#include "menu.h"
#include "user.h"
#include "globalfile.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void readerMenu(user*& Reader) {
	while (1) {
		Reader->opermenu();
		reader* man = (reader*)Reader;
		int select;
		cin >> select;
		if (select == 1) {
			cout << "修改密码" << endl;
			man->changekey();
		}
		else if (select == 2) {
			cout << "借阅书本" << endl;
			man->book_lend();
		}
		else if (select == 3) {
			cout << "归还书本" << endl;
			man->book_return();
		}
		else if (select == 4) {
			cout << "借阅情况" << endl;
			man->book_show();
		}
		else if (select == 5) {
			cout << "查找书本" << endl;
			man->book_find();
		}
		else if (select == 6) {
			cout << "退出登录" << endl;
			delete Reader;
			system("pause");
			system("cls");
			return;
		}
	}
}

void adminMenu(user*& Admin) {
	while (1) {
		Admin->opermenu();//调用管理员子菜单 
		admin* man = (admin*)Admin;//将父类指针转为子类指针,调用子类的其他接口 
		int select;
		cin >> select;
		if (select == 1) {
			cout << "添加用户" << endl;
			man->addreader();
		}
		else if (select == 2) {
			cout << "删除用户" << endl;
			man->delereader();
		}
		else if (select == 3) {
			cout << "恢复用户默认密码" << endl;
			man->recoverkey();
		}
		else if (select == 4) {
			cout << "修改图书信息" << endl;
			man->alterbook();
		}
		else if (select == 5) {
			cout << "增加图书" << endl;
			man->addbook();
		}
		else if (select == 6) {
			cout << "删除图书" << endl;
			man->delebook();
		}
		else if (select == 7) {
			cout << "查找图书" << endl;
			man->findbook();
		}
		else if (select == 8) {
			cout << "退出登录!" << endl;
			delete Admin;
			system("pause");
			system("cls");
			return;
		}
		else if (select == 9) {
			cout << "修改密码" << endl;
			cout << "请输入你要修改的密码" << endl;
			man->changekey();
		}
	}
}

void login(string filename, int type) {
	user* p = NULL;
	ifstream ifs;
	ifs.open(filename, ios::in);//c++的string不作为open的参数 
	if (!ifs.is_open())
	{
		cout << "文件不存在!" << endl;
		ifs.close();
		return;
	}
	string code;
	string key;
	cout << "请输入您的学生号或者教职工编号" << endl;
	cin >> code;
	cout << "请输入您的密码" << endl;
	cin >> key;
	if (type == 1) {
		string code1;
		string key1;
		while (ifs >> code1 && ifs >> key1) {
			if (code1 == code && key1 == key)
			{
				cout << "尊敬的读者您已登录成功!" << endl;
				system("pause");
				system("cls");
				p = new reader(code, key);
				//读者进入子菜单 
				readerMenu(p);
				return;
			}
		}
	}
	else if (type == 2) {
		string code2;
		string key2;
		while (ifs >> code2 && ifs >> key2) {
			if (code2 == code && key2 == key) {
				cout << "尊敬的管理员您已登录成功" << endl;
				system("pause");
				system("cls");
				p = new admin(code, key);
				//管理员子菜单 
				adminMenu(p);
				return;
			}
		}
	}
	cout << "验证登录失败" << endl;
	system("pause");
	system("cls");
}

int main(int argc, char** argv) {
	menu s1;
	list l1;
	List oi;
	LIST l2;
	int choice = 0;
	while (1) {
		s1.showmenu();
		cout << "请输入你的选择:" << endl;
		cin >> choice;
		switch (choice) {
		case 1:
			login(READER_FILE,1);
			break;
		case 2:
			login(ADMIN_FILE,2);
			break;
		case 3:
			l1.read();
			l1.display();
			break;
		case 4:
			l2.read1();
			l2.displayu();
			break;
		case 5:
			oi.readp();
			oi.displaypp();
			break;
		case 6:
			s1.exitsystem();
			break;
		default:
			system("cls");
			break;
		}
	}
	return 0;
}

主界面
在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值