Calculation of Postfix in C ++ leads to correct answers only in some cases

I'm currently studying data structures, and I'm trying to code a C ++ program that uses an array as a modeled stack. The code should work as follows:

The user enters a Postfix expression. The expression is read from left to right. Anytime a number is read, it gets a “pop up” on the simulated stack. When the operand is read, the upper two numbers in the simulated stack are “popped”, the operand performs a calculation with these numbers, and the received answer is “returned” back to the simulated stack. The process continues until only one number remains in the simulated stack. This number is a response to the Postfix expression, and it is read on the screen to the user.

I have code (below) that works, for the most part. However, if one of the values ​​in my simulated stack is greater than or equal to 128, this number seems to be stored incorrectly (for example, +128 becomes -128).

What am I doing wrong? Does this have anything to do with how I return an answer in a function calc? In particular, this line of code:return answer;

Any help or tips would be greatly appreciated.

    #include <string>
    #include <iostream>
    using namespace std;

    // Define the size of the stack:
    #define SIZE 50

    // Global variable declarations:
    char stack[SIZE];
    int top = -1; // The top of the array

    // Function for PUSHING operands onto the stack:
    void push(char elem)
    {
        top++;
        stack[top] = elem;
    //    cout << elem << " TESTING PUSH OUTPUT\n"; // TESTING
    }

    // Function for POPPING operands from the stack:
    char pop()
    {
        char answer;
        answer = stack[top];
        top--;
    //    cout << top << " TESTING WHAT POPPED\n"; // TESTING
        return (answer);
    }

    // Function for CALCULATING two popped numbers:
    double calc(char op)
    {
        int num1, num2, answer;

        // Pop two numbers from the stack:
        num1 = pop(); 

        num2 = pop(); // put the second popped number into 'num2'

        // Calculate the two popped numbers:
        switch (op)
        {
        case '*':
            answer = num2 * num1;
            break;
        case '/':
            answer = num2 / num1;
            break;
        case '+':
            answer = num2 + num1;
            break;
        case '-':
            answer = num2 - num1;
            break;
        default:
            cout << "Invalid expression!";
        }

        // Push the result of the calculation back onto the stack:
        push(answer);

        return answer;

    }

    // The main program, which takes in the expression:
    int main ()
    {
        string postfix;

        int solution;

        cout << "Please enter a Postfix expression, with operators and operands separated by spaces and/or commas (e.g: 3 7 5 + 2 - * 16 4 + 10 / /): \n";
        getline(cin,postfix);

        // If the User Postfix expression begins with an operator, then throw up an error message:
        if (postfix.find('/') == 0 || postfix.find('*') == 0 || postfix.find('+') == 0 || postfix.find('-') == 0)
        {
            cout << "Sorry, that an invalid Postfix expression, and it can't be evaluated.";

            return 0;

        }

        // FOR loop to read the Postfix expression:
        for(unsigned int i = 0; i < postfix.length(); i++)
        {

            // If the value is a space or a comma, move on to the next value:
            if(postfix[i] == ' ' || postfix[i] == ',') 
            {
                continue;
            } // End IF

            // If the value is a number, extract it, and continue reading and extracting the next value until it is NOT a number:
            else if(isalnum(postfix[i]))
            {

                int operand = 0;

                while(i < postfix.length() && isalnum(postfix[i]))
                {
                    // For multi-digit numbers, multiply the initial extracted number by 10 to move it into the 'tens' column, then add the next number to the initial number.
                    // Continue doing this until the next value is NOT a number
                    operand = (operand * 10) + (postfix[i] - '0');
                    i++;
                } // End WHILE

                // When WHILE exits, 'i' will be non-numeric, of the 'end of the string'. Decrement 'i', because it will be incremented in the FOR loop as longs as the FOR condition is true
                // (Stops 'i' from being incremented twice)
                i--;

                // Push the operand onto the stack:
                push(operand);
            } // End ELSE IF

            // If the value is an operator, run the calculation function:
            else
            {
                calc(postfix[i]);
            } // End ELSE

        } // End FOR

        solution = pop(); // put the solution of the equation into 'answer'

        cout << "The solution to the Postfix expression is: " << solution;

    }
+4
source share
3 answers

operandhas a type int, but pushexpects a char.

Since on most machines the sizeof(int) > sizeof(char)value operandcan be truncated if it is stored in a smaller data type, for example char.

For example, suppose sizeof(int) == 4and sizeof(char) == 1, and a byte is eight bits. Now, if only bit 8 th is set (starting from 0), a charwill not be able to hold this information, and the value 0x100is truncated to 0x00.

, int operand char.

+2

, , stack int char ( ).

+1

char, -128 127. unsigned char, , , 0 255. , ints - .

+1

Source: https://habr.com/ru/post/1614879/


All Articles