-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathStackPostfixNotation.java
72 lines (65 loc) · 2.4 KB
/
StackPostfixNotation.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
package com.thealgorithms.stacks;
import java.util.Scanner;
import java.util.Stack;
import java.util.function.BiFunction;
/**
* Utility class for evaluating postfix expressions using integer arithmetic.
* <p>
* Postfix notation, also known as Reverse Polish Notation (RPN), is a mathematical notation in which operators follow their operands.
* This class provides a method to evaluate expressions written in postfix notation.
* </p>
* <p>
* For more information on postfix notation, refer to
* <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish Notation (RPN) on Wikipedia</a>.
* </p>
*/
public final class StackPostfixNotation {
private StackPostfixNotation() {
}
private static BiFunction<Integer, Integer, Integer> getOperator(final String operationSymbol) {
// note the order of operands
switch (operationSymbol) {
case "+":
return (a, b) -> b + a;
case "-":
return (a, b) -> b - a;
case "*":
return (a, b) -> b * a;
case "/":
return (a, b) -> b / a;
default:
throw new IllegalArgumentException("exp contains an unknown operation.");
}
}
private static void performOperation(Stack<Integer> s, final String operationSymbol) {
if (s.size() < 2) {
throw new IllegalArgumentException("exp is not a proper postfix expression (too few arguments).");
}
s.push(getOperator(operationSymbol).apply(s.pop(), s.pop()));
}
private static void consumeExpression(Stack<Integer> s, final String exp) {
Scanner tokens = new Scanner(exp);
while (tokens.hasNext()) {
if (tokens.hasNextInt()) {
s.push(tokens.nextInt());
} else {
performOperation(s, tokens.next());
}
}
tokens.close();
}
/**
* @brief Evaluates the given postfix expression.
* @param exp the expression to evaluate.
* @return the value of the given expression.
* @exception IllegalArgumentException exp is not a valid postix expression.
*/
public static int postfixEvaluate(final String exp) {
Stack<Integer> s = new Stack<>();
consumeExpression(s, exp);
if (s.size() != 1) {
throw new IllegalArgumentException("exp is not a proper postfix expression.");
}
return s.pop();
}
}