欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Java解析字符串中的加减乘除四则运算的实现

程序员文章站 2022-02-14 11:19:41
...

这里分享一个工具类、这个工具类是在我写DD记账的时候用到的、那就是计算一个可运算的字符串、可直接使用java解析字符串中的四则运算、现在把这个工具类的源代码分享给大家、相信会对一些做计算器获者一些简单的运算的朋友有用、话不多说、下面直接上代码


ReportUtil.java

package com.dwtedx.income.utility;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by dwtedx(qinyl)(http://dwtedx.com) on 16/9/2.
 * Company DD博客
 * Description java字符串加法运算
 */
public class ReportUtil {

    /**
     * 格式化数字
     * @param obj 数字对象
     * @param format 格式化字符串
     * @return
     */
    public static String formatNumber(Object obj, String format) {
        if (obj == null)
            return "";

        String s = String.valueOf(obj);
        if (format == null || "".equals(format.trim())) {
            format = "#.00";
        }
        try {
            if (obj instanceof Double || obj instanceof Float) {
                if (format.contains("%")) {
                    NumberFormat numberFormat = NumberFormat.getPercentInstance();
                    s = numberFormat.format(obj);
                } else {
                    DecimalFormat decimalFormat = new DecimalFormat(format);
                    s = decimalFormat.format(obj);
                }
            } else {
                NumberFormat numberFormat = NumberFormat.getInstance();
                s = numberFormat.format(obj);
            }
        } catch (Exception e) {
        }
        return s;
    }

    /**
     * 计算字符串四则运算表达式
     * @param string
     * @return
     */
    public static String computeString(String string) {
        String regexCheck = "[\(\)\d\ \-\*/\.]*";// 是否是合法的表达式

        if (!Pattern.matches(regexCheck, string))
            return string;

        Matcher matcher = null;
        String temp = "";
        int index = -1;
        String regex = "\([\d\.\ \-\*/] \)";// 提取括号表达式
        string = string.replaceAll("\s", "");// 去除空格
        try {
            Pattern pattern = Pattern.compile(regex);
            // 循环计算所有括号里的表达式
            while (pattern.matcher(string).find()) {
                matcher = pattern.matcher(string);
                while (matcher.find()) {
                    temp = matcher.group();
                    index = string.indexOf(temp);
                    string = string.substring(0, index)
                              computeStirngNoBracket(temp)
                              string.substring(index   temp.length());
                }
            }
            // 最后计算总的表达式结果
            string = computeStirngNoBracket(string);
        } catch (NumberFormatException e) {
            return e.getMessage();
        }
        return string;
    }

    /**
     * 计算不包含括号的表达式
     * @param string
     * @return
     */
    private static String computeStirngNoBracket(String string) {
        string = string.replaceAll("(^\()|(\)$)", "");
        String regexMultiAndDivision = "[\d\.] (\*|\/)[\d\.] ";
        String regexAdditionAndSubtraction = "(^\-)?[\d\.] (\ |\-)[\d\.] ";

        String temp = "";
        int index = -1;

        // 解析乘除法
        Pattern pattern = Pattern.compile(regexMultiAndDivision);
        Matcher matcher = null;
        while (pattern.matcher(string).find()) {
            matcher = pattern.matcher(string);
            if (matcher.find()) {
                temp = matcher.group();
                index = string.indexOf(temp);
                string = string.substring(0, index)   doMultiAndDivision(temp)
                          string.substring(index   temp.length());
            }
        }

        // 解析加减法
        pattern = Pattern.compile(regexAdditionAndSubtraction);
        while (pattern.matcher(string).find()) {
            matcher = pattern.matcher(string);
            if (matcher.find()) {
                temp = matcher.group();
                index = string.indexOf(temp);
                if (temp.startsWith("-")) {
                    string = string.substring(0, index)
                              doNegativeOperation(temp)
                              string.substring(index   temp.length());
                } else {
                    string = string.substring(0, index)
                              doAdditionAndSubtraction(temp)
                              string.substring(index   temp.length());
                }
            }
        }

        return string;
    }

    /**
     * 执行乘除法
     * @param string
     * @return
     */
    private static String doMultiAndDivision(String string) {
        String value = "";
        double d1 = 0;
        double d2 = 0;
        String[] temp = null;
        if (string.contains("*")) {
            temp = string.split("\*");
        } else {
            temp = string.split("/");
        }

        if (temp.length < 2)
            return string;

        d1 = Double.valueOf(temp[0]);
        d2 = Double.valueOf(temp[1]);
        if (string.contains("*")) {
            value = String.valueOf(d1 * d2);
        } else {
            value = String.valueOf(d1 / d2);
        }

        return value;
    }

    /**
     * 执行加减法
     * @param string
     * @return
     */
    private static String doAdditionAndSubtraction(String string) {
        double d1 = 0;
        double d2 = 0;
        String[] temp = null;
        String value = "";
        if (string.contains(" ")) {
            temp = string.split("\ ");
        } else {
            temp = string.split("\-");
        }

        if (temp.length < 2)
            return string;

        d1 = Double.valueOf(temp[0]);
        d2 = Double.valueOf(temp[1]);
        if (string.contains(" ")) {
            value = String.valueOf(d1   d2);
        } else {
            value = String.valueOf(d1 - d2);
        }

        return value;
    }

    /**
     * 执行负数运算
     * @param string
     * @return
     */
    private static String doNegativeOperation(String string) {
        String temp = string.substring(1);
        if (temp.contains(" ")) {
            temp = temp.replace(" ", "-");
        } else {
            temp = temp.replace("-", " ");
        }
        temp = doAdditionAndSubtraction(temp);
        if (temp.startsWith("-")) {
            temp = temp.substring(1);
        } else {
            temp = "-"   temp;
        }
        return temp;
    }

}


调用方法

public static void main(String[] args) {
    String s = "1-7 8*2";
    s = computeString(s);
    System.out.println((int)Double.parseDouble(s));
}


以上就是整个类的代码以及使用方法、大家可以自己的运用到项目中、非常的方便