-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfixEvaluation.java
More file actions
97 lines (81 loc) · 2.69 KB
/
infixEvaluation.java
File metadata and controls
97 lines (81 loc) · 2.69 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
package Stacks;
import java.util.Stack;
/**
* Infix : val + operator + val
* For Brackets : if(op.size()==0 || ch=='(') push
* Don't forget to pop old op and push current op to the op stack.
*/
public class infixEvaluation {
private static int perform(char ch, int v1, int v2) {
switch (ch) {
case '-' -> {
return v1 - v2;
}
case '+' -> {
return v1 + v2;
}
case '*' -> {
return v1 * v2;
}
case '/' -> {
return v1 / v2;
}
default -> {
return -1;
}
}
}
public static void main(String[] args) {
String str = "9-(5+3)*4/6";
// Two stacks for value and operator.
Stack<Integer> val = new Stack<>();
Stack<Character> op = new Stack<>();
// Iterate over the loop
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// If it is a value push it to the val stack.
if ((int) ch >= 48 && (int) ch <= 57)
val.push((int) ch - 48);
// If op stack is empty push the operator.
else if (op.size() == 0 || ch == '(' || op.peek() == '(')
op.push(ch);
else if (ch == ')') {
while (op.peek() != '(') {
int v2 = val.pop();
int v1 = val.pop();
val.push(perform(op.peek(), v1, v2));
op.pop();
}
op.pop(); // '(' removed
} else {
// if '+' or '-' do the work as there is the least precedence of this two.
if (ch == '+' || ch == '-') {
int v2 = val.pop();
int v1 = val.pop();
val.push(perform(op.peek(), v1, v2));
op.pop();
op.push(ch);
}
if (ch == '*' || ch == '/') {
if (op.peek() == '*' || op.peek() == '/') {
int v2 = val.pop();
int v1 = val.pop();
val.push(perform(op.peek(), v1, v2));
op.pop();
op.push(ch);
} else {
op.push(ch);
}
}
}
}
// Emptying the stack and calculating
while (val.size() > 1) {
int v2 = val.pop();
int v1 = val.pop();
val.push(perform(op.peek(), v1, v2));
op.pop();
}
System.out.println(val.peek());
}
}