1
votes

Why do we pass exp[i]- '0' in the push if exp[i] is a digit but don't do the same in case of operator i.e, val2 operator val1 -'0'. I guess it's related to ascii say we want to insert 2 then ascii of char 2 - ascii of char 0= 2 in decimal and we push it in the stack(which is int array but the argument is char op) but we don't do the same if its an operator. Also if we are receiving the second argument in push as "char" op then why do we need to convert it into ascii and it also doesn't affect the output if we change the char to int op in the second argument of push? [a link] http://geeksquiz.com/stack-set-4-evaluation-postfix-expression/

void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}

int evaluatePostfix(char* exp)
{
struct Stack* stack = createStack(strlen(exp));
int i;
if (!stack) return -1;
for (i = 0; exp[i]; ++i)
{
    // If the scanned character is an operand or number,
    // push it to the stack.
    if (isdigit(exp[i]))
        push(stack, exp[i] - '0');

    //  If the scanned character is an operator, pop two
    // elements from stack apply the operator
    else
    {
        int val1 = pop(stack);
        int val2 = pop(stack);
        switch (exp[i])
        {
         case '+': push(stack, val2 + val1); break;
         case '-': push(stack, val2 - val1); break;
         case '*': push(stack, val2 * val1); break;
         case '/': push(stack, val2/val1);   break;
        }
    }
}
return pop(stack);
}
int main()
{
char exp[] = "231*+9-";
printf ("Value of %s is %d", exp, evaluatePostfix(exp));
return 0;
}
2

2 Answers

0
votes

Why do we pass exp[i]- '0' in the push if exp[i] is a digit but don't do the same in case of operator

This is because the value of digitChar-'0' expression is meaningful: it represents a numeric value of your digit, because numeric codes of digit characters are sequential. In contrast, subtracting anything from an operator does not give you any advantages, because numeric codes of operators are not sequential.

Also if we are receiving the second argument in push as char op then why do we need to convert it into ascii

We don't - op remains in the same encoding that your system uses.

and it also doesn't affect the output if we change the char to int op in the second argument of push?

It works because we use char op for its numeric value. When you make an assignment to array inside struct Stack, the compiler converts int to the element type of the array, so there is no difference.

0
votes

The char variable holds the characters based on their corresponding ASCII value.

To convert the ASCII representation '1' to integer value 1, you need to subtract the ASCII value of 0, ['0'] from the char value. Then you can perform the arithmetic operations, +, -, * or / on those int values.

For example, '5' has the ASCII value of 53. Subtracting ASCII value of '0' [48] leaves you with (53-48) = 5, as integer.

That is why, for the digits [note the use of isdigit(exp[i])], '0' is subtracted. However, the operators does not need this, because they are not being used as operands.

Related reading: ASCII table.