The program is supposed to throw exceptions on every illegal expression, extra operands, extra operator, dividing by zero, or the result of the division is not an integer value.
I was able to catch 4 out of 5 exceptions (extra operators, dividing by zero, not an integer value, and illegal expression) the one that is giving me trouble is extra operands for example, if the input is 2 3 + 5 it should throw an exception: not enough operator(meaning extra operand).
I have commented on whatever I have tried.
//import java.util.Stack;
import java.util.*;
/**
* Represents an integer evaluator of postfix expressions. Assumes
* the operands are constants.
*
* @author Lewis and Chase
* @version 4.0
*/
public class PostfixEvaluator
{
private final static char ADD = '+';
private final static char SUBTRACT = '-';
private final static char MULTIPLY = '*';
private final static char DIVIDE = '/';
private final static char EXPONENT = '^';
private LinkedStack<Integer> stack;
/**
* Sets up this evalutor by creating a new stack.
*/
public PostfixEvaluator()
{
stack = new LinkedStack<Integer>();
}
/**
* Evaluates the specified postfix expression. If an operand is
* encountered, it is pushed onto the stack. If an operator is
* encountered, two operands are popped, the operation is
* evaluated, and the result is pushed onto the stack.
* @param expr string representation of a postfix expression
* @return value of the given expression
*/
public int evaluate(String expr)
{
int op1, op2=0, result = 0;
int count = 0;
//int x=0,y=0;
String token;
Scanner parser = new Scanner(expr);
try
{
while (parser.hasNext())
{
token = parser.next();
if (isOperator(token))
{
// x++;
// if(y<x +1)
// {throw new PostFixEvalException("not enough operators");}
op2 = (stack.pop()).intValue();
op1 = (stack.pop()).intValue();
result = evaluateSingleOperator(token.charAt(0), op1, op2);
stack.push(new Integer(result));
// y++;
// if (x>y+1)
// {throw new PostFixEvalException("not enough operands");}
// String operators = "-+*/^";
// for (int i = 0; i < token.length(); i++) {
// if (Character.isDigit(Integer.valueOf(token))) {
// x = count++;
// }
// return x;
// }
// for (int i = 0; i <token.length(); i++)
// {char c = token.charAt(i);
// if(operators.indexOf(c)<0)
// {
// y = count++;
// }
// return y;
// }
// if(x > y )
// throw new PostFixEvalException("not enough operands");
// else if (y > x )
// throw new PostFixEvalException("not enough operator");
}
else
stack.push(new Integer(Integer.parseInt(token)));
}
}
catch(EmptyCollectionException stack)
{throw new PostFixEvalException("not enough operands"); }
catch(NumberFormatException ExtraOperands)
{ throw new PostFixEvalException("INVALID OPERAND OR OPERATOR"); }
if (stack.isFull() && op2 == (stack.pop()).intValue())
{throw new PostFixEvalException("not enough operator");}
// { throw new PostFixEvalException("not enough operators"); }
return result;
}
// public char ADD() throws PostFixEvalException
// {
// if(isOperator(token)!= ADD)
// throw new PostFixEvalException("EXTRA OPERANDS");
// }
/**
* Determines if the specified token is an operator.
* @param token the token to be evaluated
* @return true if token is operator
*/
private boolean isOperator(String token)
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/") ||
token.equals("^"));
}
/**
* Peforms integer evaluation on a single expression consisting of
* the specified operator and operands.
* @param operation operation to be performed
* @param op1 the first operand
* @param op2 the second operand
* @return value of the expression
*/
private int evaluateSingleOperator(char operation, int op1, int op2)
{
int result = 0;
switch (operation)
{
case ADD:
result = op1 + op2;
break;
case SUBTRACT:
result = op1 - op2;
break;
case MULTIPLY:
result = op1 * op2;
break;
case DIVIDE:
if(op2 == 0)
throw new PostFixEvalException("Dividing by zero");
else if ((op1%op2) == 0)
{result = op1 / op2;}
else
throw new PostFixEvalException("Not Iteger Division");
break;
case EXPONENT:
result = (int)Math.pow(op1,op2);
}
return result;
}
}
question from:
https://stackoverflow.com/questions/65661303/i-am-trying-to-catch-exceptions-in-postfixevalute 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…