infix expression converted to suffix expression

Source: Internet
Author: User

Suffix expressionin inverse Polish notation, the operator is placed after the operand. For example, when expressing "three plus four", write "3 4 +" instead of "3 + 4". If there are multiple operators, the operator is placed after the second operand, so the conventional infix notation of "3-4 + 5" in inverse Polish notation "3 4-5 +": first 3 minus 4, plus 5. One advantage of using inverse Polish notation is that you do not need to use parentheses. For example, the infix notation "3-4 * 5" and "(3-4)" is not the same, but the suffix notation in the former write "3 4 5 *-", without ambiguity to mean "3 (4 5 *)?" The latter wrote "3 4-5 *".

Source: https://zh.wikipedia.org/wiki/%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E7%A4%BA%E6%B3%95
scheduling field algorithm
  • When there is still a mark to read
      • Read a tick
        • Represents a number that is added to the output queue
        • Represents a function that presses into the stack
        • Delimiter for a function parameter (,)
          • The pop-up operator from the Occupy, and into the output queue, know that the top element of the stack is an opening parenthesis.
            • If you have never encountered an opening parenthesis
              • The delimiter has misplaced the position
              • Parentheses do not match
        • An operator, remember to do O1
          • As long as there is another O2 operator at the top of the stack
            • If O1 is left-associative and its operator precedence is less than or equal to the priority of O2 , or if the O1 is right-associative and its operator precedence is lower than O2, then O2 pops up from the top of the stack and puts it into the output queue (looping until the above conditions are not met, If the O1 is left-associative, until it encounters a lower priority than O1, stop , if O1 right-associative, until the priority is equal or lower than O1)
            • Then, press the O1 into the top of the stack
        • Opening parenthesis
          • Pressed into the stack
        • Closing parenthesis
          • Pop the operator from the stack and put it in the output queue until the popup element is left parenthesis
          • Eject the opening parenthesis from the top of the stack, but not into the output queue
          • If the stack pops up all the elements before an opening parenthesis is found, long indicates that there are mismatched parentheses in the expression
  • When no more tokens can be read
      • If there are operators in the stack
        • If the operator at the top of the station at this point is a parenthesis, then there are mismatched parentheses in the expression
        • Pop the operator one by one and put it in the output queue
  • Exit algorithm

Suffix expression (inverse Poland) evaluation is generally based on the stack, the algorithm is generally described as
Operand into the stack, when encountered operator, the operation of the stack, evaluated, the results into the stack;
Pseudo code
    • While there are input symbols
      • Read in the next symbol X
      • IF x is an operand
        • Into the stack
      • ELSE IF X is an operator
        • There is a priori table that gives the operator the N parameters required
        • If stack less than n operands
          • (error) The user did not enter sufficient number of operands
        • Else,n number of operands out of the stack
        • Calculation operators
        • Put the calculated value into the stack
      • Only one value in the IF stack
        • This value is the result of the entire formula.
      • ELSE Excess One value
        • (error) The user has entered an extra operand


How the problem string in the actual implementation process is cut into one character
 
   
  
  1. for(int i; i <expss.lengh();i++)
  2.     visit(new Character(s.charAt(2)).toString());

Determines whether a string is numeric http://javapub.iteye.com/blog/666544
The reference type of equals using Java

You need to join the regular expression
  
 
  1. public static boolean isInteger(String str) {
  2. Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
  3. return pattern.matcher(str).matches();
  4. }
Match spaces \s strings into numbers
Static member initialization intermediate throws two division note divisor and divisor are not adjusted well




infix expression converted to suffix expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.