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

发布于 2024-12-11 11:04:56 字数 2064 浏览 0 评论 0原文

好的,我已经有了后缀表示法,并且我正在发送一个字符串变量,该变量将具有后缀表示法,例如: 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();

}

我得到的错误是“Deque iterator not deferencable”

如果我能获得有关此错误的任何帮助,我们将不胜感激。 顺便说一句,我已经有几年没有使用过 C++了,所以我有点生疏了。

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:

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"

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2024-12-18 11:04:56

如果您通过使用调试器单步执行来告诉我们哪一行导致了错误,那就更容易了。不过,我想我可能已经发现了这个错误。

在这段代码中,

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

您请求 postfix_expression.at(1) ,而无需检查该元素是否存在。由于没有检查,您可能正在访问错误的内存位置。

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. Since there is no check, you might be accessing bad memory locations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文