C++ C++;stringstream函数不能正确返回 #包括 #包括 #包括 #包括 使用名称空间std; 堆栈堆栈堆栈; 流结果; 堆栈操作数1; 堆栈操作数2; stringstream和postfixExp(字符串ch){ 对于(int i=0;i对象(在函数内使用 String Strue): 错误:尝试访问空容器中的元素。对于用于测试的字符串(“2+3”),当到达“+”运算符时,堆栈中只有一个值,因此当您尝试从堆栈中弹出两个操作数时,可能会遇到错误。您的测试字符串是否为“23+”?此外,您的答案会有很大偏差,因为您添加的是字符的ascii值,而不是实际的数字。。。"结果当+之前只有1个操作数时,你尝试从堆栈中弹出两个操作数。谢谢!你非常有帮助。我将阅读更多关于你关于引用的建议,但现在对我来说重要的是让我的程序正确返回后缀表示法的结果。同时,我将代码稍微更改为。结果我的新后缀符号“34+2*”应该是14,但它显示的是“g” stringstream result; stringstream& postfixExp(string ch) { ... return result; } std::string postfixExp(const std::string& expression) { std::ostringstream resultStream; ... return result.str(); }

C++ C++;stringstream函数不能正确返回 #包括 #包括 #包括 #包括 使用名称空间std; 堆栈堆栈堆栈; 流结果; 堆栈操作数1; 堆栈操作数2; stringstream和postfixExp(字符串ch){ 对于(int i=0;i对象(在函数内使用 String Strue): 错误:尝试访问空容器中的元素。对于用于测试的字符串(“2+3”),当到达“+”运算符时,堆栈中只有一个值,因此当您尝试从堆栈中弹出两个操作数时,可能会遇到错误。您的测试字符串是否为“23+”?此外,您的答案会有很大偏差,因为您添加的是字符的ascii值,而不是实际的数字。。。"结果当+之前只有1个操作数时,你尝试从堆栈中弹出两个操作数。谢谢!你非常有帮助。我将阅读更多关于你关于引用的建议,但现在对我来说重要的是让我的程序正确返回后缀表示法的结果。同时,我将代码稍微更改为。结果我的新后缀符号“34+2*”应该是14,但它显示的是“g” stringstream result; stringstream& postfixExp(string ch) { ... return result; } std::string postfixExp(const std::string& expression) { std::ostringstream resultStream; ... return result.str(); },c++,pointers,stack,stringstream,C++,Pointers,Stack,Stringstream,后缀算术运算的测试输入应为“23+”的形式,而不是“2+3” 可能应该添加一些检查,以确保您没有像其他人提到的那样从空堆栈中弹出。正如前面指出的,当aStack容器为空时调用aStack.pop();会导致错误。这可能会产生未定义的行为(在本例中)您可以观察到应用程序崩溃 解释很简单,您可以逐个字符处理字符串“2+3”: #include <iostream> #include <stack> #include <string>

后缀算术运算的测试输入应为“23+”的形式,而不是“2+3”


可能应该添加一些检查,以确保您没有像其他人提到的那样从空堆栈中弹出。

正如前面指出的,当
aStack
容器为空时调用
aStack.pop();
会导致错误。这可能会产生未定义的行为(在本例中)您可以观察到应用程序崩溃

解释很简单,您可以逐个字符处理字符串
“2+3”

    #include <iostream>
    #include <stack>
    #include <string>
    #include <sstream>

    using namespace std;

    stack<char> aStack;
    stringstream result;
    stack<char> operand1;
    stack<char> operand2;


    stringstream &postfixExp(string ch){
      for(int i =0; i< ch.length(); i++)
      {
        if(ch[i]== '1' || ch[i]== '2' || ch[i]== '3' || ch[i]== '4' || ch[i]== '5' || ch[i]== '6' || ch[i]== '7' || ch[i]== '8' || ch[i]== '9' || ch[i]== '0' )
        {
          aStack.push(ch[i]);
        }

        else if( ch[i]== '+')
        {
          operand2.push(aStack.top());
          aStack.pop();

          operand1.push(aStack.top());
          aStack.pop();

      result << ( operand1.top() + operand1.top());
    }

  }

  return result;
}

int main()
{
    string postfix = " 2+3";

    stringstream* answer = &postfixExp(postfix);
    cout << "Result = " << answer->str() << endl;;


  return 0;
}
…那么,一旦到达
“+”
符号,您认为
“2+3”
字符串中会发生什么情况


也考虑重新设计:

for each character:
    if it is digit:
        push it to stack
    if it is '+':
        pop 2 elements

…返回一个对全局变量的引用,这意味着你要么不应该返回任何东西,要么变量不应该是全局的。但是更好的是,只考虑通过<代码> STD::String < /Cord>对象(在函数内使用<代码> String Strue):


错误:尝试访问空容器中的元素。对于用于测试的字符串(“2+3”),当到达“+”运算符时,堆栈中只有一个值,因此当您尝试从堆栈中弹出两个操作数时,可能会遇到错误。您的测试字符串是否为“23+”?此外,您的答案会有很大偏差,因为您添加的是字符的ascii值,而不是实际的数字。。。"结果当+之前只有1个操作数时,你尝试从堆栈中弹出两个操作数。谢谢!你非常有帮助。我将阅读更多关于你关于引用的建议,但现在对我来说重要的是让我的程序正确返回后缀表示法的结果。同时,我将代码稍微更改为。结果我的新后缀符号“34+2*”应该是14,但它显示的是“g”
stringstream result;

stringstream& postfixExp(string ch) {
    ...
    return result;
}
std::string postfixExp(const std::string& expression) {
    std::ostringstream resultStream;
    ...
    return result.str();
}