Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add algorithm to evaluate postfix string #1441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
raklaptudirm merged 11 commits into TheAlgorithms:master from gaurovgiri:feat/evaluate-postfix-string
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Data-Structures/Stack/EvaluateExpression.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Evaluate a numeric operations string in postfix notation using a stack.
* Supports basic arithmetic operations: +, -, *, /
* @see https://www.geeksforgeeks.org/evaluation-of-postfix-expression/
* @param {string} expression - Numeric operations expression to evaluate. Must be a valid postfix expression.
* @returns {number|null} - Result of the expression evaluation, or null if the expression is invalid.
*/
function evaluatePostfixExpression(expression) {
const stack = [];

// Helper function to perform an operation and push the result to the stack. Returns success.
function performOperation(operator) {
const rightOp = stack.pop(); // Right operand is the top of the stack
const leftOp = stack.pop(); // Left operand is the next item on the stack

if (leftOp === undefined || rightOp === undefined) {
return false; // Invalid expression
}
switch (operator) {
case '+':
stack.push(leftOp + rightOp);
break;
case '-':
stack.push(leftOp - rightOp);
break;
case '*':
stack.push(leftOp * rightOp);
break;
case '/':
if (rightOp === 0) {
return false;
}
stack.push(leftOp / rightOp);
break;
default:
return false; // Unknown operator
}
return true;
}

const tokens = expression.split(/\s+/);

for (const token of tokens) {
if (!isNaN(parseFloat(token))) {
// If the token is a number, push it to the stack
stack.push(parseFloat(token));
} else {
// If the token is an operator, perform the operation
if (!performOperation(token)) {
return null; // Invalid expression
}
}
}

return (stack.length === 1) ? stack[0] : null;
}

export { evaluatePostfixExpression };
22 changes: 22 additions & 0 deletions Data-Structures/Stack/test/EvaluateExpression.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { evaluatePostfixExpression } from '../EvaluateExpression.js';

describe('evaluatePostfixExpression', () => {
it('should evaluate a valid expression', () => {
const expression = '3 4 * 2 / 5 +'; // (3 * 4) / 2 + 5 = 11
const result = evaluatePostfixExpression(expression);
expect(result).toBe(11);
});

it('should handle division by zero', () => {
const expression = '3 0 /'; // Division by zero
const result = evaluatePostfixExpression(expression);
expect(result).toBe(null);
});

it('should handle an invalid expression', () => {
const expression = '3 * 4 2 / +'; // Invalid expression
const result = evaluatePostfixExpression(expression);
expect(result).toBe(null);
});

});

AltStyle によって変換されたページ (->オリジナル) /