-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefix_to_postfix.java
More file actions
32 lines (32 loc) · 1014 Bytes
/
Copy pathprefix_to_postfix.java
File metadata and controls
32 lines (32 loc) · 1014 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
public static String preToPost(String infix) {
char curChar;
int infixLength = infix.length();
String postFix = "";
Stack<Character> newStack = new Stack<Character>();
for (int x = 0; x < infix.length(); x++) {
System.out.println("stack="+newStack);
System.out.println("postfix="+postFix);
curChar = infix.charAt(x);
if (isOperator(curChar)) {
newStack.push(curChar);
} else {
if (!isOperator(curChar))
postFix = postFix + curChar;
while (!newStack.isEmpty() && newStack.peek() == '#') {
newStack.pop();
postFix = postFix + newStack.pop();
// newStack.pop();
}
newStack.push('#');
}
}
return postFix;
}
public static boolean isOperator(char op) {
if (op == '+' || op == '-' || op == '*' || op == '/') {
return true;
} else {
return false;
}
}
}