문제

I'm currently doing an infixToPostfix converter to be used in a calculator assignment. My problem is with that the output seems to show parenthesis when it shouldn't have. I'm stuck with this. Can someone help my out? Thanks.

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);
    }

}
도움이 되었습니까?

해결책

Thre are two minor bugs in the code. First, you are skipping the last character in the expression - which, as it turns out, is a right parenthesis:

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

Second, your code used 'x' as the multiplication operator, while your expression uses '*'.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top