#include
#include
#include
#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) {//貨架不為空時進(jìn)貨
InitStack(S1);
ElemType e;
printf("請輸入商品名:");
scanf("%s", m.name, 30);
printf("請輸入商品的生產(chǎn)日期:");
scanf("%s", m.data, 20);
printf("請輸入商品數(shù):");
scanf("%d", &num);
while (Pop(S,e)) {//放入周轉(zhuǎn)貨架
Push(S1, e);
while (num--) {//新商品入周轉(zhuǎn)貨架
if (Push(S1, m) == 0) {
printf("周轉(zhuǎn)貨架已滿,請選用更大的周轉(zhuǎn)貨架!
");
return;
while (Pop(S1, e)) {//周轉(zhuǎn)貨架入原貨架
if (Push(S, e) == 0) {
printf("原貨架已滿,請選用更大的原貨架!
");
return;
printf("進(jìn)貨成功!
");
void Inquire_Number(SqStack S) {//展示當(dāng)前貨架商品
printf("商品數(shù):%d
", S.top - S.base);
while (S.top != S.base) {
S.top--;
printf("%s %s
", S.top->name,S.top->data);
void Purchase_Product_Empty(SqStack& S,ElemType e,int num) {//貨架為空時進(jìn)貨
printf("請輸入商品的名稱:");
scanf("%s", e.name,30);
printf("請輸入商品的生產(chǎn)日期:");
scanf("%s", e.data, 20);
printf("請輸入商品數(shù):");
scanf("%d", &num);
while (num--) {
if (Push(S, e) == 0) {
printf("貨架已滿,請更換更大的貨架!
");
return;
printf("進(jìn)貨成功!
");
int main() {
SqStack S, S1;//原貨架與周轉(zhuǎn)貨架
int k,m=0,n=0;
ElemType a, b, c;
InitStack(S);
printf("請選擇你要進(jìn)行的操作
1.進(jìn)貨
2.查詢商品數(shù)
3.結(jié)束
");
while (scanf("%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:
printf("---------------------
");
Inquire_Number(S);
break;
case 3:
return 0;
printf("---------------------
");
printf("請選擇你要進(jìn)行的操作
1.進(jìn)貨
2.查詢商品數(shù)
3.結(jié)束
");