|
2 | 2 | * Evaluate a numeric operations string in postfix notation using a stack.
|
3 | 3 | * Supports basic arithmetic operations: +, -, *, /
|
4 | 4 | * @see https://www.geeksforgeeks.org/evaluation-of-postfix-expression/
|
5 |
| - * @param {string} expression - Numeric operations expression to evaluate. |
| 5 | + * @param {string} expression - Numeric operations expression to evaluate. Must be a valid postfix expression. |
6 | 6 | * @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid.
|
7 | 7 | */
|
8 | 8 | function evaluatePostfixExpression(expression) {
|
9 |
| - const stack = []; |
10 |
| - |
11 |
| - // Helper function to perform an operation and push the result to the stack |
12 |
| - function performOperation(operator) { |
13 |
| - const operand2 = stack.pop(); |
14 |
| - const operand1 = stack.pop(); |
15 |
| - if (operator === '+' || operator === '-' || operator === '*' || operator === '/') { |
16 |
| - if (operand1 === undefined || operand2 === undefined) { |
17 |
| - return null; // Invalid expression |
18 |
| - } |
19 |
| - switch (operator) { |
20 |
| - case '+': |
21 |
| - stack.push(operand1 + operand2); |
22 |
| - break; |
23 |
| - case '-': |
24 |
| - stack.push(operand1 - operand2); |
25 |
| - break; |
26 |
| - case '*': |
27 |
| - stack.push(operand1 * operand2); |
28 |
| - break; |
29 |
| - case '/': |
30 |
| - if (operand2 === 0) { |
31 |
| - return null; // Division by zero |
32 |
| - } |
33 |
| - stack.push(operand1 / operand2); |
34 |
| - break; |
35 |
| - } |
36 |
| - } else { |
37 |
| - return null; // Invalid operator |
38 |
| - } |
| 9 | + const stack = []; |
| 10 | + |
| 11 | + // Helper function to perform an operation and push the result to the stack |
| 12 | + function performOperation(operator) { |
| 13 | + const rightOp = stack.pop(); // Right operand is the top of the stack |
| 14 | + const leftOp = stack.pop(); // Left operand is the next item on the stack |
| 15 | + |
| 16 | + if (leftOp === undefined || rightOp === undefined) { |
| 17 | + return false; // Invalid expression |
39 | 18 | }
|
40 |
| - |
41 |
| - const tokens = expression.split(' '); |
42 |
| - |
43 |
| - for (const token of tokens) { |
44 |
| - if (!isNaN(parseFloat(token))) { |
45 |
| - // If the token is a number, push it to the stack |
46 |
| - stack.push(parseFloat(token)); |
47 |
| - } else { |
48 |
| - // If the token is an operator, perform the operation |
49 |
| - const result = performOperation(token); |
50 |
| - if (result === null) { |
51 |
| - return null; // Invalid expression |
| 19 | + switch (operator) { |
| 20 | + case '+': |
| 21 | + stack.push(leftOp + rightOp); |
| 22 | + break; |
| 23 | + case '-': |
| 24 | + stack.push(leftOp - rightOp); |
| 25 | + break; |
| 26 | + case '*': |
| 27 | + stack.push(leftOp * rightOp); |
| 28 | + break; |
| 29 | + case '/': |
| 30 | + if (rightOp === 0) { |
| 31 | + return false; |
52 | 32 | }
|
53 |
| - } |
| 33 | + stack.push(leftOp / rightOp); |
| 34 | + break; |
| 35 | + default: |
| 36 | + break; |
54 | 37 | }
|
55 |
| - |
56 |
| - if (stack.length === 1) { |
57 |
| - // The final result should be on the stack |
58 |
| - return stack[0]; |
| 38 | + |
| 39 | + } |
| 40 | + |
| 41 | + const tokens = expression.split(/\s+/); |
| 42 | + |
| 43 | + for (const token of tokens) { |
| 44 | + if (!isNaN(parseFloat(token))) { |
| 45 | + // If the token is a number, push it to the stack |
| 46 | + stack.push(parseFloat(token)); |
59 | 47 | } else {
|
60 |
| - return null; // Invalid expression |
| 48 | + // If the token is an operator, perform the operation |
| 49 | + const result = performOperation(token); |
| 50 | + if (result === false) { |
| 51 | + return null; // Invalid expression |
| 52 | + } |
61 | 53 | }
|
62 | 54 | }
|
63 |
| - |
64 |
| - export { evaluatePostfixExpression }; |
65 |
| - |
| 55 | + |
| 56 | + return (stack.length === 1) ? stack[0] : null; |
| 57 | +} |
| 58 | + |
| 59 | +export { evaluatePostfixExpression }; |
0 commit comments