书店管理系统(C++初学者友好版)

您所在的位置:网站首页 图书管理系统c语言报告简单易懂一点 书店管理系统(C++初学者友好版)

书店管理系统(C++初学者友好版)

2024-07-16 07:38:52| 来源: 网络整理| 查看: 265

文章目录 写在前面题目要求主体思路代码实现定义结构体book读入带空格字符串的函数通过书名查找书的编号进货函数出货函数查询某书的详细信息、销售情况、库存函数查询详细信息函数查询销售情况函数查询库存函数 查询进/出货记录函数排序函数其他函数输出带空格字符串函数输出一本书的详细信息函数输出操作列表函数 完整代码结语

写在前面

正如标题所示,本文是写给C++的初学者的一个书店管理系统实例。为了贴近目标读者的需求,本篇博客中的代码仅采用了初级的内容。比如:对于书的名字,采取了最简单粗暴的多维字符串存储而不是Trie树甚或AC自动机;对于所有排序操作,均采用简单粗暴的冒泡排序,而不是重载运算符后调用STL中的sort函数。力求简单易懂,为C++初学者提供良好的阅读体验。

此外,与我惯于对付的传统竞赛题不同的是,此类题目需要更多考虑到使用者的层次和需求。需要提供一定的使用指导和人机交互界面,还需要考虑到一些不合法的输入。因为本篇博客中的代码仅作抛砖引玉之用(以及本人的懒惰),代码中仅加入了有限的不合法输入判定,仍然存在非常多的不合法输入可以导致程序不能正常运行。同时,如果仅仅满足下方图片所示的要求,作为一个书店管理系统还是有所欠缺,可以加入诸如图形界面、撤销操作、修改操作等更多功能。

学海无涯,本人也不过先行几步。以上所述,就权作对后续学习的一点浅见。

题目要求

题目要求 (鉴于图片足够清晰,就不手打了)

主体思路

对于图书信息的储存,显然单独开若干个数组将书名、作者、出版社等信息分别储存并不是一种优美的做法(当然,从这句话可以看出这种解决方案依旧可行)。所以,我们应该将这些信息“绑定”在一起,相当于一种新的数据类型 b o o k book book。这种方法叫做定义结构体,将在下一个章节讲到。

当我们想要查找某一本书的时候,直接用for循环遍历已有的所有书并进行for循环暴力字符串匹配即可。

对于一本书的销售额、销售数量,同样可以定义两个变量一起“绑定”在 b o o k book book里,在排序时直接按照给定的关键字来排序。

至于进出货记录,可以直接开多维数组记录 进/出货、数量、交易额等信息,也可以定义结构体。

代码实现 定义结构体book

这一步很简单,掌握了相应的语法之后,只需要把你想要“绑定”在一起的各个变量、数组加进去即可。语法如下:

struct 定义的新数据类型名字 { 变量1、变量2、…… 数组1、数组2、…… };

在本题中,按照题目要求,我们定义如下结构体:

struct book { int number,publishDate[5],salesVolume; char name[50],publisher[50],author[50]; double price,sales; bool discount; }books[M];

注:右花括号与分号之间的books[M]是声明数组一个 b o o k book book类型的数组book books[M];的简化写法。

读入带空格字符串的函数

显然,书名、作者、出版社等信息都是带空格的字符串,不能简单地用scanf("%s",str)来代替。因此,我们需要单独编写对应函数。

在下方所示的函数中,传入参数时,传入了字符串数组的地址,这样就可以直接修改对应字符串数组。

第一个for循环筛除了字符串开头所有非数字且非字母的字符,其中isdigit()判断一个字符是否为数字,isalpha()判断一个字符是否为字母。

第二个for循环开始读入,直到读到换行符时停止读入。其中,(为了省事)第0个位置str[0]被我用来记录字符串的长度,str[1]~str[str[0]]为字符串内容。

void readString(char *str) { char c=getchar(); for(;!isdigit(c)&&!isalpha(c);c=getchar()); for(str[0]=0;c!='\n';c=getchar())str[++str[0]]=c; } 通过书名查找书的编号

这个就很暴力了,for循环即可。

值得注意的是,我们这里传入的数组是形参形式,函数内对数组的更改并不会影响原本的数组。

int enquireByName(char goal[]) { for(int i=1;iflag=0;break;} if(flag)return i; } return 0; } 进货函数

有了之前写的若干函数,就很简单了,先输入要进货的书的书名,在已有书中检索,如果书已存在,直接对数目进行修改;如果之前没有过这本书,就用读入带空格字符串函数读入各种信息储存到books数组里,并为新入库的书编号。在函数结尾,将进货记录保存到records数组里。

void purchase() { int id=0,tmp=0; puts("-Please input the title of the book you want to stock (Press \"Enter\" to complete your input)"); readString(tmpBook); if(id=enquireByName(tmpBook)) { puts("-The information of this book has already been recorded in system"); puts("-So you only need to input the number of newly purchased books"); scanf("%d",&tmp),books[id].number+=tmp; } else { id=++totalNumber; for(int i=0;iputs("-There is no book called this!");return;} if(books[id].discount) { puts("-This book can be discounted."); puts("-Please input the buyer's VIP level (Press \"Enter\" to complete your input)"); scanf("%d",&lev); if(lev3){puts("-Invalid level!");return;} } else puts("-This book can't be discounted."); puts("-Please input the number of the book you want to sell (Press \"Enter\" to complete your input)"); scanf("%d",&tmp); if(tmp>books[id].number){puts("-No enough books here");return;} books[id].number-=tmp; books[id].salesVolume+=tmp; books[id].sales+=tmp*VIP[lev]*books[id].price; records[++cot][0]=1,records[cot][1]=id,records[cot][2]=tmp,salesRecords[cot]=tmp*VIP[lev]*books[id].price; puts("-Opration ends"); } 查询某书的详细信息、销售情况、库存函数

这三个函数比较雷同,都是先通过书名查找,然后直接将结构体内记录的内容输出即可。如果是通过编号查找,则直接输出即可。

查询详细信息函数 void enquireBy(int p)//0-name 1-id { int id; if(p) { puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); scanf("%d",&id); if(idtotalNumber){puts("-There is no book numbered this!");return;} printBook(id); return; } puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); readString(tmpBook); id=enquireByName(tmpBook); if(!id){puts("-There is no book called this!");return;} printBook(id); puts("-Opration ends"); } 查询销售情况函数 void enquireSalesBy(int p)//0-name 1-id { int id; if(p) { puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); scanf("%d",&id); if(idtotalNumber){puts("-There is no book numbered this!");return;} printf("-Sell %d books for $%.2lf.\n",books[id].salesVolume,books[id].sales); return; } puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); readString(tmpBook); id=enquireByName(tmpBook); if(!id){puts("-There is no book called this!");return;} printf("-Sell %d books for $%.2lf.\n",books[id].salesVolume,books[id].sales); puts("-Opration ends"); } 查询库存函数 void enquireInventoryBy(int p)//0-name 1-id { int id; if(p) { puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); scanf("%d",&id); if(idtotalNumber){puts("-There is no book numbered this!");return;} printf("-The number of this book in inventory is %d.\n",books[id].number); return; } puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); readString(tmpBook); id=enquireByName(tmpBook); if(!id){puts("-There is no book called this!");return;} printf("-The number of this book in inventory is %d.\n",books[id].number); puts("-Opration ends"); } 查询进/出货记录函数

根据查找的类型,把records数组里的信息输出即可,这里出货额度我单独开了一个double类型的salesRecords数组。

void enquireRecords(int p)//0-purchase 1-sell { if(p)puts("-Sell records:"); else puts("-Purchase records:"); for(int i=1;i tool[i][0]=i; tool[i][1]=p?books[i].sales:books[i].salesVolume; }

这里第二行使用了三目运算,相当于一个if+else:

if(p)tool[i][1]=books[i].sales; else tool[i][1]=books[i].salesVolume;

之后就是双重for循环实现的冒泡排序,注意在交换元素的时候,要将tool[i][0]和tool[i][1]中的数据一起交换。

for(int i=1;i tool[i][0]=i; tool[i][1]=p?books[i].sales:books[i].salesVolume; } for(int i=1;i puts("-The title of the book is:"); printString(books[id].name); puts("-The author of the book is:"); printString(books[id].author); puts("-The publishing house of the book is"); printString(books[id].publisher); puts("-The price of the book is:"); printf("$%.2lf\n",books[id].price); if(books[id].discount)puts("-There is a discount for this book"); else puts("-There isn't a discount for this book"); puts("-The publishing date of the book is:"); for(int i=1;i int number,publishDate[5],salesVolume; char name[50],publisher[50],author[50]; double price,sales; bool discount; }books[M]; int records[M char c=getchar(); for(;!isdigit(c)&&!isalpha(c);c=getchar()); for(str[0]=0;c!='\n';c=getchar())str[++str[0]]=c; } void printString(char str[]) { for(int i=1;i for(int i=1;iflag=0;break;} if(flag)return i; } return 0; } void purchase() { int id=0,tmp=0; puts("-Please input the title of the book you want to stock (Press \"Enter\" to complete your input)"); readString(tmpBook); if(id=enquireByName(tmpBook)) { puts("-The information of this book has already been recorded in system"); puts("-So you only need to input the number of newly purchased books"); scanf("%d",&tmp),books[id].number+=tmp; } else { id=++totalNumber; for(int i=0;i puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); scanf("%d",&id); if(idtotalNumber){puts("-There is no book numbered this!");return;} printBook(id); return; } puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); readString(tmpBook); id=enquireByName(tmpBook); if(!id){puts("-There is no book called this!");return;} printBook(id); puts("-Opration ends"); } void enquireSalesBy(int p)//0-name 1-id { int id; if(p) { puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); scanf("%d",&id); if(idtotalNumber){puts("-There is no book numbered this!");return;} printf("-Sell %d books for $%.2lf.\n",books[id].salesVolume,books[id].sales); return; } puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); readString(tmpBook); id=enquireByName(tmpBook); if(!id){puts("-There is no book called this!");return;} printf("-Sell %d books for $%.2lf.\n",books[id].salesVolume,books[id].sales); puts("-Opration ends"); } void enquireInventoryBy(int p)//0-name 1-id { int id; if(p) { puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); scanf("%d",&id); if(idtotalNumber){puts("-There is no book numbered this!");return;} printf("-The number of this book in inventory is %d.\n",books[id].number); return; } puts("-Please input the title of the book you want to enquire (Press \"Enter\" to complete your input)"); readString(tmpBook); id=enquireByName(tmpBook); if(!id){puts("-There is no book called this!");return;} printf("-The number of this book in inventory is %d.\n",books[id].number); puts("-Opration ends"); } void sell() { puts("-Please input the title of the book you want to sell (Press \"Enter\" to complete your input)"); readString(tmpBook); int id=enquireByName(tmpBook),tmp,lev=0; if(!id){puts("-There is no book called this!");return;} if(books[id].discount) { puts("-This book can be discounted."); puts("-Please input the buyer's VIP level (Press \"Enter\" to complete your input)"); scanf("%d",&lev); if(lev3){puts("-Invalid level!");return;} } else puts("-This book can't be discounted."); puts("-Please input the number of the book you want to sell (Press \"Enter\" to complete your input)"); scanf("%d",&tmp); if(tmp>books[id].number){puts("-No enough books here");return;} books[id].number-=tmp; books[id].salesVolume+=tmp; books[id].sales+=tmp*VIP[lev]*books[id].price; records[++cot][0]=1,records[cot][1]=id,records[cot][2]=tmp,salesRecords[cot]=tmp*VIP[lev]*books[id].price; puts("-Opration ends"); } void enquireRecords(int p)//0-purchase 1-sell { if(p)puts("-Sell records:"); else puts("-Purchase records:"); for(int i=1;i for(int i=1;i puts("-Operate by inputting the corresponding number as follow:"); puts(" 1 Enquire a book's detailed information through its name."); puts(" 2 Enquire a book's sales."); puts(" 3 Enquire a book's inventory."); puts(" 4 Sort by sales."); puts(" 5 Sort by sales Volume."); puts(" 6 Enquire the records of stocking."); puts(" 7 Enquire the records of exporting."); puts(" 8 Purchase one kind of book."); puts(" 9 Sell one kind of book."); puts(" 0 Operation list."); puts("-Input a digit less than 0 to exit the system." ); } int main() { puts("-Welcome to use PiBook book management system!"); operationList(); for(int op,tmp;;) { scanf("%d",&op); if(op9){puts("-Invalid input!");continue;} if(op&&op


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭