Java Infix Evaluation Runtime StackUnderflowException Help!
DiggBlinkRedditDeliciousTechnorati
question by ucsb2012 | Easy
import java.util.*;
import java.io.*;
public class ExpressionEvaluation{
public static void main(String[] args){
Scanner fileIn = null;
PrintWriter fileOut = null;
try{
fileOut = new PrintWriter(new FileOutputStream("results.dat"));
}
catch(FileNotFoundException e){
System.out.println("results.dat not found.");
}
String currentElement = null;
//Load File
try{
fileIn = new Scanner(new FileInputStream("expr.dat"));
}
catch(FileNotFoundException e){
System.out.println("Data file expr.dat not found.");
System.exit(0);
}
while(fileIn.hasNextLine()){
String tempLine = fileIn.nextLine();
fileOut.println("Expression: " + tempLine);
double eval = evaluateExpression(tempLine);
fileOut.println("Value: " + eval);
fileOut.println();
}
fileOut.close();
}
// Rerturns an integer representation of an operator's order of precedence
public static int parsePrecedence(String operator){
switch (operator.charAt(0)) {
case '+':
return 2;
case '-':
return 2;
case '*':
return 3;
case '/':
return 3;
case '(':
return 1;
case '[':
return 1;
case '{':
return 1;
default:
return -1;
}
}
// Input: Whole Line from expr.dat
// Output: Double evaluation
public static double evaluateExpression(String expression){
ArrayListStack OperandStack = new ArrayListStack();
ArrayListStack OperatorStack = new ArrayListStack();
String currentElement = null;
StringTokenizer toker = new StringTokenizer(expression);
//Main Loop
int currentOrderOfPrecedence = -1;
while(toker.hasMoreTokens()){
//Read in Next input
currentElement = toker.nextToken();
char ch = currentElement.charAt(0);
// if number
if(Character.isDigit(ch)){
Double.parseDouble(currentElement);
OperandStack.push(currentElement);
}
// else if operator
else if(currentElement.equals("+") || currentElement.equals("-") || currentElement.equals("*") || currentElement.equals("/")){
currentOrderOfPrecedence = parsePrecedence(currentElement);
// pop and evaluate each operator in operator stack above
// last paren & its orderOfPrec>=current
while(parsePrecedence((String)(OperatorStack.top()))>=currentOrderOfPrecedence){
double temp1 = Double.parseDouble((String)(OperandStack.top()));
OperandStack.pop();
double temp2 = Double.parseDouble((String)(OperandStack.top()));
OperandStack.pop();
try{
OperandStack.push(evaluateSimple(temp1,(String)(OperatorStack.top()), temp2));
}
catch(ParenPairException e){
System.out.println("Invalid Parentheses Detected.");
System.exit(0);
}
OperatorStack.pop();
}
OperatorStack.push(currentElement);
}
// else if open paren
else if(currentElement.equals("{") || currentElement.equals("[") || currentElement.equals("<") || currentElement.equals("(")){
OperatorStack.push(currentElement);
}
// else if closed paren
else if(currentElement.equals("}") || currentElement.equals("]") || currentElement.equals(">") || currentElement.equals(")")){
// pop and evaluate all operators until a matching open paren
// is reached, if a non-matching, open paren is reached, throw exception
String matchingParen;
// Determine matching paren
switch(ch){
case '}':
matchingParen = "{";
break;
case ']':
matchingParen = "[";
break;
case '>':
matchingParen = "<";
break;
case ')':
matchingParen = "(";
break;
default:
matchingParen = "";
break;
}
while(!OperatorStack.top().equals(matchingParen)){
double temp1 = Double.parseDouble((String)(OperandStack.top()));
OperandStack.pop();
double temp2 = Double.parseDouble((String)(OperandStack.top()));
OperandStack.pop();
try{
OperandStack.push(evaluateSimple(temp1,(String)(OperatorStack.top()), temp2));
}
catch(ParenPairException e){
System.out.println("Invalid Parentheses Detected.");
System.exit(0);
}
OperatorStack.pop();
}
//Now disregard the matched open paren
OperatorStack.pop();
}
}
return Double.parseDouble((String)(OperatorStack.top()));
}
// Input: operand, operator, operand
// Output: Evaluation of the simple expression
public static double evaluateSimple(double operand1, String operator, double operand2){
switch(operator.charAt(0)){
case '+':
return (operand1 + operand2);
case '-':
return (operand1 - operand2);
case '*':
return (operand1 * operand2);
case '/':
return (operand1 / operand2);
case '(':
throw new ParenPairException("Parentheses do not match up. Error in Data File.");
}
return -1;
}
}
Post reply
Subscriptions
Got a Java Question?
Just Sign Up and ask the top Java experts!
|