中缀表达式转后缀表达式-Java

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

/**
 * @author cnkeysky
 * @date 2020-02-21 9:39
 */
public class PolandNotation {

    /**
     * 中缀表达式转为后缀
     * @param list 中缀表达式的 List<String> 形式
     * @return List<String> 后缀表达式
     */
    public static List<String> parseSuffixExpression(List<String> list) {
        List<String> res = new ArrayList<>(16);
        Stack<String> operator = new Stack<>();
        String lbrace = "(";
        String rbrace = ")";
        for (String str : list) {
            if (CommonUtil.isDigital(str)) {
                res.add(str);
            } else if (lbrace.equals(str)) {
                operator.push(str);
            } else if (rbrace.equals(str)) {
                while (!operator.empty() && !lbrace.equals(operator.peek())) {
                    res.add(operator.pop());
                }
                if (!operator.empty()) {
                    operator.pop();
                }
            } else {
                while (!operator.empty() && getPriorityOfOperator(operator.peek()) >= getPriorityOfOperator(str)) {
                    res.add(operator.pop());
                }
                operator.push(str);
            }
        }
        while (!operator.empty()) {
            res.add(operator.pop());
        }
        return res;
    }

    /**
     * 操作符的优先级
     * @param operator +|-|*|/
     * @return int 0 | 1 | 2
     */
    public static int getPriorityOfOperator(String operator) {
        // 默认为 0,为了处理栈顶为括号时的情况
        int priority = 0;
        switch (operator) {
            case "+":
            case "-": {
                priority = 1;
                break;
            }
            case "*":
            case "/": {
                priority = 2;
                break;
            }
            default:
                break;
        }
        return priority;
    }

}

  • 测试
import java.util.List;

/**
 * @author keysky
 */
public class DemoTest {

    public static void main(String[] args) {
        String str = "8 + ((2 + 5) * 6) +  10.5";
        List<String> list = CommonUtil.parseArithmeticStr(str);
        List<String> suffixExpression = PolandNotation.parseSuffixExpression(list);
        System.out.println(suffixExpression);
    }
    // out: [8, 2, 5, +, 6, *, +, 10.5, +]		
}
  • 计算器的实现
import java.util.List;
import java.util.Stack;

/**
 * @author keysky
 */
public class DemoTest {

    public static void main(String[] args) {
        String str = "8 + ((2 + 5) * 6) +  10.5";
        System.out.println(str.replaceAll(" +", ""));
        List<String> list = CommonUtil.parseArithmeticStr(str);
        List<String> suffixExpression = PolandNotation.parseSuffixExpression(list);
        System.out.println(suffixExpression);
        calculate(suffixExpression);
      	// out: 计算结果: 60.5
    }

    public static void calculate(List<String> list) {
        Stack<String> stack = new Stack<>();
        for (String str : list) {
            if (CommonUtil.isDigital(str)) {
                stack.push(str);
            } else {
                Double d2 = Double.parseDouble(stack.pop());
                Double d1 = Double.parseDouble(stack.pop());
                stack.push(operate(d1, str, d2));
            }
        }
        System.out.println("计算结果: " + Double.parseDouble(stack.pop()));
    }

    public static String operate(Double d1, String ope ,Double d2) {
        String str = "";
        switch (ope) {
            case "+": {
                str = (d1 + d2) + "";
                break;
            }
            case "-": {
                str = (d1 - d2) + "";
                break;
            }
            case "*": {
                str = (d1 * d2) + "";
                break;
            }
            case "/": {
                str = (d1 / d2) + "";
                break;
            }
            default:
                break;
        }
        return str;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值