1、 数学和计算机学院 C程序设计 课程 设计汇报 年级 学号 110911 姓名 刘怡然 成绩 专业 电气 试验地点 B3-401 指导老师 许景山 试验项目 简单个人书籍管理系统设计和实现 试验日期 6月23日 一、 试验题目 简单个人书籍管理系统设计和实现 二、 试验目标、要求 1、存放书籍多种相关信息。 2、提供查找功效,根据多个关键码
2、查找需要书籍,查找成功后能够修改统计相关项。 3、提供排序功效,根据多个关键码对全部书籍进行排序,比如根据购置日期进行排序。 4、其它辅助维护工作。 三、 数据结构及算法描述 1、使用C++中类来实现,其中有DATE类和Book类,进行类复合。数据均为私有数据。 class DATE { public: friend ostream & operator<<(ostream &output, DATE &t); //友元函数重载流插入运算符 friend istream & operator>>(istream &input, DATE &t); //友
3、元函数重载流提取运算符 public: DATE(int y=0,int m=0,int d=0); //初始化 virtual ~DATE(); DATE &operator=(DATE &); //重载时间等于运算符 int operator < (DATE &); //重载时间小于运算符 int operator ==(DATE &); //重载时间等于判定运算符 private: int year,month,day; }; -------------------------------------------------------------
4、 class Book { public: Book(int 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(); //删除书籍信息 in
5、t e_update(); //修改书籍信息 int showInfo(); //显示部分书籍信息 int sort(); //排序书籍信息 Book &operator=(Book &); //重载等于运算符 private: int type; //书籍类型 char name[MAX]; //书籍名称 char author[MAX]; //作者 float price; //书籍价格 DATE date; //购置日期 }; 2、定义数组Book books[200]使用流提取、流插入方法进行数据读取和存放。 3、
6、在排序函数int sort( )中使用冒泡算法排序:
for(i=0;i 7、
e_delete
f_save
f_new
f_open
各个功效模块含义以下:
1、“主函数”模块main()
此模块循环显示第一级操作命令菜单,接收键盘输入命令,检验命令是否正当,若正当则调用对应下层函数。命令菜单中包含“退出系统”命令,当接收到该命令时立即终止整个程序运行。
2、“新建”模块f_new():
此模块清空books数组;进入输入状态,接收键盘输入全部数据保留在books数组中,按“购置日期”次序输入统计。
3、“打开”模块f_open():
此模块清除books数组中原有数据,从A盘上已经存在数据文件(books.d 8、at)中读入全部数据到books数组中,并将全部数据按读入次序显示在屏幕上。
4、“保留”模块f_save():
此模块将books数组中全部有效数据保留到A盘books.dat文件中。
5、“删除”模块e_delele():
此模块接收从键盘输入一条统计“购置日期”和“书名”,在books数组中查找,如找到则从books数组中删除该统计,不然显示“未找到”。
6、“更新”模块e_update():
此模块接收键盘输入一条统计“购置日期”和“书名”,在 books 数组中查找,如找到则显示该统计原数据并提醒键盘输入新数据用以替换原有 9、数据,如未找到则显示“未找到”。
7、“显示”模块display():
此模块显示类别名称和编号,提醒用户输入类别编号,显示books数组中指定类别书籍统计,或显示全部书籍统计。
8、“排序”模块sort():
此模块对books数组中全部统计按“购置时间”排序。
五、源程序清单
文件 DATE.h:类DATE定义
// DATE.h: interface for the DATE class.
//
/////////////////////////////////////////////// 10、///////////////////////
#if !defined(AFX_DATE_H__6B5CFDD0_60A3_4347_853E_4B83FD47F383__INCLUDED_)
#define AFX_DATE_H__6B5CFDD0_60A3_4347_853E_4B83FD47F383__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include 11、eam & operator<<(ostream &output, DATE &t); //友元函数重载流插入运算符
friend istream & operator>>(istream &input, DATE &t); //友元函数重载流提取运算符
public:
DATE(int y=0,int m=0,int d=0); //初始化
virtual ~DATE();
DATE &operator=(DATE &); //重载时间等于运算符
int operator < (DATE &); //重载时间小于运算符
int operator ==(DA 12、TE &); //重载时间等于判定运算符
private:
int year,month,day;
};
#endif // !defined(AFX_DATE_H__6B5CFDD0_60A3_4347_853E_4B83FD47F383__INCLUDED_)
----------------------------------------------------------------------------------------------------------------------
DATE.cpp:类DATE实现
// DATE.cpp: impleme 13、ntation of the DATE class.
//
//////////////////////////////////////////////////////////////////////
#include "DATE.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
14、
DATE::DATE(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
DATE::~DATE()
{
}
ostream & operator<<(ostream &output, DATE &t)
{
if(t.month>0&&t.month<13&&t.day>0&&t.day<31)
output< 15、istream & operator>>(istream &input, DATE &t)
{
input>>t.year>>t.month>>t.day;
return input;
}
int DATE::operator <(DATE &operand)
{
if(year 16、else if(day 17、this;
}
----------------------------------------------------------------------------------------------------------------------
Book.h:类Book定义
// Book.h: interface for the Book class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_BOOK_H__0541F24F_ 18、979E_4421_8EAC_FD9__INCLUDED_)
#define AFX_BOOK_H__0541F24F_979E_4421_8EAC_FD9__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include 19、lass Book
{
public:
Book(int 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(); //显示部 20、分书籍信息
int sort(); //排序书籍信息
Book &operator=(Book &); //重载等于运算符
private:
int type; //书籍类型
char name[MAX]; //书籍名称
char author[MAX]; //作者
float price; //书籍价格
DATE date; //购置日期
};
#endif // !defined(AFX_BOOK_H__0541F24F_979E_4421_8EAC_FD9__INCLUDED_)
--------------------------- 21、
Book.cpp:类Book实现
// Book.cpp: implementation of the Book class.
//
//////////////////////////////////////////////////////////////////////
#include "Book.h"
Book books[200]; //定义200条数组
int num=0; // 22、初始化书籍信息条数零
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Book::Book(int t,char *n,char *a,float p,int y,int m,int d)
:date(y,m,d)
{
type=t;
price=p;
strncpy(nam 23、e,n,MAX);
name[MAX]=0;
strncpy(author,a,MAX);
author[MAX]=0;
}
Book::~Book()
{
}
int Book::f_new()
{
system("cls");
cout<<"1--学习"<<" "<<"2--名著"<<" "<<"3--小说"<<" "<<"4--散文"< 24、me);
printf("请输入作者名:");gets(books[num].author);
cout<<"请输入书籍价格:";cin>>books[num].price;
cout<<"请输入购置日期:";cin>>books[num].date;
num++;
return 1;
}
int Book::f_save()
{
ofstream outFile("book.dat",ios::out|ios::binary);
if(!outFile)
cerr<<"Open file or create file error."< 25、
else
{
outFile< 26、先输入书籍信息。"< 27、cout<<"1----------------按时间次序排序"< 28、books[i].date==books[j].date;j++)
{
ex=books[i];
books[i]=books[j];
books[j]=ex;
}
}
for(i=0;i 29、"散文";
char c;
for(;;)
{
system("cls");
cout<<"1. 显示全部书籍信息。"< 30、\n\n");
printf("类型 书名 作者 价格 日期\n\n");
for(int i=0;i 31、
strcpy(s,s2);
break;
case 3:
s=new char[strlen(s3)+1];
strcpy(s,s3);
break;
case 4:
s=new char[strlen(s4)+1];
strcpy(s,s4);
break;
}
printf("%-15s%-18s%-15s%-15.1f",s,name,author,price);cout< 32、
}
}
break;
case'2':
system("cls");
printf("\n---------------------------书籍分类列表-----------------------\n\n");
for(int i=0;i 33、oks[i].type==2)
cout<<"名著类:"< 34、<" "< 35、rcpy(s,s2);
break;
case 3:
s=new char[strlen(s3)+1];
strcpy(s,s3);
break;
case 4:
s=new char[strlen(s4)+1];
strcpy(s,s4);
break;
}
printf("%-15s%-18s%-15s%-15.1f",s,name,author,price);cout< 36、[MAX];
system("cls");
cout<<"请输入要删除书籍名:";cin>>s;
for(int i=0;i 37、 cout<<"1----------------删除并保留"< 38、 case '2':
return 0;
}
}
else
{
cout<<"没有匹配搜索对象"< 39、s[i].name)==0)
{
printf("\n---------------------------搜索书籍列表-----------------------\n\n");
printf("类型 书名 作者 价格 日期\n\n");
books[i].showInfo();
cout<<"1----------------修改此书籍信息"< 40、{
case'1':
printf("\n---------------------------修改书籍信息-----------------------\n\n");
cout<<"1--学习"<<" "<<"2--名著"<<" "<<"3--小说"<<" "<<"4--散文"< 41、"请输入修改后作者名:");gets(ex.author);
cout<<"请输入修改后书籍价格:";cin>>ex.price;
cout<<"请输入修改后购置日期:";cin>>ex.date;
books[i]=ex;
cout<<"修改成功!"< 42、right)
{
type=right.type;
strncpy(name,right.name,MAX);
name[MAX]=0;
strncpy(author,right.author,MAX);
author[MAX]=0;
price=right.price;
date=right.date;
return *this;
}
--------------------------------------------------------------------------------------------------------------- 43、
main.cpp:主函数
#include "Book.h"
int main()
{
extern Book books[200];
extern num;
char c;
Book *mptr;
mptr=&books[num];
for(;;)
{
system("cls");
printf("\n ________________ ");
printf("\n ------------------- 44、个人书籍管理系统|------------------------");
printf("\n ^^^^^^^^^^^^^^^^ \n\n\n");
cout<<" ||----------------------------按0键结束程序------------------------||"< 45、endl;
cout<<" ||--------------------------按2键显示书籍信息----------------------||"< 46、按5键排列书籍信息----------------------||"< 47、 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;
}
六、程序测试
主界面:
全部书籍列表:
修改书籍:
时间次序排序书籍信息:






