Guest User

Untitled

a guest
Jan 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. for each token in the reversed postfix expression:
  2. if token is an operator:
  3. push token onto the operator stack
  4. pending_operand ← False
  5. else if token is an operand:
  6. operand ← token
  7. if pending_operand is True:
  8. while the operand stack is not empty:
  9. operand_1 ← pop from the operand stack
  10. operator ← pop from the operator stack
  11. operand ← evaluate operator with operand_1 and operand
  12. push operand onto the operand stack
  13. pending_operand ← True
  14. result ← pop from the operand stack
  15.  
  16. const rpn = ["-", "+", "+", 1, 1, 2, "*", 3, "/", "-", "+", 1, 1, 7, 15];
  17. const operator_Stack = [];
  18. const operand_Stack = [];
  19. var pending = false;
  20.  
  21. for (i = 0; i < rpn.length; i++) {
  22. const token = rpn[i];
  23. if (typeof token === "string") {
  24. operator_Stack.push(token);
  25. pending = false;
  26. } else {
  27. var operand = token;
  28. if (pending) {
  29. while (operand_Stack.length > 0) {
  30. let operand_1 = operand_Stack.pop();
  31. let operator = operator_Stack.pop();
  32. operand = eval(operand_1 + " " + operator + " " + operand);
  33. }
  34. }
  35. operand_Stack.push(operand);
  36. pending = true;
  37. }
  38. }
  39. console.log(operand_Stack.pop());
Add Comment
Please, Sign In to add comment