C++项目开发如何实现图书管理系统(C++,开发技术)

时间:2024-04-28 14:29:42 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

一、需求分析

1.可以实现添加一条新的图书信息(图书名,图书编号,图书价格,图书作者)
2.可以查看全部图书条目
3.可以删除指定的某条图书记录

二、系统设计

2.1系统功能介绍

1.添加新图书模块:该模块可以实现将新图书信息录入到系统并将图书信息保存到文件中。
2.浏览全部图书模块:可以通过该模块获取文件中全部图书信息,确定图书是否存在,及方便删除。
3.删除图书模块:可以根据图书在文件中的记录号删除某条图书记录。

2.2系统预览

主界面

C++项目开发如何实现图书管理系统

添加新图书界面

C++项目开发如何实现图书管理系统

浏览全部图书条目

C++项目开发如何实现图书管理系统

三、代码设计

3.1 图书头文件

#defineNUM1128#defineNUM250classCBook{public:CBook(){}CBook(char*cName,char*cIsbn,char*cPrice,char*cAuthor);~CBook(){}public:char*GetName();//获取图书名称voidSetName(char*cName);//设置图书名称char*GetIsbn();//获取图书ISBN编号voidSetIsbn(char*clsbn);//设置图书ISBN编号char*GetPrice();//获得图书价格voidSetPrice(char*cPrice);//设置图书价格char*GetAuthor();//获得图书作者信息voidSetAuthor(char*cAuthor);//设置图书作者信息voidWriteData();voidDeleteData(intiCount);voidGetBookFromFile(intiCount);protected:charm_cName[NUM1];charm_cIsbn[NUM1];charm_cPrice[NUM2];charm_cAuthor[NUM2];};

3.2 类中成员函数实现

#include"Book.h"#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<stdio.h>usingnamespacestd;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(){returnm_cName;}voidCBook::SetName(char*cName){strncpy_s(m_cName,cName,NUM1);}char*CBook::GetIsbn(){returnm_cIsbn;}voidCBook::SetIsbn(char*cIsbn){strncpy_s(m_cIsbn,cIsbn,NUM1);}char*CBook::GetPrice(){returnm_cPrice;}voidCBook::SetPrice(char*cPrice){strncpy_s(m_cPrice,cPrice,NUM2);}char*CBook::GetAuthor(){returnm_cAuthor;}voidCBook::SetAuthor(char*cAuthor){strncpy_s(m_cAuthor,cAuthor,NUM2);}voidCBook::WriteData(){ofstreamofile;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"fileerroroccurred";ofile.close();}ofile.close();}voidCBook::GetBookFromFile(intiCount){charcName[NUM1];charcIsbn[NUM1];charcPrice[NUM2];charcAuthor[NUM2];ifstreamifile;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"fileerroroccurred";ifile.close();}ifile.close();}voidCBook::DeleteData(intiCount){longrespos;intiDataCount=0;fstreamfile;fstreamtmpfile;ofstreamofile;charcTempBuf[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"Inputnumbererror";}else{file.seekg((iCount)*(NUM1+NUM1+NUM2+NUM2),ios::beg);for(intj=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(inti=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<iostream>#include<iomanip>#include<stdlib.h>#include<conio.h>#include<string.h>#include<fstream>#include"Book.h"#defineCMD_COLS80#defineCMD_LINES25usingnamespacestd;voidSetScreenGrid();voidClearScreen();voidSetSysCaption();voidSetSysCaption(constchar*pText);voidShowWelcome();voidShowRootMenu();voidWaitView(intiCurPage);voidWaitUser();voidGuideInput();intGetSelect();longGetFileLength(ifstream&ifs);voidViewData(intiSelPage);voidDeleteBookFromFile();voidmainloop();voidSetScreenGrid(){charsysSetBuf[80];sprintf_s(sysSetBuf,"modeconcols=%dlines=%d",CMD_COLS,CMD_LINES);system(sysSetBuf);}voidClearScreen(){system("cls");}voidSetSysCaption(){system("titleSample");}voidSetSysCaption(constchar*pText){charsysSetBuf[80];sprintf_s(sysSetBuf,"title%s",pText);system(sysSetBuf);}voidShowWelcome(){for(inti=0;i<7;i++){cout<<endl;}cout<<setw(40);cout<<"**************"<<endl;cout<<setw(40);cout<<"*图书管理系统*"<<endl;cout<<setw(40);cout<<"**************"<<endl;}voidShowRootMenu(){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;}voidWaitView(intiCurPage){charbuf[256];gets_s(buf);if(buf[0]=='q')system("exit");if(buf[0]=='m')mainloop();if(buf[0]=='n')ViewData(iCurPage);}voidWaitUser(){intiInputPage=0;cout<<"enter返回主菜单,q退出"<<endl;charbuf[256];gets_s(buf);if(buf[0]=='q')system("exit");}voidGuideInput(){charinName[NUM1];charinIsdn[NUM1];charinPrice[NUM2];charinAuthor[NUM2];cout<<"输入书名"<<endl;cin>>inName;cout<<"输入ISDN"<<endl;cin>>inIsdn;cout<<"输入价格"<<endl;cin>>inPrice;cout<<"输入作者"<<endl;cin>>inAuthor;CBookbook(inName,inIsdn,inPrice,inAuthor);book.WriteData();cout<<"WriteFinish"<<endl;WaitUser();}intGetSelect(){charbuf[256];gets_s(buf);returnatoi(buf);}longGetFileLength(ifstream&ifs){longtmppos;longrespos;tmppos=ifs.tellg();//获得当前位置ifs.seekg(0,ios::end);respos=ifs.tellg();ifs.seekg(tmppos,ios::beg);//恢复当前位置returnrespos;}voidViewData(intiSelPage=1){intiPage=0;intiCurPage=0;intiDataCount=0;charinName[NUM1];charinIsbn[NUM1];charprice[NUM2];charinAuthor[NUM2];boolbIndex=false;intiFileLength;iCurPage=iSelPage;ifstreamifile;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(inti=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<<"throwfileexception"<<endl;throw"fileerroroccurred";ifile.close();}if(iCurPage<iPage){iCurPage=iCurPage+1;WaitView(iCurPage);}else{WaitView(iCurPage);}ifile.close();}voidDeleteBookFromFile(){intiDelCount;cout<<"Inputdeleteindex"<<endl;cin>>iDelCount;CBooktmpbook;tmpbook.DeleteData(iDelCount);cout<<"DeleteFinish"<<endl;WaitUser();}voidmainloop(){ShowWelcome();while(1){ClearScreen();ShowWelcome();ShowRootMenu();switch(GetSelect()){case1:ClearScreen();GuideInput();break;case2:ClearScreen();ViewData();break;case3:ClearScreen();DeleteBookFromFile();break;}}}voidmain(){SetScreenGrid();SetSysCaption("图书管理系统");mainloop();}

【注】开发环境为VS2013控制台程序

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:C++项目开发如何实现图书管理系统的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Java如何实现汽车租赁系统下一篇:

13 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18