I need to make a java program that evaluates an expression from an input file and returns the result in an output file. It needs to consider operator precedence, unary and binary operators, bracket matching, and has to rely on recursion only (no stacks or queues).
I've been thinking about this all night, and it frustrates me. I'm not asking for an entire java program written for me. I just need some guidance. I started by writing some pseudo-code, but I don't think it's any good:
Input: the text file to read each expression from. Output: the text file that repeats each expression, as well as printing the result.
Algorithm SecondCalc()
{
input = “expressions.txt”;
output = “out.txt”;
if (input.currentLine has something)
{
line = input.currentLine;
output.write(line);
line = line.replace(“-space-”, “”);
evaluate(line);
//...to be continued
}
}
Algorithm evaluate(line)
{
for(i = 0 to line.length)
{
if(i == “(” or “)” ) exit loop;
if(i == “!”) exit loop;
if(i == “^”) exit loop;
if(i == “*” or “/” ) exit loop;
if(i == “+” or “-” ) exit loop;
if(i == “>” or “>=” or “<” or “<=” ) exit loop;
if(i == “==” or “!=” ) exit loop;
if(i == “$”) exit loop;
}
temp1 = line from index 0 to i;
temp2 = line from index i + 1 to line.length;
if(i == “!”) then evaulate(temp1!);
//...to be continued
}
Any tips would be appreciated. Thanks.