int evaluate_postfix_from_cin(char* e)
{
struct Stack* s = createStack(strlen(e));
int i;
if (!s) return -1;
for (i = 0; e[i]; ++i)
{
if (isdigit(e[i]))
push(s, e[i] - '0');
else
{
int v1 = pop(s);
int v2 = pop(s);
switch (e[i])
{
case '+':
push(s, v2 + v1);
break;
case '-':
push(s, v2 - v1);
break;
case '*':
push(s, v2 * v1);
break;
case '/':
push(s, v2/v1);
break;
}
}
}
return pop(s);
}
Comments
Leave a comment