您好,登錄后才能下訂單哦!
項目中用到用戶定義運算公式進(jìn)行就算的需求,這樣需要進(jìn)行字符串四則運算解析,下面提供字符串公式四則運算解析與計算工具類,需要的同學(xué)可參考。
工具類如下:FormulaCalculator.java:
package org.nercita.bcp.record.util; import java.util.ArrayList; import java.util.LinkedList; /** * @author zhangwenchao * @since 2016-08-26 * 公式計算的工具類 */ public class FormulaCalculator { private static boolean isRightFormat = true; public static double getResult(String formula){ double returnValue = 0; try{ returnValue = doAnalysis(formula); }catch(NumberFormatException nfe){ System.out.println("公式格式有誤,請檢查:" + formula); }catch(Exception e){ e.printStackTrace(); } if(!isRightFormat){ System.out.println("公式格式有誤,請檢查:" + formula); } return returnValue; } private static double doAnalysis(String formula){ double returnValue = 0; LinkedList<Integer> stack = new LinkedList<Integer>(); int curPos = 0; String beforePart = ""; String afterPart = ""; String calculator = ""; isRightFormat = true; while(isRightFormat&&(formula.indexOf('(') >= 0||formula.indexOf(')') >= 0)){ curPos = 0; for(char s : formula.toCharArray()){ if(s == '('){ stack.add(curPos); }else if(s == ')'){ if(stack.size() > 0){ beforePart = formula.substring(0, stack.getLast()); afterPart = formula.substring(curPos + 1); calculator = formula.substring(stack.getLast() + 1, curPos); formula = beforePart + doCalculation(calculator) + afterPart; stack.clear(); break; }else{ System.out.println("有未關(guān)閉的右括號!"); isRightFormat = false; } } curPos++; } if(stack.size() > 0){ System.out.println("有未關(guān)閉的左括號!"); break; } } if(isRightFormat){ returnValue = doCalculation(formula); } return returnValue; } private static double doCalculation(String formula) { ArrayList<Double> values = new ArrayList<Double>(); ArrayList<String> operators = new ArrayList<String>(); int curPos = 0; int prePos = 0; int minus = 0; for (char s : formula.toCharArray()) { if ((s == '+' || s == '-' || s == '*' || s == '/') && minus !=0 && minus !=2) { values.add(Double.parseDouble(formula.substring(prePos, curPos).trim())); operators.add("" + s); prePos = curPos + 1; minus = minus +1; }else{ minus =1; } curPos++; } values.add(Double.parseDouble(formula.substring(prePos).trim())); char op; for (curPos = 0; curPos <= operators.size() - 1; curPos++) { op = operators.get(curPos).charAt(0); switch (op) { case '*': values.add(curPos, values.get(curPos) * values.get(curPos + 1)); values.remove(curPos + 1); values.remove(curPos + 1); operators.remove(curPos); curPos = -1; break; case '/': values.add(curPos, values.get(curPos) / values.get(curPos + 1)); values.remove(curPos + 1); values.remove(curPos + 1); operators.remove(curPos); curPos = -1; break; } } for (curPos = 0; curPos <= operators.size() - 1; curPos++) { op = operators.get(curPos).charAt(0); switch (op) { case '+': values.add(curPos, values.get(curPos) + values.get(curPos + 1)); values.remove(curPos + 1); values.remove(curPos + 1); operators.remove(curPos); curPos = -1; break; case '-': values.add(curPos, values.get(curPos) - values.get(curPos + 1)); values.remove(curPos + 1); values.remove(curPos + 1); operators.remove(curPos); curPos = -1; break; } } return values.get(0).doubleValue(); } public static void main(String[] args) { System.out.println(FormulaCalculator.getResult("3-(4*5)+5")); System.out.println(FormulaCalculator.getResult("7/2-(-4)")); System.out.println(FormulaCalculator.getResult("1287763200000-1276272000000")/(3600*24*1000)); } }
支持四則運算,同時支持負(fù)數(shù)解析。
另附,小數(shù)數(shù)據(jù)保留位數(shù)工具類,SetNumberPrecision.java
package org.nercita.bcp.record.util; import java.text.DecimalFormat; /** * @author zhangwenchao * 小數(shù)點 精度的工具類 */ public class SetNumberPrecision { public static String setNumberPrecision(double x,int Number){ String p="#########0"; if(Number==0){ p="#########0"; }else{ p="#########0."; for(int i=0;i<Number;i++){//for的巧妙運用 p=p+"0"; } } DecimalFormat f = new DecimalFormat(p); String s = f.format(x).toString(); return s; } }
以上這篇java實現(xiàn)字符串四則運算公式解析工具類的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。