我正在创建一个RPN计算器,其中添加了某些执行某项任务的运算符。程序运行并进行简单的加法,减法,乘法和除法,但是当我输入运算符“p”或“n”时,我得到一个堆栈空异常。当我在操作员面前明确输入两个整数时,我无法弄清楚为什么我会得到这个。此外,当我使用任何其他运算符,如“r”或“p”时,我得到0的结果,这是不应该发生的。
import java.util.*;
public class RPNCalculator {
public static int op1;
public static int op2;
public static Stack<Integer> stack;
public RPNCalculator()
{
stack = new Stack<Integer>();
}
public static int performCalculation(String expression)
{
int op1, op2, result = 0;
String token;
Scanner parser = new Scanner(expression);
while (parser.hasNext())
{
token = parser.next();
if (isOperator(token))
{
op2 = (stack.pop()).intValue();
op1 = (stack.pop()).intValue();
result = evaluateOperator(token.charAt(0), op1, op2);
stack.push(new Integer(result));
}
else
stack.push(new Integer(Integer.parseInt(token)));
}
return result;
}
private static boolean isOperator(String token)
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/") ||
token.equals("%") || token.equals("m") ||
token.equals("r") || token.equals("d") ||
token.equals("p") || token.equals("n") ||
token.equals("f") || token.equals("c") ||
token.equals("q") || token.equals("h") ||
token.equals("?") );
}
public static int evaluateOperator(char operator, int op1, int op2)
{
int result = 0;
int top = 0;
switch (operator)
{
case '+':
result = op1 + op2;
break;
case '-':
result = op1 - op2;
break;
case '*':
result = op1 * op2;
break;
case '/':
result = op1 / op2;
break;
case '%':
result = op2 / op1;
break;
case 'm':
result = op2 * -1;
break;
case 'r': op1 = op2;
op2 = op1;
stack.push(op1);
stack.push(op2);
break;
case 'd': stack.push(op2);
break;
case 'p': top = stack.peek();
System.out.println(top);
break;
case 'n': top = stack.peek();
result = top;
stack.pop();
break;
case 'f': System.out.print(stack);
break;
case 'c': stack.clear();
break;
case 'q': System.exit(1);
break;
case 'h': help();
break;
case '?': help();
break;
}
return result;
}
public static void help()
{
System.out.println("p print top");
System.out.println("n print top and remove");
System.out.println("d duplicate top");
System.out.println("r exchange top two items");
System.out.println("f print contents of stack");
System.out.println("c clear stack");
System.out.println("+ add");
System.out.println("- subtract");
System.out.println("* multiply");
System.out.println("/ integer divide");
System.out.println("% integer remainder");
System.out.println("m unary minus");
System.out.println("q quit");
System.out.println("h,? this help");
}
}
以下代码是我用来运行它的驱动程序。
import java.util.*;
public class Calculator {
public static void main(String[] args){
String expression, again;
int result;
Scanner in = new Scanner(System.in);{
do
{
RPNCalculator evaluator = new RPNCalculator();
System.out.println("Please enter at least two integer you want to calculate followed by the operator (e.g. 5 4 + 3 2 1 - + *)");
System.out.println("Each token must be an integer or an operator (+,-,*,/,p,n,d,r,f,c,%,m,q,h)");
expression = in.nextLine();
result = RPNCalculator.performCalculation(expression);
System.out.println();
System.out.println("That expression equals " + result);
System.out.print("Evaluate another expression [Y/N]? ");
again = in.nextLine();
System.out.println();
}
while (again.equalsIgnoreCase("y"));
}
}
}