jim
jim

Reputation: 37

PostFix Evaluation Result Error

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 
        }

Input:

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**

I can't figure out why my postEvaluation won't produce the correct output. If anyone could please help! I have commented extensively throughout my code so please let me know if I can clarify anything. Thanks!

Upvotes: 2

Views: 80

Answers (1)

Rolf ツ
Rolf ツ

Reputation: 8781

I dont have a computer at hand, only an old school paper and pen, my phone and some knowledge about the shunting-yard algorithm.

I think your error is this (only tested on paper):

Instead of evaluating A-E you are doing an E-A. The same goes for the other operators.

Wrong:

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

Correct:

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

evaluateStack.push(lefthand + righthand);

This is what I corrected it to:

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

Some additional tips: You should have added your helper functions and what the code does in terms of algorithms to your question. This will probably attract more people to answer your question.

Upvotes: 3

Related Questions