Skip to content

Commit de50fc0

Browse files
authored
Simplify StackPostfixNotation.postfixEvaluate (TheAlgorithms#4264)
1 parent fc274c8 commit de50fc0

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

src/main/java/com/thealgorithms/others/StackPostfixNotation.java

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,61 @@
22

33
import java.util.Scanner;
44
import java.util.Stack;
5+
import java.util.function.BiFunction;
56

7+
/**
8+
* @brief Utility class evaluating postix expressions, cf. https://en.wikipedia.org/wiki/Reverse_Polish_notation
9+
* @details The computation is done using Integers.
10+
*/
611
public final class StackPostfixNotation {
712
private StackPostfixNotation() {
813
}
914

10-
// Evaluates the given postfix expression string and returns the result.
11-
public static int postfixEvaluate(final String exp) {
12-
Stack<Integer> s = new Stack<Integer>();
15+
private static BiFunction<Integer, Integer, Integer> getOperator(final String operationSymbol) {
16+
// note the order of operands
17+
switch (operationSymbol) {
18+
case "+":
19+
return (a, b) -> b + a;
20+
case "-":
21+
return (a, b) -> b - a;
22+
case "*":
23+
return (a, b) -> b * a;
24+
case "/":
25+
return (a, b) -> b / a;
26+
default:
27+
throw new IllegalArgumentException("exp contains an unknown operation.");
28+
}
29+
}
30+
31+
private static void performOperation(Stack<Integer> s, final String operationSymbol) {
32+
if (s.size() < 2) {
33+
throw new IllegalArgumentException("exp is not a proper postfix expression (too few arguments).");
34+
}
35+
s.push(getOperator(operationSymbol).apply(s.pop(), s.pop()));
36+
}
37+
38+
private static void consumeExpression(Stack<Integer> s, final String exp) {
1339
Scanner tokens = new Scanner(exp);
1440

1541
while (tokens.hasNext()) {
1642
if (tokens.hasNextInt()) {
17-
s.push(tokens.nextInt()); // If int then push to stack
18-
} else { // else pop top two values and perform the operation
19-
if (s.size() < 2) {
20-
throw new IllegalArgumentException("exp is not a proper postfix expression (too few arguments).");
21-
}
22-
int num2 = s.pop();
23-
int num1 = s.pop();
24-
String op = tokens.next();
25-
26-
switch (op) {
27-
case "+" -> s.push(num1 + num2);
28-
case "-" -> s.push(num1 - num2);
29-
case "*" -> s.push(num1 * num2);
30-
case "/" -> s.push(num1 / num2);
31-
default -> throw new IllegalArgumentException("exp contains an unknown operation.");
32-
}
33-
// "+", "-", "*", "/"
43+
s.push(tokens.nextInt());
44+
} else {
45+
performOperation(s, tokens.next());
3446
}
3547
}
3648
tokens.close();
49+
}
50+
51+
/**
52+
* @brief Evaluates the given postfix expression.
53+
* @param exp the expression to evaluate.
54+
* @return the value of the given expression.
55+
* @exception IllegalArgumentException exp is not a valid postix expression.
56+
*/
57+
public static int postfixEvaluate(final String exp) {
58+
Stack<Integer> s = new Stack<Integer>();
59+
consumeExpression(s, exp);
3760
if (s.size() != 1) {
3861
throw new IllegalArgumentException("exp is not a proper postfix expression.");
3962
}

0 commit comments

Comments
 (0)