Click here to Skip to main content
15,566,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code is implemented using ADT by including the "stack.h" user defined header file.

The header file has code for stack operation. I am getting wrong outputs for results greater than 9.

What changes should I make in the code to get the correct output.

I have to use a stack with char data and not int data.



#include<iostream>
#include<string.h>
#include "stack.h"
using namespace std;

void posteva(char postfix[])
{
    stack s;
    int i=0;
    while(postfix[i]!='\0')
    {
        char x=postfix[i];
        if(isdigit(x))
        {
            s.push(x);
        }
        else
        {
            int op1=s.pop()-'0';
            int op2=s.pop()-'0';
            int res;
            switch(x)
            {
                case '+':
                    res=op1+op2;
                    break;
                case '-':
                    res=op1-op2;
                    break;
                case '*':
                    res=op1*op2;
                    break;
                case '/':
                    res=op1/op2;
                    break;
                case '%':
                    res=op1%op2;
                    break;

            }
            s.push(res+'0');
        }
        i++;
    }
    cout<<"\n\nRESULT :"<<s.pop();
}

int main()
{
    char postfix[20];
    cout<<"\n\nEnter the postfix : ";
    cin>>postfix;
    posteva(postfix);
}


What I have tried:

For example, for postfix expression "63*" I am getting result as B.
Posted
Updated 18-Apr-18 5:06am

C++
s.push(res+'0');

That will only work for a single digit result. In your case, 6 * 3 = 18. The character '0' is equivalent to 0x30, or decimal 48. 48 + 18 = 66 or hexadecimal 0x42, which is the numeric value of the ASCII character 'B'.
 
Share this answer
 
Quote:
for postfix expression "63*" I am getting result as B.

Guilty is:
C++
s.push(res+'0');

because the code is correct only for single digit values.

In order to handle numbers with more than 1 digit, you need to have separator between numbers, a space is usually used for this:
C++
2 63 3*+

My advice is to push values on stack after converting to integers, but you have to rewrite your code to handle entry of number > 9.
 
Share this answer
 
You are pushing single digits / characters on the stack. When a number is greater than 9 you will push multiple digits that must be all popped and converted to a number.

It all depends on how the input is formatted. Assuming something like "num1 num2 op", I suggest to write functions to push and pop numbers:
C++
int pushdigit(const char* input, int pos, stack *s)
{
    while (istdigit(input[pos])
    {
        s->push(input[pos++]);
    }
    // Push a zero as end of number indicator
    s->push(0);
    // Return position to next input character skipping space
    return (' ' == input[pos]) ? pos + 1 : pos;
}

int popdigit(stack *s)
{
    int num = 0;
    int mult = 1;
    // Assuming the stack provides a function to check if it is empty
    while (!s->isempty())
    {
        char c = s->pop();
        if (!isdigit(c))
            break;
        num += mult * (c - '0');
        mult *= 10;
    }
    return num;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900