C++ 使用 STL : Stack and Queue

标签 c++ stl stack queue

我的作业要求:

One day’s data set is available in file lot.txt on First Class.

到达/离开代码:char(A 或 D)许可证字符串(例如 BOSS) 代表军事时间的时间整数值

A EARLYBIRD 630 A WORKER 700 A CEO 730 A CLERK 730 A MANAGER 800 A VP 900 D CLERK 930 A SHOPPER 1000 D CEO 1000 D EARLYBIRD 1030 D worker 1100 A 看门人 1100 D 经理 1130

车辆应记录为包含许可证和 到达时间。为简单起见,时间将是一个整数 代表军事时间。包括 C++ 字符串类。
struct Vehicle { 字符串许可证;//许可证值 int 到达;//到达军事时间 (0 – 2359) };

读取数据文件的行并重新创建汽车的运动 进出 parking 场。车辆每小时收费 8.00 美元 在他们逗留期间。部分时间四舍五入。你 可以假设只有 parking 场内的车主离开 要求。报告当天结束时留在 parking 场的任何汽车以及 费用总和。使用讨论的模板堆栈和队列类 在讲座中。

对于每个已处理的到达,您应该报告:Car with license xxxxxx 停在 xxxx 或持有 xxxxxx 牌照的汽车在 xxxx 被拒之门外 – 很多都满了!

对于每次处理的出发,您应该报告: 有执照的汽车 xxxxxx 留在 xxxx 支付 $xx.xx

当汽车离开时,我遇到了麻烦。我不熟悉堆栈和队列,但据我了解,堆栈是 LIFO,队列是 FIFO。

这是我的:

    struct Vehicle
    {
        char ad; // Arrival departure char
        string license; // license value
        int arrival; // arrival in military time
    };

    int main()
    {
        ifstream  fin;          // declare input file stream object 
        fin.open ("lot.txt");  //open data text
        stack<string> stack; // STL Stack object
        queue<string> q; // STL Queue object

        Vehicle v; // Object of struct Vehicle

        while(!fin.fail()){
            fin >> v.ad >> v.license >> v.arrival;
            if (v.ad == 'A' && stack.size() < 5){
                stack.push(v.license);
                cout << endl << "Car with license " << v.license << " parked at " << v.arrival;
            }else if(v.ad == 'A' && stack.size() >= 5){
                cout << endl << "Car with license " << v.license << " turned away at " << v.arrival << " - LOT FULL";
            }else if(v.ad == 'D'){
                string departingcar = v.license;

                for(int i=0; i<stack.size(); i++)
//am I on the right track with a for loop?
                    q.push(v.license);
                    stack.pop();
                    q.pop();
                    if(departingcar != v.license){
                        stack.push(v.license);
                    }
                }

            }
        }
        return 0;
    }

我可以毫无问题地读取车辆,但是当我必须从堆栈中取出车辆,将它们放入队列中,然后将它们重新插入堆栈时,我迷路了。

最佳答案

目前,您的 for 循环并没有真正意义,因为在您向队列添加内容后,您还会弹出前面的任何元素,永远丢失它,因为您永远不会将它保存在其他任何地方。

我假设您在这里尝试做的是找到插入堆栈中的汽车,然后将其移除。如果是这样,请执行以下操作:

string departingcar = v.license;

//find and remove the license plate from the stack
for(int i=0; i<stack.size(); i++)
{
    if (departingcar != stack.top())
    {
        q.push(stack.top());
        stack.pop();
    }
    else
    {
        stack.pop();
        break;
    }  
 }

 //put the remaining cars back in the stack and empty out the queue
 while(!q.empty())
 {
    stack.push(q.front());
    q.pop();
 }

所以基本上这里发生的事情是我们将不等于出发汽车车牌的汽车添加到队列中。如果我们遇到要删除的汽车的牌照,那么我们只是不将其添加到队列中,而是将其从堆栈中删除,然后退出 for 循环,因为我们已经找到了我们的汽车。接下来,我们只需清空队列,将之前从堆栈中移除的汽车推回到堆栈中。

关于C++ 使用 STL : Stack and Queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8114596/

相关文章:

c++ - std::basic_string 是可逆容器吗?

c++ - 阶乘(代码厨师)

c++ - 如何最优雅地获取 std::vector 缓冲区的地址?

c++ - 从函数返回 std::vector<Point> 时出现 "Corruption of the heap"错误

c++ - For-loop 或 std::any_of,我应该使用哪一个?

assembly - 在我的程序启动之前堆栈上有什么?

Clojure - 重置!原子导致堆栈溢出

c++ - 是否可以使用 opencv 将旋转图像复制到另一个图像的 rotatedrect ROI 中?

c++ - Qt 中的 Mac OS X 链接器错误; CoreGraphics 和 CGWindowListCreate

c - 这个在 c 中将中缀转换为后缀的程序给出了运行时错误。为什么?