【C++】图书管理系统(完整版)-创新互联
图书管理系统功能概览:
成都创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为称多企业提供专业的成都做网站、成都网站设计,称多网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。- 登录,注册
- 学生,老师借书,查看自己当前借书情况,还书。
- 管理员增加书,查看当前借阅情况,查看当前所有借阅人,图书信息。
代码概览:
各个模块主要负责功能
COperationManagement.h 负责登录,注册,管理员,将图书,初始化操作,将借阅信息等从文件中读取出来,放入容器中,便于操作,不用一直对文件进行I/O.
CBook.h 用于对书抽象,并实现了>>的重载,便于文件读入,读出
CUser.h 工具人,作为老师,学生的父类
CStudent.h 对学生进行的抽象
CTeacher.h 对老师进行的抽象
CAdministrator.h 对管理的抽象
CManagementBooks.h 用户所有的相关操作的中间执行者,有设计模式中代理的思想
源.cpp 界面的的实现与系统入口
说明:代码100%完整,如果大家不想CV,也可以私聊我发你完整代码,还有就是文件读入建议大家都直接放在相应项目里面,不然就写绝对路径。有问题欢迎大家在评论区提问,喜欢就点个赞咯!
全部代码与讲解:
1.源.cpp
#include#include"CBook.h"
#include"CStudent.h"
#include"CManagementBooks.h"
#include"CTeacher.h"
#include#include"CAdministrator.h"
#include"COperationManagement.h"
using namespace std;
int main()
{CManagementBooks mb;
CUser* user = nullptr;
COperationManagement om;
om.init(mb);
cout<< "***** 欢迎来到图书馆 *****"<< endl;
cout<< "注意事项"<< endl;
cout<< "1.学生最多共可借三本书,老师最多共可借五本"<>t;
if (t == 1)
{user = new CTeacher;
}
else if(t == 2)
{user = new CStudent;
}
else if(t == 3)
{CAdministrator admin;
om.adminLogin(admin);
admin.showInfo();
om.adminOperation(admin, mb);
return 0;
}
cout<< "您是否已有账号?(y/n):"<< endl;
string c;
cin >>c;
if (c == "y")
{cout<< "请登录:"<< endl;
om.login(user,t);
user->showInfo();
}
else
{cout<< "来注册一个吧!"<< endl;
om.Register(user,t);
}
om.userOperation(user, mb);
delete user;
return 0;
}
实现效果:
2.COperationManagement.cpp
#include "COperationManagement.h"
void COperationManagement::login(CUser* user, int t)
{ifstream readFile;
ifstream readFile1;
if (t == 1)
{readFile.open("teacherLogin.txt");
readFile1.open("teacher.txt");
}
else
{readFile1.open("student.txt");
readFile.open("studentLogin.txt");
}
if (!readFile.is_open())
{cout<< "登录数据读取错误"<< endl;
}
if (!readFile1.is_open())
{cout<< "用户数据读取错误"<< endl;
}
cout<< "请输入您的学工号:"<< endl;
string account, password;
cin >>account;
int flag = 0;
while (!readFile.eof())
{string act;
readFile >>act;
if (act == account)
{ cout<< "请输入密码:"<< endl;
cin >>password;
string pwd;
readFile >>pwd;
if (pwd == password)
{ cout<< "登录成功"<< endl;
flag = 1;
while (!readFile1.eof())
{ readFile1 >>user;
if(user->getId() == act)
{break;
}
}
break;
}
else
{ cout<< "密码错误,请重新登录"<< endl;
login(user, t);
}
}
}
if (!flag)
{cout<< "学工号错误,请重输入"<< endl;
login(user, t);
}
readFile.close();
readFile1.close();
}
void COperationManagement::Register(CUser* user, int t)
{ofstream writeFile;
ofstream writeFile1;
if (t == 1)
{writeFile1.open("teacher.txt", ios::app);
writeFile.open("teacherLogin.txt",ios::app);
}
else
{writeFile1.open("student.txt", ios::app);
writeFile.open("studentLogin.txt",ios::app);
}
string pwd, act;
cout<< "请输入您的学工号作为注册账号:"<< endl;
cin >>act;
cout<< "请输入您的注册密码:"<< endl;
cin >>pwd;
writeFile<< endl<< act<< " "<< pwd;
cout<< "请完善您的相应信息:"<< endl;
string name, department, gender;
cout<< "您的姓名:"<< endl;
cin >>name;
cout<< "您的性别:"<< endl;
cin >>gender;
cout<< "您所在的院系:"<< endl;
cin >>department;
writeFile1<while (1)
{cout<< "请选择您的操作 1.借书 2.查看自己当前借书情况 3.还书 4.退出"<< endl;
int t;
cin >>t;
if (t == 1)
{ cout<< "当前图书馆情况如下:"<< endl;
mb.showCurrentAllBook();
cout<< "是否有您想要借阅的图书(y/n)"<< endl;
string s;
cin >>s;
if (s == "y")
{ user->borrowBookFromLibrary(mb);
}
}
else if (t == 2)
{ mb.checkBorrowedBook(user->getId());
}
else if (t == 3)
{ user->returnBook(mb);
}
else if (t == 4)
{ cout<< "退出成功"<< endl;
break;
}
else
{ cout<< "暂无此操作,请按提示操作"<< endl;
}
}
}
void COperationManagement::adminLogin(CAdministrator& admin)
{ifstream readFile("adminLogin.txt");
ifstream readFile1("admin.txt");
cout<< "请输入您的工号:"<< endl;
string account, password;
cin >>account;
int flag = 0;
while (!readFile.eof())
{string act;
readFile >>act;
if (act == account)
{ cout<< "请输入密码:"<< endl;
cin >>password;
string pwd;
readFile >>pwd;
if (pwd == password)
{ cout<< "登录成功,欢迎您"<< endl;
flag = 1;
while (!readFile1.eof())
{ readFile1 >>admin;
if(admin.getId() == act)
{break;
}
}
break;
}
else
{ cout<< "密码错误,请重新登录"<< endl;
adminLogin(admin);
}
}
}
if (!flag)
{cout<< "学工号错误,请重输入"<< endl;
adminLogin(admin);
}
readFile.close();
readFile1.close();
}
void COperationManagement::init(CManagementBooks& mb)
{mb.initBooks();
mb.initOutBook();
}
void COperationManagement::adminOperation(CAdministrator admin, CManagementBooks mb)
{while (1)
{cout<< "请选择您的操作 1.增加书 2.查看当前借阅情况 3.查看当前所有图书信息情况 4.查看借阅人详细信息 5.退出"<< endl;
int t;
cin >>t;
if (t == 1)
{ admin.addBook(mb);
}
else if (t == 2)
{ mb.checekOutBook();
}
else if (t == 3)
{ mb.showAllBooksInfo();
}
else if (t == 4)
{ string id;
cout<< "请输入您要查看借阅人的学工号:"<< endl;
cin >>id;
mb.viewBorrowerDetails(id);
}
else if (t == 5)
{ cout<< "退出成功"<< endl;
break;
}
else
{ cout<< "暂无此操作,请按提示操作"<< endl;
}
}
}
登录效果:
其余功能大家可以自行测试。
CUser.cpp
#include "CUser.h"
#include#include#include"CManagementBooks.h"
using namespace std;
CUser::CUser()
{m_name = "陈1";
}
void CUser::setId(string id)
{m_id = id;
}
void CUser::setName(string name)
{m_name = name;
}
void CUser::setGender(string gender)
{m_gender = gender;
}
void CUser::setDepartment(string department)
{m_department = department;
}
CUser::~CUser()
{}
void CUser::returnBook(CManagementBooks& mb)
{int all = mb.checkBorrowedBook(m_id);
if (all == 0)
{cout<< "您暂未借书,无需归还"<< endl;
}
else
{cout<< "请输入您要归还的书名:"<< endl;
string bookName;
cin >>bookName;
if (mb.checkTrueBorrow(m_id, bookName))
{mb.Return(m_id, bookName);
cout<< "还书成功"<< endl;
}
else
{cout<< "您并未借阅此书"<< endl;
}
}
}
string CUser::getId()
{return m_id;
}
string CUser::getName()
{return m_name;
}
string CUser::getGender()
{return m_gender;
}
string CUser::getDepartment()
{return m_department;
}
std::ostream& operator<<(std::ostream& os, const CUser* user)
{os<< endl<< user->m_id<< " "<< user->m_name<< " "<< user->m_gender<< " "<< user->m_department;
return os;
}
std::istream& operator>>(std::istream& is, CUser* user)
{is >>user->m_id >>user->m_name >>user->m_gender >>user->m_department;
return is;
}
CTeacher.cpp
#include "CTeacher.h"
#includeCTeacher::CTeacher()
{m_name = "刘X";
}
void CTeacher::borrowBookFromLibrary(CManagementBooks& mb)
{int all = mb.checkBorrowedBook(m_id);
if (all< 5)
{string name;
cout<< "请输入您要借的书名:"<< endl;
cin >>name;
if (mb.borrow(name))
{ofstream writeFile("borrowedBook.txt", ios::app);
mb.setMapValue(m_id, name);
writeFile<< endl<< m_id<< " "<< name;
writeFile.close();
}
}
else
{cout<< "您已经超过了大可借阅数"<< endl;
}
}
void CTeacher::showInfo()
{cout<< "姓名:"<< m_name<<" "<< "学号:"<< m_id<<" "<< "性别:"<< m_gender<<" "<< "院系:"<< m_department<< endl;
}
CStudent.cpp
#include "CStudent.h"
#includeusing namespace std;
CStudent::CStudent()
{m_class = "软件";
}
void CStudent::showInfo()
{cout<< "姓名:"<< m_name<< " "<< "学号:"<< m_id<< " "<< "性别:"<< m_gender<< " "<< "院系:"<< m_department<< " "<<"班级:"<int all = mb.checkBorrowedBook(m_id);
if (all< 3)
{string name;
cout<< "请输入您要借的书名:"<< endl;
cin >>name;
if (mb.borrow(name))
{ofstream writeFile("borrowedBook.txt", ios::app);
mb.setMapValue(m_id, name);
writeFile<< endl<< m_id<< " "<< name;
writeFile.close();
}
}
else
{cout<< "您已经超过了大可借阅数"<< endl;
}
}
void CStudent::setClass(string Class)
{m_class = Class;
}
string CStudent::getClass()
{return m_class;
}
CManagementBooks.cpp
#include "CManagementBooks.h"
using namespace std;
void CManagementBooks::showCurrentAllBook()
{for (int i = 0; i< m_allBookNum; i++)
{cout<< "书名:"<< m_books[i].getName()<<" "<< "剩余数量"<< m_books[i].getNum()<< endl;
}
}
CManagementBooks::CManagementBooks()
{m_allBookNum = 0;
}
void CManagementBooks::addBook(CBook book)
{int flag = 0;
for (int i = 0; i< m_allBookNum; i++)
{if (m_books[i].getName() == book.getName())
{ flag = 1;
m_books[i].setNum(m_books[i].getNum() + book.getNum());
ofstream writeFile("library.txt", ios::out);
for (int i = 0; i< m_allBookNum; i++)
{ writeFile<< m_books[i];
}
writeFile.close();
break;
}
}
if (!flag)
{ofstream writeFile("library.txt", ios::app);
m_books.push_back(book);
m_allBookNum++;
writeFile<< book;
writeFile.close();
}
}
int CManagementBooks::getAllBookNum()
{return m_allBookNum;
}
void CManagementBooks::showAllBooksInfo()
{for (int i = 0; i< m_allBookNum; i++)
{m_books[i].showInfo();
}
}
bool CManagementBooks::borrow(string name)
{for (int i =0; iif (m_books[i].getName() == name)
{ if (m_books[i].getNum() >= 1)
{ m_books[i].setNum(m_books[i].getNum() - 1);
cout<< "借书成功"<< endl;
ofstream writeFile("library.txt");
for (int i = 0; i< m_allBookNum; i++)
{writeFile<< m_books[i];
}
writeFile.close();
return true;
}
else
{ cout<< "此书数量不足"<< endl;
return false;
}
}
}
cout<< "很抱歉,暂无此书"<< endl;
return false;
}
void CManagementBooks::Return(string id,string bookName)
{CBook book;
book.setName(bookName);
addBook(book);
ofstream writeFile("borrowedBook.txt",ios::out);
for (auto iter =m_outBookMap.begin(); iter != m_outBookMap.end(); ++iter)
{if (iter->first == id && iter->second == bookName)
{ m_outBookMap.erase(iter);
break;
}
}
for (auto map : m_outBookMap)
{writeFile<< endl<< map.first<< " "<< map.second;
}
writeFile.close();
}
void CManagementBooks::initOutBook()
{ifstream readFile("borrowedBook.txt");
if (!readFile.is_open())
{cout<< "查看全体借书情况数据读取出错"<< endl;
}
else
{while (!readFile.eof())
{ string studentId, bookName;
readFile >>studentId >>bookName;
m_outBookMap.insert(pair(studentId, bookName));
}
}
readFile.close();
}
void CManagementBooks::checekOutBook()
{for (auto map : m_outBookMap)
{cout<< "借阅人学工号:"<< map.first<<" "<< "借阅书名:"<< map.second<< endl;
}
}
void CManagementBooks::initBooks()
{ifstream readFile;
readFile.open("library.txt");
if (!readFile.is_open())
{cout<< "图书数据读取错误"<< endl;
readFile.close();
return;
}
while (!readFile.eof())
{ CBook book;
readFile >>book;
m_allBookNum++;
m_books.push_back(book);
}
readFile.close();
}
int CManagementBooks::checkBorrowedBook(string userId)
{ int flag = 0;
for (auto map : m_outBookMap)
{if (userId == map.first)
{ if (!flag)
{ cout<< "您已经借的全部图书如下:"<< endl;
flag++;
}
else
{ flag++;
}
cout<< map.second<< " ";
}
}
if (!flag)
{cout<< "您目前没有借书"<< endl;
}
cout<< "共:"<< flag<< "本";
cout<< endl;
return flag;
}
void CManagementBooks::viewBorrowerDetails(string id)
{ifstream readFile("teacher.txt");
ifstream readFile1("student.txt");
int flag = 0;
if (!readFile1.is_open()|| !readFile.is_open())
{cout<< "用户数据读取错误"<< endl;
}
while (!readFile1.eof())
{string act1, name, department, gender;
readFile1 >>act1 >>name >>gender >>department;
if (id == act1)
{ cout<<"用户类别:"<<"学生"<<" "<< "用户姓名:"<< name<< " "<< "用户性别:"<< gender<< " "<< "用户所在部门:"<< department<< endl;
flag = 1;
}
}
if (!flag)
{while (!readFile.eof())
{ string act1, name, department, gender;
readFile >>act1 >>name >>gender >>department;
if (id == act1)
{ flag = 1;
cout<< "用户类别:"<<"老师"<<" "<< "用户姓名:"<< name<< " "<< "用户性别:"<< gender<< " "<< "用户所在部门:"<< department<< endl;
}
}
}
if (!flag)
{cout<< "无此用户!"<< endl;
}
readFile.close();
readFile1.close();
}
bool CManagementBooks::checkTrueBorrow(string id, string bookName)
{ for (auto map : m_outBookMap)
{if (map.first == id && map.second == bookName)
{ return true;
}
}
return false;
}
void CManagementBooks::setMapValue(string userId,string bookName)
{m_outBookMap.insert(pair(userId, bookName));
}
CBook.cpp
#include "CBook.h"
#include#includeusing namespace std;
CBook::CBook()
{string b = "";
string randStr = "0123456789X";
for (int i = 0; i<= 12; i++)
{if (i == 1 || i == 5 || i == 11)
{ b += '-';
}
else
{ if (i == 12)
{ b += randStr[rand() % 11];
}
else
{ b += randStr[rand() % 10];
}
}
}
m_num = 1;
m_name = "等待戈多";
m_author = "李XX";
m_isbn = b;
m_page = rand();
m_pressInfo = "XX出版社";
m_price = rand();
}
void CBook::setNum(int num)
{m_num = num;
}
int CBook::getNum()
{return m_num;
}
void CBook::setName(string name)
{m_name = name;
}
string CBook::getName()
{return m_name;
}
void CBook::setIsbn(string isbn)
{m_isbn = isbn;
}
string CBook::getIsbn()
{return m_isbn;
}
void CBook::setPressInfo(string perssInfo)
{m_pressInfo = perssInfo;
}
string CBook::getPressInfo()
{return m_pressInfo;
}
void CBook::setPrice(double price)
{m_price = price;
}
double CBook::getPrice()
{return m_price;
}
void CBook::setPage(int page)
{m_page = page;
}
int CBook::getPage()
{return m_page;
}
void CBook::setAuthor(string author)
{m_author = author;
}
string CBook::getAuthor()
{return m_author;
}
void CBook::checkIsnb()
{int sum = 0;
for (int i = 0, j = 1; i< m_isbn.size(); i++) {if (m_isbn[i] != '-' && i != 12) { sum += (m_isbn[i] - '0') * j;
j++;
}
}
sum %= 11;
char c = 'X';
if (sum< 10) c = sum + '0';
if (m_isbn[12] == c) puts("This book isbn are Right!");
else puts("This book isbn are wrong!");
}
bool CBook::isBorrowed()
{if (m_num >= 1)
{m_num--;
return true;
}
return false;
}
void CBook::showInfo()
{cout<<"作者:"<< m_author<< " "<<"isbn号码:"<< m_isbn<< " "<<"书本名称:"<< m_name<< " "
<<"总页数:"<< m_page<< " "<<"出版社:"<< m_pressInfo<< " "<<"价格:"<< m_price
<< " "<<"剩余本数:"<os<< endl<>(std::istream& is, CBook& book)
{is >>book.m_name >>book.m_isbn >>book.m_pressInfo >>book.m_price >>book.m_page >>book.m_author >>book.m_num;
return is;
}
CAdministrator.cpp
#include "CAdministrator.h"
void CAdministrator::addBook(CManagementBooks& mb)
{CBook book;
cout<< "当前图书馆情况如下"<< endl;
mb.showAllBooksInfo();
cout<< "请输入您要增加的图书信息:"<< endl;
string name, author;
double price;
int num;
cout<< "请输入增加的图书书名:"<< endl;
cin >>name;
cout<< "请输入增加的图书的作者:"<< endl;
cin >>author;
cout<< "请输入增加的图书价格:"<< endl;
cin >>price;
cout<< "请输入增加的图书的本书:"<< endl;
cin >>num;
book.setName(name);
book.setAuthor(author);
book.setNum(num);
book.setPrice(price);
mb.addBook(book);
cout<< "新增图书成功"<< endl;
}
void CAdministrator::setId(string id)
{m_id = id;
}
void CAdministrator::setName(string name)
{m_name = name;
}
void CAdministrator::setGender(string gender)
{m_gender = gender;
}
void CAdministrator::setDepartment(string department)
{m_department = department;
}
void CAdministrator::showInfo()
{cout<< "姓名:"<< m_name<< " "<< "工号:"<< m_id<< " "<< "性别:"<< m_gender<< " "<< "部门:"<< m_department<>(std::istream& is, CAdministrator& admin)
{is >>admin.m_id >>admin.m_name >>admin.m_gender >>admin.m_department;
return is;
}
string CAdministrator::getId()
{return m_id;
}
string CAdministrator::getName()
{return m_name;
}
string CAdministrator::getGender()
{return m_gender;
}
string CAdministrator::getDepartment()
{return m_department;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
文章名称:【C++】图书管理系统(完整版)-创新互联
网页URL:http://myzitong.com/article/ceoohe.html