項目中用到用戶定義運算公式進行就算的需求,這樣需要進行字符串四則運算解析,下面提供字符串公式四則運算解析與計算工具類,需要的同學可參考。
工具類如下:formulacalculator.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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實現字符串四則運算公式解析工具類的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/zmx729618/article/details/52328377