|
2 | 2 |
|
3 | 3 | import java.util.Scanner;
|
4 | 4 | import java.util.Stack;
|
| 5 | +import java.util.function.BiFunction; |
5 | 6 |
|
| 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 | + */ |
6 | 11 | public final class StackPostfixNotation {
|
7 | 12 | private StackPostfixNotation() {
|
8 | 13 | }
|
9 | 14 |
|
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) { |
13 | 39 | Scanner tokens = new Scanner(exp);
|
14 | 40 |
|
15 | 41 | while (tokens.hasNext()) {
|
16 | 42 | 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()); |
34 | 46 | }
|
35 | 47 | }
|
36 | 48 | 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); |
37 | 60 | if (s.size() != 1) {
|
38 | 61 | throw new IllegalArgumentException("exp is not a proper postfix expression.");
|
39 | 62 | }
|
|
0 commit comments