将中缀转换为后缀并计算

时间:2018-04-13 06:45:41

标签: java

我想将中缀表达式转换为后缀表达式。使用下面的代码,表达式25*1-(8+3)/4+2将转换为[25, 1, *, -, 8, (, 3, +, 4, /, ), 2, +]而不是[25, 1, *, 8, 3, +, 4, /, -, 2, +, ]

我在StackOverflow中找到了一些其他类似的转换器,并与我的源码进行了比较,但我找不到问题。问题是什么?

package calTest;
import java.util.ArrayList;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Calc2 {

    private static Stack stack = new Stack();

    public static void main(String[] args) {


        String enteredExpression = "25*1-(8+3)/4+2";

        if (!enteredExpression.equals(0)) {
            ArrayList<String> finalExp = converter(enteredExpression);
            //Double result = getResult(finalExp);
            // calModel.setResult(result);
            System.out.println(finalExp);
        } else {
            clear();
        }
    }


    public static ArrayList<String> converter(String enteredExpression) {
        Stack<String> stack = new Stack<String>();
        System.out.println("enteredExpression : " + enteredExpression);
        String regEx = "(\\d+)|(\\D)";

        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(enteredExpression);


        ArrayList<String> listMatches = new ArrayList<String>();

        ArrayList<String> finalExp = new ArrayList<String>();


        while (m.find())
            listMatches.add(m.group());

        for (int i = 0; i < listMatches.size(); i++) {

            String numberExp = "(\\d+)";
            Pattern numberP = Pattern.compile(numberExp);
            Matcher numberMatch = numberP.matcher(listMatches.get(i));


            if (numberMatch.find()) {

                finalExp.add(listMatches.get(i));
            } else {

                while (!stack.isEmpty() && getPriority(stack.peek()) >= getPriority(listMatches.get(i))) {

                    finalExp.add(stack.pop());
                }

                stack.push(listMatches.get(i));
            }
        }

        while (!stack.isEmpty())

            finalExp.add(stack.pop());
        return finalExp;

    }

    public static int getPriority(String op) {
        int result = 0;
        switch (op) {
            case "(":
            case ")":
                result = 3;
            case "+":
            case "-":
                result = 1;
                break;
            case "*":
            case "/":
                result = 2;
                break;
        }
        return result;
    }

    public static Double getResult(ArrayList<String> finalExp) {
        Stack<Double> calculateStack = new Stack<Double>();
        Double a;
        Double b;


        for (int i = 0; i < finalExp.size(); i++) {
            String regEx = "(\\d+)";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(finalExp.get(i));


            if (m.find()) {
                calculateStack.push(Double.valueOf(finalExp.get(i)));
            } else {

                switch (finalExp.get(i)) {
                    case "*":
                        b = calculateStack.pop();
                        a = calculateStack.pop();
                        calculateStack.push(Double.valueOf(a * b));
                        break;
                    case "/":
                        b = calculateStack.pop();
                        a = calculateStack.pop();
                        calculateStack.push(Double.valueOf(a / b));
                        break;
                    case "+":
                        b = calculateStack.pop();
                        a = calculateStack.pop();
                        calculateStack.push(Double.valueOf(a + b));
                        break;
                    case "-":
                        b = calculateStack.pop();
                        a = calculateStack.pop();
                        calculateStack.push(Double.valueOf(a - b));
                        break;
                }

            }
        }
        return calculateStack.pop();
    }

    public static void clear() {
    }
}

0 个答案:

没有答案