Question: 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

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 

For step 2, use this stack class with the exception, feel free to modify the class:

Stack Class:

public class Stack { private LL theStack; public Stack() { theStack = new LL(); } public boolean isEmpty() { return theStack.isEmpty(); } public boolean isFull() { return false; } public void push(T value) { theStack.insertAtHead(value); } public T pop() throws StackException { T retval = null; try { retval = theStack.deleteFromHead(); } catch (LLException e) { throw new StackException("Stack underflow."); } return retval; } public static void main(String args[]) throws StackException { Stack st = new Stack(); for (int i = 0; i < 20; i++) { st.push(i); } while (!st.isEmpty()) System.out.println(st.pop()); } } 

Stack Exception Class:

public class StackException extends Exception { public StackException(String s) { super(s); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!