Your task is to write a program that will: 1) read in a series of infix expressions from a file (one per line), 2) evaluate each expression and display it and its value in a TextArea. For this lab infix expressions contain only ints, the 4 binary arithmetic operators (/*-+), spaces, and parentheses. E.g.:
static int evaluate(String inputLine) method. Thus, the code in your Frame might simply be:
while (mr.hasMoreData()) {
String nextLine = mr.giveMeTheNextLine();
emit(nextLine + "=" + PostFixEvaluator.evaluate(nextLine) + "\n");
}
StringTokenizer st = new StringTokenizer(s, "+-*/ ", true); will work
well) -- or, if you prefer learn to use a Scanner to do it.
Test your code to be sure it is generating the correct stream of tokens.Here is the algorithm presented in class:
while (more input) {
nextToken = get next token
if (nextToken is an operand)
push
else { // must be an operator
x = pop();
y = pop();
push(y nextToken x); // need to implement the operation as a method
} // else
}
output(pop()); // the answer will be on top of the stack
String InfixToPostFix.convert(String) method following
the algorithm presented in
lecture,
then...