-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfixExpression.java
34 lines (29 loc) · 1001 Bytes
/
InfixExpression.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
// 1.3.9 Write a program that takes an expression without left parentheses
// and prints the equivalent infix expression with the parentheses inserted.
package pkg;
import java.util.Stack;
public class InfixExpression {
public static void printInfix(String input) {
String[] values = input.split("\\s");
Stack<String> operators = new Stack<String>();
Stack<String> operands = new Stack<String>();
for (int i = 0; i < values.length; i++) {
String value = values[i];
if (value.equals(")")) {
String b = operands.pop();
String a = operands.pop();
String op = operators.pop();
operands.push("( " + a + " " + op + " " + b + " )");
} else if (value.equals("+") || value.equals("-") || value.equals("*") || value.equals("/")) {
operators.push(value);
} else {
// value is an integer
operands.push(value);
}
}
System.out.println(operands.pop());
}
public static void main(String[] args) {
printInfix("1 + 2 ) * 3 - 4 ) * 5 - 6 ) ) )");
}
}