图书馆库存信息管理.docx
《图书馆库存信息管理.docx》由会员分享,可在线阅读,更多相关《图书馆库存信息管理.docx(28页珍藏版)》请在冰点文库上搜索。
图书馆库存信息管理
图书馆库存信息管理
一、系统描述
图书馆库存信息管理的基础强化训练要求学生自己设计并编制一个小型并具有一定功能的图书管理系统,该系统要求能对图书的库存信息进行一定的统计,不要求采用数据库和图形化界面,只要求在命令提示符的界面下对图书的入库、出库、修改、增加进行操作即可,对数据的存储以文件的形式存储在外存中。
二、需求分析
1、功能需求
(1)图书入库(一本书的各个信息,书名ISBN等)
(2)图书出库
(3)修改一条图书记录
(4)查询图书记录
2、数据物理存储结构以及逻辑存储结构
序号
数据项
数据类型
描述
1
图书序号
整型
一本书在文件中的序号
2
ISBN
字符串
唯一的标志一本书
3
书名
字符串
一本书的名字
4
出版社
字符串
书籍的出版社
5
作者
字符串
书籍的作者
6
入库时间
字符串
一本书进入图书库的时间
7
出库时间
字符串
一本书离开图书馆的时间
8
库存数
整型
一本书的当前库存量
9
单价
双精度
一本书的价格
10
下一本书
指针
指向下一本书的节点
(1)每本书采用一个结构体来定义其特点
如structLNode
{intsequenceNum;//图书序号
stringISBN;//ISBN编号
stringBookName;//书名
stringpress;//出版社
stringauthor;//作者
stringdate_of_in;//入库时间
stringdate_of_out;//出库时间
intstore_number;//库存数
doubleprice;//单价
LNode*next;//指向下一个图书
};
在进行数据处理的时候,每本书的消息记录存储在一个文本文档中,以便对大量外存中的数据进行操作
(2)逻辑结构
采用一个单链表来对每本书进行索引,单链表的节点为书本信息的结构体,当对一个节点进行操作是,若是打印节点的信息,则从文件中首先取出信息写到内存中,然后再在命令提示符中打印出来;若是进行图书出库或者图书信息的修改,则是先写到节点中,然后在保存到文件中。
三、详细设计
#include//获取系统日期
#include
#include
#include
#include//istringstream
#include
#include
usingnamespacestd;
enumquery_form{BookName,press,author,ISBN};
templateboolfrom_string(T&t,conststring&s,ios_base&(*f)(ios_base&))//字符串类型转换
{
istringstreamiss(s);
return!
(iss>>f>>t).fail();
}
/*函数声明*/
voidmenu();//主菜单
voidsub_menu_Query();//查询记录菜单
voidADD();//增加记录
voidModify();//修改记录
voidDelete();//删除记录
voidquery(intchoice_of_query,stringquery_keyword);//查询记录
stringget_date()//获取当前系统日期并转换成yyyy-mm-dd格式
{
stringstr;
chars[10];
SYSTEMTIMEst;
GetSystemTime(&st);
sprintf(s,"%d",st.wYear);str=s;str+="-";
sprintf(s,"%d",st.wMonth);str+=s;str+="-";
sprintf(s,"%d",st.wDay);str+=s;
returnstr;
}
/*类型定义*/
structLNode
{
intsequenceNum;//图书序号
stringISBN;//ISBN编号
stringBookName;//书名
stringpress;//出版社
stringauthor;//作者
stringdate_of_in;//入库时间
stringdate_of_out;//出库时间
intstore_number;//库存数
doubleprice;//单价
LNode*next;//指向下一个图书
};
/*链表基本操作*/
LNode*CreateDefaultLink()//创建以默认数据建立的链表
{
ifstreambook("Bookmanagement.txt");
if(!
book)
{
cerr<<"打开文件失败!
"<char*s="Bookmanagement.txt";
cout<<"请重新输入完整文件路径及文件名(盘符:
\\路径\\文件名.txt):
"<cin>>s;
book.open(s);
}
LNode*p1,*p2,*head=NULL;
p1=p2=newLNode;
p1->next=NULL;
inti=0,j=1;
stringread,r[11];//read用来读取每一行关键字,r[1]-r[9]依次用来保存每个结点的关键字
getline(book,read);
read.append(1,'\n');
while(j<10)
{
stringtemp;
while(read[i]!
='\n'&&read[i++]!
='\t')
{
temp.append(1,read[i-1]);//依次分离一个单词,每个单词之间用垂直制表符分割
}
r[j]=temp;
j++;
}
from_stringt>(p1->sequenceNum,r[1],dec);
p1->ISBN=r[2];
p1->BookName=r[3];
p1->author=r[4];
p1->press=r[5];
from_string(p1->store_number,r[6],dec);
from_string(p1->price,r[7],dec);
p1->date_of_in=r[8];
p1->date_of_out=r[9];
while(!
book.eof())
{
if(head==NULL)head=p1;
elsep2->next=p1;
p2=p1;
p1=newLNode;
p1->next=NULL;
getline(book,read);
read.append(1,'\n');
i=0,j=1;
while(j<10)
{
stringtemp;
while(read[i]!
='\n'&&read[i++]!
='\t')
{
temp.append(1,read[i-1]);//依次将一个单词
}
r[j]=temp;
j++;
}
from_string(p1->sequenceNum,r[1],dec);//将r[1]转换成int型,序号
p1->ISBN=r[2];
p1->BookName=r[3];
p1->author=r[4];
p1->press=r[5];
from_string(p1->store_number,r[6],dec);//将r[6]转换成int型,库存数
from_string(p1->price,r[7],dec);//将r[7]转换成double型,单价
p1->date_of_in=r[8];
p1->date_of_out=r[9];
}
returnhead;
book.close();
}
voidmain()
{
system("colorf1");//改变当前控制台窗口的背景颜色为F(白色),字体颜色为1(蓝色)
menu();
system("pause");
}
/*函数实现*/
voidmenu()
{
cout<<"********************************************************************************"<cout<<"*"<cout<<"*图书库存管理系统"<cout<<"*选择一个操作:
*"<cout<<"*<1>图书入库*"<cout<<"*<2>修改一条图书记录*"<cout<<"*<3>图书出库*"<cout<<"*<4>查询图书记录*"<cout<<"*<5>清屏*"<cout<<"*<6>退出系统*"<cout<<"********************************************************************************"<cout<intchoice=6;
cout<<"请输入您的选择(1-6):
"<cin>>choice;
while(choice<1||choice>6)
{
cout<<"输入不正确!
请重新再输入一个1至6之间的整数:
"<cin>>choice;
cout<<"\n\n\n\n";
}
switch(choice)
{
case1:
cout<<"\n\n\n\n";ADD();break;
case2:
cout<<"\n\n\n\n";Modify();break;
case3:
cout<<"\n\n\n\n";Delete();break;
case4:
cout<<"\n\n\n\n";sub_menu_Query();break;
case5:
system("cls");menu();break;
case6:
exit(0);
}
}
voidADD()
{
cout<<"请依次按照顺序输入图书的信息:
ISBN编号书名作者出版社单价"<LNode*new_book=newLNode;
chart[60];
cin>>new_book->ISBN;
cin>>new_book->BookName;
cin.get();
gets(t);new_book->author=t;
cin>>new_book->press;
cin.get();
gets(t);
new_book->price=atof(t);
new_book->date_of_in=get_date();
new_book->sequenceNum=0;//初始化图书的序号,后面再修改。
cout<<"您输入的图书的信息是:
"<cout<<"ISBN编号书名作者出版社单价"<cout<ISBN<<"\t"<BookName<<"\t"<author<<"\t"<press<<"\t"<price<<"\t"<date_of_in<//查找该书是否存在,如果是,打印信息,并将其库存数加1.如果没有,则插入到链表的最后。
LNode*x=CreateDefaultLink();
LNode*head=x;
intexist_tag=0;//图书存在识别标志
while(x->next!
=NULL&&(int)x->store_number)
{
if(new_book->ISBN.compare(x->ISBN)==0)//判断入库图书是否已经存在,使用ISBN判别
{
cout<cout<<"此书已经存在,库存数目加1"<x->store_number++;
exist_tag=1;//图书存在识别标志.
break;
}
x=x->next;
}
if(exist_tag==0)
{
cout<<"此书是新书,文件记录中将放到最后"<new_book->sequenceNum=x->sequenceNum+1;
new_book->store_number=1;
x->next=new_book;
new_book->next=NULL;
}
//写入文件,因为是文本模式打开的,所以要全部重写文件。
ofstreamoutput_book("Bookmanagement.txt",ios:
:
trunc);
cout<<"是否将修改写入文件?
(Y/N)"<chardecision;
cin>>decision;
if(decision=='y'||decision=='Y')
{
output_book<<"序号ISBN书名作者出版社库存数单价入库时间出库时间\n";
head=head->next;//跳过第一行
while(head)
{
output_book<sequenceNum<<"\t"<ISBN<<"\t"<BookName<<"\t"<author<<"\t"<press<<"\t"<store_number<<"\t"<price<<"\t"<date_of_in<<"\t"<date_of_out<<"\n";
head=head->next;
}
cout<<"写入完毕\n\n";
output_book.close();
system("%windir%\\notepad.exeBookmanagement.txt");
system("pause");
}
cout<<"是否继续?
(Y/N):
"<cin>>decision;
if(decision=='y'||decision=='Y')menu();
elsecout<<"谢谢使用!
"<}
voidDelete()
{
cout<<"请选择要出库的ISBN编号:
";
LNode*new_book=newLNode;
cin>>new_book->ISBN;
LNode*x=CreateDefaultLink();
LNode*head=x;
intexist_tag=0;//图书存在识别标志
while(x!
=NULL&&x->store_number)
{
if(new_book->ISBN.compare(x->ISBN)==0)//判断入库图书是否已经存在,使用ISBN判别
{
cout<cout<<"此书书库中存在"<cout<<"ISBN编号书名作者出版社单价"<cout<ISBN<<"\t"<BookName<<"\t"<author<<"\t"<press<<"\t"<price<<"\t"<date_of_in<x->store_number--;//库存数减少1
exist_tag=1;//图书存在识别标志.
break;
}
x=x->next;
}
if(exist_tag==0)
{
cout<<"此书书库中不存在,不可以出库!
"<}
//写入文件,因为是文本模式打开的,所以要全部重写文件。
else
{
ofstreamoutput_book("Bookmanagement.txt");
cout<<"是否将修改写入文件?
(Y/N)"<chardeci;
cin>>deci;
if(deci=='y'||deci=='Y')
{
output_book<<"序号ISBN书名作者出版社库存数单价入库时间出库时间\n";
head=head->next;//跳过第一行
while(head)
{
output_book<sequenceNum<<"\t"<ISBN<<"\t"<BookName<<"\t"<author<<"\t"<press<<"\t"<store_number<<"\t"<price<<"\t"<date_of_in<<"\t"<date_of_out<<"\n";
head=head->next;
}
cout<<"写入完毕!
"<output_book.close();
}
}
chardecision;
cout<<"是否继续?
(Y/N):
"<cin>>decision;
if(decision=='y'||decision=='Y')menu();
elsecout<<"谢谢使用!
"<}
voidModify()
{
cout<<"请选择要修改的ISBN编号:
"<stringISBN_of_Modify;
cin>>ISBN_of_Modify;
LNode*x=CreateDefaultLink();
LNode*head=x;
intexist_tag=0;//图书存在识别标志
while(x!
=NULL&&x->store_number)
{
if(ISBN_of_Mpare(x->ISBN)==0)//判断入库图书是否已经存在,使用ISBN判别
{
cout<cout<<"此书书库中存在"<cout<ISBN<<"\t"<BookName<<"\t"<author<<"\t"<press<<"\t"<price<<"\t"<date_of_in<cout<<"请依次按照顺序输入图书的信息:
ISBN编号书名作者出版社单价"<chart[60];
cin>>x->ISBN;
cin>>x->BookName;
cin.get();
gets(t);x->author=t;
cin>>x->press;
cin.get();
gets(t);
x->price=atof(t);
x->date_of_in=get_date();
cout<<"您输入的图书的信息是:
"<cout<<"ISBN编号书名作者出版社单价"<cout<ISBN<<"\t"<BookName<<"\t"<author<<"\t"<press<<"\t"<price<<"\t"<date_of_in<exist_tag=1;//图书存在识别标志.
break;
}
x=x->next;
}
if(exist_tag==0)
{
cout<<"此书书库中不存在!
"<}
//写入文件,因为是文本模式打开的,所以要全部重写文件。
else
{
ofstreamoutput_book("Bookmanagement.txt",ios:
:
trunc);
cout<<"是否将修改写入文件?
(Y/N)"<chardeci;
cin>>deci;
if(deci=='y'||deci=='Y')
{
output_book<<"序号ISBN书名作者出版社库存数单价入库时间出库时间\n";
head=head->next;//跳过第一行
while(head)
{
output_book<sequenceNum<<"\t"<ISBN<<"\t"<BookName<<"\t"<author<<"\t"<press<<"\t"<store_number<<"\t"<price<<"\t"<date_of_in<<"\t"<date_of_out<<"\n";
head=head->next;
}
cout<<"写入完毕!
"<output_book.close();
}
}
chardecision;
cout<<"是否继续?
(Y/N):
"<cin>>decision;
if(decision=='y'||decision=='Y')menu();
elsecout<<"谢谢使用!
"<}
voidsub_menu_Query()
{
cout<<"*选择一个查询方式*"<cout<<"*<1>按书名查询*"<cout<<"*<2>