Question: Design and implement a utility class ExpressionSolver to solve any good simple expression using the algorithm discussed in class. Simple expressions can have +,-,*,/, and
Design and implement a utility class "ExpressionSolver" to solve any good simple expression using the algorithm discussed in class. Simple expressions can have +,-,*,/, and parenthesis .
Write a driver program to read expressions from a text file ( one expression on each line) and print out the orginal expressions and their evaluated results on screen. Don't forget to prompt the user for the input filename.
Requirements:
- No advanced data structures( stack, queue, list,...)
- Error checking and user-friendliness are required
Things to turn in:
1) source code printouts- don't forget to comment your code
Not sure how to complete the program. Need to add more stuff on this one
public class ExpressionSolver
{
private static int index = 0;
private static String expression = "";
public static double solve(String exp)
{
index = 0;
expression = exp;
double r = 0;
double o1, o2, o3;
char op1, op2;
o1 = getOperand();
op1 = getOperator();
o2 = getOperand();
op2 = getOperator();
o3 = getOperand();
;
return(r);
}
private static double getOperand()
{
String s="";
double r;
while(expression.charAt(index) != '+' &&
expression.charAt(index) != '-' &&
expression.charAt(index) != '*' &&
expression.charAt(index) != '/' &&
index < expression.length()
)
{
s += expression.charAt(index);
index++;
}
r = Integer.parseInt(s);
System.out.println(r);
return r;
}
private static char getOperator()
{
char o;
o = expression.charAt(index);
index++;
System.out.println(o);
return o;
}
public static void main(
String[] args
)
{
// TODO Auto-generated method stub
System.out.println("1+2+3*4"
);
System.out.println( solve("1+2+3*4")
);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
