Reverse Polish expression evaluation javascript version

Code address:  http://runjs.cn/code/r06uftvp

  1. First of all, we have to figure out what reverse Polish expressions are. See:  http://www.cnblogs.com/chenying99/p/3675876.html

  2. Summarize roughly

    1. Our usual calculation method, where operators are placed between two numbers is called an infix expression

      1. eg 2 + 3 * (5 - 1)

    2. An operator placed before two numbers is called a prefix expression, also called polish

      1. eg + 2 * 3 - 5 1 

      2. From left to right until two numbers are encountered, use the operator before the two numbers to calculate

    3. Operators placed after two numbers are called postfix expressions, also known as reverse Polish

      1. eg 2 3 5 1 - * +

      2. From right to left until two numbers are encountered, use the operator after the two numbers to calculate

  3. According to the reverse Polish calculation method, we can draw some simple expression verification methods

    1. The first two digits must be numbers

    2. The number of operators can only be one less than the number of numbers

    3. The last bit must be an operator

    4. The length of the consecutive operator can only be one less than the preceding number (eg: 3 3 - - 3 3 -, which is incorrect)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325471016&siteId=291194637