Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for each token in the reversed postfix expression:
- if token is an operator:
- push token onto the operator stack
- pending_operand ← False
- else if token is an operand:
- operand ← token
- if pending_operand is True:
- while the operand stack is not empty:
- operand_1 ← pop from the operand stack
- operator ← pop from the operator stack
- operand ← evaluate operator with operand_1 and operand
- push operand onto the operand stack
- pending_operand ← True
- result ← pop from the operand stack
- const rpn = ["-", "+", "+", 1, 1, 2, "*", 3, "/", "-", "+", 1, 1, 7, 15];
- const operator_Stack = [];
- const operand_Stack = [];
- var pending = false;
- for (i = 0; i < rpn.length; i++) {
- const token = rpn[i];
- if (typeof token === "string") {
- operator_Stack.push(token);
- pending = false;
- } else {
- var operand = token;
- if (pending) {
- while (operand_Stack.length > 0) {
- let operand_1 = operand_Stack.pop();
- let operator = operator_Stack.pop();
- operand = eval(operand_1 + " " + operator + " " + operand);
- }
- }
- operand_Stack.push(operand);
- pending = true;
- }
- }
- console.log(operand_Stack.pop());
Add Comment
Please, Sign In to add comment