C++项目开发实现图书管理系统
本文实例为大家分享了C++实现图书管理系统的具体代码,供大家参考,具体内容如下
一、需求分析
1.可以实现添加一条新的图书信息(图书名,图书编号,图书价格,图书作者)
2.可以查看全部图书条目
3.可以删除指定的某条图书记录
二、系统设计
2.1系统功能介绍
1.添加新图书模块:该模块可以实现将新图书信息录入到系统并将图书信息保存到文件中。
2.浏览全部图书模块:可以通过该模块获取文件中全部图书信息,确定图书是否存在,及方便删除。
3.删除图书模块:可以根据图书在文件中的记录号删除某条图书记录。
2.2系统预览
主界面
文章图片
添加新图书界面
文章图片
浏览全部图书条目
文章图片
三、代码设计
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<(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"); }
3.3主函数代码
#include#include #include #include #include #include #include "Book.h" #define CMD_COLS 80#define CMD_LINES 25using namespace std; void SetScreenGrid(); void ClearScreen(); void SetSysCaption(); void SetSysCaption(const char *pText); void ShowWelcome(); void ShowRootMenu(); void WaitView(intiCurPage); void WaitUser(); void GuideInput(); int GetSelect(); long GetFileLength(ifstream & ifs); void ViewData(int iSelPage); void DeleteBookFromFile(); void mainloop(); void SetScreenGrid(){char sysSetBuf[80]; sprintf_s(sysSetBuf, "mode con cols=%d lines=%d", CMD_COLS, CMD_LINES); system(sysSetBuf); }void ClearScreen(){system("cls"); }void SetSysCaption(){system("title Sample"); }void SetSysCaption(const char *pText){char sysSetBuf[80]; sprintf_s(sysSetBuf, "title %s", pText); system(sysSetBuf); }void ShowWelcome(){for (int i = 0; i<7; i++){cout << endl; }cout << setw(40); cout << "**************" << endl; cout << setw(40); cout << "*图书管理系统*" << endl; cout << setw(40); cout << "**************" << endl; }void ShowRootMenu(){cout << setw(40); cout << "请选择功能:" << endl; cout << endl; cout << setw(38); cout << "1 添加新书" << endl; cout << endl; cout << setw(38); cout << "2 浏览全部" << endl; cout << endl; cout << setw(38); cout << "3 删除图书" << endl; } void WaitView(intiCurPage){char buf[256]; gets_s(buf); if (buf[0] == 'q')system("exit"); if (buf[0] == 'm')mainloop(); if (buf[0] == 'n')ViewData(iCurPage); }void WaitUser(){int iInputPage = 0; cout << "enter返回主菜单,q退出" << endl; char buf[256]; gets_s(buf); if (buf[0] == 'q')system("exit"); }void GuideInput(){char inName[NUM1]; char inIsdn[NUM1]; char inPrice[NUM2]; char inAuthor[NUM2]; cout << "输入书名" << endl; cin >> inName; cout << "输入ISDN" << endl; cin >> inIsdn; cout << "输入价格" << endl; cin >> inPrice; cout << "输入作者" << endl; cin >> inAuthor; CBook book(inName, inIsdn, inPrice, inAuthor); book.WriteData(); cout << "Write Finish" << endl; WaitUser(); } int GetSelect(){char buf[256]; gets_s(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 = 1){int iPage = 0; int iCurPage = 0; int iDataCount = 0; char inName[NUM1]; char inIsbn[NUM1]; char price[NUM2]; char inAuthor[NUM2]; bool bIndex = false; int iFileLength; iCurPage = iSelPage; ifstream ifile; ifile.open("book.dat", ios::binary); //|ios::nocreateiFileLength = GetFileLength(ifile); iDataCount = iFileLength / (NUM1 + NUM1 + NUM2 + NUM2); if (iDataCount >= 1)bIndex = true; iPage = iDataCount / 20 + 1; //每页20条记录 ClearScreen(); cout << " 共有记录" << iDataCount << " "; cout << " 共有页数" << iPage << " "; cout << " 当前页数" << iCurPage << " "; cout << " n显示下一页 m返回" << endl; cout << setw(5) << "Index"; cout << setw(22) << "Name" << setw(22) << "Isbn"; cout << setw(15) << "Price" << setw(15) << "Author"; cout << 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(price, 0, 50); memset(inAuthor, 0, 50); if (bIndex)cout << setw(3) << ((iCurPage - 1) * 20 + i); ifile.read(inName, NUM1); cout << setw(24) << inName; ifile.read(inIsbn, NUM1); cout << setw(24) << inIsbn; ifile.read(price, NUM2); cout << setw(12) << price; ifile.read(inAuthor, NUM2); cout << setw(12) << inAuthor; cout << endl; //一条纪录if (ifile.tellg()<0)bIndex = false; elsebIndex = true; }}}catch (...){cout << "throw file exception" << endl; throw "file error occurred"; ifile.close(); }if (iCurPage > 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(); break; case 3:ClearScreen(); DeleteBookFromFile(); break; }}}void main(){ SetScreenGrid(); SetSysCaption("图书管理系统"); mainloop(); }
【C++项目开发实现图书管理系统】【注】开发环境为VS2013控制台程序
根据《C++项目开发全程实录》修改
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- 使用Java实现一个JS脚本引擎
- C++基础知识|C++(char型总结一些细节知识点)
- Python基础|快速傅里叶变换及python代码实现
- 问答系统|简单问答系统实现原理 - 基于机器学习的
- 深度学习|【深度学习】经典网络-VGG复现(使用Tensorflow实现)
- github|【GitHubDailyShare】现代机器学习系统的设计原理以及实现
- 前端实现复制功能
- C++|c++中string的模拟实现
- C/C++|C++中简单的string类的实现
- c++|c++的string类的模拟实现 以及basic_string::_M_construct null not valid错误的原因