I need help with Java Program that Evaluates Algebraic Expressions
Author |
Message |
Christ1m
|
Posted: Sat Apr 18, 2009 11:48 pm Post subject: I need help with Java Program that Evaluates Algebraic Expressions |
|
|
Java: | class number_or_op {
boolean flag;
double number;
char operator;
number_or_op(boolean flag1,double number1,char operator1)
{
flag = flag1;
number = number1;
operator = operator1;
}
}
class expr {
number_or_op first;
expr rest;
expr(number_or_op first1,expr rest1)
{
first = first1;
rest = rest1;
}
} |
The idea is to scan the string from left to right, looking for a simple expression: a number an operator and a number, in that order. If you find such an expression, replace it with its value and continue. We'll do this once for the high-precedence operators (* and /) and again for the low precedence operators (+ and -). So we need two functions that are very similar: each one scans the string and rewrites it. The first function will handle multiplication and division, the high-precedence operators, leaving addition and subtraction operators untouched. The second function will take the output from the first function and do the remaining, low-precedence operations. If the string is well-formed, the second function will reduce it to a string of just one number -- the answer. We will ignore parentheses for now.
In more detail: write the recursive function
Java: | public static expr eval(expr expr1) |
If expr1 is null, return null. If expr1 starts with a number, an operator, and a number, compute the value of this expression. Turn the resulting number into a number-or-op object. Remove the first three objects from the list and replace them with this new object. Recursively evaluate this new expression, and return the resulting expr. If the list is non-empty and it doesn't start with a number, an operator and a number, then recursively evaluate the rest of the list. Add first object in the list to the result of the recursve call, and return that list (this just means that we leave the first object in its place and continue).
Your main function should read a string containing digits, +, -, * and /. Turn it into an object of type expr, evaluate, and print the result. For example:
code: | expression: 5-4+1
value: 2 |
a As an example, suppose the input was 2+3*5. The first recursive function, taking the high-precedence operators, would produce the following sequence of recursive calls:
code: | argument: 2+3*5
return: 2+15
argument: +3*5
return: +15
argument: 3*5
return: 15
argument:15
return: 15
argument: empty string
return: empty string |
The second function takes care of low-precedence operators:
code: | argument:2+15
return:17
argument:17
return:17
argument: empty string
return: empty string |
I need help with the main method... Also im not to sure if the recursive function is correct. IS there some way I can check the recursive function
Java: |
import java.util.*;
public class hmwrk7 {
public static Expr eval (Expr expr1 )
{
if(expr1== null)
return null;
if(expr1. first. flag && expr1. rest == null)
return expr1;
double firstnum = expr1. first. number;
double secondnum;
Expr rest = expr1. rest;
Expr result;
if(rest. first. operator == '+' )
{
rest = eval (rest. rest);
secondnum = rest. first. number;
result = new Expr (new Number_or_op (true,firstnum+secondnum, 'x'), null);
}
else if(rest. first. operator == '*')
{
rest = eval (rest. rest);
secondnum = rest. first. number;
result = new Expr (new Number_or_op (true,firstnum*secondnum, 'x'),rest. rest);
}
else if(rest. first. operator == '/')
{
rest = eval (rest. rest);
secondnum = rest. first. number;
result = new Expr (new Number_or_op (true, firstnum/secondnum, 'x'),rest. rest);
}
else
{
rest = eval (rest. rest);
secondnum = rest. first. number;
result = new Expr (new Number_or_op (true,firstnum-secondnum, 'x'),rest. rest);
}
return result;
}
public static void main (String[] args ) {
Scanner sc = new Scanner (System. in);
System. out. print("experssion: ");
String s=sc. next();
Double. parseDouble(s );
}
}
class number_or_op {
boolean flag;
double number;
char operator;
number_or_op (boolean flag1, double number1,char operator1 )
{
flag = flag1;
number = number1;
operator = operator1;
}
}
class expr {
number_or_op first;
expr rest;
expr (number_or_op first1,expr rest1 )
{
first = first1;
rest = rest1;
}
}
|
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="java"]Code Here[/syntax] |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|