栈货架管理系统模拟

用栈来模拟货架管理系统

[问题描述]
商店货架以栈的方式摆放商品。生产日期越近的越靠近栈底,出货时从栈顶取货。一天营业结束,如果货架不满,则需上货。入货直接将商品摆放到货架上,则会使生产日期越近的商品越靠近栈顶。这样就需要倒货架,使生产日期越近的越靠近栈底。
[基本要求]
设计一个算法,保证每一次上货后始终保持生产日期越近的商品越靠近栈底。
[实现提示]
可以用一个队列和一个临时栈作为周转。
[测试数据]
由学生任意指定。

#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct sqstack
{
    char *base;
    char *top;
}sqstack,*Stack;
typedef struct sqqueue
{
    char *quefront;
    char *querear;
}sqqueue,*Queue;
void Initstack(sqstack &sta)
{
    sta.base=new char[MAXSIZE];
    sta.top=sta.base;
}
char push(sqstack &sta,char c)
{
    if(sta.top-sta.base>=MAXSIZE)return -1;
    else {
        *sta.top++=c;
        return c;
    }
}
char pop(sqstack &sta)
{
    if(sta.top==sta.base)
        return -1;
    else{
        sta.top--;
        return *sta.top;
    }
}
void Initqueue(sqqueue &que)
{
    que.quefront=new char[MAXSIZE];
    que.querear=que.quefront;
}
char push(sqqueue &que,char c)
{
    if(que.querear-que.quefront>=MAXSIZE)return -1;
    else *que.querear++=c;
    return c;
}
char pop(sqqueue &que)
{
    if(que.querear==que.quefront)return -1;
    else {
        return *que.quefront++;
    }
}
bool isStackEmpty(sqstack sta)
{
    if(sta.base==sta.top)return true;
    else return false;
}
bool isQueueEmpty(sqqueue que)
{
    if(que.quefront==que.querear)return true;
    else return false;
}
void display(sqstack sta)
{
    while(!isStackEmpty(sta))
    {
        cout<<pop(sta)<<" ";
    }
}
int main()
{
    sqstack sta;
    sqstack que;
    Initstack(sta);
    Initstack(que);
    int control;
    cout<<"功能:1上货,2出货,0显示当前货物,-1退出"<<endl;
    cout<<"控制台:";
    cin>>control;
    while(control!=-1)
    {
        cout<<"*************************************************"<<endl;
        if(control==1)
        {
            cout<<"上货以#结束"<<endl;
            if(isStackEmpty(sta))
            {
                char goods;
                while(cin>>goods)
                {
                    if(goods=='#')break;
                    push(sta,goods);
                }
            }
            else
            {
                char exgoods;
                char goods;
                while(!isStackEmpty(sta))
                {
                    exgoods=pop(sta);
                    push(que,exgoods);
                }
                while(cin>>goods)
                {
                    if(goods=='#')break;
                    push(sta,goods);
                }
                while(!isStackEmpty(que))
                {
                    exgoods=pop(que);
                    push(sta,exgoods);
                }
            }
            cout<<"上货完成"<<endl;
        }
        else if(control==2)
        {

            if(!isStackEmpty(sta))
            {
                cout<<"出货成功:"<<pop(sta)<<endl;
            }
            else cout<<"货物不足,出货失败"<<endl;
        }
        else if(control==0)
        {
            display(sta);
            cout<<endl;
        }
        cout<<"控制台:";
        cin>>control;
    }
    cout<<"成功退出系统"<<endl;
}

管理系统总结:
这道题起初我打算用一个队列一个栈来模拟上货和出货中间进行转变的过程,后来调试出了bug,如果货架用栈,中间用队列来进行中转,只有当上上一次的物品被卖光以后才不会出问题,不然在第三次添加中转的过程中第一次的剩余物品会到第二次的后面,导致商品顺序错误。因此为了避免这种情况,中转也用了栈来代替。保证每次都是最早添加的那一批商品最先出货。
其中,上货需要分情况,前面的货物卖光可以直接向货架栈添加,如果还有剩余物品则需要先将货架栈中的物品先转移到中转栈中,再向货架栈中上新货,然后将中转栈中的货物转移进来,完成操作。
商品货架的管理系统有很多种方式的写法,用顺序表来模拟是最基础的一种,也可以用链栈来模拟,更为简单方便。

测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值