-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
executable file
·219 lines (180 loc) · 9.22 KB
/
Calculator.java
File metadata and controls
executable file
·219 lines (180 loc) · 9.22 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* Calculation the formula
* when lower than Java 1.6
*
* @author beanfondue@gmail.com
* @version 1.0
*/
import java.util.regex.*;
class Calculator
{
public static boolean isExistClass(String classname) {
try {
Class.forName(classname);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
private static String parseCalculate(String arg) throws Exception {
int calc_in_start = arg.lastIndexOf("(");
int calc_in_end = 0;
int i = 0;
int i_len = 0;
String str_calc_in_end = "";
if (calc_in_start > -1) {
for (i = calc_in_start, i_len = arg.length(); i < i_len; i++) {
str_calc_in_end = arg.substring(i, i + 1);
if (")".equals(str_calc_in_end)) {
calc_in_end = i;
break;
}
}
if (calc_in_start > -1 && calc_in_end > -1 && calc_in_start < calc_in_end) {
String arg_in = arg.substring(calc_in_start + 1, calc_in_end);
double calc_in = extendedCalculate(arg_in);
arg_in = arg_in.replaceAll("(-|\\+|\\*)", "\\\\$1"); // escape meta characters for regualr expression replacement (-, +, *). not(/, %)
arg = arg.replaceFirst("\\(" + arg_in + "\\)", String.valueOf(calc_in));
return parseCalculate(arg);
} else {
return arg;
}
} else {
return arg;
}
}
private static double extendedCalculate(String arg) throws Exception {
if (Pattern.matches(".*?[^-\\+\\d\\./%\\*\\(\\)\\s].*?", arg)) {
throw new Exception("NaN");
}
arg = arg.replaceAll("\\s", "");
arg = parseCalculate(arg);
double rtn = 0;
boolean debug = false;
int i = 0;
int i_len = 0;
boolean b = true;
String as = arg.replaceAll("\\d+(?:\\.\\d+)?[/%\\*][-\\+]?\\d+(?:\\.\\d+)?(?:[/%\\*][-\\+]?\\d+(?:\\.\\d+)?)*", "?");
String md = arg;
int rtn_md_len = as.replaceAll("[^?]", "").length();
double[] rtn_md = new double[rtn_md_len];
int rtn_md_index = 0;
Pattern p_as = Pattern.compile("^([-\\+]?\\d+(?:\\.\\d+)?).*$");
Pattern p_md = Pattern.compile(".*?(\\d+(?:\\.\\d+)?[/%\\*][-\\+]?\\d+(?:\\.\\d+)?(?:[/%\\*][-\\+]?\\d+(?:\\.\\d+)?)*).*?");
Matcher m = null;
if (debug == true) {
System.out.println("=========================================");
System.out.println("as: " + as);
System.out.println("md: " + md);
System.out.println("md.length: " + as.replaceAll("[^?]", "").length());
System.out.println("=========================================");
}
// ------------------------------------------------------------------------------- multiplication, division
if (rtn_md_len > 0) {
while (b == true) {
m = p_md.matcher(md);
if (m.matches()) {
double rtn_md_temp = 0;
String rtn_md_each = m.group(1);
String[] rtn_md_number = rtn_md_each.split("[/%\\*]");
String[] rtn_md_symbol = rtn_md_each.split("[-\\+]?\\d+(?:\\.\\d+)?");
if (debug == true) {
System.out.println("rtn_md_each: " + rtn_md_each + ", rtn_md_symbol.length: " + rtn_md_symbol.length);
}
for (i = 0, i_len = rtn_md_number.length; i < i_len; i++) {
if (debug == true) {
System.out.println("rtn_md_number[" + i + "]: " + rtn_md_number[i] + ", rtn_md_symbol[" + i + "]: " + rtn_md_symbol[i]);
}
if (i == 0) {
rtn_md_temp = Double.parseDouble(rtn_md_number[i]);
} else {
if ("*".equals(rtn_md_symbol[i])) {
rtn_md_temp *= Double.parseDouble(rtn_md_number[i]);
} else if ("/".equals(rtn_md_symbol[i])) {
rtn_md_temp /= Double.parseDouble(rtn_md_number[i]);
} else if ("*-".equals(rtn_md_symbol[i])) {
rtn_md_temp = 0 - rtn_md_temp * Math.abs(Double.parseDouble(rtn_md_number[i]));
} else if ("/-".equals(rtn_md_symbol[i])) {
rtn_md_temp = 0 - rtn_md_temp / Math.abs(Double.parseDouble(rtn_md_number[i]));
} else if ("%".equals(rtn_md_symbol[i])) {
rtn_md_temp %= Double.parseDouble(rtn_md_number[i]);
}
}
}
rtn_md[rtn_md_index++] = rtn_md_temp;
md = md.replaceFirst(p_md.pattern(), "");
if (debug == true) {
System.out.println("-----------------------------------------");
}
} else {
b = false;
}
}
for (i = 0, i_len = rtn_md.length; i < i_len; i++) {
if (debug == true) {
System.out.println("rtn_md[" + i + "]: " + rtn_md[i]);
}
as = as.replaceFirst("\\?", String.valueOf(rtn_md[i]));
}
if (debug == true) {
System.out.println("mounted calculate: " + as);
}
}
b = true;
// ------------------------------------------------------------------------------- addition, subtraction
as = as.replaceAll("--", "+").replaceAll("\\+-", "-").replaceAll("-\\+", "-").replaceAll("\\+\\+", "+");
//System.out.println("mounted calculate 2: " + as);
while (b == true) {
m = p_as.matcher(as);
if (m.matches()) {
//System.out.println(rtn + "::::" + as + "::::" + m.group(1));
rtn += Double.parseDouble(m.group(1));
as = as.replaceFirst("^([-\\+]?\\d+(?:\\.\\d+)?)", "");
} else {
b = false;
}
}
return rtn;
}
public static double calculate(String formula) {
double rtn = 0;
try {
rtn = extendedCalculate(formula);
} catch (Exception e) {
e.printStackTrace();
}
return rtn;
}
public static void test(String[] args) throws Exception {
//String formula = "1-2*(34*(2-22*3))";
//String formula = "1-2*(34*(22-2*3))";
// String formula = "1+(2+3)*3"; // 16
// String formula = "1-2*34*(22-2)*3";
// String formula = "1-22";
// String formula = "1+(2+(3+4)*5)+(6+7)/8+(9+10)";
// String formula = "1-2*(34*((2-22)*3))";
// String formula = "1+2-3*4/(5+6-7)*8/9+10";
//String formula = "1+1";
//String formula = "1+2 *3 + ABC";
String formula = "1+((2-3)*(4/(5+6-7*8)/9+10-1)*2)/((3+(4-5)*6/(7+(8-(9*10/1+(2-3)*(4/5+(6-7*8/9+(10-1*(2/3+(4-5))*6)/7)+8-9*10)/1+2)-3*4)/5)+6)-(7*(8/(9+10))))";
//String formula = "1+((2-3)*(4/(5+6-7*8)/9+10-1)*2)/(3+(4-5)*6/(7+(8-(9*10/1+(2-3)*(4/5+(6-7*8/9+(10-1*(2/3+(4-5))*6)/7)+8-9*10)/1+2)-3*4)/5)+6)-7*8/9+10";
//String formula = "1+((2-3)*(4/(5+6-7*8)/9+10-1)*2)/3+4-5*6/7+8-(9*10/1+(2-3)*(4/5+(6-7*8/9+(10-1*(2/3+(4-5))*6)/7)+8-9*10)/1+2)-3*4/5+6-7*8/9+10";
//String formula = "9*10/1+(2-3)*(4/5+(6-7*8/9+(10-1*(2/3+(4-5))*6)/7)+8-9*10)";//-3*4/5+6-7*8/9+10";
//String formula = "(4/5+(6-7*8/9+(10-1*(2/3+(4-5))*6)/7)+8-9*10)";
//String formula = "(10-1*(2/3+(4-5))*6)";
//String formula = "10-1*(2/3+(4-5))";
double l = extendedCalculate(formula);
System.out.println("*****************************************");
System.out.println("formula-val: \"" + formula + "\"");
if (isExistClass("javax.script.ScriptEngineManager")) {
// using the standard (Java 1.6+) scripting library OR http://www.gnu.org/software/jel/
// import ScriptEngineManager;
// import javax.script.ScriptEngine;
javax.script.ScriptEngineManager manager = new javax.script.ScriptEngineManager();
javax.script.ScriptEngine engine = manager.getEngineByName("JavaScript");
System.out.println("result-eval: " + engine.eval(formula));
}
System.out.println("result-mine: " + l);
System.out.println("*****************************************");
}
}