Java 后缀计算器中的 StackOverFlowError
-
19-09-2019 - |
题
下面的类被另一个程序使用。当它被访问时,它会抛出 StackOverFlowError。这是我在大学必须做的一个项目的 Postfix 计算器的一部分。
任何帮助将不胜感激,提前谢谢您。我对 Java 很陌生,我不知道该怎么做。
代码:
import java.util.Queue;
import java.util.Stack;
public class MyPostfixMachine implements PostfixMachineInterface {
MyMathOperations mmo = new MyMathOperations();
MyPostfixMachine mpm = new MyPostfixMachine();
public String evaluate(Queue q) {
if (q.isEmpty()) {//if the input is empty, terminate the program
System.exit(0);
}
if (q.size() == 1) {//if there is only one number in the queue, return it as the solution
if (mpm.isParsableToDouble(String.valueOf(q.remove()))) {
return String.valueOf(q.remove());
}
}
Stack<String> finalxp = new Stack<String>();//create an empty stack
if (mpm.isParsableToDouble(String.valueOf(q.remove()))) {//if first element of queue q is a number,push it into the stack
finalxp.push(String.valueOf(q.remove()));
} else {//depending on the operator perform the corresponding operations
if (q.remove() == "+") {
String str = String.valueOf(finalxp.pop());
String str2 = String.valueOf(finalxp.pop());
finalxp.push(mmo.addition(str, str2));
}
if (q.remove() == "-") {
String str = String.valueOf(finalxp.pop());
String str2 = String.valueOf(finalxp.pop());
finalxp.push(mmo.substraction(str, str2));
}
if (q.remove() == "*") {
String str = String.valueOf(finalxp.pop());
String str2 = String.valueOf(finalxp.pop());
finalxp.push(mmo.product(str, str2));
}
if (q.remove() == "/") {
String str = String.valueOf(finalxp.pop());
String str2 = String.valueOf(finalxp.pop());
finalxp.push(mmo.division(str, str2));
}
if (q.remove() == "fibo") {
String str = String.valueOf(finalxp.pop());
finalxp.push(mmo.fibonacci(str));
}
if (q.remove() == "fac") {
String str = String.valueOf(finalxp.pop());
finalxp.push(mmo.factorial(str));
}
if (q.remove() == "han") {
String str = String.valueOf(finalxp.pop());
finalxp.push(mmo.hanoi(str));
}
}
return String.valueOf(finalxp.pop());
}
public boolean isParsableToDouble(String candidate) {
try {
Double.parseDouble(candidate);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
}
public class MyMathOperations implements MathOperationsInterface {
public String addition(String s1, String s2) {
double A = Double.parseDouble(s1);
double B = Double.parseDouble(s2);
return String.valueOf((A + B));
}
public String substraction(String s1, String s2) {
double A = Double.parseDouble(s1);
double B = Double.parseDouble(s2);
return String.valueOf((A - B));
}
public String product(String s1, String s2) {
double A = Double.parseDouble(s1);
double B = Double.parseDouble(s2);
return String.valueOf((A * B));
}
public String division(String s1, String s2) {
double A = Double.parseDouble(s1);
double B = Double.parseDouble(s2);
return String.valueOf((A / B));
}
public String fibonacci(String s) {
int n = Integer.parseInt(s);
return String.valueOf(fibo(n));
}
public int fibo(int f) {
if (f < 0) {
throw new IllegalArgumentException("Cannot apply Fibonacci method");
} else if (f == 0) {
return 0;
} else if (f == 1) {
return 1;
} else {
return fibo(f - 1) + fibo(f - 2);
}
}
public String hanoi(String s) {
int a = Integer.parseInt(s);
int han = 0;
if (a < 0) {
throw new IllegalArgumentException("Not a valid integer");
} else {
han = (int) Math.pow(2, a) - 1;
}
return String.valueOf(han);
}
public String factorial(String s) {
int a = Integer.parseInt(s);
if (a < 0) {
throw new IllegalArgumentException("Incorrect argument for factorial operatiion");
}
switch (a) {
case 0:
case 1:
return String.valueOf(1);
default:
int res = a;
while (true) {
if (a == 1) {
break;
}
res *= --a;
}
return String.valueOf(res);
}
}
private static double pDouble(String s) {
double res = 0d;
try {
res = Double.parseDouble(s);
} catch (NumberFormatException e) {
System.exit(1);
}
return res;
}
}
解决方案
的问题是,您的类MyPostfixMachine 有一个私人领域的 MyPostfixMachine MPM 这是一个新MyPostfixMachine初始化。由于这项新MyPostfixMachine也具有与新MyPostfixMachine初始化私有字段MyPostfixMachine MPM ......你得到它。 :)这推移和永远(或直到堆栈已满)。
下面是有问题的代码:
public class MyPostfixMachine implements PostfixMachineInterface {
MyMathOperations mmo = new MyMathOperations();
MyPostfixMachine mpm = new MyPostfixMachine(); // problem is here
// ...
}
我觉得你可以简单地删除私人领域之外。只需调用当前实例的方法。因此,而不是:
if (mpm.isParsableToDouble(String.valueOf(q.remove()))) {...}
可以简单的写:
if (isParsableToDouble(String.valueOf(q.remove()))) {...}
或(equivallent但更明确的):
if (this.isParsableToDouble(String.valueOf(q.remove()))) {...}
总之,只要除去私有字段MPM和StackOverflowException应该消失。
其他提示
我不确定你是如何得到 StackOverflowError (我在这段代码中没有看到任何循环或递归),但一个明确的问题是你过度使用 Queue.remove()
. 。每次你看到你的队列 if
子句,你要删除第一个元素——我希望这段代码会被删除 NoSuchElementException
s。
更不用说所有的 EmptyStackException
你应该从空的弹出中得到 Stack
.
所以我想说....
- 当您应该调用 `peek()` 时,请退出调用 `remove()`。
- 停止从空堆栈中弹出;您想从输入队列中提取这些值,是吗?
- 给您带来“StackOverFlowError”的问题在其他地方。(除非我忽略了某些事情——总是有可能的!)寻找循环或递归调用。
不隶属于 StackOverflow