Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Expression evaluation using stacks In this homework assignment, you will be implementing an expression evaluator that evaluates arithmetic expressions consisting of operations + , -
Expression evaluation using stacks
In this homework assignment, you will be implementing an expression evaluator that evaluates
arithmetic expressions consisting of operations and number operands.
For example should be evaluated as instead of since has a
higher precedence than
We will first convert the given infix expression to the postfix expression which can then be
evaluated easily using a stack. You need to use operator precedence rules to arrive at the unique
postfix expression that can be evaluated in one way.
For example should be converted to since has a higher precedence than
In general, has highest precedence followed by which have equal precedence and then
followed by which have equal precedence In case of operators with equal precedence, we
use lefttoright order of evaluation. For example should be converted to
You can force order of evaluation using parentheses which may be nested. For example,
should be converted to
You are provided a starter file for ExpressionEvaluator.java. You also need to download the
following files : ExpressionToken.java, OperatorToken.java, OperandToken.java. You will
be completing the following functions in ExpressionEvaluator.java.
Do not change classes OperatorToken.java, OperandToken.java or any of the function
signatures or the class name in ExpressionEvaluator.java. Note that the function should throw
an exception for an invalid expression as indicated in the Javadoc for the function.
Convert Infix expression given as a list of operator and operand tokens to
a postfix expression as a list of operator and operand tokens
@param infixExpr
@return
@throws Exception when the expression is not valid
such as insufficient number of operators or operands eg
or not having balanced parentheses eg
public static List convertToPostFixList
infixExpr throws Exception
Evaluate postfix expression given as a list of operator and operand tokens
and return the result
@param postfixExpr
@return
@throws Exception when the expression is not valid
such as insufficient number of operators or operands eg
public static double postFixEvalList postfixExpr throws
Exception
In implementing the convertToPostfix function, check operator precedence by using the function precedes of OperatorToken class. For example
if currentTokenprecedestopOfStackToken
Also Complete evalDirect function that first parses the infix expression
into tokens as before but then directly evaluates the infix expression instead of converting it into post fix. You will get credit only if all the unit tests pass
Code to edit:
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.Iterator;
public class ExpressionEvaluator
Use this function for applying operator in postfixEval
private static double applyOpOperatorToken opType, double op double op
switchopType
case ADD:
return op op;
case SUBTRACT:
return op op;
case MULTIPLY:
return op op;
case DIVIDE :
return op op;
case MOD :
return op op;
case EXP:
return Math.powopop;
default:
return ;
Parse an expression into a list of operator and operand tokens
@param str
@return
public static List parseExprString str
List expr new ArrayList;
String strTokList strsplits;
for String strTok : strTokList
String str strTok.trim;
if strisEmpty
continue;
OperatorToken operToken OperatorToken.opTypestr;
expr.addoperToken null new OperandTokenstr : operToken;
return expr;
Convert Infix expression given as a list of operator and operand tokens to
a postfix expression as a list of operator and operand tokens
@param infixExpr
@return
@throws Exception when the expression is not valid
such as insufficient number of operators or operands eg
or not having balanced parentheses eg
public static List convertToPostFixList infixExpr throws Exception
Complete the code here only for homework.
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