What is Reverse Polish?

1. What is reverse Polish?

Reverse Polish notation, also known as Reverse Polish Notation (RPN), is a method used in mathematics and computer science to represent arithmetic expressions. Its characteristic is that the operator is behind the operand, and parentheses are not needed to change the priority of the operation.

For example, the arithmetic expression we usually write is "2 + 3". In reverse Polish notation, this expression would be written "2 3 +".

A major advantage of reverse Polish notation is that it eliminates parentheses in expressions and makes the order in which operations are performed more explicit. In computer science, reverse Polish notation allows for simpler and more efficient evaluation of expressions. For example, reverse Polish expressions can be easily evaluated using the stack data structure.

Reverse Polish notation is named after the Polish logician Jan Łukasiewicz. While his original "Polish notation" puts the operator before the operand (like "+ 2 3"), "reverse Polish notation" puts the operator after the operand, which makes it easier in practice Used, especially in computer science.

Let’s look at some examples of Reverse Polish Notation (RPN). Here are some common arithmetic expressions and their corresponding reverse Polish representations:

Regular expression: (7 - 4) * 2, corresponding RPN representation: 7 4 - 2 *

Explanation: Subtract 4 from 7, then multiply the result by 2.

Regular expression: 5 + ((1 + 2) * 4) - 3, corresponding RPN representation: 5 1 2 + 4 * + 3 -

Explanation: First do 1+2, then multiply the result by 4, then add it to 5, and finally subtract 3 from it.

Regular expression: (3 + 4) * (5 * 6), corresponding RPN representation: 3 4 + 5 6 * *

Explanation: First perform 3+4, and simultaneously perform 5*6, and then multiply the two results.

2. Calculate according to the reverse Polish formula

This is a simple example of a reverse Polish (RPN) calculator implemented in Java. The calculator can handle RPN expressions containing four basic arithmetic operations.

import java.util.Stack;
 
public class RPNCalculator {
    
    
    public static int evalRPN(String[] tokens) {
    
    
        Stack<Integer> stack = new Stack<>();
 
        for (String token : tokens) {
    
    
            switch (token) {
    
    
                case "+":   //但是符号时,就进行运算
                    stack.push(stack.pop() + stack.pop());    //取出当前栈里面的2个元素,进行运算后,再push进去
                    break;
 
                case "-":
                    stack.push(-stack.pop() + stack.pop());
                    break;
 
                case "*":
                    stack.push(stack.pop() * stack.pop());
                    break;
 
                case "/":
                    int n1 = stack.pop(), n2 = stack.pop();
                    stack.push(n2 / n1);
                    break;
 
                default: // 如果是普通字符,则直接入栈
                    stack.push(Integer.parseInt(token));
            }
        }
 
        return stack.pop();
    }
 
    public static void main(String[] args) {
    
    
        String[] rpnExpression = {
    
    "2", "1", "+", "3", "*"};
        System.out.println(evalRPN(rpnExpression));
    }
}

In this example, the evalRPN function accepts an array of strings, each string representing an element (a number or an operator) in the RPN expression. It then uses a stack to store and manipulate numbers.

For each element, it checks whether it is an operator ("+", "-", "*" or "/"). If so, it pops two elements from the stack, performs the appropriate operation, and then pushes the result back onto the stack. If the element is a number, it pushes it onto the stack.

After all elements have been processed, the element at the top of the stack is the result of the expression.

In the main function, we create a reverse Polish expression string array {"2", "1", "+", "3", "*"}. This expression is equivalent to the regular expression (2+1 )*3, then we call the evalRPN function to calculate it. The calculation results will be printed.

Taking 3+4 as an example, for analysis, the corresponding Polish form is 3 4 +:
1) According to the Polish form, push onto the stack
Insert image description here
2) After the push is completed, remove the elements from the stack from top to bottom. Generally, the top level must be one Operator
When the operation symbol is taken out from the stack, the next two elements will be taken out, then operated, and then pushed onto the stack.
After taking out the + sign, take out 4, then take out 3, perform operations, get 7, and then push it onto the stack.

The state of the stack at this time:
Insert image description here
3) Continue to push subsequent polishes onto the stack, and push numbers 5, 6, and * in sequence.
Insert image description here
4)
Continue to push subsequent polishes onto the stack, and push numbers 5, 6, and * in sequence.

When the top layer of the stack is a symbol, operations need to be performed again. Take out the following 2 elements and perform operations.

5*6=30, push 30 onto the stack

The state of the stack at this time:
Insert image description here
5) Continue to push the content in Polish, which is the last * sign

Insert image description here
30*7 + 210

3. How to convert ordinary expressions into reverse Polish form

How is (3 + 4) * (5 * 6) converted to 3 4 + 5 6 * *?

We know that A + B needs to be organized as: AB + Then the same is true for complex formulas. We can regard A as an expression of (3+4), which is easier to understand.

Then the order of processing (3 + 4) * (5 * 6):
1) Convert the large module first (3 + 4) (5 * 6) *
1) Replace 3 + 4 in step 1 with 3 4 +
2) Replace 5 * 6 in step 1 with 5 6 *
3) Get the final 3 4 + 5 6 ***

Note :上面的是代数思想,不是计算机的处理方式,如过想写个程序进行计算,那么需要从左至右进行处理,基本原理和RPNCalculator 类似,只不过是反着写

reference

What is Reverse Polish?

Guess you like

Origin blog.csdn.net/m0_45406092/article/details/131705613