JAVA 计算算式表达式

JAVA 实现算术表达式求值


原理

中缀表达式转后缀表达式的方法:
1.遇到操作数:直接输出(添加到后缀表达式中)
2.栈为空时,遇到运算符,直接入栈
3.遇到左括号:将其入栈
4.遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,左括号不输出。
5.遇到其他运算符:加减乘除:弹出所有优先级大于或者等于该运算符的栈顶元素,然后将该运算符入栈
6.最终将栈中的元素依次出栈,输出。

代码

package com.code.cn;

import java.util.ArrayList;
import java.util.List;

public class Zhong {

    public static void main(String[] args) {
        Zhong z = new Zhong();
        System.out.println(z.countExp("1+(2-3/4)*5"));
    }

    /**
     * 根据后缀表达式计算值
     * @param expList
     * @return
     */
    private String countExp(String expression) {
        List expList = middleToBehind(expression);
        while (expList.size() > 1) {
            for (int i = 0; i < expList.size(); i++) {
                if (isOperator(expList.get(i))) {
                    expList.add(i - 2, twoOperators(expList.get(i - 2), expList.get(i - 1), expList.get(i)));
                    expList.remove(i-1);
                    expList.remove(i-1);
                    expList.remove(i-1);
                    break;
                }
            }
        }
        return expList.get(0);
    }

    /**
     * 完成两个数的四则运算
     * @param operator1
     * @param operator2
     * @param o
     * @return
     */
    private String twoOperators(String operator1, String operator2, String o){
        String result = "";
        int o1 = Integer.parseInt(operator1);
        int o2 = Integer.parseInt(operator2);
        int count = 0;
        if(o.equals("+")){
            count=o1+o2;
        }else if(o.equals("-")){
            count=o1-o2;
        }else if(o.equals("*")){
            count=o1*o2;
        }else if(o.equals("/")){
            count=o1/o2;
        }
        return result+count;
    }

    /**
     * 判定是否是运算符
     * @param s
     * @return
     */
    private boolean isOperator(String s) {
        if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/"))
            return true;
        return false;
    }

    /**
     * 中缀表达式转后缀表达式
     * @param expression
     * @return
     */
    private List middleToBehind(String expression) {
        List result = new ArrayList();
        if (expression.length() == 0)
            return result;
        int begin = 0;
        char[] operator = new char[100];
        int oTop = -1;
        while (begin < expression.length()) {
            String now = "";
            if (expression.charAt(begin) <= '9' && expression.charAt(begin) >= '0') {
                while (true) {
                    if (begin < expression.length() && expression.charAt(begin) <= '9'
                            && expression.charAt(begin) >= '0') {
                        now = now + expression.charAt(begin++);
                    } else {
                        break;
                    }
                }
                result.add(now);
            } else {
                char o = expression.charAt(begin++);
                if (oTop == -1) {
                    operator[++oTop] = o;
                } else if (o == ')') {
                    // 一直弹出至'('
                    while (true) {
                        if (operator[oTop] == '(') {
                            oTop--;
                            break;
                        } else {
                            result.add("" + operator[oTop--]);
                        }
                    }
                } else if (comOperation(operator[oTop], o)) {
                    result.add("" + operator[oTop--]);
                    operator[++oTop] = o;
                } else {
                    operator[++oTop] = o;
                }
            }
        }
        while (oTop > -1) {
            result.add("" + operator[oTop--]);
        }
        return result;
    }

    /**
     * 判定操作符优先级
     * @param first
     * @param second
     * @return
     */
    private boolean comOperation(char first, char second) {
        if (first == '+') {
            if (second == '+')
                return true;
            else if (second == '-')
                return true;
            else if (second == '*')
                return false;
            else if (second == '/')
                return false;
            else if (second == '(')
                return false;
            else if (second == ')')
                return true;
        } else if (first == '-') {
            if (second == '+')
                return true;
            else if (second == '-')
                return true;
            else if (second == '*')
                return false;
            else if (second == '/')
                return false;
            else if (second == '(')
                return false;
            else if (second == ')')
                return true;
        } else if (first == '*') {
            if (second == '+')
                return true;
            else if (second == '-')
                return true;
            else if (second == '*')
                return true;
            else if (second == '/')
                return true;
            else if (second == '(')
                return false;
            else if (second == ')')
                return true;
        } else if (first == '/') {
            if (second == '+')
                return true;
            else if (second == '-')
                return true;
            else if (second == '*')
                return true;
            else if (second == '/')
                return true;
            else if (second == '(')
                return false;
            else if (second == ')')
                return true;
        } else if (first == '(') {
            if (second == '+')
                return false;
            else if (second == '-')
                return false;
            else if (second == '*')
                return false;
            else if (second == '/')
                return false;
            else if (second == '(')
                return false;
            else if (second == ')')
                return true;
        } else if (first == ')') {
            return true;
        }
        return false;
    }

}

你可能感兴趣的:(数据结构,算式表达式,java,栈)