1.系统的功能如下:
(1)显示图书馆的所有图书的基本信息
(2)显示本馆的所有读者的基本信息
(3)增加新的图书
(4)增加新的读者,
(5)显示图书馆本月借书量与还书量
(6)借书
(7)还书
(8)查询书籍信息
(9)查询读者信息
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 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;
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);//还书
};
#endif
2.2头文件相关方法的定义文件:
#include"Libraray1.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);
}
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;
}
}
2.3主程序文件
/***********************************************************************************************************************************************
//此版本为Library0的加强版本
//增加了借书、还书的功能,读者的信息增加了借阅量、需要还书的数量;书本的信息增加了检索号、总借阅量、未还书量、总馆藏量、现馆藏量、馆藏地点
//此外还增加了书籍、读者排序的功能,按作者和书名查询书籍、按读者编号查询读者信息的功能
***************************************************************************************************************************************************/
#include"Libraray1.h"
int main() {
cout.setf(ios::left);
Library DegeLibrary;
int cho;
do {
system("cls");
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 << "10:退出" << endl;
cout << "--------------------------------------------------------------------" << endl;
cout << "请输入你的功能选项:";
cin >> cho;
switch (cho) {
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;
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 7: {//还书
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 8: {//查阅书籍
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 9: {//查阅学生信息
system("cls");
int id;
cout << " 欢迎来到德哥图书管理系统^-^" << endl;
cout << "--------------------------------------------------------------------" << endl;
cout << "请输入你的读者编号:";
cin >> id;
DegeLibrary.SearchReader(id);
cout << "--------------------------------------------------------------------" << endl;
system("pause");
break;
}
}
} while (cho<10&&cho>=1);
return 0;
}