商品货架管理

4.商品货架管理

  1. 需求分析:
    商品货架可以看为栈,在每次上货时,需翻转货架,使新商品在栈的底部,实现进货,出售商品,展示货架上商品的操作
  2. 概要设计:
  • 抽象数据结构:

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{

    1. Purchase_Product_Empty() {
      Push()
      }
    2. Purchase_Product_noEmpty() {
      • InitStack()
      • Pop()
      • Push()
        }
  • Sell_Product(){
    Pop()
    }
  • Inquire_Number()
    }
  • 主程序流程:
    初始化原货架–>用户选择操作–>操作结束
    ###3. 详细设计:
  • 定义商品的结构体:
typedef int Status;
typedef struct Elem{
	char name[50] = { '\0' };//商品名
	char data[30] = { '\0' };//商品截止日期
}ElemType;
  • 定义顺序栈的基本数据结构:
#define maxsize 100
typedef int Status;
typedef int 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;
}
  • 定义货架为空时,进货的函数:
    货架为空,直接入栈即可
    注意需判断满
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");
}
  • 定义货架不为空时,进货的函数:
    先将原货架中商品依次拿出,放入周转货架中,再将新货放入周转货架中,再将周转货架中商品依次取出,放入原货架中。
    假设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");
}
  • 出售商品:
    用户输入购买商品数,商品出栈,判断是否达到用户购买商品数,若未达到,则让用户选择是否继续购买,若需购买,则该栈变为空栈,若不需购买,则顶部指针返回原位
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("商品数:%d\n", S.top - S.base);
	while (S.top != S.base) {
		S.top--;
		printf("%s\t\t\t\t%s\n", S.top->name,S.top->data);
	}
}
  1. 完整代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define maxsize 100
typedef int Status;
typedef struct Elem{
	char name[50] = { '\0' };
	char data[30] = { '\0' };
}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("商品数:%d\n", S.top - S.base);
	while (S.top != S.base) {
		S.top--;
		printf("%s\t\t\t\t%s\n", 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("---------------------\n\n\n");
		printf("1.进货\n2.出售\n3.查询商品数\n4.结束\n");
		printf("请选择:");
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值