-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposixNotation.java
More file actions
41 lines (36 loc) · 842 Bytes
/
posixNotation.java
File metadata and controls
41 lines (36 loc) · 842 Bytes
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
import java.util.*;
public class posixNotation {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "33+4*";
evaluate(str);
}
public static void evaluate(String str) {
Stack mystack = new Stack();
for (int i=0; i<str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 0 && ch <= 9) {
mystack.push(ch);
} else {
Integer int2 = Integer.parseInt((String) (mystack.pop()));
Integer int1 = Integer.parseInt((String) (mystack.pop()));
switch (ch) {
case '+':
mystack.push(int1 + int2);
break;
case '-':
mystack.push(int1 - int2);
break;
case '*':
mystack.push(int1 * int2);
break;
case '/':
mystack.push(int1 / int2);
break;
}
}
}
int res = (int) mystack.pop();
System.out.println(res);
}
}