Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Problem with infix evaluator?

  1. #1
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Infix evaluator returning zero as the result everytime. Need Help

    I don't know if the evaluator actually works correctly because it always returns a result of zero. I have been messing with it for a while and can't seem to figure out why it won't give any other value than zero for the result. Thanks ahead of time for the help.

    Java Code:
    import java.util.Scanner;
    
    public class InFixEvaluator
    {
      public static int evaluate(String expression)
      {
        BoundedStackInterface<String>  opStack    = new ArrayStack<String>(50);  
        BoundedStackInterface<Integer> valueStack = new ArrayStack<Integer>(50);
        
        String thisOp;
        int value;
        int operand1;
        int operand2;
    
        int result = 0;
    
        Scanner tokenizer = new Scanner(expression);
    
        while (tokenizer.hasNext()){
          if (tokenizer.hasNextInt())
          {
            value = tokenizer.nextInt();
            valueStack.push(value);
          }
          else{
            thisOp = tokenizer.next();
          	opStack.push(thisOp);
          }
        }
        while(!opStack.isEmpty()){
        	thisOp = opStack.top();
        	opStack.pop();
          if (thisOp == ")") 
            while(thisOp != "("){
            	operand1 = valueStack.top();
            	valueStack.pop();
            	operand2 = valueStack.top();
            	valueStack.pop();
            	thisOp = opStack.top();
            	opStack.pop();
            // Perform operation.
            if(thisOp.equals("*")){
              result = operand1 * operand2;
            valueStack.push(result);
            }
            else
            if(thisOp.equals("+")){
              result = operand1 + operand2;
              valueStack.push(result);
            }
            else
            if(thisOp.equals("-")){
              result = operand1 - operand2;
              valueStack.push(result);
            }
            else
            	System.out.println("Invalid Operator");
          }
          if(opStack.isEmpty())
        	  break;
          else {
             operand1 = valueStack.top();
             valueStack.pop();
             operand2 = valueStack.top();
             valueStack.top();
             thisOp = opStack.top();
             opStack.pop();
           if(thisOp.equals("*"))
            	result = operand1 * operand2;
           else
           if(thisOp.equals("+"))
            	result = operand1 + operand2;
           else
           if(thisOp.equals("-"))
                result = operand1 = operand2;
           else
        	   System.out.println("Invalid operator");
           valueStack.push(result);
             }
          result = valueStack.top();
        }
        return result;
      }
    }
    That is the evaluator and here is the code main class.
    Java Code:
    import java.util.Scanner;
    
    public class IFixConsole {
    
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		
    		String line;
    		String more;
    		
    		int result;
    		
    		do{
    			System.out.println("Enter a infix expression to be evaluated: ");
    			line = input.nextLine();
    			result = InFixEvaluator.evaluate(line);
    			System.out.println();
    			System.out.println("Result = " + result);
    			System.out.println();
    			System.out.println("Evaluate another expression? (Y = Yes): ");
    			more = input.nextLine();
    			System.out.println();
    		}
    		while (more.equalsIgnoreCase("y"));
    		
    		System.out.println("Program completed.");
    	}
    }
    Last edited by chris1; 10-11-2011 at 05:57 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem with infix evaluator?

    There's a lot going on in that evaluate() method. Sprinkle in some System.out.println( ... ) statements and print the current stack top, operators and operands so you can see what's actually happening in there.

    kind regards,

    Jos
    chris1 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Re: Problem with infix evaluator?

    Thanks I didn't even think about trying that.

  4. #4
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default Re: Problem with infix evaluator?

    Java Code:
    import java.util.Scanner;
    
    public class InFixEvaluator
    {
      public static int evaluate(String expression)
      {
        BoundedStackInterface<String>  opStack    = new ArrayStack<String>(50);  
        BoundedStackInterface<Integer> valueStack = new ArrayStack<Integer>(50);
        
        String thisOp;
        int value;
        int operand1;
        int operand2;
    
        int result = 0;
    
        Scanner tokenizer = new Scanner(expression);
    
        while (tokenizer.hasNext()){
          if (tokenizer.hasNextInt()){
            value = tokenizer.nextInt();
            valueStack.push(value);
          }
          else{
            thisOp = tokenizer.next();
            opStack.push(thisOp);
          }
        }
        while(!opStack.isEmpty()){
            thisOp = opStack.top();
            opStack.pop();
          if (thisOp == ")"){
            while(thisOp != "("){
                operand1 = valueStack.top();
                valueStack.pop();
                System.out.println(operand1);
                operand2 = valueStack.top();
                valueStack.pop();
                System.out.println(operand2);
                thisOp = opStack.top();
                opStack.pop();
                System.out.println(thisOp);
                
            if(thisOp.equals("*")){
              result = operand1 * operand2;
            valueStack.push(result);
            }
            else
            if(thisOp.equals("+")){
              result = operand1 + operand2;
              valueStack.push(result);
            }
            else
            if(thisOp.equals("-")){
              result = operand1 - operand2;
              valueStack.push(result);
            }
          }
          }
          else {
             operand2 = valueStack.top();
             valueStack.pop();
             operand1 = valueStack.top();
             valueStack.pop();
           if(thisOp.equals("*"))
                result = operand1 * operand2;
           else
           if(thisOp.equals("+"))
                result = operand1 + operand2;
           else
           if(thisOp.equals("-"))
                result = operand1 - operand2;
           
           valueStack.push(result);
          }
          result = valueStack.top();
        }
        return result;
      }
    }
    That is what I have so far and it works when there is no parentheses. I need it to work with parentheses and I need the operators to work in the correct precedence which I am having trouble figuring out how to do that.

Similar Threads

  1. Infix to Prefix
    By Sasarai in forum Advanced Java
    Replies: 4
    Last Post: 12-08-2010, 04:57 PM
  2. Infix to Postfix using array
    By Franneldort in forum New To Java
    Replies: 0
    Last Post: 10-11-2010, 06:57 PM
  3. Lucene as Conditional Evaluator / Indexer?
    By cuebei in forum Lucene
    Replies: 0
    Last Post: 01-11-2010, 08:36 PM
  4. Infix to Post fix
    By Tenn in forum New To Java
    Replies: 0
    Last Post: 05-03-2009, 10:21 PM
  5. Replies: 1
    Last Post: 07-05-2008, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •