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