美文网首页
Java使用后缀表达式进行四则运算

Java使用后缀表达式进行四则运算

作者: 骑驴上塔楼 | 来源:发表于2017-01-04 17:05 被阅读0次

更加便捷的进行四则运算,且代码足够简单。
一般的逻辑是按照正常人的思路来解析,这样难度太大。
转换思路和原理,可以参考将中缀表达式转化为后缀表达式

package com.piggsoft;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Pattern;

public class Arithmetic {
    //操作符stack
    private Stack<String> operator = new Stack<String>();
    //后缀表达式
    private List<String> postFix = new ArrayList<String>();

    private Pattern operatorPattern = Pattern.compile("[\\d\\.]");
    private Pattern arithmeticPattern = Pattern.compile("[\\(\\)\\+\\-/\\*]");

    //将中缀表达式换为后缀表达式
    public void parse(String s) {
        s = replace(s);
        int j = 0;
        for (int i = 0; i < s.length(); i++) {
            String temp = s.substring(i, i + 1);
            if (operatorPattern.matcher(temp).matches()) {
                continue;
            }
            if (arithmeticPattern.matcher(temp).matches()) {
                j = process(j, i, s, temp);
            } else if ("".equals(temp)) {
                return;
            }
        }
        if (j < s.length()) {
            postFix.add(s.substring(j, s.length()));
        }
        while (!this.operator.isEmpty()) {
            postFix.add(operator.pop());
        }
    }

    private String replace(String s) {
        return s.replaceAll("\\s", "");
    }

    private int process(int startIndex, int currentIndex, String str, String word) {
        if (startIndex != currentIndex) {
            postFix.add(str.substring(startIndex, currentIndex));
        }
        addOperator(word);
        startIndex = currentIndex + 1;
        if (startIndex > str.length()) {
            startIndex = str.length();
        }
        return startIndex;
    }

    public void addOperator(String operator) {
        if ("(".equals(operator)) {

        } else if (")".equals(operator)) {
            while (!this.operator.isEmpty()) {
                String temp = this.operator.pop();
                if ("(".equals(temp)) {
                    break;
                } else {
                    postFix.add(temp);
                }

            }
            return;
        } else if (!this.operator.isEmpty()) {
            while (!this.operator.isEmpty()) {
                String temp = this.operator.peek();
                if (needPop(temp, operator)) {
                    this.postFix.add(this.operator.pop());
                } else {
                    break;
                }
            }
        }
        this.operator.add(operator);
    }

    public boolean needPop(String inStackTop, String current) {
        return getLevel(current.charAt(0)) <= getLevel(inStackTop.charAt(0));
    }

    public int getLevel(char operator) {
        switch (operator) {
            case '+':
                return 1;
            case '-':
                return 1;
            case '*':
                return 2;
            case '/':
                return 2;
            default:
                return -1;
        }
    }

    public BigDecimal compute() {
        Stack<BigDecimal> stack = new Stack<BigDecimal>();
        for (int i = 0; i < this.postFix.size(); i++) {
            if (arithmeticPattern.matcher(postFix.get(i)).matches()) {
                BigDecimal bd2 = stack.pop();
                BigDecimal bd1 = stack.pop();
                BigDecimal temp = compute(postFix.get(i).charAt(0), bd1, bd2);
                stack.add(temp);
            } else {
                stack.add(new BigDecimal(postFix.get(i)));
            }
        }
        return stack.pop();
    }

    private BigDecimal compute(char operator, BigDecimal bd1, BigDecimal bd2) {
        switch (operator) {
            case '+':
                return bd1.add(bd2);
            case '-':
                return bd1.subtract(bd2);
            case '*':
                return bd1.multiply(bd2);
            case '/':
                return bd1.divide(bd2);//应当使用bd1.divide(divisor, scale, roundingMode);
            default:
                return null;
        }
    }

    public static void main(String[] args) {
        Arithmetic arithmetic = new Arithmetic();
        arithmetic.parse("9 + (3 - 1) * 3 + 10 / 2");
        System.out.println(arithmetic.postFix);
        System.out.println(arithmetic.compute());

        arithmetic = new Arithmetic();
        arithmetic.parse("9 + (3 - 1) * 3 + 10 / 2 + 1000 - 200 - (100 + 5 - 9 / 3)");
        System.out.println(arithmetic.postFix);
        System.out.println(arithmetic.compute());

        arithmetic = new Arithmetic();
        arithmetic.parse("9 + (3 - 1) * 3 + 10 / 2 + 1000 - 200 - (100 + 5 - 9 / 3)");
        System.out.println(arithmetic.postFix);
        System.out.println(arithmetic.compute());
    }
}

相关文章

  • 四则运算(JAVA)

    计算过程 1.将四则运算串分割出中缀表达式2.将中缀表达式转换为后缀表达式3.对后缀表达式进行求值得出结果

  • 算法(3)简单四则运算

    1.0 问题描述 实现10以内四则运算(只包含数字,+-*/和小括号) 2.0 问题分析 四则运算使用“后缀表达式...

  • 前缀,中缀,后缀表达式

    全文转载自:前缀、中缀、后缀表达式(逆波兰表达式),侵删。 前缀表达式,中缀表达式,后缀表达式都是四则运算的表达方...

  • Java使用后缀表达式进行四则运算

    更加便捷的进行四则运算,且代码足够简单。一般的逻辑是按照正常人的思路来解析,这样难度太大。转换思路和原理,可以参考...

  • 深度透析逆波兰表达式

    逆波兰表达式 1、概念 标准四则运算表达式---中缀表达式 计算机采用一种计算,变成后缀表达式: 2、计算机进行转...

  • 四则运算表达式求值

    利用栈求解四则运算:求解思路为先将中缀表达式转换为后缀表达式,再利用后缀表达式求解中缀表达式:运算符出现在两个数字...

  • Java数据结构-栈-简单应用

    1,三种表达式 1)三种表达式四则运算的三种表达方式,用于四则运算表达式求值中缀表达式:(3+4)*5-6后缀表达...

  • 计算器

    使用Java写的一个可以计算+,-,*,/ 的计算器。首先用栈把中缀表达式转化成后缀表达式,再利用栈对后缀表达式求...

  • 一些算法题

    1.四则运算表达式求值: 两个栈存储,中缀表达式转为后缀表达式 okcalculate1 & calculate2...

  • 栈用例——中缀表达式转后缀表达式、二叉树求表达式

    一、中缀表达式转后缀表达式 中缀表达式:就是我们平时书写的带括号的标准四则运算表达式,如9+(3-1)*3+10/...

网友评论

      本文标题:Java使用后缀表达式进行四则运算

      本文链接:https://www.haomeiwen.com/subject/bdtavttx.html