使用堆栈了解 Java 代码中的后缀表达式求值 [英] Understanding Postfix-expression Evaluation in Java code using a stack

查看:39
本文介绍了使用堆栈了解 Java 代码中的后缀表达式求值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一段代码来破译、解释并提供任何改进建议.有人告诉我它有效,我们无法运行代码来测试它.我几乎理解它,但只需要由某人运行它以确保我对它的理解是正确的,请帮助解释我不理解的内容.我一直在做大量的研究,但仍有一些问题.

I was given a snippet of code to decipher, explain and offer any recommendations for improvement. I was told it works and we can't run the code to test it. I pretty much understand it but just need to run it by someone to make sure what I do understand about it is correct and please get any help with explaining what I don't understand.I have been doing a ton of research and still have some questions.

代码实现用于读取仅使用乘法和加法的后缀表达式.然后评估表达式,同时将结果保存到堆栈中.然后它打印出结果.操作数被压入堆栈,然后当它读取一个运算符时,它从堆栈中弹出顶部的 2 个操作数执行计算并将结果存储回堆栈.

The code is implementing used to read a postfix expression that only uses multiplication and addition. Then evaluate the expression while saving the results onto a stack. Then it prints out the results. Operands are pushed onto the stack and then when it reads an operator, it pops the top 2 operands from the stack performs the calculation and stores the result back into the stack.

该程序假定整数和运算符由某种字符(如空格或其他东西)分隔,但根本不检查输入的合法性.

The program assumes that the integers and operators are delimited by some kind of character like a blank space or something, but doesn't check the legality of the input at all.

 public static void main(String[] args)
{
    char[] a = args[0].toCharArray();
    int N =a.length;
    intStack s = new intStack();
    for (int i = 0; i<N; i++)
    {
        if (a[i]=='+')
        {
            s.push(s.pop() + s.pop());
        }
        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'));
        }
        Out.println(s.pop() + "");
    }
}

后修复表达式示例:2 3 5 + * = 16

Post fix expression example: 2 3 5 + * = 16

当谈到最后一个 if 语句和 while 循环时,我很困惑.

所以第一次推送一个 0-9 数字字符时,它会存储一个#0,然后弹出那个 0,乘以 10 并将其添加到下一个数字字符(如果有的话)被转换为一个 int 并将结果推回堆栈?如果是这样,为什么将 0 压入堆栈?

So the first time it pushes a 0-9 number character it will store a # 0, then pop out that 0, multiple it by 10 and add it to the next number character (if there is one) that is converted into an int and push the result back into the stack? If so, why is a 0 pushed onto the stack?

难道不应该将第一个 0-9 编号字符转换为 int 数据类型,将其压入堆栈然后转到 while 循环吗?

Shouldn't it convert the first 0-9 numbered character to an int datatype, push it onto the stack and then go to the while loop?

然后在 While 循环中,读取数组并继续将 0-9 编号字符转换为 int 数据类型并将它们压入堆栈,直到读取到不同的字符?

then in the While loop, read the the array and keep converting 0-9 numbered characters to int datatypes and pushing them on the stack until a character is read that is different?

我也没有看到它在 while 循环中在哪里增加 int i 或从 while 循环中跳出以前进到下一个字符?

I also don't see where it is incrementing int i in the while loop or breaking out of the while loop to advance to the next character?

推荐答案

最初的零推送有点令人困惑.理解发生了什么的关键是观察 i 没有增加.例如,当代码看到 '4',它是 "42" 中的第一个数字时,if 语句压入零;然后循环立即弹出它,乘以 10,加上 4,然后推回.i 前进到下一个字符(即2),然后循环弹出4,乘以十,加上2,并存储回 42 - 所需的结果.

The initial push of zero is a little confusing. The key to understanding what's going on there is to observe that i is not incremented. When the code sees, say, '4', which is a first digit in "42", the if statement pushes zero; then the loop immediately pops it, multiplies by ten, adds 4, and pushes back. i is advanced to the next character (i.e. 2), and then the loop pops 4, multiplies by ten, adds 2, and stores back 42 - the desired result.

代码中有一个错误:当表达式以数字结尾时,会得到一个索引越界异常.例如,42 是一个有效的后缀表达式(没有运算符也可以,对吧?)内部循环会超过字符串的末尾,导致异常.

There is an error in the code: when the expression ends in a number, you get an index out of bounds exception. For example, 42 is a valid postfix expression (having no operators is OK, right?) the inner loop would advance past the end of the string, causing an exception.

这篇关于使用堆栈了解 Java 代码中的后缀表达式求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆