-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInfixToPostfix.java
78 lines (67 loc) · 2.36 KB
/
InfixToPostfix.java
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
/*
Author:- Siri Lalitha Adapa
Convert Infix to Postix Expression
Objective: Given an Infix expression, write an algorithm to convert it into Postfix expression.
Algo rules:
=> If the character is operand, add to result.
=> If character is operator
-> if stack is empty, push into stack
-> else if stack is not empty
- if the operator precedence is >= top operator, push the
character to the operator stack
- else "pop out an operator from the stack and add it to result until stack is empty or top elements precedence is >=, then push the operator"
=> If character is '(', then push
=> If the character is ')', then pop until '(' and add them to output and pop '('
=> once the expression is ended pop all the operators from stack and append to result
*/
// Ex: input: a+b*c/d%e^f/g+k-l^g output: abc*d/ef^g/+k+lg^-
import java.util.Scanner;
import java.util.Stack;
class InfixToPostfix {
public static int precedence(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
}
return -1;
}
public static String infixToPostfix(String infix) {
String postfix = "";
Stack<Character> operators = new Stack<>();
for (char ch : infix.toCharArray()) {
if (precedence(ch) > 0) {
while (operators.isEmpty() == false && precedence(operators.peek()) >= precedence(ch)) {
postfix += operators.pop();
}
operators.push(ch);
} else if (ch == ')') {
char x = operators.pop();
while (x != '(') {
postfix += x;
x = operators.pop();
}
} else if (ch == '(') {
operators.push(ch);
} else {
postfix += ch;
}
}
while (!operators.isEmpty()) {
postfix += operators.pop();
}
return postfix;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String infixExpression = in.next();
String postfixExpression = infixToPostfix(infixExpression);
System.out.println(postfixExpression);
}
}