嵌入式Linux C++练习7——栈和队列实现简单的停车场管理系统

题目:

设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆根据到达先后依次从停车场最里面向大门口处停放(最先到达的第一辆车放在停车场的最里面)。如果停车场已放满n辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排在便道上的第一辆车就进入停车场。停车场内如有某辆车要开走,在他之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些车辆在依原来的次序进场。编制模拟该停车场的管理的程序。

思路:

最里面的车子想出来,需要先让外面的车子都出去,等该车出去后再依次停进,在停车场外,有一个等待进入停车场的队列,后续的车子都跟在尾巴上,当停车场内有汽车出来时,在队列头的汽车依次停入

一点碎碎念:这次的代码属于想到哪写到哪的那种,比较凌乱,以后有时间会重新梳理一遍

#include <iostream>
#include <string>
#include <stack>
#include <queue>
using namespace std;

#define name_max 20
#define stack_max 3 //停车场容量

class mystack
{
    
    
public:
    void mypush(string name);
    void mypop();
    int mysize();
    string mytop();
    int stackfull = -1; //-1为空,0为不满,1为满

protected:
    string name[name_max];
    int top;
    stack<string> stk;
};

int mystack::mysize()
{
    
    
    return stk.size();
}

string mystack::mytop()
{
    
    
    return stk.top();
}

void mystack::mypush(string name)
{
    
    
    stackfull = 0;
    stk.push(name);

    if (stk.size() == stack_max)
    {
    
    
        stackfull = 1;
    }
}

void mystack::mypop()
{
    
    
    stk.pop();

    if (stk.size() == 0)
    {
    
    
        stackfull = -1;
    }
}

class myqueue
{
    
    
public:
    void mypush(string name);
    string mypop();
    bool queuefull = 0; //车道上是否有车,有为1,无为0

private:
    string name[name_max];
    queue<string> q;
};

void myqueue::mypush(string name)
{
    
    
    q.push(name);
    queuefull = 1;
}

string myqueue::mypop()
{
    
    
    string head = q.front();
    q.pop();
    if (q.size() <= 0)
    {
    
    
        queuefull = 0;
    }
    return head;
}

class carstop
{
    
    
public:
    void pushsk(string name);
    void pushqueue(string name);

    void popsk(int temp); //stack temp;
    void popqueue();

    void displaystack(); //显示停车场里的车流信息
    void displayqueue(); //显示路边的车流信息

private:
    myqueue q;
    mystack sk;
};

void carstop::pushsk(string name)
{
    
    
    if (sk.stackfull != 1)
    {
    
    
        sk.mypush(name);
    }
    else
    {
    
    
        pushqueue(name);
    }
}

void carstop::pushqueue(string name)
{
    
    
    q.mypush(name);
}

void carstop::popsk(int temp)
{
    
    
    stack<string> sk_copy; //辅助栈

    // if (sk.stackfull = -1)
    // {
    
    
    //     cout << "当前停车场里没有车!" << endl;
    //     return;
    // }

    for (int i = 0; i <= (sk.mysize() - temp); i++)
    {
    
    
        sk_copy.push(sk.mytop());
        sk.mypop();
    }
    cout << sk.mytop() << "离开!" << endl;
    sk.mypop();

    while (sk_copy.size() > 0)
    {
    
    
        sk.mypush(sk_copy.top());
        // cout << sk_copy.top() << endl;
        sk_copy.pop();
    }

    sk.stackfull = 0; //车辆离开,停车场有空位
}

void carstop::popqueue()
{
    
    
    sk.mypush(q.mypop());
}

void carstop::displaystack()
{
    
    
    stack<string> stk_copy;

    if (sk.stackfull == -1)
    {
    
    
        cout << "当前停车场里没有车!" << endl;
    }

    else if (sk.stackfull >= 0)
    {
    
    
        while (sk.stackfull != -1)
        {
    
    
            stk_copy.push(sk.mytop());
            // cout << stk_copy.top() << endl;
            sk.mypop();
        }

        cout << "当前停车场里有车:" << endl;

        while (stk_copy.size() > 0)
        {
    
    
            sk.mypush(stk_copy.top());
            cout << stk_copy.top() << endl;
            stk_copy.pop();
        }
    }
}

void carstop::displayqueue()
{
    
    
    if (q.queuefull == 0)
    {
    
    
        cout << "当前没有汽车等待!" << endl;
    }
    else
    {
    
    
        cout << "目前等待的车有:" << endl;
        queue<string> q_copy;

        while (q.queuefull == 1)
        {
    
    
            string data = q.mypop();
            cout << data << " " << endl;
            q_copy.push(data);
        }

        while (q_copy.size() != 0)
        {
    
    
            q.mypush(q_copy.front());
            q_copy.pop();
        }
    }
}

int main()
{
    
    
    carstop park;

    park.pushsk("苏A1111");
    park.pushsk("苏A1112");
    park.pushsk("苏A1113");
    park.pushsk("苏A1114");
    park.pushsk("苏A1115");
    park.pushsk("苏A1116");
    park.pushsk("苏A1117");

    park.displaystack();
    park.displayqueue();

    park.popsk(1);
    park.popqueue();

    park.displaystack();
    park.displayqueue();

    park.popsk(3);
    park.popqueue();

    park.displaystack();
    park.displayqueue();

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45792897/article/details/119655226