-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvaluateInfixExpression.java
More file actions
110 lines (96 loc) · 3.89 KB
/
EvaluateInfixExpression.java
File metadata and controls
110 lines (96 loc) · 3.89 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
package datastructure.stack.program;
import datastructure.stack.Stack;
public class EvaluateInfixExpression {
public static void main(String[] args) throws Exception {
String expression = "5+3*4-8";// "((4 + 4) * 4) / 2 - 1";
Integer result = evaluateExpression(expression);
System.out.println("Result is: " + result);
}
private static Integer evaluateExpression(String expression) throws Exception {
Stack<Integer> operand = new Stack<>();
Stack<Character> operator = new Stack<>();
char[] charArray = expression.toCharArray();
for (Character character : charArray) {
// if empty character then skip the iteration.
if (character == ' ')
continue;
// if current character is a number push it on the operand stack
if (Character.isDigit(character))
operand.push(Character.getNumericValue(character));
else {
// check current and previous operator precedence
int currPrecedence = getPrecedence(character);
int prevPrecedence = (operator.peek() != null) ? getPrecedence(operator.peek()) : 0;
// character is open parenthesis then push on to the operator
// stack
if (character == '(') {
operator.push(character);
continue;
}
// if the operator is closed parenthesis then pop the operator
// stack once and operand stack twice, apply and get the result
// and push it on the operand stack on each iteration until we
// encounter left parenthesis
if (character == ')') {
while (operator.peek() != '(') {
applyOpAndPushResult(operand, operator);
}
operator.pop();
continue;
}
// if the operator on stack top has more precedence then pop the
// operator stack and pop the operand stack twice, apply the
// operator on them and push the result back in the stack;
while (currPrecedence < prevPrecedence) {
applyOpAndPushResult(operand, operator);
prevPrecedence = (operator.peek() != null) ? getPrecedence(operator.peek()) : 0;
}
operator.push(character);
}
}
// Evaluate the by popping the stacks
while (operator.peek() != null)
applyOpAndPushResult(operand, operator);
// pop the result which will be there on the top of the stack
return operand.pop();
}
private static void applyOpAndPushResult(Stack<Integer> operand, Stack<Character> operator) throws Exception {
char op = operator.pop();
int op1 = operand.pop();
int op2 = operand.pop();
int result = calculate(op, op1, op2);
operand.push(result);
}
private static int calculate(char op, int op1, int op2) throws Exception {
switch (op) {
case '+':
return op2 + op1;
case '-':
return op2 - op1;
case '*':
return op2 * op1;
case '/':
return op2 / op1;
default:
throw new Exception("Invalid operator");
}
}
private static int getPrecedence(Character op) throws Exception {
switch (op) {
case '+':
return 2;
case '-':
return 1;
case '*':
return 3;
case '/':
return 4;
case '(':
return 0;
case ')':
return 5;
default:
throw new Exception("Invalid operator");
}
}
}