C个人书籍管理系统设计方案与实现 .docx
精品名师归纳总结封面可编辑资料 - - - 欢迎下载精品名师归纳总结作者: PanHongliang仅供个人学习数学与运算机学院C 程序设计课程设计报告年级 2021 学号 2021110911 姓名刘怡然成果专业电气试验的点 B3-401 指导老师许景山可编辑资料 - - - 欢迎下载精品名师归纳总结2021 年 6 月 23 日试验工程简洁个人书籍治理系统的设计与实现试验日期可编辑资料 - - - 欢迎下载精品名师归纳总结一、试验题目简洁个人书籍治理系统的设计与实现二、试验目的、要求1、储备书籍各种相关信息。2、供应查找功能,依据多种关键码查找需要的书籍,查找成功后可以修改记录的相关项。3、供应排序功能,依据多种关键码对全部的书籍进行排序,例如依据购买日期进行排序。4、其他帮忙的爱护工作。三、数据结构及算法描述1、使用 C+ 中的类来实现,其中有DATE 类和 Book 类,进行类的复合。数据均为私有数据。class DATEpublic:friend ostream & operator<<ostream &output, DATE &t 。 / 友元函数重载流插入运算符friend istream & operator>>istream &input, DATE &t 。 / 友元函数重载流提取运算符public:DATEint y=0,int m=0,int d=0 。 /初始化virtual DATE 。DATE &operator=DATE & 。 /重载时间等于运算符int operator < DATE & 。 /重载时间小于运算符int operator =DATE & 。 /重载时间等于判定运算符private:int year,month,day。 。class Bookpublic:Bookint t,char *n,char *a,float p,int y,int m,int d 。可编辑资料 - - - 欢迎下载精品名师归纳总结Book 。virtual Book 。int f_new。 /新建书籍信息int f_save。 /储存书籍信息int f_open。 /打开书籍信息int display 。 /显示书籍信息int e_delete。 /删除书籍信息int e_update。 /修改书籍信息int showInfo 。 /显示个别书籍信息int sort。 /排序书籍信息Book &operator=Book & 。 /重载等于运算符private:int type。 /书籍类型char nameMAX 。 /书籍名称char authorMAX 。 /作者float price。 /书籍价格DATE date。 /购买日期 。2、定义数组 Bookbooks200使用流提取、流插入方式进行数据读取和储备。3、在排序函数 int sort 中使用冒泡算法排序:fori=0 。i<num-1。i+forj=i+1。booksi.date<booksj.date|booksi.date=booksj.date。j+可编辑资料 - - - 欢迎下载精品名师归纳总结四、程序流程图ex=booksi。booksi=booksj 。booksj=ex。可编辑资料 - - - 欢迎下载精品名师归纳总结程序整体的功能模块如下所示:main可编辑资料 - - - 欢迎下载精品名师归纳总结displayfileeditsort各个功能模块的含义如下:f_openf_newf_savee_deletee_update1、“主函数”模块 main此模块循环显示第一级操作命令菜单 ,接收键盘输入的命令 ,检查命令是否合法,如合法就调用相应下层函数。命令菜单中包含“退出系统”命令,当接收到该命令时马上终止整个程序的运行。2、“新建”模块 f_new:此模块清空 books 数组。进入输入状态,接收键盘输入的全部数据储存在books 数组中,按“购买日期”次序输入记录。3、“打开”模块 f_open:此模块清除books 数组中原有数据,从A 盘上已经存在的数据文件books.dat中读入全部数据到books 数组中,并将全部数据按读入次序显示在屏幕上。4、“储存”模块 f_save:此模块将 books 数组中全部有效数据储存到 A 盘 books.dat文件中。5、“删除”模块 e_delele:此模块接收从键盘输入的一条记录的“购买日期”和“书名”,在books数组中查找,如找到就从 books 数组中删除该记录,否就显示“未找到”。6、“更新”模块 e_update:此模块接收键盘输入的一条记录的“购买日期”和“书名”,在books 数组中查找,如找到就显示该记录的原数据并提示键盘输入新数据用以替换原有数据,如未找到就显示“未找到”。7、“显示”模块 display:此模块显示类别名称和编号,提示用户输入类别编号,显示books 数组中指定类别的书籍记录,或显示全部书籍记录。8、“排序”模块 sort:此模块对 books 数组中全部记录按“购买时间”排序。五、源程序清单文件 DATE.h:类 DATE 的定义/ DATE.h: interface for the DATE class./可编辑资料 - - - 欢迎下载精品名师归纳总结/#if .definedAFX_DATE_H6B5CFDD0_60A3_4347_853E_4B83FD47F383_INCLUDED_#defineAFX_DATE_H6B5CFDD0_60A3_4347_853E_4B83FD47F383INCLUDED_#if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000 #include <iostream.h>class DATEpublic:friend ostream & operator<<ostream &output, DATE &t 。 / 友元函数重载流插入运算符friend istream & operator>>istream &input, DATE &t 。 / 友元函数重载流提取运算符public:DATEint y=0,int m=0,int d=0 。 /初始化virtual DATE 。DATE &operator=DATE & 。 /重载时间等于运算符int operator < DATE & 。 /重载时间小于运算符int operator =DATE & 。 /重载时间等于判定运算符private:int year,month,day。 。#endif/ .definedAFX_DATE_H6B5CFDD0_60A3_4347_853E_4B83FD47F383 INCLUDED_DATE.cpp:类 DATE 的实现/ DATE.cpp: implementation of the DATE class./可编辑资料 - - - 欢迎下载精品名师归纳总结/ #include "DATE.h"/ Construction/Destruction/ DATE:DATEint y,int m,int dyear=y。month=m。day=d。DATE:DATEostream & operator<<ostream &output, DATE &tift.month>0&&t.month<13&&t.day>0&&t.day<31 output<<t.year<<"-"<<t.month<<"-"<<t.day<<endl 。elseoutput<<" 日期错误! "<<endl。return output。istream & operator>>istream &input, DATE &tinput>>t.year>>t.month>>t.day。return input。int DATE:operator <DATE &operandifyear<operand.year return 1。else ifyear>operand.year return 0。else ifmonth<operand.month return 1。else ifmonth>operand.month return 0。else ifday<operand.day return 1。可编辑资料 - - - 欢迎下载精品名师归纳总结else return 0。int DATE:operator =DATE &rightifyear=right.year&&month=right.month&&day=right.day return 1。可编辑资料 - - - 欢迎下载精品名师归纳总结elsereturn 0。可编辑资料 - - - 欢迎下载精品名师归纳总结DATE &DATE:operator =DATE &rightyear=right.year。month=right.month。day=right.day。return *this。Book.h:类 Book 的定义/ Book.h: interface for the Book class./#if .definedAFX_BOOK_H0541F24F_979E_4421_8EAC_926616201FD9 INCLUDED_#defineAFX_BOOK_H0541F24F_979E_4421_8EAC_926616201FD9INCLUDED_#if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000 #include <string.h>#include "fstream.h" #include <stdio.h> #include <stdlib.h>可编辑资料 - - - 欢迎下载精品名师归纳总结#include <conio.h> #include "DATE.h" #define MAX 12 class Bookpublic:Bookint t,char *n,char *a,float p,int y,int m,int d 。Book 。virtual Book 。int f_new。 /新建书籍信息int f_save。 /储存书籍信息int f_open。 /打开书籍信息int display 。 /显示书籍信息int e_delete。 /删除书籍信息int e_update。 /修改书籍信息int showInfo 。 /显示个别书籍信息int sort。 /排序书籍信息Book &operator=Book & 。 /重载等于运算符private:int type。 /书籍类型char nameMAX 。 /书籍名称char authorMAX 。 /作者float price。 /书籍价格DATE date。 /购买日期 。#endif/ .definedAFX_BOOK_H0541F24F_979E_4421_8EAC_926616201FD9I NCLUDED_Book.cpp:类 Book 的实现/ Book.cpp: implementation of the Book class./可编辑资料 - - - 欢迎下载精品名师归纳总结#include "Book.h"Book books200。 /定义 200 条数组int num=0。 /初始化书籍信息条数零/ Construction/Destruction/ Book:Bookint t,char *n,char *a,float p,int y,int m,int d:datey,m,dtype=t。price=p。strncpyname,n,MAX。nameMAX=0 。strncpyauthor,a,MAX。authorMAX=0 。Book:Bookint Book:f_newsystem"cls"。cout<<"1- 学习"<<""<<"2- 名著"<<""<<"3- 小说"<<""<<"4- 散文"<<endl<<endl<<endl。cout<<"请输入书籍类型: "。cin>>booksnum.type。printf" 请输入书名: "。getsbooksnum.name。printf" 请输入作者名: "。getsbooksnum.author。cout<<"请输入书籍价格: "。cin>>booksnum.price。cout<<"请输入购买日期: "。cin>>booksnum.date。num+。return 1。int Book:f_save可编辑资料 - - - 欢迎下载精品名师归纳总结ofstream outFile"book.dat",ios:out|ios:binary。if.outFilecerr<<"Open file or create file error."<<endl。可编辑资料 - - - 欢迎下载精品名师归纳总结elseoutFile<<num。outFile.writechar *&books,sizeofbooks 。outFile.close。cout<<"储存成功! "<<endl。可编辑资料 - - - 欢迎下载精品名师归纳总结return 1。int Book:f_opensystem"cls"。int i。ifstream infile"book.dat",ios:in|ios:binary 。infile>>num 。ifnum=0cout<<"没有记录!请先输入书籍信息。 "<<endl。system"pause"。return 1。fori=0 。i<num。i+infile.readchar *&booksi,sizeofbooks 。infile.close 。return 1。int Book:sortchar c。可编辑资料 - - - 欢迎下载精品名师归纳总结system"cls"。printf"n-排序书籍信息 -nn" 。cout<<"1- 按时间次序排序 "<<endl<<"2按书名顺序排序"<<endl。c=getch。switchccase'1':system"cls"。 int i,j 。Book ex。printf"n-时间次序排序书籍信息 -可编辑资料 - - - 欢迎下载精品名师归纳总结-nn" 。fori=0 。i<num-1。i+可编辑资料 - - - 欢迎下载精品名师归纳总结forj=i+1。booksi.date<booksj.date|booksi.date=booksj.date。j+ex=booksi。booksi=booksj 。booksj=ex。fori=0 。i<num-1。i+booksi.showInfo 。system"pause"。return 1。return 1。int Book:displaychar *s。char *s1="学习" 。char *s2="名著 "。char *s3="小说 "。char *s4="散文"。char c。可编辑资料 - - - 欢迎下载精品名师归纳总结for。可编辑资料 - - - 欢迎下载精品名师归纳总结nn"。system"cls"。cout<<"1. 显示全部书籍信息。 "<<endl。cout<<"2. 显示指定类别书籍信息。 "<<endl。cout<<"0. 返回上一层。 "<<endl。c=getch。ifc='0'break。switchccase'1':system"cls"。printf"n-全 部书 籍 列 表 -printf" 类型书名作者价格日期nn"。可编辑资料 - - - 欢迎下载精品名师归纳总结forint i=0 。i<num。i+switchbooksi.typecase 1:s=new charstrlens1+1。strcpys,s1。break。case 2:s=new charstrlens2+1。strcpys,s2。break。case 3:s=new charstrlens3+1。strcpys,s3。break。case 4:可编辑资料 - - - 欢迎下载精品名师归纳总结s=new charstrlens4+1。strcpys,s4。break。printf"%-15s%-18s%-15s%-15.1f",s,name,author,price。cout<<date<<endl。break。case'2':system"cls"。printf"n-书籍分类列表 -可编辑资料 - - - 欢迎下载精品名师归纳总结-nn" 。forint i=0 。i<num。i+ ifbooksi.type=1cout<<" 学 习类 : "<<endl<<booksi.name<<"可编辑资料 - - - 欢迎下载精品名师归纳总结"<<booksi.author<<endl 。fori=0 。i<num。i+ifbooksi.type=2cout<<" 名 著类 : "<<endl<<booksi.name<<""<<booksi.author<<endl 。fori=0 。i<num。i+ifbooksi.type=3cout<<" 小 说类 : "<<endl<<booksi.name<<""<<booksi.author<<endl 。fori=0 。i<num。i+ifbooksi.type=4cout<<" 散 文类 : "<<endl<<booksi.name<<""<<booksi.author<<endl 。system"pause"。return 1。可编辑资料 - - - 欢迎下载精品名师归纳总结int Book:showInfochar *s。char *s1="学习" 。char *s2="名著 "。char *s3="小说 "。char *s4="散文"。switchtypecase 1:s=new charstrlens1+1。strcpys,s1。break。case 2:s=new charstrlens2+1。strcpys,s2。break。case 3:s=new charstrlens3+1。strcpys,s3。break。case 4:s=new charstrlens4+1。strcpys,s4。break。printf"%-15s%-18s%-15s%-15.1f",s,name,author,price。cout<<date<<endl。return 1。int Book:e_deletechar c。char sMAX 。system"cls"。cout<<"请输入要删除的书籍名: "。cin>>s。forint i=0 。i<num。i+可编辑资料 - - - 欢迎下载精品名师归纳总结ifstrcmps,booksi.name=0可编辑资料 - - - 欢迎下载精品名师归纳总结-nn" 。nn"。"<<endl。printf"n-搜寻书籍列表 -printf" 类型书名作者价格日期booksi.showInfo 。cout<<"1- 删除并储存 "<<endl<<"2返回c=getch。switchccase'1': booksi=books200。num-。system"cls"。cout<<"删除成功! "<<endl。system"pause"。return 0。case '2':return 0。可编辑资料 - - - 欢迎下载精品名师归纳总结可编辑资料 - - - 欢迎下载精品名师归纳总结elsecout<<"没有匹配的搜寻对象 "<<endl。system"pause"。可编辑资料 - - - 欢迎下载精品名师归纳总结return 0。return 0。int Book:e_update可编辑资料 - - - 欢迎下载精品名师归纳总结char c。char nMAX 。Book ex。system"cls"。cout<<"请输入要修改的书籍名: "。cin>>n。forint i=0 。i<num。i+ifstrcmpn,booksi.name=0可编辑资料 - - - 欢迎下载精品名师归纳总结-nn" 。nn"。printf"n-搜寻书籍列表 -printf" 类型书名作者价格日期booksi.showInfo 。cout<<"1- 修改此书籍信息 "<<endl<<"2-可编辑资料 - - - 欢迎下载精品名师归纳总结-返回"<<endl 。c=getch。switchccase'1':printf"n-修改书籍信息 -nn" 。cout<<"1- 学习"<<""<<"2-名著"<<""<<"3- 小说"<<""<<"4- 散文"<<endl<<endl<<endl。cout<<"请输入修改后的书籍类型: "。cin>>ex.type。printf" 请输入修改后的书名: "。getsex.name。printf" 请输入修改后的作者名: "。getsex.author。cout<<"请输入修改后的书籍价格: " 。cin>>ex.price。cout<<"请输入修改后的购买日期: " 。cin>>ex.date。booksi=ex。cout<<"修改成功! "<<endl。system"pause"。return 1。可编辑资料 - - - 欢迎下载精品名师归纳总结case'2':return 1。return 1。Book &Book:operator =Book &righttype=right.type。strncpyname,right.name,MAX。nameMAX=0 。strncpyauthor,right.author,MAX。authorMAX=0 。price=right.price。 date=right.date。return *this。main.cpp:主函数#include "Book.h" int mainextern Book books200。extern num。char c。Book *mptr 。mptr=&booksnum 。for。可编辑资料 - - - 欢迎下载精品名师归纳总结system"cls"。printf"n"。可编辑资料 - - - 欢迎下载精品名师归纳总结printf"n-|个人书籍治理系统 |-可编辑资料 - - - 欢迎下载精品名师归纳总结-" 。printf"nnnn" 。cout<<"|- 按 0 键终止程序 -可编辑资料 - - - 欢迎下载精品名师归纳总结-|"<<endl。cout<<"|- 按 1 键添加书籍信息 -|"<<endl。cout<<"|- 按 2 键显示书籍信息 -|"<<endl。cout<<"|- 按 3 键删除书籍信息 -|"<<endl。cout<<"|- 按 4 键修改书籍信息 -|"<<endl。cout<<"|- 按 5 键排列书籍信息 -|"<<endl。cout<<"电气七班刘怡然"<<endl。c=getch。ifc='0'break。switchccase '1':mptr->f_open。mptr->f_new。mptr->f_save。break。case '2':mptr->f_open。mptr->display。break。case '3':mptr->f_open。mptr->e_delete。mptr->f_save。break。可编辑资料 - - - 欢迎下载精品名师归纳总结case '4':mptr->f_open。mptr->e_update。mptr->f_save。break。case '5':mptr->f_open。mptr->sort。break。return 1。六、程序测试主界面: 全部书籍列表: 修改书籍:时间次序排序书籍信息:版权申明本文部分内容,包括文字、图片、以及设计等在网上搜集整理。版权为潘宏亮个人全部This article includes some parts, including text,pictures, and design. Copyright is Pan Hongliang's personalownership.用户可将本文的内容或服务用于个人学习、争论或观看,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关可编辑资料 - - - 欢迎下载精品名师归纳总结法律的规定,不得侵害本网站及相关权益人的合法权益。除此以 外,将本文任何内容或服务用于其他用途时,须征得本人及相关权益人的书面许可,并支付酬劳。Users may use the contents or services of this article for personal study, research or appreciation, and other non-commercial or non-profit purposes, but at the same time, they shall abide by the provisions of copyright law and other relevant laws, and shall not infringe upon the legitimate rights of this website and its relevant obligees. In addition, when any content or service of this article is used for other purposes, written permission