Login
Order Now
Support
theprogrammingassignmenthelp

JAVA Assignment - Evaluation Of Infix Expression

  • 5th Aug, 2020
  • 22:02 PM

Java Program Evaluation Of Infix Expression

 

 

package test;

import java.lang.Math; 

import java.util.StringTokenizer;

import test.MyBridgesStack;

public class InfixExpressionEvaluator {
    
    
    //private MyStack stack = new MyStack();
    
    public static double evaluateInfix(String infix, int[] values) {
        
        infix = infix.replaceAll("\\s+", "");
        
        MyBridgesStack elements  = new MyBridgesStack();
        MyBridgesStack ops = new MyBridgesStack();
        
        StringTokenizer tokens = new StringTokenizer(infix, "{}()*/+^-", true);
        
        
        while(tokens.hasMoreTokens())
        {
            String token = tokens.nextToken();
            int val = 0;
            
            if(token.matches("[a-f]+") || token.matches("[A-F]+") || token.matches("[0-9]+") )
            {
                if(token.matches("[A-F]+"))
                {
                    val = values[(int)token.charAt(0) - 65] ;
                }
                else if(token.matches("[a-f]+"))
                {    
                
                    val = values[(int)token.charAt(0) - 97] ;
                }
                else {
                    val = Integer.parseInt(token);
                }
                System.out.println(val);
                elements.push(Double.valueOf(val));
                
                elements.visualize();
                
                
            }
            
            else if(token.equals("("))
            {
                
                ops.push(token);     
            }
            
            else if (token.equals(")")) 
            { 
                if(ops.isEmpty() )
                {
                    throw new IllegalStateException("IllegalStateException : Unbalanced Paranthesis.");
                }
                
                while ( ops.peek().charAt(0) != '('  ) 
                {
                    Double op2 = elements.pop(); 
                    elements.visualize();
                    
                    String oprnd = ops.pop();
                    Double op1 = elements.pop();
                    elements.visualize();
                    
                    double result = 0.0;
                    
                    if(oprnd.equals("*"))
                    {
                        result = op1*op2;
                    } else if(oprnd.equals("/"))
                    {
                        result = op1/op2;
                    } else if(oprnd.equals("+"))
                    {
                        result = op1+op2;
                    } else if(oprnd.equals("-"))
                    {
                        result = op1-op2;
                    }
                    else if(oprnd.equals("^"))
                    {
                        result = Math.pow(op1, op2);
                    }
                    
                    elements.push(result);
                    elements.visualize();
                    
                    if(ops.isEmpty() )
                    {
                        throw new IllegalStateException("IllegalStateException : Unbalanced Paranthesis.");
                    }
                    
                }
                    
                ops.pop();
            } 
            
         // Current token is an operator. 
            else if (token.equals("+") || token.equals("-") || 
                    token.equals("*") || token.equals("/") || token.equals("^")) 
            { 
                // While top of 'ops' has same or greater precedence to current 
                // token, which is an operator. Apply operator on top of 'ops' 
                // to top two elements in values stack 
                while (!ops.isEmpty() && hasPrecedence(token.charAt(0), ops.peek().charAt(0))) 
                {
                    Double op2 = elements.pop(); 
                    elements.visualize();
                    
                    String oprnd = ops.pop();
                    
                    Double op1 = elements.pop();
                    elements.visualize();
                    
                    double result =0.0;
                    
                    if(oprnd.equals("*"))
                    {
                        result = op1*op2;
                    } else if(oprnd.equals("/"))
                    {
                        result = op1/op2;
                    } else if(oprnd.equals("+"))
                    {
                        result = op1+op2;
                    } else if(oprnd.equals("-"))
                    {
                        result = op1-op2;
                    }
                    else if(oprnd.equals("^"))
                    {
                        result = Math.pow(op1, op2);
                    }
                    
                    elements.push(result);
                    elements.visualize();
                }
                  
  
                // Push current token to 'ops'. 
                ops.push(token); 
            } 
        }    
            
        while (!ops.isEmpty() && ops.peek().charAt(0) != '(') 
        {
            Double op2 = elements.pop(); 
            elements.visualize();
            
            String oprnd = ops.pop();
            
            Double op1 = elements.pop();
            elements.visualize();
            
            double result =0.0;
            
            if(oprnd.equals("*"))
            {
                result = op1*op2;
            } else if(oprnd.equals("/"))
            {
                result = op1/op2;
            } else if(oprnd.equals("+"))
            {
                result = op1+op2;
            } else if(oprnd.equals("-"))
            {
                result = op1-op2;
            }
            else if(oprnd.equals("^"))
            {
                result = Math.pow(op1, op2);
            }
            elements.push(result);
            elements.visualize();
            
            
        }
          
        
        if(!ops.isEmpty() && ops.peek().charAt(0) == '(' )
        {
            throw new IllegalStateException("IllegalStateException : Unbalanced Paranthesis.");
        }
        // Top of 'values' contains result, return it 
        Double op33 = elements.pop(); 
        elements.visualize();
        return op33;
        
    }
    
    public static boolean hasPrecedence(char op1, char op2) 
    { 
        if (op2 == '(' || op2 == ')') 
            return false; 
        if ((op1 == '*' || op1 == '/' || op1 == '^') && (op2 == '+' || op2 == '-')) 
            return false; 
        else
            return true; 
    } 
    
    public static void main(String[] args) 
    { 
        int[] variableValues = {0,0,0,0,0,0};
        String input = "7 / 3";
        double actual = InfixExpressionEvaluator.evaluateInfix(input,variableValues);
        
        System.out.println(actual);
    }

}

Share this post