Java RPN (Reverse Polish Notation) infix to postfix

后端 未结 2 1967
名媛妹妹
名媛妹妹 2020-12-09 07:15

I am pretty sure, that stacks are used for building PRN and \'(\' are ignored, but it does not seem to be the case. For example:

  • input 1: 52+(
2条回答
  •  悲哀的现实
    2020-12-09 07:27

    The algorithm is pretty simple (and here is a good explanation). Every operation has a binding weight, with + and - being the lowest. There are two rules:

    • print out numbers immediately
    • never put a lighter item on a heavier item
    • left parentheses go on the stack
    • right parentheses pop off the stack until you hit a left parentheses, and then remove the left parentheses

    Given your first example, 52+(1+2)*4-3, here is the stack:

     52+          => +
     52+(         => + (
     52+(1+       => + ( + 
     52+(1+2)     => +       //right parentheses popped +
     52+(1+2)*4   => + * 
     52+(1+2)*4-3 => + -     //can't put - on top of *, so pop off *
     ... and then pop the stack until it's empty.
    

    Replacing your switch loop with the following (closest analog to what you had) will give correct answers for your three examples. In a real parser you would give each operator a weight and generalize the pop mechanism.

    for (int i = 0; i < in.length; i++)
            switch (in[i]) {
            case '+':
            case '-':
                while (!stack.empty() && (stack.peek() == '*' || stack.peek() == '/')) {
                    out.append(' ');
                    out.append(stack.pop());
                }
                out.append(' ');
                stack.push(in[i]);
                break;
            case '*':
            case '/':
                out.append(' ');
                stack.push(in[i]);
                break;
            case '(':
                stack.push(in[i]);
                break;
            case ')':
                while (!stack.empty() && stack.peek() != '(') {
                    out.append(' ');
                    out.append(stack.pop());
                }
                stack.pop();
                break;
            default:
                out.append(in[i]);
                break;
            }
    

提交回复
热议问题