图书管理系统.docx
《图书管理系统.docx》由会员分享,可在线阅读,更多相关《图书管理系统.docx(19页珍藏版)》请在冰豆网上搜索。
![图书管理系统.docx](https://file1.bdocx.com/fileroot1/2022-11/28/7b013d9b-e349-4146-b7b8-bd2b90f62d62/7b013d9b-e349-4146-b7b8-bd2b90f62d621.gif)
图书管理系统
图书管理系统设计(C++)
实习报告
学号:
班级:
姓名:
用C++设计一个小型的图书管理系统
一、目的
1、学会在自己的软件工程中利用STL提供的成熟数据类型(class),这里主要要用的类有:
ostream(包括两个对象cin与cout,以及读文件类ifstream与写文件类ofstream等)、stringistringstream等;
2、掌握冒泡法。
掌握数据类型大小(operator<或operator>)的重载,进而利用冒泡法对数据进行排序。
3、重点学习文件读写方式。
4、了解开发简单应用程序的全过程,包括:
分析、设计、编程、调试和编写报告。
二、摘要
1.可以预览所有图书的所有内容.
2.可以向系统的任意地方插入图书.
3.可以删除任意一本图书.
4.可以用多种方试查找所耐须要的图书.
5.将图书信息保存到到文本文件中
三、系统设计
用记事本新建一文本文件,文件名为:
“book.txt”,将某图书信息输入到这个文件中
菜单系统
图书管理系统
保存
删除
退出
添加
查找
预览
退出
书名
作者
编号
系统实现
启动系统之后,出现如下界面:
1.预览文本文件“book.txt”中的内容
运行程序后,选择1(显示文本文件“book.txt”中的内容)
2.查找
运行程序后,选择2进行查找.
3添加、保存
运行程序后,选择3进行添加并保存到”book.txt”中保存
此时”book.txt”中的内容为
5.删除、保存
程序如下
1、图书类:
(1)书籍类头文件内容
#pragmaonce
#include
#include
#include
usingnamespacestd;
classCBook
{
public:
stringID;
stringname;
stringauthor;
stringpress;
stringpublic_time;
floatprice;
CBook*next;
friendostream&operator<<(ostream&output,CBook&book);
friendistream&operator>>(istream&input,CBook&book);
};
(2)书籍类实现文件内容
#include"stdafx.h"
#include"CBook.h"
ostream&operator<<(ostream&output,CBook&book)
{
output<:
left)<<<""<returnoutput;
}
istream&operator>>(istream&input,CBook&book)
{
input>>book.ID>>book.name>>book.author>>book.press>>book.public_time>>book.price;
returninput;
}
2链表
1)链表类的头文件
#include"CBook.h"
classLink
{
CBook*head;
CBook*p;
public:
intcreat();//创建一个链表
intinsert();//插入一本书
intdel();//删除一本书
voidfind();//查找一本书
voidshow();//显示所有图书
voidmenu();//总菜单
voiddel_menu();//删除菜单
voidinsert_menu();//插入菜单
voidfind_menu();//查找菜单
voidput_into_file();//写入文本文件中
};
2)链表的实现
#include"stdafx.h"
#include"Link.h"
#include"CBook.h"
#include
#include
#include
#include
usingnamespacestd;
intLink:
:
creat()//创建一个链表
{
head=newCBook;
p=head;
charstr[255];
ifstreaminfile("book.txt");
if(!
infile)
{
cerr<<"openerror!
"<abort();
}
infile.getline(str,255);
while(!
infile.eof())
{
infile.getline(str,255);
istrstreamis(str);
is>>*p;
p->next=newCBook;
p=p->next;
}
p->next=0;
p=head;
infile.close();
return0;
}
intLink:
:
insert()//插入一本书
{
intnum;
while
(1)
{
insert_menu();
CBook*q=newCBook;
cin>>num;
if(num==1)
{
cout<<"inputinformationaboutthenewbook:
\n";
cin>>*q;
q->next=head;
p=q;
}
elseif(num==2)
{
cout<<"inputinformationaboutthenewbook:
\n";
cin>>*q;
cout<<"inputthebook'snamewhichyouwanttoinsertfellow\n";
stringstr;
cin>>str;
while(p->name!
=str)
{
p=p->next;
}
q->next=p->next;
p->next=q;
p=head;
}
else
{
break;
p=head;
}
}
return0;
}
intLink:
:
del()//删除一本书
{
intnum;
while
(1)
{
del_menu();
cin>>num;
if(num==1)
{
p=p->next;
}
elseif(num==2)
{
cout<<"inputthebook'snumberwhichyouwanttodelete:
";
stringstr;
cin>>str;
p=head;
while(p->next->ID!
=str)
{
p=p->next;
}
CBook*q;
q=p->next;
p->next=q->next;
p=head;
}
else
{
break;
p=head;
}
}
return0;
}
voidLink:
:
find()//查找一本书
{
intnum;
while
(1)
{
find_menu();
cin>>num;
if(num==1)
{
cout<<"inputthebook'sIDwhichyouwanttofind:
";
stringstr;
cin>>str;
boolprime=false;
while(p->next!
=0)
{
if(p->ID==str)
{
prime=true;
cout<<*p<break;
}
p=p->next;
}
if(prime==false)
cout<<"error!
thisbookisnotexit!
\n";
}
elseif(num==2)
{
cout<<"inputthebook'snamewhichyouwanttofind:
";
stringstr;
cin>>str;
boolprime=false;
while(p->next!
=0)
{
if(p->name==str)
{
prime=true;
cout<<*p<}
p=p->next;
}
if(prime==false)
cout<<"error!
thisbookisnotexit!
\n";
}
elseif(num==3)
{
cout<<"inputthebook'sauthorwhichyouwanttofind:
";
stringstr;
cin>>str;
boolprime=false;
while(p->next!
=0)
{
if(p->author==str)
{
prime=true;
cout<<*p;
}
p=p->next;
}
if(prime==false)
cout<<"error!
thisbookisnotexit!
\n";
}
elseif(num==4)
{
cout<<"inputthebook'spresswhichyouwanttofind:
";
stringstr;
cin>>str;
boolprime=false;
while(p->next!
=0)
{
if(p->press==str)
{
prime=true;
cout<<*p<}
p=p->next;
}
if(prime==false)
cout<<"error!
thisbookisnotexit!
\n";
}
elseif(num==5)
{
cout<<"inputthebook'spublic_timewhichyouwanttofind:
";
stringstr;
cin>>str;
boolprime=false;
while(p->next!
=0)
{
if(p->public_time==str)
{
prime=true;
cout<<*p<}
p=head;
}
if(prime==false)
cout<<"error!
thisbookisnotexit!
\n";
}
elseif(num==0)
break;
else
{
cout<<"operateerror!
"<break;
}
}
p=head;
}
voidLink:
:
show()
{
while(p->next!
=0)
{
cout<<*p<p=p->next;
}
p=head;
}
voidLink:
:
menu()
{
cout<<"********************图书管理系统**********************\n";
cout<<"***1.预览2.查找3.添加4.保存5.删除0.退出"<cout<<"******************************************************\n";
cout<}
voidLink:
:
insert_menu()
{
cout<<"-------------------按什么方式添加--------------------\n";
cout<<"***1.从头添加2.从中间或末尾添加0.退出添加\n";
cout<<"-----------------------------------------------------\n";
}
voidLink:
:
del_menu()
{
cout<<"-------------------按什么方式删除--------------------\n";
cout<<"***1.从头删除2.从中间或末尾删除0.退出删除\n";
cout<<"-----------------------------------------------------\n";
}
voidLink:
:
find_menu()
{
cout<<"-----------------按什么方式查找----------------------\n";
cout<<"***1.编号.书名.作者.出版社.出版时间0.退出查找"<cout<<"-----------------------------------------------------\n";
}
voidLink:
:
put_into_file()
{
ofstreamoutfile("book.txt");
outfile.seekp(0,ios:
:
beg);
outfile<<"编号"<<""<<"书名"<<""<<"作者"<<""<<"出版社"<<""<<"出版时间"<<""<<"价格\n";
while(p->next->next!
=0)
{
outfile<<*p<p=p->next;
}
outfile<<*p;
p=head;
outfile.close();
}
3.主函数
//2008程序设计.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"CBook.h"
#include"Link.h"
#include
usingnamespacestd;
LinkCB;
int_tmain(intargc,_TCHAR*argv[])
{
CB.creat();
while
(1)
{
CB.menu();
cout<<"pleasechioce:
";
intnum;
cin>>num;
if(num==1)
CB.show();
elseif(num==2)
CB.find();
elseif(num==3)
CB.insert();
elseif(num==4)
CB.put_into_file();
elseif(num==5)
CB.del();
elseif(num==0)
break;
else
{
cout<<"error!
"<break;
}
}
return0;
}
1.将类的声明和成员函数的定义分离,体现封装性,也便于调试和改正。
2.用动态链表将不同的书连接起来,以便进行插入和删除等操作