-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution005.java
71 lines (61 loc) · 2.09 KB
/
Solution005.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
package algorithm.alg4;
import java.util.*;
/**
* @author: mayuan
* @desc:
* @date: 2018/10/28
*/
public class Solution005 {
public static void main(String[] args) {
String str1 = "( 1.2 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )";
System.out.println(str1 + " == " + calculate(str1));
}
/**
* 双栈算术表达式求值 (支持 sqrt)
*
* @param str
* @return
*/
public static double calculate(String str) {
String[] strs = str.split("\\s+");
// 运算符栈
Deque<String> ops = new ArrayDeque<>();
// 操作数栈
Deque<Double> values = new ArrayDeque<>();
for (String s : strs) {
//忽略左括号
if ("(".equals(s)) {
continue;
} else if ("+".equals(s) ||
"-".equals(s) ||
"*".equals(s) ||
"/".equals(s) ||
"sqrt".equals(s) ||
"SQRT".equals(s)) {
ops.offerLast(s);
} else if (")".equals(s)) {
//遇到右括号,弹出运算符和操作数,计算并将结果压入栈中
String op = ops.pollLast();
double val = values.pollLast();
//根据运算符选择不同的运算
if ("+".equals(op)) {
val += values.pollLast();
} else if ("-".equals(op)) {
val -= values.pollLast();
} else if ("*".equals(op)) {
val *= values.pollLast();
} else if ("/".equals(op)) {
val /= values.pollLast();
} else if ("sqrt".equals(op) || "SQRT".equals(op)) {
val = Math.sqrt(val);
}
// 将运算结果压入栈中
values.offerLast(val);
} else {
// 如果该字符既不是运算符也不是括号,将它作为double 值压入栈
values.offerLast(Double.parseDouble(s));
}
}
return values.pollLast();
}
}