-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressionEvaluator.java
77 lines (66 loc) · 2.54 KB
/
ExpressionEvaluator.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
import java.util.ArrayDeque;
import java.util.Deque;
public class ExpressionEvaluator {
public double stringss(String expression) {
String cleanedExpression = expression.replaceAll("\\s+", "");
String[] tokens = cleanedExpression.split("(?<=\\D)|(?=\\D)");
Deque<Double> operandStack = new ArrayDeque<>();
Deque<Character> operatorStack = new ArrayDeque<>();
for (String token : tokens) {
if (isNumeric(token)) {
double operand = Double.parseDouble(token);
operandStack.push(operand);
} else if (isOperator(token.charAt(0))) {
char c = token.charAt(0);
while (!operatorStack.isEmpty() && hasPrecedence(c, operatorStack.peek())) {
performOperation(operandStack, operatorStack);
}
operatorStack.push(c);
} else if (token.equals("(")) {
operatorStack.push('(');
} else if (token.equals(")")) {
while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
performOperation(operandStack, operatorStack);
}
operatorStack.pop();
}
}
while (!operatorStack.isEmpty()) {
performOperation(operandStack, operatorStack);
}
return operandStack.pop();
}
public boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
public boolean hasPrecedence(char op1, char op2) {
if (op2 == '(' || op2 == ')') {
return false;
}
return (op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-');
}
public void performOperation(Deque<Double> operandStack, Deque<Character> operatorStack) {
double operand2 = operandStack.pop();
double operand1 = operandStack.pop();
char operator = operatorStack.pop();
double result = 0.0;
switch (operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
operandStack.push(result);
}
public boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
}