【C++】圖書管理系統(tǒng)(完整版)-創(chuàng)新互聯(lián)

圖書管理系統(tǒng)功能概覽:

成都創(chuàng)新互聯(lián)長(zhǎng)期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為稱多企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),稱多網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
  1. 登錄,注冊(cè)
  2. 學(xué)生,老師借書,查看自己當(dāng)前借書情況,還書。
  3. 管理員增加書,查看當(dāng)前借閱情況,查看當(dāng)前所有借閱人,圖書信息。

代碼概覽:

各個(gè)模塊主要負(fù)責(zé)功能
 COperationManagement.h  負(fù)責(zé)登錄,注冊(cè),管理員,將圖書,初始化操作,將借閱信息等從文件中讀取出來,放入容器中,便于操作,不用一直對(duì)文件進(jìn)行I/O.
 CBook.h  用于對(duì)書抽象,并實(shí)現(xiàn)了>>的重載,便于文件讀入,讀出
 CUser.h  工具人,作為老師,學(xué)生的父類
 CStudent.h  對(duì)學(xué)生進(jìn)行的抽象
 CTeacher.h 對(duì)老師進(jìn)行的抽象
 CAdministrator.h 對(duì)管理的抽象
 CManagementBooks.h  用戶所有的相關(guān)操作的中間執(zhí)行者,有設(shè)計(jì)模式中代理的思想
 源.cpp  界面的的實(shí)現(xiàn)與系統(tǒng)入口

說明:代碼100%完整,如果大家不想CV,也可以私聊我發(fā)你完整代碼,還有就是文件讀入建議大家都直接放在相應(yīng)項(xiàng)目里面,不然就寫絕對(duì)路徑。有問題歡迎大家在評(píng)論區(qū)提問,喜歡就點(diǎn)個(gè)贊咯!

全部代碼與講解:

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<< "注意事項(xiàng)"<< endl;
	cout<< "1.學(xué)生最多共可借三本書,老師最多共可借五本"<>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<< "您是否已有賬號(hào)?(y/n):"<< endl;
	string c;
	cin >>c;
	if (c == "y")
	{cout<< "請(qǐng)登錄:"<< endl;
		om.login(user,t);
		user->showInfo();
	}
	else
	{cout<< "來注冊(cè)一個(gè)吧!"<< endl;
		om.Register(user,t);
	}
	om.userOperation(user, mb);
	delete user;
	return 0;
}

實(shí)現(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ù)讀取錯(cuò)誤"<< endl;
	}
	if (!readFile1.is_open())
	{cout<< "用戶數(shù)據(jù)讀取錯(cuò)誤"<< endl;
	}
	cout<< "請(qǐng)輸入您的學(xué)工號(hào):"<< endl;
	string account, password;
	cin >>account;
	int flag = 0;
	while (!readFile.eof())
	{string act;
		readFile >>act;
		if (act == account)
		{	cout<< "請(qǐng)輸入密碼:"<< 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<< "密碼錯(cuò)誤,請(qǐng)重新登錄"<< endl;
				login(user, t);
			}
		}

	}
	if (!flag)
	{cout<< "學(xué)工號(hào)錯(cuò)誤,請(qǐng)重輸入"<< 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<< "請(qǐng)輸入您的學(xué)工號(hào)作為注冊(cè)賬號(hào):"<< endl;
	cin >>act;
	cout<< "請(qǐng)輸入您的注冊(cè)密碼:"<< endl;
	cin >>pwd;
	writeFile<< endl<< act<< " "<< pwd;
	cout<< "請(qǐng)完善您的相應(yīng)信息:"<< endl;
	string  name, department, gender;
	cout<< "您的姓名:"<< endl;
	cin >>name;
	cout<< "您的性別:"<< endl;
	cin >>gender;
	cout<< "您所在的院系:"<< endl;
	cin >>department;
	writeFile1<while (1)
	{cout<< "請(qǐng)選擇您的操作 1.借書 2.查看自己當(dāng)前借書情況 3.還書 4.退出"<< endl;
		int t;
		cin >>t;
		if (t == 1)
		{	cout<< "當(dāng)前圖書館情況如下:"<< 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<< "暫無此操作,請(qǐng)按提示操作"<< endl;
		}
	}
}

void COperationManagement::adminLogin(CAdministrator& admin)
{ifstream readFile("adminLogin.txt");
	ifstream readFile1("admin.txt");
	cout<< "請(qǐng)輸入您的工號(hào):"<< endl;
	string account, password;
	cin >>account;
	int flag = 0;
	while (!readFile.eof())
	{string act;
		readFile >>act;
		if (act == account)
		{	cout<< "請(qǐng)輸入密碼:"<< 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<< "密碼錯(cuò)誤,請(qǐng)重新登錄"<< endl;
				adminLogin(admin);
			}
		}

	}
	if (!flag)
	{cout<< "學(xué)工號(hào)錯(cuò)誤,請(qǐng)重輸入"<< 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<< "請(qǐng)選擇您的操作 1.增加書 2.查看當(dāng)前借閱情況 3.查看當(dāng)前所有圖書信息情況 4.查看借閱人詳細(xì)信息 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<< "請(qǐng)輸入您要查看借閱人的學(xué)工號(hào):"<< endl;
			cin >>id;
			mb.viewBorrowerDetails(id);
		}
		else if (t == 5)
		{	cout<< "退出成功"<< endl;
			break;
		}
		else
		{	cout<< "暫無此操作,請(qǐng)按提示操作"<< endl;
		}
	}
}

登錄效果:
在這里插入圖片描述其余功能大家可以自行測(cè)試。

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<< "請(qǐng)輸入您要?dú)w還的書名:"<< 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<< "請(qǐng)輸入您要借的書名:"<< 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é)號(hào):"<< 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<< " "<< "學(xué)號(hào):"<< m_id<< " "<< "性別:"<< m_gender<< " "<< "院系:"<< m_department<< " "<<"班級(jí):"<int all = mb.checkBorrowedBook(m_id);
    if (all< 3)
    {string name;
        cout<< "請(qǐng)輸入您要借的書名:"<< 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; 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<< "此書數(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ù)讀取出錯(cuò)"<< 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<< "借閱人學(xué)工號(hào):"<< map.first<<" "<< "借閱書名:"<< map.second<< endl;
	}
}

void CManagementBooks::initBooks()
{ifstream readFile;
	readFile.open("library.txt");
	if (!readFile.is_open())
	{cout<< "圖書數(shù)據(jù)讀取錯(cuò)誤"<< 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ù)讀取錯(cuò)誤"<< 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(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號(hào)碼:"<< m_isbn<< " "<<"書本名稱:"<< m_name<< " "
		<<"總頁數(shù):"<< m_page<< " "<<"出版社:"<< m_pressInfo<< " "<<"價(jià)格:"<< m_price
		<< " "<<"剩余本數(shù):"<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<< "當(dāng)前圖書館情況如下"<< endl;
	mb.showAllBooksInfo();
	cout<< "請(qǐng)輸入您要增加的圖書信息:"<< endl;
	string name, author;
	double price;
	int num;
	cout<< "請(qǐng)輸入增加的圖書書名:"<< endl;
	cin >>name;
	cout<< "請(qǐng)輸入增加的圖書的作者:"<< endl;
	cin >>author;
	cout<< "請(qǐng)輸入增加的圖書價(jià)格:"<< endl;
	cin >>price;
	cout<< "請(qǐng)輸入增加的圖書的本書:"<< 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<< " "<< "工號(hào):"<< 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;
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

分享名稱:【C++】圖書管理系統(tǒng)(完整版)-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://muchs.cn/article44/ceoohe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、企業(yè)網(wǎng)站制作、電子商務(wù)、云服務(wù)器移動(dòng)網(wǎng)站建設(shè)、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化