1、图书馆库存信息管理图书馆库存信息管理一、系统描述 图书馆库存信息管理的基础强化训练要求学生自己设计并编制一个小型并具有一定功能的图书管理系统,该系统要求能对图书的库存信息进行一定的统计,不要求采用数据库和图形化界面,只要求在命令提示符的界面下对图书的入库、出库、修改、增加进行操作即可,对数据的存储以文件的形式存储在外存中。二、需求分析1、功能需求(1)图书入库(一本书的各个信息,书名 ISBN等)(2)图书出库(3)修改一条图书记录(4)查询图书记录2、数据物理存储结构以及逻辑存储结构序号数据项数据类型描述1图书序号整型一本书在文件中的序号2ISBN字符串唯一的标志一本书3书名字符串一本书的名
2、字4出版社字符串书籍的出版社5作者字符串书籍的作者6入库时间字符串一本书进入图书库的时间7出库时间字符串一本书离开图书馆的时间8库存数整型一本书的当前库存量9单价双精度一本书的价格10下一本书指针指向下一本书的节点 (1)每本书采用一个结构体来定义其特点 如 struct LNode int sequenceNum; /图书序号 string ISBN; /ISBN编号 string BookName; /书名string press; /出版社string author; /作者 string date_of_in; /入库时间 string date_of_out; /出库时间 int s
3、tore_number; /库存数 double price; /单价 LNode * next; /指向下一个图书;在进行数据处理的时候,每本书的消息记录存储在一个文本文档中,以便对大量外存中的数据进行操作(2)逻辑结构 采用一个单链表来对每本书进行索引,单链表的节点为书本信息的结构体,当对一个节点进行操作是,若是打印节点的信息,则从文件中首先取出信息写到内存中,然后再在命令提示符中打印出来;若是进行图书出库或者图书信息的修改,则是先写到节点中,然后在保存到文件中。三、详细设计#include/获取系统日期#include#include#include#include/istringstr
4、eam#include#includeusing namespace std;enum query_form BookName,press,author,ISBN;template bool from_string(T& t, const string& s, ios_base& (*f)(ios_base&)/字符串类型转换 istringstream iss(s); return !(iss f t).fail();/*函数声明*/void menu(); /主 菜单void sub_menu_Query(); /查询记录 菜单void ADD(); /增加记录void Modify();
5、 /修改记录void Delete(); /删除记录void query(int choice_of_query,string query_keyword); /查询记录 string get_date()/获取当前系统日期并转换成yyyy-mm-dd格式 string str; char s10; SYSTEMTIME st; 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; retu
6、rn str;/*类型定义*/struct LNode int sequenceNum; /图书序号 string ISBN; /ISBN编号 string BookName; /书名 string press; /出版社 string author; /作者 string date_of_in; /入库时间 string date_of_out; /出库时间 int store_number; /库存数 double price; /单价 LNode * next; /指向下一个图书;/*链表基本操作*/LNode * CreateDefaultLink( )/创建以默认数据建立的链表 if
7、stream book(Bookmanagement.txt); if(!book) cerr打开文件失败!endl; char * s=Bookmanagement.txt; cout请重新输入完整文件路径及文件名(盘符:路径文件名.txt):s; book.open(s); LNode *p1,*p2,*head=NULL; p1=p2=new LNode; p1-next =NULL; int i=0,j=1; string read,r11; /read用来读取每一行关键字,r1-r9依次用来保存每个结点的关键字 getline(book,read); read.append(1,n)
8、; while(j10) string temp; while( readi!= n& readi+!=t) temp.append(1,readi-1);/依次分离一个单词,每个单词之间用 垂直制表符分割 rj=temp; j+; from_string(p1-sequenceNum, r1, dec); p1-ISBN = r2; p1-BookName = r3; p1-author = r4; p1-press = r5; from_string(p1-store_number, r6, dec); from_string(p1-price, r7, dec); p1-date_of_
9、in=r8; p1-date_of_out=r9; while(!book.eof() if(head=NULL)head=p1; else p2-next=p1; p2=p1; p1=new LNode; p1-next=NULL; getline(book,read); read.append(1,n); i=0,j=1; while(j10) string temp; while(readi!=n & readi+!=t) temp.append(1,readi-1);/依次将一个单词 rj=temp; j+; from_string(p1-sequenceNum, r1, dec);/
10、将r1转换成int型,序号 p1-ISBN = r2; p1-BookName = r3; p1-author = r4; p1-press = r5; from_string(p1-store_number, r6, dec);/将r6转换成int型,库存数 from_string(p1-price, r7, dec);/将r7转换成double型,单价 p1-date_of_in=r8; p1-date_of_out=r9; return head; book.close();void main() system(color f1); /改变当前控制台窗口的背景颜色为F(白色),字体颜色为
11、1(蓝色) menu(); system(pause);/*函数实现*/void menu() cout*endl; cout* endl; cout* 图书库存管理系统endl; cout* 选择一个操作: *endl; cout* 图书入库 *endl; cout* 修改一条图书记录 *endl; cout* 图书出库 *endl; cout* 查询图书记录 *endl; cout* 清屏 *endl; cout* 退出系统 *endl; cout*endl; coutendl; int choice=6; cout 请输入您的选择(1-6):choice; while (choice6)
12、 cout 输入不正确!请重新再输入一个1至6之间的整数:choice; coutnnnn; switch(choice) case 1:coutnnnn;ADD();break; case 2:coutnnnn;Modify();break; case 3:coutnnnn;Delete();break; case 4:coutnnnn;sub_menu_Query();break; case 5:system(cls);menu();break; case 6:exit(0); void ADD() cout请依次按照顺序输入图书的信息: ISBN编号 书名 作者 出版社 单价new_bo
13、ok-ISBN; cinnew_book-BookName; cin.get(); gets(t); new_book-author=t; cinnew_book-press; cin.get(); gets(t); new_book-price=atof(t); new_book-date_of_in=get_date(); new_book-sequenceNum=0;/初始化图书的序号,后面再修改。 cout您输入的图书的信息是:endl; coutISBN编号 书名 作者 出版社 单价endl; coutISBNtBookNametauthortpresstpricetdate_of_
14、innext!=NULL & (int)x-store_number) if( new_book-ISBN.compare(x-ISBN)=0)/判断入库图书是否已经存在,使用ISBN判别 coutendl; cout此书已经存在,库存数目加1store_number+; exist_tag=1; /图书存在识别标志. break; x=x-next; if(exist_tag=0) cout此书是新书,文件记录中将放到最后sequenceNum=x-sequenceNum+1; new_book-store_number=1; x-next=new_book; new_book-next=N
15、ULL; /写入文件,因为是文本模式打开的,所以要全部重写文件。 ofstream output_book(Bookmanagement.txt,ios:trunc); cout是否将修改写入文件?(Y/N)decision; if(decision =y |decision =Y) output_booknext;/跳过第一行 while(head) output_booksequenceNumtISBNtBookNametauthortpresststore_numbertpricetdate_of_intdate_of_outnext; cout写入完毕nn; output_book.c
16、lose(); system(%windir%notepad.exe Bookmanagement.txt); system(pause); cout是否继续?(Y/N):decision; if(decision =y |decision =Y)menu(); else cout谢谢使用!endl; void Delete() coutnew_book-ISBN; LNode *x= CreateDefaultLink(); LNode *head=x; int exist_tag=0; /图书存在识别标志 while(x!=NULL & x-store_number) if( new_bo
17、ok-ISBN.compare(x-ISBN)=0)/判断入库图书是否已经存在,使用ISBN判别 coutendl; cout此书书库中存在endl; coutISBN编号 书名 作者 出版社 单价endl; coutISBNtBookNametauthortpresstpricetdate_of_instore_number-;/库存数减少1 exist_tag=1; /图书存在识别标志. break; x=x-next; if(exist_tag=0) cout此书书库中不存在,不可以出库!endl; /写入文件,因为是文本模式打开的,所以要全部重写文件。 else ofstream ou
18、tput_book(Bookmanagement.txt); cout是否将修改写入文件?(Y/N)deci; if(deci =y |deci =Y) output_booknext;/跳过第一行 while(head) output_booksequenceNumtISBNtBookNametauthortpresststore_numbertpricetdate_of_intdate_of_outnext; cout写入完毕!endl; output_book.close(); char decision; cout是否继续?(Y/N):decision; if(decision =y
19、|decision =Y)menu(); else cout谢谢使用!endl;void Modify() cout请选择要修改的ISBN编号:ISBN_of_Modify; LNode *x= CreateDefaultLink(); LNode *head=x; int exist_tag=0; /图书存在识别标志 while(x!=NULL & x-store_number) if( ISBN_of_Mpare(x-ISBN)=0)/判断入库图书是否已经存在,使用ISBN判别 coutendl; cout此书书库中存在endl; coutISBNtBookNametauthortpres
20、stpricetdate_of_inendl; cout请依次按照顺序输入图书的信息: ISBN编号 书名 作者 出版社 单价x-ISBN; cinx-BookName; cin.get(); gets(t); x-author=t; cinx-press; cin.get(); gets(t); x-price=atof(t); x-date_of_in=get_date(); cout您输入的图书的信息是:endl; coutISBN编号 书名 作者 出版社 单价endl; coutISBNtBookNametauthortpresstpricetdate_of_innext; if(ex
21、ist_tag=0) cout此书书库中不存在!endl; /写入文件,因为是文本模式打开的,所以要全部重写文件。 else ofstream output_book(Bookmanagement.txt,ios:trunc); cout是否将修改写入文件?(Y/N)deci; if(deci =y |deci =Y) output_booknext;/跳过第一行 while(head) output_booksequenceNumtISBNtBookNametauthortpresststore_numbertpricetdate_of_intdate_of_outnext; cout写入完毕!endl; output_book.close(); char decision; cout是否继续?(Y/N):decision; if(decision =y |decision =Y)menu(); else cout谢谢使用!endl;void sub_menu_Query() cout* 选择一个查询方式 *endl; cout* 按 书名 查询 *endl; cout*