Java后缀计算器中的StackOverflower错误

Java后缀计算器中的StackOverflower错误,java,queue,calculator,stack-overflow,postfix-notation,Java,Queue,Calculator,Stack Overflow,Postfix Notation,下面的类由另一个程序使用。当它被访问时,它抛出一个StackOverflower错误。这是后缀计算器的一部分,我必须在大学里做一个项目 任何帮助都将不胜感激,提前谢谢你。我是Java新手,不知道该怎么办 代码: import java.util.Queue; import java.util.Stack; public class MyPostfixMachine implements PostfixMachineInterface { MyMathOperations mmo =

下面的类由另一个程序使用。当它被访问时,它抛出一个StackOverflower错误。这是后缀计算器的一部分,我必须在大学里做一个项目

任何帮助都将不胜感激,提前谢谢你。我是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;
}
import java.util.Queue;
导入java.util.Stack;
公共类MyPostfixMachine实现PostfixMachineInterface{
MyMathOperations mmo=新的MyMathOperations();
MyPostfixMachine mpm=新的MyPostfixMachine();
公共字符串求值(队列q){
如果(q.isEmpty()){//如果输入为空,则终止程序
系统出口(0);
}
如果(q.size()==1){//如果队列中只有一个数字,则将其作为解决方案返回
if(mpm.isparsabletouble(String.valueOf(q.remove())){
返回字符串.valueOf(q.remove());
}
}
Stack finalxp=new Stack();//创建一个空堆栈
如果(mpm.isparsableTouble(String.valueOf(q.remove())){//如果队列q的第一个元素是一个数字,则将其推入堆栈
finalxp.push(String.valueOf(q.remove());
}else{//根据操作员执行相应的操作
如果(q.remove()=“+”){
String str=String.valueOf(finalxp.pop());
String str2=String.valueOf(finalxp.pop());
最终推送(mmo.添加(str,str2));
}
如果(q.remove()=“-”){
String str=String.valueOf(finalxp.pop());
String str2=String.valueOf(finalxp.pop());
最终推送(mmo.减法(str,str2));
}
如果(q.remove()=“*”){
String str=String.valueOf(finalxp.pop());
String str2=String.valueOf(finalxp.pop());
最终推送(mmo.product(str,str2));
}
如果(q.remove()==“/”){
String str=String.valueOf(finalxp.pop());
String str2=String.valueOf(finalxp.pop());
最终推送(mmo部门(str、str2));
}
if(q.remove()=“fibo”){
String str=String.valueOf(finalxp.pop());
最终推送(mmo.fibonacci(str));
}
如果(q.remove()=“fac”){
String str=String.valueOf(finalxp.pop());
最终推(mmo.factorial(str));
}
if(q.remove()=“han”){
String str=String.valueOf(finalxp.pop());
最终推送(mmo.hanoi(str));
}
}
返回字符串.valueOf(finalxp.pop());
}
公共布尔值isParsableTouble(字符串候选){
试一试{
Double.parseDouble(候选人);
返回true;
}捕获(NumberFormatException nfe){
返回false;
}
}
}
公共类MyMathOperations实现MathOperationsInterface{
公共字符串添加(字符串s1、字符串s2){
double A=double.parseDouble(s1);
double B=double.parseDouble(s2);
返回字符串.valueOf((A+B));
}
公共字符串减法(字符串s1、字符串s2){
double A=double.parseDouble(s1);
double B=double.parseDouble(s2);
返回字符串.valueOf((A-B));
}
公共字符串产品(字符串s1、字符串s2){
double A=double.parseDouble(s1);
double B=double.parseDouble(s2);
返回字符串.valueOf((A*B));
}
公共字符串分割(字符串s1、字符串s2){
double A=double.parseDouble(s1);
double B=double.parseDouble(s2);
返回字符串.valueOf((A/B));
}
公共字符串斐波那契(字符串s){
int n=整数.parseInt(s);
返回字符串.valueOf(fibo(n));
}
公共int fibo(int f){
if(f<0){
抛出新的IllegalArgumentException(“无法应用斐波那契方法”);
}else如果(f==0){
返回0;
}else如果(f==1){
返回1;
}否则{
返回fibo(f-1)+fibo(f-2);
}
}
河内公共字符串(字符串s){
int a=整数.parseInt(s);
int-han=0;
if(a<0){
抛出新的IllegalArgumentException(“不是有效整数”);
}否则{
han=(int)Math.pow(2,a)-1;
}
返回字符串.valueOf(han);
}
公共字符串阶乘(字符串s){
int a=整数.parseInt(s);
if(a<0){
抛出新的IllegalArgumentException(“阶乘运算的参数不正确”);
}
开关(a){
案例0:
案例1:
返回字符串.valueOf(1);
违约:
int res=a;
while(true){
如果(a==1){
打破
}
res*=-a;
}
返回字符串.valueOf(res);
}
}
专用静态双P双工(字符串s){
双分辨率=0d;
试一试{
res=Double.parseDouble(s);
}捕获(数字格式){
系统出口(1);
}
返回res;
}

}

我不确定您是如何得到StackOverflower错误的(我在这段代码中没有看到任何循环或递归),但一个明确的问题是您过度使用了
队列。remove()
。每次查看
if
子句中的队列时,您都在删除第一个元素——我希望这段代码会吐出
NoTouchElementException
s

更不用说所有的
EmptyStackException
s了,你应该从一个空的
堆栈中弹出

所以我想说

  • 当您应该调用'peek()'时,请停止调用'remove()'。
  • 停止从空堆栈中弹出;您想从输入队列中提取这些值,是吗?
  • 给你'StackOverflowerError'的问题在别处。(除非我忽略了什么——总是可能的!)寻找循环或递归调用。
    我不知道你是怎么得到StackOverflower错误的(我不知道)
    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()))) {...}
    
    if (this.isParsableToDouble(String.valueOf(q.remove()))) {...}