您好,登錄后才能下訂單哦!
項目中用到用戶定義運算公式進行就算的需求,這樣需要進行字符串四則運算解析,下面提供字符串公式四則運算解析與計算工具類,需要的同學可參考。
工具類如下: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("有未關閉的右括號!"); isRightFormat = false; } } curPos++; } if(stack.size() > 0){ System.out.println("有未關閉的左括號!"); 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)); } }
支持四則運算,同時支持負數解析。
另附,小數數據保留位數工具類,SetNumberPrecision.java
package org.nercita.bcp.record.util; import java.text.DecimalFormat; /** * @author zhangwenchao * 小數點 精度的工具類 */ 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實現字符串四則運算公式解析工具類的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。