The project uses user-defined calculation formulas for calculation, so that the four-character string calculation analysis is required. The following is a class of four-character string formulas for calculation analysis and calculation tools. Students who need it can refer to it.
The tool class is as follows:
package ; import ; import ; /** * @author zhangwenchao * @since 2016-08-26 * Formula calculation tool class */ public class FormulaCalculator { private static boolean isRightFormat = true; public static double getResult(String formula){ double returnValue = 0; try{ returnValue = doAnalysis(formula); }catch(NumberFormatException nfe){ ("The formula format is incorrect, please check:" + formula); }catch(Exception e){ (); } if(!isRightFormat){ ("The formula format is incorrect, please check:" + 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&&(('(') >= 0||(')') >= 0)){ curPos = 0; for(char s : ()){ if(s == '('){ (curPos); }else if(s == ')'){ if(() > 0){ beforePart = (0, ()); afterPart = (curPos + 1); calculator = (() + 1, curPos); formula = beforePart + doCalculation(calculator) + afterPart; (); break; }else{ ("There is an unclosed closing bracket!"); isRightFormat = false; } } curPos++; } if(() > 0){ ("There are unclosed opening brackets!"); 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 : ()) { if ((s == '+' || s == '-' || s == '*' || s == '/') && minus !=0 && minus !=2) { (((prePos, curPos).trim())); ("" + s); prePos = curPos + 1; minus = minus +1; }else{ minus =1; } curPos++; } (((prePos).trim())); char op; for (curPos = 0; curPos <= () - 1; curPos++) { op = (curPos).charAt(0); switch (op) { case '*': (curPos, (curPos) * (curPos + 1)); (curPos + 1); (curPos + 1); (curPos); curPos = -1; break; case '/': (curPos, (curPos) / (curPos + 1)); (curPos + 1); (curPos + 1); (curPos); curPos = -1; break; } } for (curPos = 0; curPos <= () - 1; curPos++) { op = (curPos).charAt(0); switch (op) { case '+': (curPos, (curPos) + (curPos + 1)); (curPos + 1); (curPos + 1); (curPos); curPos = -1; break; case '-': (curPos, (curPos) - (curPos + 1)); (curPos + 1); (curPos + 1); (curPos); curPos = -1; break; } } return (0).doubleValue(); } public static void main(String[] args) { (("3-(4*5)+5")); (("7/2-(-4)")); (("1287763200000-1276272000000")/(3600*24*1000)); } }
Supports four-character operations and supports negative number analysis.
Attached, the decimal data reserved number of digits tool class,
package ; import ; /** * @author zhangwenchao * Decimal point Tools for precision */ 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++){//The clever use of for p=p+"0"; } } DecimalFormat f = new DecimalFormat(p); String s = (x).toString(); return s; } }
The above method of implementing the four-character formula analysis tool class for Java strings is all the content I share with you. I hope you can give you a reference and I hope you can support me more.