PostFix评估结果错误

时间:2015-04-20 21:59:59

标签: java

public static int postFixEvaluation(int[] numValues, String postfix){
            Stack<Integer> evaluateStack = new Stack<Integer>();
            char[] chars = postfix.toCharArray();
            int length = chars.length;
            int currentNumValue =0; 
            int currentLocation =0; 
            for(int i = 0; i < length; i++){
                char currentChar = chars[i];
            if(Character.isLetter(currentChar))//checks to see if character is a letter 
            {
                //replace all letters with values 
                currentLocation = charToNum(currentChar);//this retrieves the location of specific letter
                currentNumValue= (numValues[currentLocation]);//retrieves the value of that location
                evaluateStack.push(currentNumValue);//get the number value of that variable and push it on stack
                System.out.println(Arrays.toString(evaluateStack.toArray()));//prints out stack elements 

            }

                else if(isOperator(currentChar)){//checks if character is an operator 
                    switch(currentChar){//switches evaluation according to operator 
                    case '+': evaluateStack.push(evaluateStack.pop() + evaluateStack.pop()); break;
                    case '*': evaluateStack.push(evaluateStack.pop() * evaluateStack.pop()); break;
                    case '-': evaluateStack.push(evaluateStack.pop() - evaluateStack.pop()); break;
                    case '/': evaluateStack.push(evaluateStack.pop() / evaluateStack.pop()); break;
                    }
                }

            }
            if (!evaluateStack.isEmpty()) //as long as the stack is not empty 
                return evaluateStack.pop();//returns the result
            else
                return 0;//if it is empty returns zero 
        }

输入:

C = -13
X = 5
H = 25
D = 4
$PART2
C-(B+(C+(A-E)))-X
(D+C)-(H*X-C)
(A-H)/(D+B)
A*H+C Infix: C-(B+(C+(A-E)))-X
Postfix: CBCAE-++-X-
[-13]
[-13, 2]
[-13, 2, -13]
[-13, 2, -13, 1]
[-13, 2, -13, 1, 5]
[6, 5]
Result: -1**Result should be: -3**

Infix: (D+C)-(H*X-C)
Postfix: DC+HX*C--
[4]
[4, -13]
[-9, 25]
[-9, 25, 5]
[-9, 125, -13]
Result: -129 **Result should be: -147**

Infix: (A-H)/(D+B)
Postfix: AH-DB+/
[1]
[1, 25]
[24, 4]
[24, 4, 2]
Result: 0**Result should be: -4**


Infix: A*H+C
Postfix: AH*C+
[1]
[1, 25]
[25, -13]
Result: 12**Correct Result**

我无法弄清楚为什么我的postEvaluation不会产生正确的输出。如果有人可以请求帮助!我在整个代码中都进行了广泛的评论,所以如果我能澄清一下,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:3)

我手边没有电脑,只有一张旧学校的纸和笔,我的手机以及有关分流码算法的一些知识。

我认为你的错误就是这个(仅在纸上测试):

而不是评估A-E,你正在做一个E-A。其他运营商也是如此。

错:

evaluateStack.push(evaluateStack.pop() + evaluateStack.pop());

正确:

//Don't change pop order here!
int righthand = evaluateStack.pop();
int lefthand = evaluateStack.pop();

evaluateStack.push(lefthand + righthand);

这是我纠正的原因:

public static int postFixEvaluation(int[] numValues, String postfix){
    Stack<Integer> evaluateStack = new Stack<Integer>();
    char[] chars = postfix.toCharArray();
    int length = chars.length;
    int currentNumValue =0;
    int currentLocation =0;

    for (int i = 0; i < length; i++){
        char currentChar = chars[i];

        //checks to see if character is a letter
        if (Character.isLetter(currentChar)){
            //replace all letters with values 
            currentLocation = charToNum(currentChar);//this retrieves the location of specific letter
            currentNumValue = (numValues[currentLocation]);//retrieves the value of that location
            evaluateStack.push(currentNumValue);//get the number value of that variable and push it on stack
            System.out.println(Arrays.toString(evaluateStack.toArray()));//prints out stack elements 
        }


        //checks if character is an operator 
        if (isOperator(currentChar)){
            int righthand = evaluateStack.pop();
            int lefthand = evaluateStack.pop();
            switch (currentChar){
            //switches evaluation according to operator 
                case '+': evaluateStack.push(lefthand + righthand); break;
                case '*': evaluateStack.push(lefthand * righthand); break;
                case '-': evaluateStack.push(lefthand - righthand); break;
                case '/': evaluateStack.push(lefthand / righthand); break;
            }
        }
    }
    if (!evaluateStack.isEmpty()){ //as long as the stack is not empty
        return evaluateStack.pop();//returns the result
    } else {
        return 0; //if it is empty returns zero 
    }
}

一些额外的提示:您应该已经添加了辅助函数以及代码在算法方面对您的问题所做的工作。这可能会吸引更多人回答你的问题。