1. turn postfix infix expression

Package com.vi.stack; 

Import of java.util.ArrayList;
 Import java.util.List;
 Import the java.util.Stack; 

/ ** 
 * infix expressions turn into postfix notation 
 * @author VI 
 * 
 * / 
public  class InfixToSuffixDemo {
     public  static  void main (String [] args) { 
        String expression the = ". 1 + ((2 +. 3) *. 4) -5" ;
         // expressions stored in the List 
        List <String> target = changeToList ( expression The); 
        System.out.println (target); 
        // start conversion
         // S1 operator stack
        Stack <String> S1 = new new Stack <String> ();
         // S2 with the results stored list 
        List <String> S2 = new new the ArrayList <String> ();
         for (String Item: target) {
             // If the number matches, into S2 
            iF (item.matches ( "\\ + D" )) { 
                s2.add (Item); 
            } the else {
                     // matching non-numeric, it is judged whether or not "(" left parenthesis direct stack 
                    iF ( '(' . the equals (Item)) { 
                        s1.push (Item); 
                    } the else  IF ( ")".equals (Item)) {
                             // is ")", that is the case, we need to compare whether the top element (not just pop stack, into s2 until the top of the stack is "(", and then disappear left parenthesis 
                            the while (s1.size ()> 0 && s1.peek () the equals ( "("!. )) { 
                                s2.add (s1.pop ()); 
                            } 
                            s1.pop (); 
                        } the else {
                             // comparison operation priority symbols and the top element, the top element below the top of the stack in the pop-up list is added, it is pushed on the stack than 
                            the while (s1.size ()> 0 && priority (Item)> = priority (S1 .peek ())) { 
                                s2.add (s1.pop ()); 
                            } 
                            s1.push (Item);
                        }
                    } 
                } 
            // the remaining elements in the stack s1 sequentially eject, in s2 keep 
            the while (s1.size ()> 0 ) 
                s2.add (s1.pop ()); 
            // print s2, is transferred suffix obtained Expression formula result 
            System.out.println (S2); 
        } 
    
    
    / ** 
     * the suffix expression resolves target, stored in the List 
     * @param expression the 
     * @return 
     * / 
    public  static List <String> changeToList (expression the String) { 
        List <String> Result = new new the ArrayList <String> ();
         for ( int I = 0; I <expression.length (); I ++ ) { 
            String CUR = expression.charAt (I) + "" ;
             // determines whether the obtained digital 
            IF (cur.matches ( "\\ + D")) { // the number if it is further necessary to determine whether a digital, is then re-stored after splicing 
                int J = I +. 1 ;
                 // if the index does not exceed the length of the expression, and the expression is a number, for splicing 
                the while (J <expression.length () && ( expression.charAt (J) + ".") The matches ( "\\ + D" )) { 
                    CUR = CUR + expression.charAt (J) + "" ; 
                    J ++ ; 
                } 
                result.add (CUR); 
                I = J --1 ;
            } else{
                 // if the number is not directly stored in list 
                result.add (CUR); 
            } 
        } 
        return Result; 
    } 
    
    / ** 
     * returns the priority of the destination string 
     * @param target 
     * @return 
     * / 
    public  static  int priority ( target String) {
         IF ( "+" the equals (target.) || "-" .equals (target)) {
             return . 1 ; 
        } 
        the else  IF (. "*" the equals (target) || "/" .equals (target )) {
             return 2 ; 
        }the else { // If bracket 
            return . 3 ; 
        } 
    } 
}

Print Results:

 

Summary of steps:

1. Prepare two stacks, the stack operators s1, store the result stack s2 (available instead of the list)

2. The target expression scanning

3. If the digital scan, into the stack s2

4. If the operator to scan

  4.1 determine whether "(" s1 is then directly into the stack

  4.2 determines whether ")", is determined, then the top element s1 is "(", it will not pop the top element s1, s2 pressed into the stack, and the process repeated until a stack s1 is "(", this when "(" pop-up

  4.3 Analyzing stack current operators and operators priority (brackets> * /> + -), if the priority is lower than the top element s1, s1 of the current element is pressed into the stack, or top of the stack pop, pressed s2 stack, the current element is then pressed into the stack s1

5. Repeat steps 3 and 4 until you have scanned the target expression, and finally the remaining elements in the stack pop s1, s2 successively pressed into the stack.

6. s2 stack pop elements, the result is the reverse order of the target expression corresponding postfix expression! ! !

 

Guess you like

Origin www.cnblogs.com/blogforvi/p/11521239.html