简体   繁体   English

在C ++中使用堆栈评估后缀表达式

[英]Using Stacks in C++ to Evaluate the Postfix Expression

Ok, I already have it in postfix notation and I am sending over a string variable that will have the postfix notation as something such as: 5 15 2 *+ Here is my code: 好的,我已经用后缀符号表示了,并且我正在发送一个字符串变量,该变量将具有后缀符号,例如:5 15 2 * +这是我的代码:

int evaluatePostFix(string postfix_expression){
//Create a new stack
stack<int> theStack;
//Loops while the postfix expression string still contains values
while(postfix_expression.length()>=1){
    //Loops on a number an whitespace
    while(isdigit(postfix_expression.at(0)) || isspace(postfix_expression.at(0))){
        //Holds a number that is above two digits to be added to the stack
        string completeNum;
        if(isdigit(postfix_expression.at(0))){ 
            //Add the digit so it can become a full number if needed
            completeNum+=postfix_expression.at(1);
        }
        else {
            //Holds the integer version of completeNum
            int intNum;
            //Make completeNum an int
            intNum=atoi(completeNum.c_str());
            //push the number onto the stack
            theStack.push(intNum);
        }
        //Check to see if it can be shortened
        if(postfix_expression.length()>=1){
            //Shorten the postfix expression
            postfix_expression=postfix_expression.substr(1);
        }
    }
    //An Operator has been found
    while(isOperator(postfix_expression.at(0))){
        int num1, num2;
        char op;
        //Grabs from the top of the stack
        num1=theStack.top();
        //Pops the value from the top of the stack - kinda stupid how it can return the value too
        theStack.pop();
        //Grabs the value from the top of the stack
        num2=theStack.top();
        //Pops the value from the top of the stack
        theStack.pop();
        //Grab the operation
        op=postfix_expression.at(0);
        //Shorten the postfix_expression
        postfix_expression=postfix_expression.substr(1);
        //Push result onto the stack
        theStack.push(Calculate(num1,num2, op));
    }
}
return theStack.top();

} }

The error I get is "Deque iterator not deferencable" 我得到的错误是“不可延迟的Deque迭代器”

Any help that I can get on this error would be much appreciated. 我可以就此错误获得的任何帮助将不胜感激。 btw I haven't used C++ in a couple of years so I'm a bit rusty. 顺便说一句,我已经有两年没有使用C ++了,所以我有点生锈。

It would be easier if you told us which line was causing the error by stepping through with a debugger. 如果您通过逐步调试通过告诉我们哪一行导致了错误,那会更容易。 However, I think I may have spotted the error. 但是,我认为我可能已经发现了错误。

In this block of code 在这段代码中

if(isdigit(postfix_expression.at(0))){ 
    //Add the digit so it can become a full number if needed
    completeNum+=postfix_expression.at(1);
}

You ask for the postfix_expression.at(1) without ever checking if that element exists. 您要求postfix_expression.at(1),而无需检查该元素是否存在。 Since there is no check, you might be accessing bad memory locations. 由于没有检查,您可能正在访问错误的内存位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM