java - 带括号的中缀到后缀转换器

标签 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[] 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);
    }

}

最佳答案

代码中有两个小错误。首先,您将跳过表达式中的最后一个字符 - 事实证明,它是一个右括号:

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

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

关于java - 带括号的中缀到后缀转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18710252/

相关文章:

java - Parcelable 协议(protocol)需要一个名为 CREATOR 的 Parcelable.Creator 对象(我确实有 CREATOR)

c++ - 如何重新排序算术运算符的 C++ 标准优先级,以便减法具有更高的优先级

java - 如何使用 HashMap 为计算器创建内存?

c++ - 试图理解调车场算法

python - 在 python 中评估后缀?

java - Android 金融 API

java - 为注释字段设置默认空值时出错

Java 相当于 Python 的 struct.pack?

c - 不使用字符串的可变长度算术计算器?

parsing - 我无法创建支持中缀、后缀和前缀函数等的语言有什么原因吗?