Question
Implement a java program: ComputeInfix.java. The program should prompt the user to enter an arithmetic expression and output the value of the expression. Assume that
Implement a java program: ComputeInfix.java.
The program should prompt the user to enter an arithmetic expression and output the value of the expression. Assume that the input contains integers (no variables), operations +, -, *, /, and parenthesis ( and ). No other characters should be in the input string. The integers are all non-negative. You may assume that input expression always has correct syntax (balanced parentheses, etc.). The program does not have to detect syntactic errors in the expression. Integers may have more than one digit in them, e.g. 25, 143, etc. To extract integers from a strings you may use the following method: How to extract an integer from a string Dialog with the user may look like the following:
Please enter an expression: 6+(2+3*4)/2 The expression evaluates to 13
Pseudocode to evaluate an infix expression without full parentheses is the following:
__________________________________ 1. Initialize two stacks: a numbers stack and an operators stack. 2. do
if (the next input is a left parenthesis) Read the left parenthesis and push it onto the operators stack
else if (the next input is a number) Read the number and push it onto the numbers stack
else if (the next input is one of the operation symbols)
{ while (1) the operators stack is not empty, and
(2) the top symbol on the operators stack is parenthesis, and
(3) the top symbol on the operators stack is lower precedence than the next input
NOT a left NOT an operation with
do the following: pop an operation off the operators stack and use that operation by popping two numbers off the numbers stack,
applying the operation to the two numbers, and
pushing the answer back on the numbers stack After finishing the while loop read the next input (operation) and
push it onto the operators stack }
else {
Read and discard the next input symbol (which should be a right parenthesis).
while the top symbol on operators stack is NOT a left parenthesis and the operators stack is not empty
do the following: pop operation off the operators stack and use that operation by popping two numbers off the numbers stack,
If no
applying the operation to the two numbers, and
pushing the answer back on the numbers stack left parenthesis is encountered in the operators stack, then print an error message indicating unbalanced parentheses and halt.
Pop and discard the left parentheses from the operators stack. }
while (there is more of the expression to read) 3. while the operators stack is not empty do the following:
pop operation off the operators stack and use that operation by popping two numbers off the numbers stack,
applying the operation to the two numbers, and
pushing the answer back on the numbers stack (There should be no remaining left parentheses; otherwise,
the input expression did not have balanced parentheses.) __________________________________
If the infix expression has correct syntax (balanced parentheses, etc.), then after execution of the pseudocode, the operators stack will be empty and the numbers stack will contain one number which is the value of the expression.
********************************
// File: Node.java from the package edu.colorado.nodes // Complete documentation is available from the Node link in: // http://www.cs.colorado.edu/~main/docs
// package edu.colorado.nodes;
/****************************************************************************** * A Node
/** * Initialize a node with a specified initial data and link to the next * node. Note that the initialLink may be the null reference, * which indicates that the new node has nothing after it. * @param initialData * the initial data of this new node * @param initialLink * a reference to the node after this new node--this reference may be null * to indicate that there is no node after this new node. * @postcondition * This node contains the specified data and link to the next node. **/ public Node(E initialData, Node
/** * Modification method to add a new node after this node. * @param element * the data to place in the new node * @postcondition * A new node has been created and placed after this node. * The data for the new node is element. Any other nodes * that used to be after this node are now after the new node. * @exception OutOfMemoryError * Indicates that there is insufficient memory for a new * Node. **/ public void addNodeAfter(E element) { link = new Node
/** * Copy part of a list, providing a head and tail reference for the new copy. * @param start/end * references to two nodes of a linked list * @param copyHead/copyTail * the method sets these to refer to the head and tail node of the new * list that is created * @precondition * start and end are non-null references to nodes * on the same linked list, * with the start node at or before the end node. * @return * The method has made a copy of the part of a linked list, from the * specified start node to the specified end node. The return value is an * array where the [0] component is a head reference for the copy and the * [1] component is a tail reference for the copy. * @exception IllegalArgumentException * Indicates that start and end do not satisfy * the precondition. * @exception OutOfMemoryError * Indicates that there is insufficient memory for the new list. **/ public static
return cursor; }
/** * Search for a particular piece of data in a linked list. * @param head * the head reference for a linked list (which may be an empty list in * which case the head is null) * @param target * a target to search for * @return * The return value is a reference to the first node that contains the * specified target. If the target is non-null, then the * target.equals method is used to find such a node. * The target may also be null, in which case the return value is a * reference to the first node that contains a null reference for its * data. If there is no node that contains the target, then the null * reference is returned. **/ public static
/** * Modification method to remove the node after this node. * @param - none * @precondition * This node must not be the tail node of the list. * @postcondition * The node after this node has been removed from the linked list. * If there were further nodes after that one, they are still * present on the list. * @exception NullPointerException * Indicates that this was the tail node of the list, so there is nothing * after it to remove. **/ public void removeNodeAfter( ) { link = link.link; } /** * Modification method to set the data in this node. * @param newData * the new data to place in this node * @postcondition * The data of this node has been set to newData. * This data is allowed to be null. **/ public void setData(E newData) { data = newData; } /** * Modification method to set the link to the next node after this node. * @param newLink * a reference to the node that should appear after this node in the linked * list (or the null reference if there is no node after this node) * @postcondition * The link to the node after this node has been set to newLink. * Any other node (that used to be in this link) is no longer connected to * this node. **/ public void setLink(Node
**************************
// File: LinkedStack.java from the package edu.colorado.collections // Complete documentation is available from the LinkedStack link in: // http://www.cs.colorado.edu/~main/docs/
// package edu.colorado.collections; import java.util.EmptyStackException; // import edu.colorado.nodes.Node;
/****************************************************************************** * A LinkedStack
is a stack of references to * E
objects. * *
- Limitations:
- * Beyond
Int.MAX_VALUE
items,size
is wrong. *
/** * Initialize an empty stack. * @param - none *
/** * Generate a copy of this stack. * @param - none * @return * The return value is a copy of this stack. Subsequent changes to the * copy will not affect the original, nor vice versa. * @exception OutOfMemoryError * Indicates insufficient memory for creating the clone. **/ public LinkedStacktrue
if this stack is empty; * false
otherwise. **/ public boolean isEmpty( ) { return (top == null); }
/** * Get the top item of this stack, without removing the item. * @param - none *
/** * Get the top item, removing it from this stack. * @param - none *
/** * Push a new item onto this stack. The new item may be the null * reference. * @param item
* the item to be pushed onto this stack *
/** * Accessor method to determine the number of items in this stack. * @param - none * @return * the number of items in this stack **/ public int size( ) { // The generic listLength method gets the type of E from top. return Node.listLength(top); }
/** * Return a string representation of this stack. The string * returned by this method will show the stack with top on the left * and bottom on the right, elements separated by '|' * * @return a string representation of this stack. **/ public String toString() { if (top != null) { String answer = ""; Node
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started