java实现字符串四则运算公式解析工具类的方法
程序员文章站
2024-02-20 22:21:40
项目中用到用户定义运算公式进行就算的需求,这样需要进行字符串四则运算解析,下面提供字符串公式四则运算解析与计算工具类,需要的同学可参考。
工具类如下:formulacal...
项目中用到用户定义运算公式进行就算的需求,这样需要进行字符串四则运算解析,下面提供字符串公式四则运算解析与计算工具类,需要的同学可参考。
工具类如下: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实现字符串四则运算公式解析工具类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。