java解析四则运算字符串,并计算

package com.reptile.reptileTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class calculatorTest {

    public static void main(String[] args) {
        String str = "((9*6+8/4/2*6)+9)*2+((9-6)*2/3)";
        //String str = "3+2*9+6-8";
        //先处理括号 1.得到所有括号的位置
        String str2 = getNumIndex(str);
        System.out.println(str2);
        //从左到右处理乘除
        //再处理加减
    }

    private static String getNumIndex(String str) {
        char[] chars = str.toCharArray();
        List<Integer> leftBrackets = new ArrayList<>();
        List<Integer> rightBrackets = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
            if (Objects.equals("(", String.valueOf(chars[i]))) {
                leftBrackets.add(i);
            }
            if (Objects.equals(")", String.valueOf(chars[i]))) {
                rightBrackets.add(i);
            }
        }
        if (leftBrackets.size() > 0 && rightBrackets.size() > 0) {
            return getBrackets(str, leftBrackets, rightBrackets);
        }else{
            Pattern p1 = Pattern.compile("\\d*([*/]|[+-])\\d*");
            Matcher m1 = p1.matcher(str);
            while (m1.find()){
                str = getMultiDivision(str);
                str = getAddSubtr(str);
                m1 = p1.matcher(str);
            }

            /*Pattern p2 = Pattern.compile("\\d*([+-])\\d*");
            Matcher m2 = p2.matcher(str);
            while (m2.find()){
                str = getMultiDivision(str);
                str = getAddSubtr(str);
                m2 = p2.matcher(str);
            }*/
            return str;
        }
    }

    //获取最小单元括号内的内容 `
    private static String getBrackets(String str, List<Integer> leftBrackets, List<Integer> rightBrackets) {
        //2.获取括号前后分组
        Integer t1 = null;
        Integer t2 = null;
        for (Integer rightB : rightBrackets) {
            Integer min = null;
            for (Integer leftB : leftBrackets) {
                if (rightB > leftB) {
                    if (min == null || min > rightB - leftB) {
                        min = rightB - leftB;
                        t1 = leftB;
                        t2 = rightB;
                    }
                }
            }
            break;
        }
        leftBrackets.remove(t1);
        rightBrackets.remove(t2);
        String str2 = str.substring(t1 + 1, t2);
        String str3 = str2;
        while (str2.split("[*/]").length > 1) {
            str2 = getMultiDivision(str2);
        }
        while (str2.split("[+-]").length > 1) {
            str2 = getAddSubtr(str2);
        }
        str = str.replace("("+str3+")", str2);
        if (leftBrackets.size() > 0 && rightBrackets.size() > 0) {
            str = getNumIndex(str);
        }

        Pattern p1 = Pattern.compile("\\d*([+|-|*|/])\\d*");
        Matcher m1 = p1.matcher(str);
        while (m1.find()){
            str = getMultiDivision(str);
            str = getAddSubtr(str);
            m1 = p1.matcher(str);
        }

        return str;
    }

    //从左至右获取最基础的乘除运算表达式
    private static String getMultiDivision(String str2) {
        //Pattern pattern = Pattern.compile("\\d*([*/])\\d*");
        Pattern pattern = Pattern.compile("\\d*([*|/])\\d*");
        Matcher matcher = pattern.matcher(str2);
        //String jichu = str2.substring(matcher.start(), matcher.end());
        if (matcher.find()) {
            String jichu = matcher.group();

            String[] strs = jichu.split("[*/]");
            Integer i1 = Integer.parseInt(strs[0]);
            Integer i2 = Integer.parseInt(strs[1]);
            if (jichu.indexOf("*") > 0) {
                return str2.replace(jichu, String.valueOf(i1 * i2));
            } else {
                return str2.replace(jichu, String.valueOf(i1 / i2));
            }
        }
        return str2;
    }

    //从左至右获取最基础的加减运算表达式
    private static String getAddSubtr(String str2) {
        //Pattern pattern = Pattern.compile("\\d*([+|-])\\d*");
        Pattern p2 = Pattern.compile("\\d*[+-]\\d*");
        Matcher m2 = p2.matcher(str2);
        //String jichu = str2.substring(matcher.start(), matcher.end());
        if (m2.find()) {
            String jichu = m2.group();

            String[] strs = jichu.split("[+-]");
            Integer i1 = Integer.parseInt(strs[0]);
            Integer i2 = Integer.parseInt(strs[1]);
            if (jichu.indexOf("+") > 0) {
                return str2.replace(jichu, String.valueOf(i1 + i2));
            } else {
                return str2.replace(jichu, String.valueOf(i1 - i2));
            }
        }
        return str2;
    }
}

如果有不对或者可以改进的地方,希望各位能够指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值