Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

infix notation - How to calculate expression in java?

How to calculate user given Expression in java.

E:g, if the given exp is 3*4+(5*6) how to calculate this. can anyone help me out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found this code after a quick google:

import java.util.Stack;

/**
 * Class to evaluate infix and postfix expressions.
 * 
 * @author Paul E. Davis ([email protected])
 */
public class InfixPostfixEvaluator {

        /**
         * Operators in reverse order of precedence.
         */
        private static final String operators = "-+/*";
        private static final String operands = "0123456789";

        public int evalInfix(String infix) {
                return evaluatePostfix(convert2Postfix(infix));
        }

        public String convert2Postfix(String infixExpr) {
                char[] chars = infixExpr.toCharArray();
                Stack<Character> stack = new Stack<Character>();
                StringBuilder out = new StringBuilder(infixExpr.length());

                for (char c : chars) {
                        if (isOperator(c)) {
                                while (!stack.isEmpty() && stack.peek() != '(') {
                                        if (operatorGreaterOrEqual(stack.peek(), c)) {
                                                out.append(stack.pop());
                                        } else {
                                                break;
                                        }
                                }
                                stack.push(c);
                        } else if (c == '(') {
                                stack.push(c);
                        } else if (c == ')') {
                                while (!stack.isEmpty() && stack.peek() != '(') {
                                        out.append(stack.pop());
                                }
                                if (!stack.isEmpty()) {
                                        stack.pop();
                                }
                        } else if (isOperand(c)) {
                                out.append(c);
                        }
                }
                while (!stack.empty()) {
                        out.append(stack.pop());
                }
                return out.toString();
        }

        public int evaluatePostfix(String postfixExpr) {
                char[] chars = postfixExpr.toCharArray();
                Stack<Integer> stack = new Stack<Integer>();
                for (char c : chars) {
                        if (isOperand(c)) {
                                stack.push(c - '0'); // convert char to int val
                        } else if (isOperator(c)) {
                                int op1 = stack.pop();
                                int op2 = stack.pop();
                                int result;
                                switch (c) {
                                case '*':
                                        result = op1 * op2;
                                        stack.push(result);
                                        break;
                                case '/':
                                        result = op2 / op1;
                                        stack.push(result);
                                        break;
                                case '+':
                                        result = op1 + op2;
                                        stack.push(result);
                                        break;
                                case '-':
                                        result = op2 - op1;
                                        stack.push(result);
                                        break;
                                }
                        }
                }
                return stack.pop();
        }
        private int getPrecedence(char operator) {
                int ret = 0;
                if (operator == '-' || operator == '+') {
                        ret = 1;
                } else if (operator == '*' || operator == '/') {
                        ret = 2;
                }
                return ret;
        }
        private boolean operatorGreaterOrEqual(char op1, char op2) {
                return getPrecedence(op1) >= getPrecedence(op2);
        }

        private boolean isOperator(char val) {
                return operators.indexOf(val) >= 0;
        }

        private boolean isOperand(char val) {
                return operands.indexOf(val) >= 0;
        }

}

From: http://willcode4beer.com/design.jsp?set=evalInfix


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...