C++詳細實現(xiàn)完整圖書管理功能
圖書管理系統(tǒng)功能概覽:
- 登錄,注冊
- 學(xué)生,老師借書,查看自己當前借書情況,還書。
- 管理員增加書,查看當前借閱情況,查看當前所有借閱人,圖書信息。
代碼概覽:
各個模塊主要負責(zé)功能
COperationManagement.h 負責(zé)登錄,注冊,管理員,將圖書,初始化操作,將借閱信息等從文件中讀取出來,放入容器中,便于操作,不用一直對文件進行I/O.
CBook.h 用于對書抽象,并實現(xiàn)了>>的重載,便于文件讀入,讀出
CUser.h 工具人,作為老師,學(xué)生的父類
CStudent.h 對學(xué)生進行的抽象
CTeacher.h 對老師進行的抽象
CAdministrator.h 對管理的抽象
CManagementBooks.h 用戶所有的相關(guān)操作的中間執(zhí)行者,有設(shè)計模式中代理的思想
源.cpp 界面的的實現(xiàn)與系統(tǒng)入口
說明:代碼100%完整,如果大家不想CV,也可以私聊我發(fā)你完整代碼,還有就是文件讀入建議大家都直接放在相應(yīng)項目里面,不然就寫絕對路徑。有問題歡迎大家在評論區(qū)提問,喜歡就點個贊咯!
全部代碼與講解:
1.源.cpp
#include<iostream> #include"CBook.h" #include"CStudent.h" #include"CManagementBooks.h" #include"CTeacher.h" #include<fstream> #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.學(xué)生最多共可借三本書,老師最多共可借五本"<<endl<<endl; cout << "請選擇您的登錄方式 1:老師 2:學(xué)生 3:管理員" << endl; int t; cin >> 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; }
實現(xiàn)效果:
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 << "登錄數(shù)據(jù)讀取錯誤" << endl; } if (!readFile1.is_open()) { cout << "用戶數(shù)據(jù)讀取錯誤" << endl; } cout << "請輸入您的學(xué)工號:" << 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 << "學(xué)工號錯誤,請重輸入" << 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 << "請輸入您的學(xué)工號作為注冊賬號:" << endl; cin >> act; cout << "請輸入您的注冊密碼:" << endl; cin >> pwd; writeFile << endl << act << " " << pwd; cout << "請完善您的相應(yīng)信息:" << endl; string name, department, gender; cout << "您的姓名:" << endl; cin >> name; cout << "您的性別:" << endl; cin >> gender; cout << "您所在的院系:" << endl; cin >> department; writeFile1 <<endl << act << " " << name << " " << gender << " " << department;//這里不能用user,因為在登錄時才用,并且并沒有初始化 writeFile.close(); writeFile1.close(); cout << "注冊成功! 趕緊登錄進入圖書管吧!" << endl; login(user, t); } void COperationManagement::userOperation(CUser* user, CManagementBooks mb) { 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 << "學(xué)工號錯誤,請重輸入" << 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 << "請輸入您要查看借閱人的學(xué)工號:" << endl; cin >> id; mb.viewBorrowerDetails(id); } else if (t == 5) { cout << "退出成功" << endl; break; } else { cout << "暫無此操作,請按提示操作" << endl; } } }
登錄效果:
其余功能大家可以自行測試。
CUser.cpp
#include "CUser.h" #include<iostream> #include<fstream> #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" #include<fstream> CTeacher::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 << "您已經(jīng)超過了最大可借閱數(shù)" << endl; } } void CTeacher::showInfo() { cout << "姓名:" << m_name<<" " << "學(xué)號:" << m_id<<" " << "性別:" << m_gender<<" " << "院系:" << m_department << endl; }
CStudent.cpp
#include "CStudent.h" #include<fstream> using namespace std; CStudent::CStudent() { m_class = "軟件"; } void CStudent::showInfo() { cout << "姓名:" << m_name << " " << "學(xué)號:" << m_id << " " << "性別:" << m_gender << " " << "院系:" << m_department << " "<<"班級:"<<m_class<<endl; } void CStudent::borrowBookFromLibrary(CManagementBooks& mb) { 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 << "您已經(jīng)超過了最大可借閱數(shù)" << 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() <<" " << "剩余數(shù)量" << 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; i <m_allBookNum; i++) { if (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 << "此書數(shù)量不足" << 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 << "查看全體借書情況數(shù)據(jù)讀取出錯" << endl; } else { while (!readFile.eof()) { string studentId, bookName; readFile >> studentId >> bookName; m_outBookMap.insert(pair<string, string>(studentId, bookName)); } } readFile.close(); } void CManagementBooks::checekOutBook() { for (auto map : m_outBookMap) { cout << "借閱人學(xué)工號:" << map.first<<" " << "借閱書名:" << map.second << endl; } } void CManagementBooks::initBooks() { ifstream readFile; readFile.open("library.txt"); if (!readFile.is_open()) { cout << "圖書數(shù)據(jù)讀取錯誤" << 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 << "您已經(jīng)借的全部圖書如下:" << 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 << "用戶數(shù)據(jù)讀取錯誤" << endl; } while (!readFile1.eof()) { string act1, name, department, gender; readFile1 >> act1 >> name >> gender >> department; if (id == act1) { cout<<"用戶類別:"<<"學(xué)生"<<" " << "用戶姓名:" << 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<string, string>(userId, bookName)); }
CBook.cpp
#include "CBook.h" #include <iostream> #include<fstream> using 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 << " " <<"總頁數(shù):" << m_page << " " <<"出版社:" << m_pressInfo << " " <<"價格:" << m_price << " " <<"剩余本數(shù):"<<m_num<< endl; } std::ostream& operator <<(std::ostream& os, const CBook& book) { os << endl <<book.m_name << " " <<book.m_isbn << " " << book.m_pressInfo << " " <<book.m_price << " " << book.m_page << " " << book.m_author << " " << book.m_num; return os; } std::istream& operator>>(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 <<endl; } std::istream& operator>>(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; }
到此這篇關(guān)于C++詳細實現(xiàn)完整圖書管理功能的文章就介紹到這了,更多相關(guān)C++圖書管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT連接SQLServer數(shù)據(jù)庫的實現(xiàn)
要使用Qt連接SQL Server數(shù)據(jù)庫,需要使用Qt提供的SQL模塊和SQL Server驅(qū)動程序,具有一定的參考價值,感興趣的可以了解一下2023-09-09c++中將二維數(shù)組元素變換為逆向存放的實現(xiàn)代碼
編程將一個二維數(shù)組元素變換為逆向存放,即按元素在內(nèi)存中的物理排列位置,第一個元素變成倒數(shù)第一個元素,第二個元素變成倒數(shù)第二個元素,依此類推2020-11-11C++ OpenCV實戰(zhàn)之網(wǎng)孔檢測的實現(xiàn)
這篇文章主要介紹了如何利用C++和OpenCV實現(xiàn)網(wǎng)孔檢測,文中的示例代碼講解詳細,對我們學(xué)習(xí)OpenCV有一定幫助,感興趣的小伙伴可以了解一下2022-05-05C++ 虛函數(shù)和純虛函數(shù)的區(qū)別分析
這篇文章主要介紹了C++ 虛函數(shù)和純虛函數(shù)的區(qū)別,幫助大家更好的理解和學(xué)習(xí)c++的相關(guān)知識,感興趣的朋友可以了解下2020-10-10C++聚合體初始化aggregate initialization詳細介紹
這篇文章主要介紹了C++聚合體初始化aggregate initialization,C++有很多初始化對象的方法。其中之一叫做 聚合體初始化(aggregate initialization) ,這是聚合體專有的一種初始化方法2023-02-02C語言 詳細講解數(shù)組參數(shù)與指針參數(shù)
這篇文章主要介紹了C語言中數(shù)組參數(shù)與指針參數(shù)的分析,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04