Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在堆栈中移动元素_C++_Data Structures_Stack - Fatal编程技术网

C++ 在堆栈中移动元素

C++ 在堆栈中移动元素,c++,data-structures,stack,C++,Data Structures,Stack,我有一个使用数组实现的堆栈,它接受6个字符作为用户的输入(小写和大写字母)。然后将大写字母转移到另一个堆栈(将其从原始堆栈中移除) 我有下面的代码,它将元素沿着堆栈移动到顶部,然后在到达顶部后弹出,并将其推到新数组上 for (int i = P->top(); i >= 0; i--) { if (P->data[i] >= 65 && P->data[i] <= 90) { for (int j = i; j < P-

我有一个使用数组实现的堆栈,它接受6个字符作为用户的输入(小写和大写字母)。然后将大写字母转移到另一个堆栈(将其从原始堆栈中移除)

我有下面的代码,它将元素沿着堆栈移动到顶部,然后在到达顶部后弹出,并将其推到新数组上

for (int i = P->top(); i >= 0; i--)
{
  if (P->data[i] >= 65 && P->data[i] <= 90)
  {
    for (int j = i; j < P->top(); i++)
    {
      char swap = P->data[i + 1];
      P->data[i + 1] = P->data[i];
      P->data[i] = swap;
      stackT.push(P->pop());
      i = P->top();
    }
  }
}
for(inti=P->top();i>=0;i--)
{
如果(P->data[i]>=65&&P->data[i]top();i++)
{
字符交换=P->数据[i+1];
P->data[i+1]=P->data[i];
P->数据[i]=交换;
stackT.push(P->pop());
i=P->top();
}
}
}
我应该有另一个带有大写字母和小写字母的数组。

它会得到奇怪的结果,有时它会完全破坏原始数组,有时它只传输一个大写元素,而将其他元素弄乱

我只是没有主意

是的,问题仅限于堆栈的使用,不,这不是家庭作业


提前谢谢

不要乱动堆栈内的东西。
(我希望本练习的目的是只使用堆栈操作,并且只能在“顶部”修改堆栈-其他任何内容都不是堆栈。)

而是使用第三个临时堆栈

stack original = .. whatever ...;
stack uppercase;
stack other;
// Classify the contents
while (!original.empty())
{
    char c = original.top();
    original.pop();
    if (std::isupper(c))
    {
        uppercase.push(c);
    }
    else
    {
        other.push(c);
    }
}
// Restore the non-uppercase stack.
while (!other.empty())
{
    original.push(other.top());
    other.pop();
}

一个最小的、完整的、可编译的代码会很有帮助。除非你只是在寻找建议,否则我会说使用调试器或尝试使用单元测试。你问了一个非问题。从小处做起。如果堆栈只包含一个元素并且代码运行,那么堆栈会发生什么情况?回答这个问题可能会让你找到你所描述的问题的原因。代码清晰:将>=65更改为='a',并且我将要创建的临时堆栈的大小如何?我不知道堆栈界面是什么样子。如何确定“大写”堆栈的大小?使用我制作的堆栈实现->