Advertisement
haikid

postfix evaluate

Mar 25th, 2024
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. double evalute() {
  2.     OurStack<String> postFixStack = new OurStack<String>();
  3.  
  4.     while (!postFix.isEmpty())
  5.     {
  6.         curr = postFix.remove();
  7.        
  8.         if (!isoperand(curr)) {
  9.             // curr is +, -, *, /
  10.  
  11.             double operand2 = postFixStack.pop();
  12.             double operand1 = postFixStack.pop();
  13.  
  14.             if curr == "+"
  15.                 postFixStack.push(operand1 + operand2);
  16.             else if curr == "-"
  17.                 postFixStack.push(operand1 - operand2);
  18.             else if curr == "*"
  19.                 postFixStack.push(operand1 * operand2);
  20.             else
  21.                 postFixStack.push(operand1 / operand2);
  22.         } else {
  23.             // curr is a number
  24.  
  25.             postFixStack.push(Double.parseDouble(curr));
  26.         }
  27.     }
  28.  
  29.     return postFixStack.peek(); // last number remaining on stack
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement