-4

I'm trying to finish an assignment right now. I have a 'calculator' without parentheses, that converts this infix expression to postfix, and is trying to handle the situation of where there are operators in the operator stack.

I am referring to this algorithm: http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm

Right now my converter has issues with the case of there being precedence of the top of the stack and the scanned character. I have a while loop to take care of this process, where it pops the higher priority option or it will simply push it to the top of the stack. But, it seems to not want to leave that while loop. It should only be in that loop IF the stack is NOT EMPTY and there is precedence of the top of stack over the scanned character.

However, stack.isEmpty currently isn't changing at all in the in the debugger.

Here is my code:

import java.util.Stack;


public class Calculator{



   public static String[] convert(String expression){

      Stack<String> ops = new Stack<String>();

      String[] eval = expression.split(" ");
      String[] post = new String[eval.length];


      int postIndex = 0;
      int stackSize = 0;



      for(int i = 0; i < eval.length; i++){
         if(!isOperator(eval[i])){
            post[postIndex] = eval[i];
            postIndex++;
         }

         else if(isOperator(eval[i]) && ops.empty()){
            ops.push(eval[i]);
            stackSize++;
         }

         else if(isOperator(eval[i]) && (!ops.empty())){
           //ITS EMPTY, SO WHY DO YOU TRY TO GO HERE???
            while( (precedence(ops.peek(),eval[i]) && (stackSize > 0)) ) { 
               if(precedence(ops.peek(),eval[i])){
                  post[postIndex] = ops.pop();
                  stackSize--;
                  postIndex++;

               }

               else{
                  ops.push(eval[i]);
                  stackSize++;
               }
            }   
         }
      }


      while(stackSize > 0){
         post[postIndex] = ops.pop();
         stackSize--;
      }

      return post;   
   }

   public static double evaluatePostfix(String[] postfixExpression) {
      Stack<Double> opStack = new Stack<Double>();


      for(int i = 0; i < postfixExpression.length; i++){

         if(!isOperator(postfixExpression[i])){
            opStack.push(Double.parseDouble(postfixExpression[i]));
         }



         else{ 
            double left= opStack.pop();
            double right= opStack.pop();
            opStack.push(evaluateWithOperator(postfixExpression[i],right,left));
         }
      }




      return opStack.pop();
   }



   private static boolean isOperator(String s) {
      return "+-*/".contains(s);
   }



   private static boolean precedence(String opA, String opB){
      if((opA.equals("*") || opA.equals("/")) && (opB.equals("+") || opB.equals("-"))) {
         return true;
      }

      return false;
   }

   private static double evaluateWithOperator(String operator, double operandA, double operandB) {
      switch(operator) {
         case "+":
            return operandA + operandB;
         case "-":
            return operandA - operandB;
         case "*":
            return operandA * operandB;
         case "/":
            return operandA / operandB;
         default:
            return -1;
      }
   }

   public static void main(String[] args){
      String expe = "2 * 3 + 5 * 5 * 2 / 10 - 1 * 1";

      evaluatePostfix(convert(expe));


   }
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • 1. It is poor form to post a link to code instead of posting code. If you read a handful of posts here that do not have negative votes, you will see that. 2. Your code comments state something that seems certain to be untrue - that an if-condition block is entered even though the condition is false. It is unlikely in the extreme that you have found a bug of this type in Java itself. I would have made an attempt, but my eclipse appears to be down. – arcy Oct 19 '13 at 12:09

1 Answers1

1

Your problem is in your loop:

while( (precedence(ops.peek(),eval[i]) && (stackSize > 0)) ) {
               if(precedence(ops.peek(),eval[i])){
                  post[postIndex] = ops.pop();
                  stackSize--;
                  postIndex++;

               }

ops.pop() takes something out of ops, and peek depends on something being in there. It isn't reevaluating ops.empty().

arcy
  • 12,845
  • 12
  • 58
  • 103