(자바) 산술 표현 식 의 계산 접미사 표현 식

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

/*
    :            ,      ,         ,          。
      :operandStack(       )、operatorStack(       )。

            ,         。          , operatorStack     ,
         operandStack        ,        operandStack .

       :
1.    :           ,     、      。
    1.1.             ,    operandStack 。
    1.2.          + -    ,  +、-                    
           ,      +  -         ,    operatorStack          
             ,             operatorStack 。
    1.3.          * /    ,   operatorStack     * /         
         ,              operatorStack 。
    1.4.             "(",     operatorStack 。
    1.5.             ")",     operatorStack      ,           ")"。
2.     :      operatorStack      ,  operatorStack    。
*/
class Demo {

    public static int evaluateExpression(String expression) {

        Stack operandStack = new Stack();//        
        Stack operatorStack = new Stack();//        
        //        
        ArrayList result = new ArrayList();
        String num = "";
        for (int i = 0; i < expression.length(); i++) {
            if (Character.isDigit(expression.charAt(i))) {
                num = num + expression.charAt(i);
            } else {
                result.add(num);
                result.add(expression.charAt(i) + "");
                num = "";
            }
        }
        result.add(num);
        //            
//       for(String str:result){
//       System.out.print(str+" ");
//       }
        //      
        for (int i = 0; i < result.size(); i++) {

            if (result.get(i).equals("+") || result.get(i).equals("-")) {
                //       "+"  "-",                
                while (!operatorStack.isEmpty() && (operatorStack.peek() == '+' || operatorStack.peek() == '-'
                        || operatorStack.peek() == '*' || operatorStack.peek() == '/')) {
                    processOneOperator(operandStack, operatorStack);
                }
                operatorStack.push(result.get(i).charAt(0));//            
            } else if (result.get(i).equals("*") || result.get(i).equals("/")) {
                //       "*"  "/",              
                while (!operatorStack.isEmpty() && (operatorStack.peek() == '*' || operatorStack.peek() == '/')) {
                    processOneOperator(operandStack, operatorStack);
                }
                operatorStack.push(result.get(i).charAt(0));
            } else if (result.get(i).equals("(")) {
                //        ,            
                operatorStack.push('(');
            } else if (result.get(i).equals(")")) {
                //        ,        ,         
                while (operatorStack.peek() != '(') {
                    processOneOperator(operandStack, operatorStack);
                }
                operatorStack.pop();//             
            } else {
                //          ,              
                operandStack.push(Integer.parseInt(result.get(i)));
            }
        }
        //          ,       
        while (!operatorStack.isEmpty()) {
            processOneOperator(operandStack, operatorStack);
        }
        //                    
        return operandStack.pop();
    }

    /**
     *                 
     * 
     * @param operandStack
     * @param operatorStack
     */
    public static void processOneOperator(Stack operandStack, Stack operatorStack) {
        char op = operatorStack.pop();
        //          
        int op1 = operandStack.pop();
        //          
        int op2 = operandStack.pop();
        //          
        if (op == '+') {
            //       +,             ,           
            operandStack.push(op2 + op1);
        } else if (op == '-') {
            operandStack.push(op2 - op1);
        } else if (op == '*') {
            operandStack.push(op2 * op1);
        } else if (op == '/') {
            operandStack.push(op2 / op1);
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        System.out.println("       (           ):");
        while (cin.hasNext()) {
            String s = cin.next();
            // System.out.println("       :");
            System.out.println(s + "=" + evaluateExpression(s));
        }

        cin.close();
    }
}

좋은 웹페이지 즐겨찾기