本文实例为大家分享了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 tname(char* cname);//设置图书名称 char* getisbn();//获取图书isbn编号 void tisbn(char* clsbn);//设置图书isbn编号 char* getprice();//获得图书价格 void tprice(char* cprice);//设置图书价格 char* getauthor();//获得图书作者信息 void tauthor(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 <string>#include <fstream>#include <iostream>#include <iomanip>#include <stdio.h>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::tname(char* cname){ strncpy_s(m_cname, cname, num1);}char* cbook::getisbn(){ return m_cisbn;}void cbook::tisbn(char* cisbn){ strncpy_s(m_cisbn, cisbn, num1);}char* cbook::getprice(){ return m_cprice;}void cbook::tprice(char*cprice){ strncpy_s(m_cprice, cprice, num2);}char* cbook::getauthor(){ return m_cauthor;}void cbook::tauthor(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.clo(); } ofile.clo();}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.ekg(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.clo(); } ifile.clo();}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.ekg(0, ios::end); respos = file.tellg(); idatwoman的复数形式acount = respos / (num1 + num1 + num2 + num2); if (icount < 0 && icount > idatacount) { throw "input number error"; } el { file.ekg((icount)*(num1 + num1 + num2 + num2), ios::beg); for (int j = 0; j<(idatacount - icount); j++) { memt(ctempbuf, 0, num1 + num1 + num2 + num2); file.read(ctempbuf, num1 + num1 + num2 + num2); tmpfile.write(ctempbuf, num1 + num1 + num2 + num2); } file.clo(); tmpfile.ekg(0, ios::beg); ofile.open("book.dat"); ofile.ekp((icount - 1)*(num1 + num1 + num2 + num2), ios::beg); for (int i = 0; i<(idatacount - icount); i++) { memt(ctempbuf, 0, num1 + num1 + num2 + num2); tmpfile.read(ctempbuf, num1 + num1 + num2 + num2); ofile.write(ctempbuf, num1 + num1 + num2 + num2); } } tmpfile.clo(); ofile.clo(); remove("temp.dat");}
3.3主函数代码
#include <iostream>#include <iomanip>#include <stdlib.h>#include <conio.h>#include <string.h>#include <fstream>#include "book.h"#define cmd_cols 80#define cmd_lines 25using namespace std;void tscreengrid();void clearscreen();void tsyscaption();void tsyscaption(const char *ptext);void showwelcome();void showrootmenu();void waitview(int icurpage);void waitur();void guideinput();int getlect();long getfilelength(ifstream & ifs);void viewdata(int ilpage);void deletebookfromfile();void mainloop();void tscreengrid(){ char systbuf[80]; sprintf_s(systbuf, "mode con cols=%d lines=%d", cmd_cols, cmd_lines); system(systbuf);}void clearscreen(){ system("cls");}void tsyscaption(){ system("title sample");}void tsyscaption(const char *ptext){ char systbuf[80]; sprintf_s(systbuf, "title %s", ptext); system(systbuf);}void showwelcome(){ for (int i = 0; i<7; i++) { cout << endl; } cout << tw(40); cout << "**************" << endl; cout << tw(40); cout << "*图书管理系统*" << endl; cout << tw(40); cout << "**************" << endl;}void showrootmenu(){ cout << tw(40); cout << "请选择功能:" << endl; cout << endl; cout << tw(38); cout << "1 添加新书" << endl; cout << endl; cout << tw(38); cout << "2 浏览全部" << endl; cout << endl; cout << tw(38); cout << "3 删除图书" << endl;}void waitview(int icurpage){ char buf[256]; gets_s(buf); if (buf[0] == 'q') system("exit"); if (buf[0] == 'm') mainloop(); if (buf[0] == 'n') viewdata(icurpage);}void waitur(){ 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; waitur();}int getlect(){ char buf[256]; gets_s(buf); return atoi(buf);}long getfilelength(ifstream & ifs){ long tmppos; long respos; tmppos = ifs.tellg();//获得当前位置 ifs.ekg(0, ios::end); respos = ifs.tellg(); ifs.ekg(tmppos, ios::beg);//恢复当前位置 return respos;}void viewdata(int ilpage = 1){ int ipage = 0; int icurpage = 0; int idatacount = 0; char inname[num1]; char inisbn[num1]; char price[num2]; char inauthor[num2]; bool bindex = fal; int ifilelength; icurpage = ilpage; ifstream ifile; ifile.open("book.dat", ios::binary);//|ios::nocreate ifilelength = 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 << tw(5) << "index"; cout << tw(22) << "name" << tw(22) << "isbn"; cout << tw(15) << "price" << tw(15) << "author"; cout << endl; try { ifile.ekg((icurpage - 1) * 20 * (num1 + num1 + num2 + num2), ios::beg); if (!ifile.fail()) { for (int i = 1; i<21; i++) { memt(inname, 0, 128); memt(inisbn, 0, 128); memt(price, 0, 50); memt(inauthor, 0, 50); if (bindex) cout << tw(3) << ((icurpage - 1) * 20 + i); ifile.read(inname, num1); cout << tw(24) << inname; ifile.read(inisbn, num1); cout << tw(24) << inisbn; ifile.read(price, num2); cout << tw(12) << price; ifile.read(inauthor, num2); cout << tw(12) << inauthor; cout << endl;//一条纪录 if (ifile.tellg()<0) bindex = fal; el bindex = true; } } } catch (...) { cout << "throw file exception" << endl; throw "file error occurred"; ifile.clo(); } if (icurpage<八班班级口号ipage) { icurpage = icurpage + 1;龙子湖 waitview(icurpage); } el { waitview(icurpage); } ifile.clo();}void deletebookfromfile(){ int idelcount; cout << "input delete index" << endl; cin >> idelcount; cbook tmpbook; tmpbook.deletedata(idelcount); cout << "delete finish" << endl; waitur();}vo什么游戏能赚rmbid mainloop(){ showwelcome(); while (1) { clearscreen(); showwelcome(); showrootmenu(); switch (getlect()) { ca 1: clearscreen(); guideinput(); break; ca 2: clearscreen(); viewdata(); break; ca 3: clearscreen(); deletebookfromfile(); break; } }}void main(){ tscreengrid(); tsyscaption("图书管理系统"); mainloop();}
【注】开发环境为vs2013控制台程序
根据《c++项目开发全程实录》修改
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-06 03:17:51,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/123ece8bf27b128d27ba13fa5e421113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C++项目开发实现图书管理系统.doc
本文 PDF 下载地址:C++项目开发实现图书管理系统.pdf
留言与评论(共有 0 条评论) |