【问题标题】:Last command's output is doubled (trying to print stack elements)最后一个命令的输出加倍(尝试打印堆栈元素)
【发布时间】:2020-01-22 21:32:21
【问题描述】:

因此,任务是使用数组实现简单的堆栈,并编写一个使用命令行命令调用堆栈方法的程序。例如:

  • set_size 5 – 调用一个返回包含 5 个元素的堆栈的函数
  • push N – 调用 stack.push(N)
  • pop - 调用 stack.pop()
  • print – 调用堆栈打印

对应的代码如下。 :

#include <iostream>
#include <vector>
#include <string>
#include <regex>
#include <sstream>

template <class T>
class Stack
{
    int size = 0;
    T* Array;
    int top = 0;

public:
    Stack(size_t Size);
    ~Stack()
    {
        delete[] Array;
    }
    void push(T element);
    void pop();
    void print();
};
template <class T>
Stack<T>::Stack(size_t Size)
{
    size = Size;
    top = -1;
    Array = new T[size];
}
template <class T>
void Stack<T>::push(T element)
{
    if (top >= (size - 1))
    {
        std::cout << "overflow" << std::endl;
    }
    else
    {
        Array[++top] = element;
    }
}
template <class T>
void Stack<T>::pop()
{
    if (top < 0)
    {
        std::cout << "underflow" << std::endl;
    }
    else
    {
        std::cout << Array[top--] << std::endl;
    }

}
template <class T>
void Stack<T>::print()
{
    if (top == -1)
    {
        std::cout << "empty" << std::endl;
    }
    int i = -1;
    while (++i <= top)
    {
        std::cout << Array[i] << " ";
    }
    std::cout << std::endl;
}
template <class T>
Stack<T> set_size(int Size)
{
    return Stack<T>(Size);
}

int main()
{   
    int size = 0;
    std::string command, line, element;
    std::cin >> command >> size;
    if (command == "set_size")
    {
        auto stack = set_size<std::string>(size);
        while (std::getline(std::cin, line))
        {
            std::istringstream is(line);
            is >> command;
            if (command == "push")
            {
                is >> element;
                if (is.rdbuf()->in_avail() == 0)
                {
                    stack.push(element);
                }
                else
                {
                    std::cout << "error" << std::endl;
                }
            }
            if (command == "pop")
            {
                stack.pop();
            }
            if (command == "print")
            {
                stack.print();
            }
        }
    }
    return 0;
}

但由于某种原因,测试实用程序的输出与我在 Visual Studio 中得到的不同——最后一个命令重复了两次。实用程序的输入:

set_size 5
pop
push 1 10
push 2
push 3
push 4
push 5
print
push 6
pop
push 7
print

编辑: 出于某种原因,在命令行中按 enter 时会重复上一个命令,因此该实用程序会重复最后一个 print。但我仍然不知道为什么以及如何解决这个问题。

以下是输出:

实用程序:

underflow
error
2 3 4 5 
6
2 3 4 5 7 
2 3 4 5 7 

所需的输出(我也在 Visual Studio 中得到的输出):

underflow
error
2 3 4 5
6
2 3 4 5 7

我认为这在某种程度上与 istringstream 的使用或我的命令输入方式有关,但我无法弄清楚它为什么不同以及如何解决它。或者有没有办法让它更聪明/更简单? (注意:我需要检查是否只有 1 个 push 参数,所以整个 istringstream 就是为了做到这一点)

【问题讨论】:

  • 我没有得到副本。
  • @0x499602D2 在没有输入的情况下按 Enter 键时出现重复项 - 重复最后一个命令。有什么办法解决吗?

标签: c++ arrays stack


【解决方案1】:

当您输入一个空字符串时,随后的输入操作is &gt;&gt; command 将失败,留下command 的最后一个值,即print。要解决此问题,请创建一个局部变量以在循环内使用,或在输入操作之前调用 command.clear()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2021-09-03
    • 2017-10-01
    • 1970-01-01
    相关资源
    最近更新 更多