How to realize the Book Management system in the Development of C++ Project
This article "C++ project development how to achieve library management system" article knowledge point most people do not understand, so Xiaobian to you summarized the following content, detailed content, clear steps, has a certain reference value, I hope you can read this article to gain, let's take a look at this article "C++ project development how to achieve library management system" article bar.
I. Demand analysis
1. You can add a new book information (book name, book number, book price, book author)
2. You can view all book entries.
3. You can delete a specified book record
II. System design
2.1 Introduction to system functions
1. Add a new book module: This module can realize the new book information into the system and save the book information to the file.
2. Browse all books module: you can get all the book information in the file through this module, determine whether the book exists, and facilitate deletion.
3. Delete books module: you can delete a book record according to the record number of the book in the file.
2.2 System Preview
main interface
Add a new book interface
浏览全部图书条目
三、代码设计
3.1 图书头文件
#define NUM1 128#define NUM2 50class CBook{ public: CBook(){} CBook(char* cName,char*cIsbn,char* cPrice,char* cAuthor); ~CBook(){}public: char* GetName();//获取图书名称 void SetName(char* cName);//设置图书名称 char* GetIsbn();//获取图书ISBN编号 void SetIsbn(char* clsbn);//设置图书ISBN编号 char* GetPrice();//获得图书价格 void SetPrice(char* cPrice);//设置图书价格 char* GetAuthor();//获得图书作者信息 void SetAuthor(char* cAuthor);//设置图书作者信息 void WriteData(); void DeleteData(int iCount); void GetBookFromFile(int iCount);protected: char m_cName[NUM1]; char m_cIsbn[NUM1]; char m_cPrice[NUM2]; char m_cAuthor[NUM2];};
3.2 类中成员函数实现
#include "Book.h"#include #include #include #include #include using namespace std;CBook::CBook(char* cName, char*cIsbn, char* cPrice, char* cAuthor){ strncpy_s(m_cName,cName,NUM1); strncpy_s(m_cIsbn, cIsbn, NUM1); strncpy_s(m_cPrice, cPrice, NUM2); strncpy_s(m_cAuthor, cAuthor, NUM2);} char* CBook::GetName(){ return m_cName;} void CBook::SetName(char* cName){ strncpy_s(m_cName, cName, NUM1);} char* CBook::GetIsbn(){ return m_cIsbn;} void CBook::SetIsbn(char* cIsbn){ strncpy_s(m_cIsbn, cIsbn, NUM1);} char* CBook::GetPrice(){ return m_cPrice;} void CBook::SetPrice(char*cPrice){ strncpy_s(m_cPrice, cPrice, NUM2);} char* CBook::GetAuthor(){ return m_cAuthor;} void CBook::SetAuthor(char* cAuthor){ strncpy_s(m_cAuthor, cAuthor, NUM2);} void CBook::WriteData(){ ofstream ofile; ofile.open("book.dat", ios::binary | ios::app); try { ofile.write(m_cName, NUM1); ofile.write(m_cIsbn, NUM1); ofile.write(m_cPrice, NUM2); ofile.write(m_cAuthor, NUM2); } catch (...) { throw "file error occurred"; ofile.close(); } ofile.close();}void CBook::GetBookFromFile(int iCount){ char cName[NUM1]; char cIsbn[NUM1]; char cPrice[NUM2]; char cAuthor[NUM2]; ifstream ifile; ifile.open("book.dat", ios::binary); try { ifile.seekg(iCount*(NUM1 + NUM1 + NUM2 + NUM2), ios::beg); ifile.read(cName, NUM1); if (ifile.tellg()>0) strncpy_s(m_cName, cName, NUM1); ifile.read(cIsbn, NUM1); if (ifile.tellg()>0) strncpy_s(m_cIsbn, cIsbn, NUM1); ifile.read(cPrice, NUM2); if (ifile.tellg()>0) strncpy_s(m_cIsbn, cIsbn, NUM2); ifile.read(cAuthor, NUM2); if (ifile.tellg()>0) strncpy_s(m_cAuthor, cAuthor, NUM2); } catch (...) { throw "file error occurred"; ifile.close(); } ifile.close();}void CBook::DeleteData(int iCount){ long respos; int iDataCount = 0; fstream file; fstream tmpfile; ofstream ofile; char cTempBuf[NUM1 + NUM1 + NUM2 + NUM2]; file.open("book.dat", ios::binary | ios::in | ios::out); tmpfile.open("temp.dat", ios::binary | ios::in | ios::out | ios::trunc); file.seekg(0, ios::end); respos = file.tellg(); iDataCount = respos / (NUM1 + NUM1 + NUM2 + NUM2); if (iCount
< 0 && iCount >iDataCount) { throw "Input number error"; } else { file.seekg((iCount)*(NUM1 + NUM1 + NUM2 + NUM2), ios::beg); for (int j = 0; j