理解后缀表达式求值代码

发布于 2024-12-09 08:22:51 字数 913 浏览 1 评论 0原文

我正在尝试理解这段代码。它的作用是后缀表达式求值。我在理解代码时遇到问题。如果有人能帮助我,我将非常感激。

#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
int main()
{


    //suppose a contains the string..
    //n is the length of string...
    char a[10]="A+B/C";
    int n = strlen(a)
    stack<int>s;
    for (int i=0;i<n;i++)
    {
        if (a[i]=='+')
        {
            s.push(s.pop()+s.pop());
            int temp1 = s.top();
            s.pop();
            int temp2 = s.top();
            s.pop();
            s.push(temp1 * temp2);
        }
        if (a[i]=='*')
            s.push(s.pop() * s.pop());

        if ((a[i]>='0') && (a[i]<='9'))
            s.push(0);
        while ((a[i]>='0') && (a[i]<='9'))
            s.push(10*s.pop()+(a[i++]-'0'));
    }

    cout<<s.pop()<<endl;

    return 0;
}

提前致谢。

I am trying to understand this piece of code. What it does it postfix expression evaluation. I am having problems understanding the code. I will be very thankful if anyone can help me out.

#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
int main()
{


    //suppose a contains the string..
    //n is the length of string...
    char a[10]="A+B/C";
    int n = strlen(a)
    stack<int>s;
    for (int i=0;i<n;i++)
    {
        if (a[i]=='+')
        {
            s.push(s.pop()+s.pop());
            int temp1 = s.top();
            s.pop();
            int temp2 = s.top();
            s.pop();
            s.push(temp1 * temp2);
        }
        if (a[i]=='*')
            s.push(s.pop() * s.pop());

        if ((a[i]>='0') && (a[i]<='9'))
            s.push(0);
        while ((a[i]>='0') && (a[i]<='9'))
            s.push(10*s.pop()+(a[i++]-'0'));
    }

    cout<<s.pop()<<endl;

    return 0;
}

Thanks in advance.

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

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

发布评论

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

评论(1

囚我心虐我身 2024-12-16 08:22:51

这个网站看起来是一个很好的资源,可以了解这里发生的事情,但表达式应该使用数字,而不是字母。

假设您有中缀表达式 1+2*3-4*5。相应的后缀为 123*+45*-。首先,您首先从左到右扫描字符串。前三个数字是操作数,因此它们将以 1(底部)、2(中间)、3(顶部)的顺序存储在堆栈中。接下来,有一个 * 运算符。为了解决这个问题,请将前两个操作数从堆栈中弹出,然后将它们相乘(弹出的第一个操作数是右操作数,第二个操作数是左操作数)。这将计算为 2*3=6,并且 6 将存储在堆栈中,使其成为 1, 6。

接下来,有一个 + 运算符。 1 和 6 被弹出并相加,7 被存储在堆栈中。此后,4 和 5 也被压入堆栈(7,4,5)。下一个字符是另一个 * 运算符,因此它计算 4*5=20 并将 20 压入堆栈 (7, 20)。

最后,还有一个 - 运算符。 7 和 20 被弹出并计算为 7-20=(-13)。它被压入堆栈并准备好作为最终答案弹出。

希望这有助于消除任何困惑(假设我正确阅读了您的问题)。

This site looks to be a good resource on what's happening here, but the expression should use numbers, not letters.

Say you have the infix expression 1+2*3-4*5. The corresponding postfix would be 123*+45*-. First, you start by scanning the string from left to right. The first three numbers are operands so they will be stored on the stack in the order 1 (bottom), 2 (middle), 3 (top). Next, there is a * operator. To deal with this, pop the first two operands off of the stack and multiply them (the first one popped is the right operand and the second is the left). This will evaluate to 2*3=6, and 6 will be stored on the stack, making it 1, 6.

Next, there is a + operator. 1 and 6 are popped and added and 7 is stored on the stack. After this, 4 and 5 are pushed on the stack as well (7, 4, 5). The next character is another * operator, so it evaluates 4*5=20 and pushes 20 onto the stack (7, 20).

Finally, there is a - operator. 7 and 20 are popped and evaluated as 7-20=(-13). This is pushed onto the stack and is ready to be popped as your final answer.

Hope that helps clear up any confusion (assuming I read your question correctly).

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