简体   繁体   中英

Understanding Postfix-expression Evaluation in Java code using a stack

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.

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() + "");
    }
}

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

I am confused when it comes to last if statement and the while loop.

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?

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?

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?

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?

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 (ie 2 ), and then the loop pops 4 , multiplies by ten, adds 2 , and stores back 42 - the desired result.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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