图书管理系统简易版C++

学习c++中,练手用
图书管理系统简易版(C++) 一、开发背景 在信息技术发展迅速的大背景下,采用图书管理系统对图书企业的经营运作进行全程管理,提高企业的管理效率,降低企业的管理成本,增加经济效益。
二、需求分析 1、能够对图书信息进行集中管理;
2、能够大大提高管理人员的工作效率;
3、能够对图书的部分信息进行查询;
三、系统设计 能够实现图书的增、删、查三个功能,‘改’功能仍在设计中。
系统预览:
图书管理系统简易版C++
文章图片

该系统共分为添加新书、浏览全部、删除图书三个模块来编写,首先我们需要创建一个CBook类来实现图书信息的写入、删除、浏览,CBook类中包含m_cName、m_cIsbn、m_cPrice、m_cAuthor四个成员变量分别代表图书的书名、ISBN号、价格、作者,另外该类中还有一些设置属性和获取属性的成员函数,CBook类设计图如下:
图书管理系统简易版C++
文章图片

CBook.h

#pragma once #define NUM1 128 #define NUM2 30 class CBook { public: CBook(){} CBook(char* cName, char* cIsbn, char* cPrice, char* cAuthor); ~CBook(){} public: char* getName(); char* getIsbn(); char* getPrice(); char* getAuthor(); void setName(char* cName); void setIsbn(char* cIsbn); void setPrice(char* cPrice); 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]; };

CBook,cpp
#include "Book.h" #include #include #include using namespace std; CBook::CBook(char* cName, char* cIsbn, char* cPrice, char* cAuthor) { strncpy_s(m_cName, sizeof(m_cName), cName, NUM1); strncpy_s(m_cIsbn, sizeof(m_cIsbn), cIsbn, NUM1); strncpy_s(m_cPrice, sizeof(m_cPrice), cPrice, NUM2); strncpy_s(m_cAuthor, sizeof(m_cAuthor), cAuthor, NUM2); }char* CBook::getName(){ return m_cName; } char* CBook::getIsbn() { return m_cIsbn; } char* CBook::getPrice() { return m_cPrice; } char* CBook::getAuthor() { return m_cAuthor; } void CBook::setName(char* cName) { strncpy_s(m_cName, sizeof(m_cName), cName, NUM1); } void CBook::setIsbn(char* cIsbn) { strncpy_s(m_cIsbn, sizeof(m_cIsbn), cIsbn, NUM1); } void CBook::setPrice(char* cPrice) { strncpy_s(m_cPrice, sizeof(m_cPrice), cPrice, NUM2); } void CBook::setAuthor(char* cAuthor) { strncpy_s(m_cAuthor, sizeof(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::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 < (iDataCount - iCount); j++) { memset(cTempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2); file.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2); tmpfile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2); } file.close(); tmpfile.seekg(0, ios::beg); ofile.open("book.dat"); ofile.seekp((iCount - 1) * (NUM1 + NUM1 + NUM2 + NUM2), ios::beg); for (int i = 0; i < (iDataCount - iCount); i++) { memset(cTempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2); tmpfile.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2); ofile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2); } } tmpfile.close(); ofile.close(); remove("temp.dat"); } 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, sizeof(m_cName), cName, NUM1); ifile.read(cIsbn, NUM1); if (ifile.tellg() > 0) strncpy_s(m_cIsbn, sizeof(m_cIsbn), cIsbn, NUM1); ifile.read(cPrice, NUM2); if (ifile.tellg() > 0) strncpy_s(m_cPrice, sizeof(m_cPrice), cPrice, NUM2); ifile.read(cAuthor, NUM2); if (ifile.tellg() > 0) strncpy_s(m_cAuthor, sizeof(m_cAuthor), cAuthor, NUM2); } catch (...) { throw "file error occurred"; ifile.close(); } ifile.close(); }

CBook类设计完毕,系统基本就成型了,接下来设计主窗口和增删查实现代码:
Main.cpp
#include "Book.h" #include #include #include #include #include #define CMD_COLS 80 #define CMD_LINES 25 using namespace std; void setScreenGrid(); //设置屏幕显示的行数和列数 void clearScreen(); //清屏 void setSysCaption(const char* pText); //设置窗体标题栏 void showWelcome(); //显示欢迎信息 void showRootMenu(); //显示开始菜单 void waitView(int iCurPage); //浏览数据时等待数据操作 void waitUser(); //等待用户操作 void guideInput(); //使用向导添加图书信息 int getSelect(); //获得用户菜单选择 long getFileLength(ifstream& ifs); //获取文件长度 void viewData(int iSelPage); //浏览所有图书记录 void deleteBookFromFile(); //在文件中产生图书信息 void mainloop(); //主循环int main() { mainloop(); }void setScreenGrid() //设置屏幕显示的行数和列数 { char sysSetBuf[80]; sprintf_s(sysSetBuf, sizeof(sysSetBuf),"mode con cols=%d lines=%d", CMD_COLS, CMD_LINES); system(sysSetBuf); } void clearScreen() //清屏 { system("cls"); } void setSysCaption(const char* pText) //设置窗体标题栏 { system("title sample"); } void showWelcome() //显示欢迎信息 { for (int i = 0; i < 9; i++) { cout << endl; } cout << setw(66) << "--------------" << endl; cout << setw(66) << "图书馆管理系统" << endl; cout << setw(66) << "--------------" << endl; } void showRootMenu() //显示开始菜单 { cout << setw(64) << "请选择功能" << endl << endl; cout << setw(64) << "1 添加新书" << endl << endl; cout << setw(64) << "2 浏览全部" << endl << endl; cout << setw(64) << "3 删除图书" << endl; } void waitView(int iCurPage) //浏览数据时等待数据操作 { char a; cout << "m 显示上一页 n 显示下一页 q 返回主菜單" << endl; cin >> a; if (a == 'm') { if (iCurPage == 1) viewData(iCurPage); else viewData(iCurPage - 1); } else if (a == 'n') { viewData(iCurPage); } else if (a == 'q') { system("exit"); } else system("exit"); } void waitUser() //等待用户操作 { int iInputPage = 0; cout << "q返回主菜單" << endl; char buf[256]; cin >> buf; if (buf[0] == 'q') { system("exit"); } else system("exit"); } void guideInput() //使用向导添加图书信息 { char inName[NUM1]; char inIsbn[NUM1]; char inPrice[NUM2]; char inAuthor[NUM2]; cout << "输入书名: "; cin >> inName; cout << "输入ISBN: "; cin >> inIsbn; cout << "输入价格: "; cin >> inPrice; cout << "输入作者: "; cin >> inAuthor; CBook book(inName, inIsbn, inPrice, inAuthor); book.writeData(); cout << "Write Finish!" << endl; waitUser(); } int getSelect() //获得用户菜单选择 { char buf[256]; cin >> buf; return atoi(buf); } long getFileLength(ifstream& ifs) //获取文件长度 { long tmppos; long respos; tmppos = ifs.tellg(); ifs.seekg(0, ios::end); respos = ifs.tellg(); ifs.seekg(tmppos, ios::beg); return respos; } void viewData(int iSelPage) //浏览所有图书记录 { int iPage = 0; int iCurPage = 0; int iDataCount = 0; char inName[NUM1]; char inIsbn[NUM1]; char inPrice[NUM2]; char inAuthor[NUM2]; bool bIndex = false; int iFileLength; iCurPage = iSelPage; ifstream ifile; ifile.open("book.dat", ios::binary); iFileLength = getFileLength(ifile); iDataCount = iFileLength / (NUM1 + NUM1 + NUM2 + NUM2); if (iDataCount > 0) { bIndex = true; } iPage = iDataCount / 20 + 1; clearScreen(); cout << "共有图书" << " " << iDataCount << " "; cout << "共有页数" << " " << iPage << " "; cout << "当前页数" << " " << iCurPage << " " << endl; cout << setw(5) << "Index"; cout << setw(22) << "Name"; cout << setw(22) << "Isbn"; cout << setw(20) << "Price"; cout << setw(25) << "Author" << endl; try { ifile.seekg((iCurPage - 1) * 20 * (NUM1 + NUM1 + NUM2 + NUM2), ios::beg); if (!ifile.fail()) { for (int i = 1; i < 21; i++) { memset(inName, 0, 128); memset(inIsbn, 0, 128); memset(inPrice, 0, 30); memset(inAuthor, 0, 30); if (bIndex) cout << setw(3) << (iCurPage - 1) * 20 + i; ifile.read(inName, NUM1); cout << setw(26) << inName; ifile.read(inIsbn, NUM1); cout << setw(26) << inIsbn; ifile.read(inPrice, NUM2); cout << setw(14) << inPrice; ifile.read(inAuthor, NUM2); cout << setw(25) << inAuthor << endl; if (ifile.tellg() < 0) bIndex = false; else bIndex = true; } } } catch (...) { cout << "throw file exception" << endl; throw "file error occurred"; ifile.close(); } if (iCurPage < iPage) { iCurPage++; waitView(iCurPage); } else { waitView(iCurPage); } ifile.close(); } void deleteBookFromFile() //在文件中产生图书信息 { int iDelCount; cout << "input delete index" << endl; cin >> iDelCount; CBook tmpbook; tmpbook.deleteData(iDelCount); cout << "Delete Finish" << endl; waitUser(); } void mainloop() //主循环 { showWelcome(); while (1) { clearScreen(); showWelcome(); showRootMenu(); switch (getSelect()) { case 1: clearScreen(); guideInput(); break; case 2: clearScreen(); viewData(1); break; case 3: clearScreen(); deleteBookFromFile(); break; } } }

【图书管理系统简易版C++】系统最终演示‘浏览全部’:
图书管理系统简易版C++
文章图片

————————————————————分割线——————————————————————
系统仍在改进,博客持续更新。

    推荐阅读