If it wasn\'t recursion I wouldn\'t be asking. I am completely STUMPED on coding
ID: 3623792 • Letter: I
Question
If it wasn't recursion I wouldn't be asking. I am completely STUMPED on coding a recursive expression evaluator.
here are some examples of expressions my code needs to evaluate:
3
Xyz
3-4*5
a-(b+A[B[2]])*d+3
A[2*(a+b)]
(varx + vary*varz[(vara+varb[(a+b)*33])])/55
I already created two previous methods. The first tells whether or not my code is legally matched or not, the second separated the expression (which is a string) into scalar or arrays lists using the string tokenizer.
... here's the extremely helpful tips to help you implement evaluate via my assignment's website:
Rules and guidelines for Implementing evaluate
An expression may contain sub-expressions within parentheses - you MUST use RECURSION to evaluate sub-expressions.
Recursion MUST also be used to evaluate array subscripts as well, since a subscript is an expression.
A stack may be used to store the values of operands as well as the results from evaluating subexpressions - see next point.
Since * and / have precedence over + and -, it would help to store operators in another stack. (Think of how you would evaluate a+b*c.)
When you implement the evaluate method, you may want to test as you go, implementing code for and testing simple expressions, then building up to more complex expressions.
The following is an example sequence of the kinds of expressions you may want to build with:
3
a
3+4
a+b
3+4*5
a+b*c
Then introduce parentheses
Then try nested parentheses
Then introduce array subscripts, but no parentheses
Then try nested subscripts, but no parentheses
Then try using parentheses as well as array subscripts
Then try mixing arrays within parentheses, parentheses within array subscripts, etc.
I would really appreciate any tips, pointers or algorithms. code would be great.
Explanation / Answer
public class EE
{
public static void main (String[] args)
{
String str;
Scanner input = new Scanner(System.in);
system.out.println("Enter String");
str =input. readLine();
Expression e = new Expression(str);
}
}
public class Expression()
{
protected stringTokenizer strtoken;
protected string token;
public Expression(String str)
{
strtoken = new StringTokenizer(str);
token = strtoken.nextToken();
}
public int ExpressionEvalute()
{
return ExpressionEvl();
}
protected double value()
{
double output;
return output = Double.valueOf(token).doubleValue();
}
protected int valueexpression()
{
double nextValue;
double output;
if( token.equals("("))
{
token = strtoken .nextToken();
output = ExpressionEvl();
}
while(token.equals("*"))
{
token = strtoken.nextToken();
nextValue = value();
output *= nextValue;
}
while(token.equals("/"))
{
token = strtoken.nextToken();
nextValue = value();
output /= nextValue;
}
return output;
}
protected int ExpressionsEvl()
{
double nextValue;
double output;
while(token.equals("+"))
{
token = strtoken.nextToken();
nextValue = value();
output += nextValue;
}
while(token.equals("-"))
{
token = strtoken.nextToken();
nextValue = value();
output -= nextValue;
}
return output;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.