Stanky-kun
Stanky-kun

Reputation: 85

How can I properly display the contents of this stack?

I've been trying to take a postfix string that has already been converted from infix and evaluate it to display its correct value. Here is my method to parse the postfix string and put digits in a stack (postStack of int value):

for (int i = 0; i < post.length(); i++)
            {
                int result;
                if (isdigit(post[i]))
                {
                    int j = post[i] - '0';
                    postStack.push(j);
                    continue;
                }
                else
                {
                    token = post[i];   //token = char type;
                    int num2 = postStack.top(); postStack.pop();
                    int num1 = postStack.top(); postStack.pop();
                    result = evaluate(num1, token, num2);   
                    postStack.push(result);
                    continue;
                }
            }

And here is the evalutaion method that receives both digits and the operator:

int evaluate(int num1, char token, int num2)
{
    int result = 0;
    switch (token)
    {
    case '+': result = num1 + num2;
    case '-': result = num1 - num2;
    case '*': result = num1 * num2;
    case '^': result = (int)(pow(num1, num2) + 0.5);
    }

    return result;
}

The output I have been receiving from this when printing has been large negative numbers or zeros. What am I missing here? Output:

1:  2 + 3 * 5
    235*+
    -2147483648

2:  2 + 3 * 5 ^ 6
    2356^*+
    0

3:  2 + 3 - 5 + 6 - 4 + 2 - 1
    23+5-6+4-2+1-
    -2147483647

4:  2 + 3 * (5 - 6) - 4
    2356-*+4-
    0

5:  2 * 3 ^ 5 * 6 - 4
    235^*6*4-
    -2147483648

6:  (2 + 3) * 6 ^ 2
    23+62^*
    -2147483648

And here is what I'm using to print the stack:

while (!postStack.empty())
                {
                    int itemp = postStack.top(); postStack.pop();
                    cout << itemp;
                }
                cout << endl << endl;

Upvotes: 0

Views: 56

Answers (2)

yabhishek
yabhishek

Reputation: 419

You were missing break statement in each of case blocks.

#include <bits/stdc++.h>
using namespace std;
int evaluate(int num1, char token, int num2){
    int result = 0;
    switch (token){
        case '+': result = num1 + num2; break;
        case '-': result = num1 - num2; break;
        case '*': result = num1 * num2; break;
        case '^': result = (int)(pow(num1, num2) + 0.5); break;
    }
    return result;
}
int main(){
    string post = "2356^*+";
    stack<int> postStack;
    for (int i = 0; i < post.length(); i++){
        int result;
        if (isdigit(post[i])){
            int j = post[i] - '0';
            postStack.push(j);
        }
        else{
            char token = post[i];   //token = char type;
            int num2 = postStack.top(); postStack.pop();
            int num1 = postStack.top(); postStack.pop();
            result = evaluate(num1, token, num2);   
            postStack.push(result);
        }
    }
    cout << postStack.top() <<endl;
}

Upvotes: 1

Lukas-T
Lukas-T

Reputation: 11340

Your switch is missing a break after each case. It falls trough and executes every case statement beyond the one that first matched. Meaning in the end you are always calculting the power of num1 to num2. This gives you ridiculous high numbers that don't fit inside an int and you get an overflow, thus the int turns negative.

For example the input "235*+" actually calculates 2 ^ (3 ^ 5) = 2 ^ 243. But an int can only hold values up to 2 ^ 31 - 1.

Upvotes: 3

Related Questions