Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming: Create a function to evaluate infix double expressions. Your f

ID: 3702100 • Letter: J

Question

 Java Programming: Create a function to evaluate infix double expressions.  Your function should accept a string containing a valid infix expression of constant doubles, and return a double representing the value of the expression. If you call your function with the string "10.0 + 3 * 45.0" it should return the value 145.0. If you call it with the string "(10.0 + 3) * 45.0" it should return 585.0  1) Figure out how to parse the string into a series of tokens -- operands, operators, and parentheses. For the second example, you would probably want to turn the input string into an array of  strings '(' '10.0' '+' '3' ') '*' '45.0', then use this array to drive the conversion to a  value.  You might find it helpful to look at java's StringTokenizer class.  You will want to use the version of the appropriate function which takes a string, a list of terminators, and a boolean.  2) Once you have that working, then you will want to implement the "shunting yard algorithm" to convert your array of tokens into a value.  This algorithm is described here:  https://en.wikipedia.org/wiki/Shunting-yard_algorithm

Explanation / Answer

Steps to be followed during the evaluation of an expression as follows:

The first element pop assigned to op2

The second element is aasigned op

The third assigned element assigned to op1

  The fourth popped element is the remaining opening bracket  which can be discarded.

Evaluate op1 op op2

package com.java2novice.ds.stack;

import java.util.StringTokenizer;

public class InfixFullParantEx {

    public static String evaluateInfix(String exps){

        

        exps = exps.replaceAll("\s+", "");

        Stack1<String> stack = new Stack1<String>(exps.length());

                StringTokenizer tokens = new StringTokenizer(exps, "{}()*/+-", true);

        while(tokens.hasMoreTokens()){

            String tkn = tokens.nextToken();

            if(tkn.equals("(")

                    || tkn.equals("{")

                    || tkn.matches("[0.0-9.0]+")

                    || tkn.equals("*")

                    || tkn.equals("/")

                    || tkn.equals("+")

                    || tkn.equals("-")){

                /**push token to the stack**/

                stack.push(tkn);

            } else if(tkn.equals("}") || tkn.equals(")")){

                try {

                    int op2 = Integer.parseInt(stack.pop());

                    String oprnd = stack1.pop();

                    int op1 = Integer.parseInt(stack.pop());

                    if(!stack.isStackEmpty()){

                        stack.pop();

                    }

                    int result = 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;

                    }

                  

                    stack.push(result+"");

                } catch (Exception e) {

                    e.printStackTrace();

                    break;

                }

            }

        }

        String finalResult = "";

        try {

            finalResult = stack.pop();

        } catch (Exception e) {

            e.printStack1();

        }

        return finalResult;

    }

     

    public static void main(String a[]){

        String expr = "((2*5)+(6/2))";

        System.out.println("Expression: "+expr);

        System.out.println("Final Result: "+evaluateInfix(expr));

        expr = "(((2 * 5) - (1 * 2)) / (11 - 9))";

        System.out.println("Expression: "+expr);

        System.out.println("Final Result: "+evaluateInfix(expr));

         

    }

}

public class Stack1<T extends Object> {

    private int stackSize;

    private T[] stackArr;

    private int top;

    public Stack1(int size) {

        this.stackSize = size;

        this.stackArr = (T[]) new Object[stackSize];

        this.top = -1;

    }

    public void push(T entry){

        if(this.isStackFull()){

            System.out.println(("Stack is full. Increasing the capacity."));

            this.increaseStackCapacity();

        }

        System.out.println("Adding: "+entry);

        this.stackArr[++top] = entry;

    }

    

    public T pop() throws Exception {

        if(this.isStackEmpty()){

            throw new Exception("Stack is empty. Can not remove element.");

        }

        T entry = this.stackArr[top--];

        System.out.println("Removed entry: "+entry);

        return entry;

    }

     

   

    public T peek() {

        return stackArr[top];

    }

    private void increaseStackCapacity(){

         

        T[] newStack = (T[]) new Object[this.stackSize*2];

        for(int i=0;i<stackSize;i++){

            newStack[i] = this.stackArr[i];

        }

        this.stackArr = newStack;

        this.stackSize = this.stackSize*2;

    }

    public boolean isStackEmpty() {

        return (top == -1);

    }

    public boolean isStackFull() {

        return (top == stackSize - 1);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote