-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA008InPostPreFIX.java
More file actions
70 lines (62 loc) · 1.95 KB
/
A008InPostPreFIX.java
File metadata and controls
70 lines (62 loc) · 1.95 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
public class A008InPostPreFIX{
private int myDelimiter = -362;
private static final String S = " ";
public void evaluateInfixToPostFix(String[] arr){
A004StackUsingLinkedList stack = new A004StackUsingLinkedList();
StringBuilder sb = new StringBuilder();
for (String s : arr) {
int num = getNo(s);
if(num == myDelimiter){
//check for operator
}else{
sb.append(num+S);
}
}
}
public void evaluateInfixToPreFix(String[] arr){
}
public void evalPostFix(String[] arr){//evaluate the expression
A004StackUsingLinkedList stack = new A004StackUsingLinkedList();
String[] input = {"10", "20" , "-"};
for (String in : input) {
int num = getNo(in);
if(num == myDelimiter){
//is operator
int op2 = stack.pop();
int op1 = stack.pop();
int result = getResult(op1 , op2 , in);
stack.push(result);
}else{
stack.push(num);
}
}
System.out.println("Result: "+stack.pop());
}
public void evalPreFix(String[] arr){
}
public int getResult(int op1 , int op2 , String operator){
switch(operator){
case "+":
return op1 + op2;
case "*":
return op1 * op2;
case "/":
return op1 / op2;
case "-":
return op1 - op2;
}
return 0 ;
}
public int getNo(String s ){
try{
int no = Integer.parseInt(s);
return no ;
}catch(Exception e){
return -362;
}
}
public static void main(String []args){
A008InPostPreFIX ob = new A008InPostPreFIX();
ob.evalPostFix(args);
}
}