图书管理系统的c++实现Library2版(3)

喻选
2023-12-01

1.系统的功能如下:

(1)实现用账号和名字进行登录的功能
(2)对于图书管理者,其功能如下:
a.打印图书馆所有书籍目录的功能
b.查看所有图书馆读者信息的功能
c.实现增加图书的功能
d.实现增加读者的功能
e.实现图书馆借还统计的功能
f.实现查阅书籍的功能
g.实现查阅读者信息的功能
h.实现修改图书信息的功能
(3)对于读者,其功能如下:
a.实现打印所有书籍目录的功能
b.实现查看读者个人信息的功能
c.实现借书功能
d.实现还书功能
e.查阅书籍
(4)对于新的读者,还有实现注册的功能

2.系统的实现文件

2.1头文件

#pragma once
#ifndef _LIBRARAY1_H_
#define  _LIBRARAY1_H_
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
/*struct BackBookIfo {//该结构用于存储要还书籍的信息
	int bookId;//书籍检索号
	string Backtime;//还书时间
};*/
struct Manager {//管理员的信息
	int WorkId;//工号
	string Workername;//姓名
};
struct  Reader{
	int id;
	string name;
	int Borrownum;//读者借阅量
	int NeedBacknum;//未还的书籍数量
	//BackBookIfo NeedbookIfo[10];//需要还书的信息
};
struct Book {
	int BookId;//检索号
	string BookName;//书名
	string BookAuthor;//作者
	int TatalBorrow;//总借阅量
	int TatalBack;//现外借量
	int tatalsum;//总馆藏
	int Nowsum;//现库存量
	string location;//馆藏地点
};
class Library{
private:
	int Borrownum;//每月外借量
	int Backnum;//每月还书量
	vector<Reader> readers;
	vector<Book> books;
	vector<Manager> worker;
public:
	Library();
	int GetBorrownum();//每个月的外借量
	int GetBacknum();//每个月的还书量
	void Addbook(Book b);//增加书籍
	void AddReader(Reader r);//增加读者
	void ShowAllReader();//显示所有的读者信息
	void ShowAllBook() ;//显示所有馆藏书籍信息
	void PrintAbook(Book &b);//显示一本书的信息
	void PrintAreader(Reader &r)  ;//显示单个读者的信息
	void SortBookWhId();//按索引号给书籍从小到大排序
	void SortReaderId();//按读者编号给读者从小到大排序
	int FindBook(string name, string author);//用书名和作者找书籍的索引号
	void SearchBook(string name, string author);//按书名和作者名查询书籍信息
	void SearchReader(int id);//按读者编号查询读者信息
	void BorrowBook(string name, string author,int id);//借书
	void BackBook(string name, string author,int id);//还书

	bool SearchWorker(int id, string name);//判断是否为图书馆的管理员
	int TatalReaders();//返回图书馆的读者数目
	void ReviseBook(string  name,string author);//修改图书信息
	bool FindReader(int id, string name);//判断是否为该系统的读者
};
#endif

2.2头文件相关方法的定义文件

#include"Libraray2.h"
//using namespace std;
bool CmpBookId(Book b1, Book b2) {//按书本的检索号升序排序
	return b1.BookId < b2.BookId;
}
bool CmpReaderId(Reader r1, Reader r2) {//按读者检索号升序排序
	return r1.id < r2.id;
}
Library::Library() {
	Borrownum = 0;
	Backnum = 0;
	Reader r = { 0,"nane",0,0 };
	//for (int i = 0; i < 10; i++) {
		//r.NeedbookIfo[i] = { -1,"0000-00-00" };
	//}
	readers.push_back(r);
	Book b = { 0,"none","none",0,0, 0,0,"none"};
	books.push_back(b);
	Manager m = { 0,"德哥" };
	worker.push_back(m);
}
int Library::GetBorrownum() {//每个月的外借量
	return Borrownum;
}
int Library::GetBacknum() {//每个月的还书量
	return Backnum;
}
void Library::Addbook(Book b) {
	
	books.push_back(b);
}
void Library::AddReader(Reader r) {
	
	readers.push_back(r);
}

void Library::PrintAbook(Book &b)  {//显示一本书的信息
	cout.setf(ios::left);//左端对齐
	cout << setw(10) << "检索号" << setw(10) << "书名" << setw(10) << "作者" << setw(10) << "总借阅量" << setw(10) << "未还书量" <<
		setw(10) << "现库存" << setw(10) << "总库存" << setw(10) << "馆藏地" << endl;
	cout << setw(10) << b.BookId;
	cout << setw(10) << b.BookName;
	cout << setw(10) << b.BookAuthor;
	cout << setw(10) << b.TatalBorrow;
	cout << setw(10) << b.TatalBack;
	cout << setw(10) << b.Nowsum;
	cout << setw(10) << b.tatalsum;
	cout << setw(10) << b.location << endl;
}
void Library::PrintAreader(Reader& r) {//显示单个读者的信息
	cout.setf(ios::left);//左端对齐
	cout << setw(10) << "读者编号" << setw(10) << "名字" << setw(10) << "阅读量" << setw(10) << "未还数量" << endl;
	cout << setw(10) << r.id;
	cout << setw(10) << r.name;
	cout << setw(10) << r.Borrownum;
	cout << setw(10) << r.NeedBacknum << endl;
}
void Library::ShowAllReader()  {
	cout.setf(ios::left);//左端对齐
	cout << setw(10) << "读者编号" << setw(10) << "名字" << setw(10) << "阅读量" << setw(10) << "未还数量" << endl;
	for (int i = 1; i < (int)readers.size(); i++) {//读者的下标均从1开始,0的位置为“哨兵”,只做初始化用
		cout << setw(10) << readers[i].id;
		cout << setw(10) << readers[i].name;
		cout << setw(10) << readers[i].Borrownum;
		cout << setw(10) << readers[i].NeedBacknum << endl;
		//PrintAreader(readers[i]);
	}
}
void Library::ShowAllBook()  {//显示所有的读者信息
	cout.setf(ios::left);//左端对齐
	cout<<setw(10)<< "检索号" << setw(10) << "书名" << setw(10) << "作者" << setw(10) << "总借阅量" << setw(10)<< "未还书量" << 
		setw(10) << "现库存" << setw(10)<< "总库存"<< setw(10)<< "馆藏地" << endl;
	for (int i = 1; i < (int)books.size(); i++) {//书的下标均从1开始,0的位置为“哨兵”,只做初始化用
		cout<< setw(10)<<books[i].BookId;
		cout <<setw(10)<< books[i].BookName;
		cout <<setw(10)<< books[i].BookAuthor;
		cout << setw(10)<< books[i].TatalBorrow;
		cout << setw(10)<< books[i].TatalBack;
		cout << setw(10) << books[i].Nowsum;
		cout << setw(10) << books[i].tatalsum;
		cout<< setw(10)<< books[i].location << endl;
		//PrintAbook(books[i]);
	}
}
void Library::SortBookWhId() {//按索引号给书籍从小到大排序
	sort(books.begin(), books.end(), CmpBookId);
}
void Library::SortReaderId() {//按读者编号给读者从小到大排序
	sort(readers.begin(), readers.end(), CmpReaderId);
}
int Library::FindBook(string name, string author) {//用书名和作者找书籍的索引号
	SortBookWhId();//按书籍检索号排序,使书籍的索引号与所在书籍向量的序号一致,即建立Hash表
	int index;
	int flag = 0;//是否找到属的标记
	for (index = 0; index < (int)books.size(); index++) {
		if (books[index].BookName == name && books[index].BookAuthor == author) {
			flag = 1;
			return index;
		}
	}
	if (!flag) {
		cout << "抱歉,找不到你想找的书!~~>-<~~" << endl;
		return 0;
	}
}
void Library::SearchBook(string name, string author) {//按书名和作者名查询书籍信息
	int flag=0;//是否找到属的标志
	for (int i = 1; i < (int)books.size(); i++) {
		if (books[i].BookName == name && books[i].BookAuthor == author) {
			flag = 1;
			PrintAbook(books[i]);
		}
	}
	if (!flag) {
		cout << "抱歉!找不到你想要找的书籍!" << endl;
	}
}
void Library::SearchReader(int id) {//按读者编号查询读者信息
	SortReaderId();
	int flag = 0;
	for (int i = 0; i < (int)readers.size(); i++) {
		flag = 1;
		PrintAreader(readers[id]);
		break;
	}
	if (!flag) {
		cout << "找不到该读者号!请检查是否输入正确!" << endl;
	}
}
void Library::BorrowBook(string name, string author,int id) {//借书
	SortReaderId();
	int bookid = FindBook(name, author);
	if (bookid) {
		//更改书籍信息
		books[bookid].Nowsum--;
		books[bookid].TatalBack++;
		books[bookid].TatalBorrow++;
		//更改读者信息
		readers[id].Borrownum++;
		readers[id].NeedBacknum++;
		//更改图书馆的借阅信息
		Borrownum++;
		cout << "借阅成功^-^!记得按时还书!" << endl;
	}
}
void Library::BackBook(string name, string author,int id) {//还书
	SortReaderId();
	int bookid = FindBook(name, author);//寻找书籍的检索号
	if (bookid) {
		//更改书籍信息
		books[bookid].Nowsum++;
		books[bookid].TatalBack--;
		//更改读者信息
		readers[id].NeedBacknum--;
		//更改图书馆的借阅信息
		Backnum++;
		cout << "还书成功^-^!欢迎再次借阅!" << endl;
	}
}
bool Library::FindReader(int id,string name) {//判断是否为该系统的读者
	int flag = 0;//是否找到读者的标记
	for (int i = 1; i < (int)readers.size(); i++) {
		if (readers[i].id == id&&readers[i].name==name ) {
			flag = 1;
			return true;
		}
	}
	if (!flag) {
		return false;
	}
}
bool Library::SearchWorker(int id, string name) {//判断是否为图书馆的管理员
	int flag = 0;//是否找到管理员的标记
	for (int i = 0; i < (int)worker.size(); i++) {
		if (worker[i].WorkId == id && worker[i].Workername == name) {
			flag = 1;
			return true;
		}
	}
	if (!flag) {
		cout << "抱歉,你不是德哥图书馆的管理员,你不能进入该系统!" << endl;
		system("pause");
		return false;
	}

}
int Library::TatalReaders() {//返回图书馆的读者数目
	return (int)readers.size();
}
void Library::ReviseBook(string  name, string author) {//修改图书信息
	SortBookWhId();//将书本按检索号排序
	int index = FindBook(name, author);
	if (index) {
		int cho;
		do {
			system("cls");
			cout << "-------------------------------------------------------------------------------" << endl;
			cout << "修改图书信息的菜单如下:" << endl;
			cout << "                          1:总借阅量" << endl;
			cout << "                          2:未还书量" << endl;
			cout << "                          3:总馆藏量" << endl;
			cout << "                          4:现馆藏量" << endl;
			cout << "                          5:馆藏地点" << endl;
			cout << "                          6:退出" << endl;
			cout << "---------------------------------------------------------------------------------" << endl;
			cout << "请输入你要修改的选项:";
			cin >> cho;
			switch (cho) {
			case 1: {//修改总还书量
				system("cls");
				int borrownum;
				cout << "请输入新的借阅量:";
				cin >> borrownum;
				books[index].TatalBorrow = borrownum;
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case 2: {//修改总未还书量
				system("cls");
				int backnum;
				cout << "请输入新的未还书量:";
				cin >> backnum;
				books[index].TatalBack = backnum;
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case 3: {//修改总馆藏量
				system("cls");
				int sum;
				cout << "请输入新的总馆藏量:";
				cin >> sum;
				books[index].tatalsum = sum;
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case 4: {//修改现馆藏量
				system("cls");
				int nowsum;
				cout << "请输入新的借阅量:";
				cin >> nowsum;
				books[index].Nowsum = nowsum;
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			case 5: {//修改现馆藏地点
				system("cls");
				string newlocation;
				cout << "请输入新的馆藏地点:";
				cin >> newlocation;
				books[index].location = newlocation;
				cout << "修改成功!" << endl;
				system("pause");
				break;
			}
			}
		} while (cho > 0 && cho < 6);
	}
	else {
		cout << "找不到你要修改的书籍~~<--<~~!请检查输入" << endl;
	}

}

2.3主程序文件

/***********************************************************************************************************************************************
//此版本为Library1的加强版本
//在Library1的基础上,增加了用密码和账号登录、新读者可以注册的功能;并将系统分为管理员系统和读者系统。
//管理员初始的账号和名字分别为0、德哥;若为新的读者,可以选择注册成为德哥图书管理系统的用户或者离开系统。
//同时,增加管理员的数据域,并增加管理员能够修改图书信息的功能
***************************************************************************************************************************************************/
#include"Libraray2.h"
int main() {
	cout.setf(ios::left);
	Library DegeLibrary;
	int cho0;
	do {
		system("cls");
		cout << "----------------------------------------------------------------------------------------------------------" << endl;
		cout << "                                  欢迎来到德哥图书管理系统" << endl;
		cout << "请选择你的登录身份:" << endl;
		cout << "                       1:管理员身份登录" << endl;
		cout << "                       2:读者身份登录" << endl;
		cout << "请输入你的登录选项:" << endl;
		cin >> cho0;
		cout << "-----------------------------------------------------------------------------------------------------------" << endl;
		//管理员登录
		if (cho0 == 1) {//管理员身份登录
			system("cls");
			int workid;
			string workname;
			cout << "-------------------------------------------------------------------------------------------------------" << endl;
			cout << "                                             德哥图书管理系统管理员的登录界面" << endl;
			cout << "请输入你的管理员账号:";
			cin >> workid;
			cout << "请输入你的姓名:";
			cin >> workname;
			cout << "--------------------------------------------------------------------------------------------------------" << endl;

			int cho1;
			if (DegeLibrary.SearchWorker(workid, workname)) {
				do {
					system("cls");
					cout << "----------------------------------------------------------------------------------------------------------------" << endl;
					cout << "                               欢迎来到德哥图书管理系统" << endl;
					cout << "你的登录身份:管理员" << 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 << "请选择你的功能选项:";
					cin >> cho1;
					switch (cho1) {
						//打印所有书籍目录
					case 1: {//打印所有书籍目录
						system("cls");
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						DegeLibrary.ShowAllBook();
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //查看所有读者信息
					case 2: {//查看所有读者信息
						system("cls");
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						DegeLibrary.ShowAllReader();
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;

					}
						  //增加图书
					case 3: {//增加图书
						//Book temp;
						system("cls");
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						Book temp;
						cout << "请输入检索号:";
						cin >> temp.BookId;
						//string name, author;
						//cout << "注意:#作为书名,名字的结束符!!!" << endl;
						cout << "请输入书名:";
						//cin >> name;
						//getline(cin,temp.BookName,'#');//输入#号作为结束输入的终止符
						cin >> temp.BookName;
						cin.clear();
						cin.ignore();
						//system("pause");
						cout << "请输入作者名字:";
						//cin >> author;
						//cin.clear();
						//cin.ignore();
						//getline(cin, temp.BookAuthor,'#');
						cin >> temp.BookAuthor;
						temp.TatalBack = 0;
						temp.TatalBorrow = 0;
						cout << "请输入总库存:";
						cin >> temp.tatalsum;
						temp.Nowsum = temp.tatalsum;
						cout << "请输入馆藏地点:";
						cin >> temp.location;
						DegeLibrary.Addbook(temp);
						cout << "增加图书成功!" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //增加读者
					case 4: {//增加读者
						system("cls");
						Reader temp;
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "请输入读者编号:";
						cin >> temp.id;
						cout << "请输入读者名字:";
						cin >> temp.name;
						cin.clear();
						temp.Borrownum = 0;
						temp.NeedBacknum = 0;
						DegeLibrary.AddReader(temp);
						//getline(cin, name);
						cout << "成功增加读者!^-^!" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //本月借还统计
					case 5: {//本月借还统计
						system("cls");
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "德哥图书馆本月的借阅量为:";
						cout << DegeLibrary.GetBorrownum() << endl;
						cout << "德哥图书馆本月的还书量为:";
						cout << DegeLibrary.GetBacknum() << endl;
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //查阅书籍
					case 6: {//查阅书籍
						system("cls");
						string name, author;
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "请输入你要查找书的书名:";
						cin >> name;
						cout << "请输你要查找书的作者名字:";
						cin >> author;
						DegeLibrary.SearchBook(name, author);
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //查阅读者信息
					case 7: {//查阅读者信息
						system("cls");
						int id;
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "请输入你要找的读者编号:";
						cin >> id;
						DegeLibrary.SearchReader(id);
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //修改图书信息
					case 8: {//修改图书信息
						system("cls");
						string name;
						string author;
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "请输入你要修改的书的名字:";
						cin >> name;
						cout << "请输入你要修改的书的作者名字:";
						cin >> author;
						DegeLibrary.ReviseBook(name, author);
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}

					}

				} while (cho1 < 9 && cho1 >= 1);

			}
		}
		//读者登录
		else if (cho0 == 2) {//读者登录
			system("cls");
			int readerid;
			string readername;
			cout << "----------------------------------------------------------------------------------------------------------------" << endl;
			cout << "                                             德哥图书管理系统读者的登录界面" << endl;
			cout << "请输入你的读者账号:";
			cin >> readerid;
			cout << "请输入你的姓名:";
			cin >> readername;
			cout << "----------------------------------------------------------------------------------------------------------------" << endl;
			//成功登录
			if (DegeLibrary.FindReader(readerid, readername)) {
				int cho2;
				do {
					system("cls");
					cout << "--------------------------------------------------------------------------------------------------------------" << endl;
					cout << "                                               欢迎来的德哥图书管理系统" << endl;
					cout << "你的登录身份:读者" << endl;
					cout << "德哥图书管理系统的读者功能菜单如下:" << endl;
					cout << "1:打印所有书籍目录" << endl;
					cout << "2:查看读者个人信息" << endl;
					cout << "3:借书" << endl;
					cout << "4:还书" << endl;
					cout << "5:查阅书籍" << endl;
					cout << "6:退出" << endl;
					cout << "-------------------------------------------------------------------------------------------------------------" << endl;
					cout << "请选择你的功能选项:";
					cin >> cho2;

					switch (cho2) {
						//打印所有书籍的目录
					case 1: {
						system("cls");
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						DegeLibrary.ShowAllBook();
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //查看读者个人信息
					case 2: {
						system("cls");
						int id;
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "请输入你的读者编号:";
						cin >> id;
						DegeLibrary.SearchReader(id);
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //借书
					case 3: {
						system("cls");
						string name, author;
						int id;
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "请输入你的读者号:";
						cin >> id;
						cout << "请输入你要借的书的书名:";
						cin >> name;
						cout << "请输入你要借书的作者名:";
						cin >> author;
						DegeLibrary.BorrowBook(name, author, id);
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //还书
					case 4: {
						system("cls");
						string name, author;
						int id;
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "请输入你的读者号:";
						cin >> id;
						cout << "请输入你要还的书的书名:";
						cin >> name;
						cout << "请输入你要还书的作者名:";
						cin >> author;
						DegeLibrary.BackBook(name, author, id);
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
						  //查阅书籍
					case 5: {
						system("cls");
						string name, author;
						cout << "                欢迎来到德哥图书管理系统^-^" << endl;
						cout << "--------------------------------------------------------------------" << endl;
						cout << "请输入你要查找书的书名:";
						cin >> name;
						cout << "请输你要查找书的作者名字:";
						cin >> author;
						DegeLibrary.SearchBook(name, author);
						cout << "--------------------------------------------------------------------" << endl;
						system("pause");
						break;
					}
					}
				} while (cho2 < 6 && cho2>0);
			}
			else {
				system("cls");
				int cho21;
				cout << "你还不是德哥图书管理系统的用户,是否注册?" << endl;
				cout << "                                  1.注册" << endl;
				cout << "                                  2.我只想看看" << endl;
				cout << "请输入你的选择:";
				cin >> cho21;
				//注册
				if (cho21 == 1) {
					Reader temp;
					while (1) {
						system("cls");
						cout << "请输入你的读者编号:";
						cin >> temp.id;
						if (temp.id < DegeLibrary.TatalReaders() -1|| temp.id == DegeLibrary.TatalReaders()-1) {
							
							cout << "读者的编号重复!!!" << endl;
							cout << "你的读者编号要大于" << DegeLibrary.TatalReaders()-1 << endl;
							cout << "请重新输入读者编号!!!" << endl;
							system("pause");
							continue;
						}
						else {
							break;
						}
					}
					cout << "请输入你的名字:";
					cin >> temp.name;
					DegeLibrary.AddReader(temp);
					cout << "注册成功!开始你的学习之旅吧!" << endl;
					cout << "若忘记了账号,请及时找图书馆的工作人员!" << endl;
					system("pause");
				}
			}

		}

		else {
			cout << "你选择了错误功能选项!被强制退出德哥图书管理系统!~~~>--<~~~" << endl;
			break;
		}
		
		
}while (cho0 < 3 && cho0>0);

	return 0;
}

 类似资料: