ADT{
InitStack(SqStack& S) //初始化栈S
Push(SqStack& S, ElemType e) //入栈
Pop(SqStack& S, ElemType& e) //出栈
Purchase_Product_noEmpty //货架不为空时进货
Purchase_Product_Empty //货架为空时进货
Sell_Product(SqStack& S, int num)//出售商品
void Inquire_Number(SqStack S)//展示货架当前商品
}
main{
Purchase_Product_Empty() {typedef int Status; typedef struct Elem{char name[50] = { ' ' };//商品名char data[30] = { ' ' };//商品截止日期 }ElemType; 12345 定义顺序栈的基本数据结构:
#define maxsize 100 typedef int Status; typedef int ElemType; typedef struct {//顺序栈数据结构ElemType* base;ElemType* top;int stacksize; }SqStack; 12345678 定义栈的初始化函数:
Status InitStack(SqStack& S) {//初始化栈S.base = (ElemType*)malloc(maxsize * sizeof(ElemType));if (!S.base) return 0;S.top = S.base;S.stacksize = maxsize; } 123456 定义入栈函数:
Status Push(SqStack& S, ElemType e) {//入栈if (S.top-S.base==S.stacksize) return 0;*(S.top++) = e;return 1; } 12345 定义货架为空时,进货的函数:
货架为空,直接入栈即可
注意需判断满
void Purchase_Product_Empty(SqStack& S,ElemType e,int num) {//货架为空时进货printf("请输入商品名:");scanf_s("%s", e.name,30);printf("请输入商品的截止日期:");scanf_s("%s", e.data, 20);printf("请输入商品数:");scanf_s("%d", &num);while (num--) {if (Push(S, e) == 0) {printf("货架已满,请更换更大的货架!n");return;}}printf("进货成功!n"); } 123456789101112131415 定义货架不为空时,进货的函数:
先将原货架中商品依次拿出,放入周转货架中,再将新货放入周转货架中,再将周转货架中商品依次取出,放入原货架中。
假设S代表原货架,S1代表周转货架,操作为S出栈,入S1,新商品入栈S1,S1出栈,入S
注意判断S,S1在入栈时是否为满
void Purchase_Product_noEmpty(SqStack& S, SqStack& S1,int num,ElemType m) {//货架不为空时进货InitStack(S1);ElemType e;printf("请输入商品名:");scanf_s("%s", m.name, 30);printf("请输入商品的截止日期:");scanf_s("%s", m.data, 20);printf("请输入商品数:");scanf_s("%d", &num);while (Pop(S,e)) {//放入周转货架Push(S1, e);}while (num--) {//新商品入周转货架if (Push(S1, m) == 0) {printf("周转货架已满,请选用更大的周转货架!n");return;}}while (Pop(S1, e)) {//周转货架入原货架if (Push(S, e) == 0) {printf("原货架已满,请选用更大的原货架!n");return;}}printf("进货成功!n"); }
1234567891011121314151617181920212223242526 出售商品:void Sell_Product(SqStack& S, int num) {//出售商品ElemType m;ElemType* p = S.top;int a;printf("请选择出售商品数:");scanf_s("%d", &num);while (num--) {if (Pop(S, m) == 0) {printf("商品不足!仅能购买%d个n", p - S.base);printf("是否还购买商品?n");printf("1.继续购买n2.不购买n");printf("请选择:");scanf_s("%d", &a);if (a == 2) {S.top = p;return;}printf("出售成功!n");return;}}printf("出售成功!n"); } }
123456789101112131415161718192021222324 展示当前货架上商品:void Inquire_Number(SqStack S) {//展示当前货架商品printf("商品数:%dn", S.top - S.base);while (S.top != S.base) {S.top--;printf("%stttt%sn", S.top->name,S.top->data);} } 1234567 完整代码:
#include <cstdio> #include <cstdlib> #include <cstring> #define maxsize 100 typedef int Status; typedef struct Elem{char name[50] = { ' ' };char data[30] = { ' ' }; }ElemType; typedef struct {//顺序栈数据结构ElemType* base;ElemType* top;int stacksize; }SqStack; Status InitStack(SqStack& S) {//初始化栈S.base = (ElemType*)malloc(maxsize * sizeof(ElemType));if (!S.base) return 0;S.top = S.base;S.stacksize = maxsize; } Status Push(SqStack& S, ElemType e) {//入栈if (S.top - S.base == S.stacksize) return 0;*(S.top++) = e;return 1; } Status Pop(SqStack& S, ElemType& e) {//出栈if (S.top == S.base) return 0;e = *(--S.top);return 1; } void Purchase_Product_noEmpty(SqStack& S, SqStack& S1,int num,ElemType m) {//货架不为空时进货InitStack(S1);ElemType e;printf("请输入商品名:");scanf_s("%s", m.name, 30);printf("请输入商品的截止日期:");scanf_s("%s", m.data, 20);printf("请输入商品数:");scanf_s("%d", &num);while (Pop(S,e)) {//放入周转货架Push(S1, e);}while (num--) {//新商品入周转货架if (Push(S1, m) == 0) {printf("周转货架已满,请选用更大的周转货架!n");return;}}while (Pop(S1, e)) {//周转货架入原货架if (Push(S, e) == 0) {printf("原货架已满,请选用更大的原货架!n");return;}}printf("进货成功!n"); } void Sell_Product(SqStack& S, int num) {//出售商品ElemType m;ElemType* p = S.top;int a;printf("请选择出售商品数:");scanf_s("%d", &num);while (num--) {if (Pop(S, m) == 0) {printf("商品不足!仅能购买%d个n", p - S.base);printf("是否还购买商品?n");printf("1.继续购买n2.不购买n");printf("请选择:");scanf_s("%d", &a);if (a == 2) {S.top = p;return;}printf("出售成功!n");return;}}printf("出售成功!n"); } void Inquire_Number(SqStack S) {//展示当前货架商品printf("商品数:%dn", S.top - S.base);while (S.top != S.base) {S.top--;printf("%stttt%sn", S.top->name,S.top->data);} } void Purchase_Product_Empty(SqStack& S,ElemType e,int num) {//货架为空时进货printf("请输入商品名:");scanf_s("%s", e.name,30);printf("请输入商品的截止日期:");scanf_s("%s", e.data, 20);printf("请输入商品数:");scanf_s("%d", &num);while (num--) {if (Push(S, e) == 0) {printf("货架已满,请更换更大的货架!n");return;}}printf("进货成功!n"); } int main() {SqStack S, S1;//原货架与周转货架int k,m=0,n=0;ElemType a, b, c;InitStack(S);printf("1.进货n2.出售n3.查询商品数n4.结束n");printf("请选择:");while (scanf_s("%d", &k) != EOF) {switch (k) {case 1:if (S.top == S.base) {//货架为空Purchase_Product_Empty(S, a, m);}else {Purchase_Product_noEmpty(S, S1, n, b);}break;case 2:Sell_Product(S, m);break;case 3:Inquire_Number(S);break;case 4:return 0;}printf("---------------------nnn");printf("1.进货n2.出售n3.查询商品数n4.结束n");printf("请选择:");} }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132相关知识
实承 货架仓储货架层架仓库货物架子家用金属货架超市展示架钢制储物架置物架 轻型白色主架 100*40*200=4层 205元
母婴用品店货架定制生产
商品养护的技术和方法.pptx
电商,到了该回归货架的时候 – 人人都是产品经理,
鲜花店鲜花陈列的注意事项有哪些?花店货架怎么排放?求答案
创意客厅落地办公室隔断 铝型材书架置物架 展示架 货架 物料架 书柜
如何判断货架承重是否合格?
花店商品摆放要点让你客流滚滚
商品库存管理系统哪个好
爱库存,商品库存管理系统
网址: 商品货架管理 https://m.huajiangbk.com/newsview948700.html
上一篇: 电商产品上架专员岗位职责(工作内 |
下一篇: Lazada商品上架步骤详解,完 |