Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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_Calculator_Postfix Notation_Infix Notation - Fatal编程技术网

Java 带括号的中缀到后缀转换器

Java 带括号的中缀到后缀转换器,java,calculator,postfix-notation,infix-notation,Java,Calculator,Postfix Notation,Infix Notation,我目前正在做一个infixToPostfix转换器,用于计算器作业。我的问题是,输出似乎在不应该显示的时候显示了括号。我受够了。有人能帮我吗?谢谢 import java.util.ArrayList; import java.util.Stack; class infixToPostfix{ Stack<String> stack; ArrayList<String> operators; String postFix; int[

我目前正在做一个infixToPostfix转换器,用于计算器作业。我的问题是,输出似乎在不应该显示的时候显示了括号。我受够了。有人能帮我吗?谢谢

import java.util.ArrayList;
import java.util.Stack;

class infixToPostfix{

    Stack<String> stack;
    ArrayList<String> operators;

    String postFix;

    int[] operand = {-1, -1, 1};
    int[] plusorminus = {1,2,-1};
    int[] timesordivide = {3,4,-1};
    int[] raiseto = {6,5,-1};
    int[] openparenthesis = {-1,0,-1};

    public infixToPostfix(String infix) {

        stack = new Stack<String>();
        operators = new ArrayList<String>();

        operators.add("+");
        operators.add("-");
        operators.add("x");
        operators.add("/");
        operators.add("^");
        operators.add("(");
        operators.add(")");

        postFix = new String();

        while(infix.length() > 1){

            String operand = new String();
            String operator = new String();

            if(!operators.contains(infix.substring(0, 1))){
                while(!operators.contains(infix.substring(0, 1)) && !infix.isEmpty()){
                    operand = infix.substring(0,1);
                    infix = infix.substring(1);
                }
                postFix = postFix + operand;
            }
            else if(operators.get(5).equals(infix.substring(0, 1))){
                stack.push(infix.substring(0, 1));
                infix = infix.substring(1);
            }
            else if(operators.get(6).equals(infix.substring(0, 1))){
                while(!stack.peek().equals("(")){
                    postFix = postFix + stack.pop();
                }
                stack.pop();
                infix = infix.substring(1);
            }
            else{
                operator = infix.substring(0,1);

                int[] current = getICPandISP(operator);

                if(!stack.isEmpty()){
                    int[] top = getICPandISP(stack.peek());
                    while(current[0] < top[1] && !stack.isEmpty()){
                        postFix = postFix + stack.pop();
                        if(!stack.isEmpty())
                            top = getICPandISP(stack.peek());
                    }
                }
                stack.push(operator);
                infix = infix.substring(1);
            }
        }
        postFix = postFix + infix;

        while(!stack.isEmpty()){
            postFix = postFix + stack.pop();
        }
    }

    public String toString(){
        return postFix;
    }

    private int[] getICPandISP(String operator){
        if(operator.equals("+") || operator.equals("-")){
            return plusorminus;
        }
        else if(operator.equals("x") || operator.equals("/")){
            return timesordivide;
        }
        else if(operator.equals("^")){
            return raiseto;
        }
        else{
            return openparenthesis;
        }
    }

    public static void main(String[] args){
        infixToPostfix convert = new infixToPostfix("A+B/C-(A/D)*(A+(C-E^F))");
        System.out.println(convert);
    }

}
import java.util.ArrayList;
导入java.util.Stack;
类infixToPostfix{
堆叠;
ArrayList算子;
字符串后缀;
int[]操作数={-1,-1,1};
int[]plusor减号={1,2,-1};
int[]timesordivide={3,4,-1};
int[]raiseto={6,5,-1};
int[]开括号={-1,0,-1};
公共infixToPostfix(字符串中缀){
堆栈=新堆栈();
运算符=新的ArrayList();
运算符。添加(“+”);
运算符。添加(“-”);
运营商。添加(“x”);
运算符。添加(“/”);
运算符。加入(“^”);
运算符。添加(“”);
运算符。添加(“)”;
后缀=新字符串();
while(infix.length()>1){
字符串操作数=新字符串();
字符串运算符=新字符串();
如果(!operators.contains(中缀子字符串(0,1))){
而(!operators.contains(infix.substring(0,1))&&&!infix.isEmpty(){
操作数=中缀子串(0,1);
中缀=中缀子串(1);
}
后缀=后缀+操作数;
}
else if(运算符.get(5).equals(中缀子字符串(0,1))){
stack.push(中缀子串(0,1));
中缀=中缀子串(1);
}
else if(运算符.get(6).equals(中缀子字符串(0,1))){
而(!stack.peek().equals(“”){
postFix=postFix+stack.pop();
}
stack.pop();
中缀=中缀子串(1);
}
否则{
运算符=中缀子串(0,1);
int[]当前=getICPandISP(操作员);
如果(!stack.isEmpty()){
int[]top=getICPandISP(stack.peek());
while(当前[0]
代码中有两个小错误。首先,您跳过了表达式中的最后一个字符,结果是右括号:

while(infix.length() > 1){ //should be infix.length() > 0
    // ....
}

第二,您的代码使用了
'x'
作为乘法运算符,而您的表达式使用了
'*'

这就像本周关于将中缀转换为后缀的第四个问题。一定有某个类刚刚分配了它,在其中还没有人真正知道如何编程。