Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的解析器函数不工作_Java_Parsing - Fatal编程技术网

Java 我的解析器函数不工作

Java 我的解析器函数不工作,java,parsing,Java,Parsing,我在设计一个简单的parserit,它是一个简单版本的调车场算法。这是我的代码,我没有处理关联性 public class Parser { String stack[] = new String[50]; String res = ""; int top = 0; Operator o1 = new Operator("", 0, 0); public String parse(String x) { push("(");

我在设计一个简单的parserit,它是一个简单版本的调车场算法。这是我的代码,我没有处理关联性

public class Parser {
    String stack[] = new String[50];
    String res = "";
    int top = 0;
    Operator o1 = new Operator("", 0, 0);

    public String parse(String x) {
        push("(");
        x = x + ")";
        for (int i = 0; i < x.length(); i++) {
            if (o1.isNumber(x.charAt(i) + "")) {
                res = res + x.charAt(i);
            } else if (x.charAt(i) == '(') {
                push("(");
            } else if (o1.isOperator("" + x.charAt(i))) {
                if (top != -1) {
                    while ((top != -1) && (o1.isOperator(stack[top]))) {
                        int m = o1.getOperatorIndex(stack[top]);
                        int mp = o1.op[m].prec;
                        int xp = o1.op[o1.getOperatorIndex("" + x.charAt(i))].prec;
                        if (m >= xp) {
                            res = res + stack[top];
                        }
                        top--;
                    }
                }
                push("" + x.charAt(i));
            } else {
                if (top != -1) {
                    while ((top != -1) && (stack[top] != ")")) {
                        if (o1.isOperator(stack[top])) {
                            res = res + stack[top];
                        }
                        top--;
                    }
                }
            }

        }
        return res;
    }

    public void push(String m) {
        if (top != 49) {
            stack[top] = m;
            top++;
        } else {
            System.out.println("Overflow");
        }
    }
}

我想您不需要运算符类的代码。当我尝试执行parse1+2时,它只返回12,而不是+号。怎么了?是的,o[0]是+,o[1]是-,o[2]是*,o[3]是/

使用一些Java调试器来找到它发生的原因,然后尝试自己修复代码;是的,我试过调试器。这帮不了什么忙。