#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
int i, top = -1;
int op1, op2, res, s[20];
char postfix[90], symb;
void push(int item)
{
top = top + 1;
s[top] = item;
}
int pop()
{
int item;
item = s[top];
top = top - 1;
return item;
}
void main()
{
printf("\nEnter a valid postfix expression:\n"); scanf("%s", postfix);
for (i = 0; postfix[i] != '\0'; i++)
{
symb = postfix[i];
if (isdigit(symb))
{
push(symb - '0');
}
else
{
op2 = pop();
op1 = pop();
switch (symb)
{
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
case '$':
case '^':
break;
default:
push(0);
}
}
}
res = pop();
printf("\n Result = %d", res
); }
I2luY2x1ZGU8c3RkaW8uaD4KCiNpbmNsdWRlPHN0ZGxpYi5oPgojaW5jbHVkZTxjdHlwZS5oPgojaW5jbHVkZTxtYXRoLmg+CgoKaW50IGksIHRvcCA9IC0xOwppbnQgb3AxLCBvcDIsIHJlcywgc1syMF07CmNoYXIgcG9zdGZpeFs5MF0sIHN5bWI7Cgp2b2lkIHB1c2goaW50IGl0ZW0pCnsKdG9wID0gdG9wICsgMTsKc1t0b3BdID0gaXRlbTsKfQoKaW50IHBvcCgpCnsKaW50IGl0ZW07Cml0ZW0gPSBzW3RvcF07CnRvcCA9IHRvcCAtIDE7CnJldHVybiBpdGVtOwp9Cgp2b2lkIG1haW4oKQp7CnByaW50ZigiXG5FbnRlciBhIHZhbGlkIHBvc3RmaXggZXhwcmVzc2lvbjpcbiIpOwpzY2FuZigiJXMiLCBwb3N0Zml4KTsKZm9yIChpID0gMDsgcG9zdGZpeFtpXSAhPSAnXDAnOyBpKyspCnsKc3ltYiA9IHBvc3RmaXhbaV07CmlmIChpc2RpZ2l0KHN5bWIpKQp7CnB1c2goc3ltYiAtICcwJyk7Cn0KZWxzZQp7Cm9wMiA9IHBvcCgpOwpvcDEgPSBwb3AoKTsKc3dpdGNoIChzeW1iKQp7CiBjYXNlICcrJzoKcHVzaChvcDEgKyBvcDIpOwpicmVhazsKY2FzZSAnLSc6CnB1c2gob3AxIC0gb3AyKTsKYnJlYWs7CmNhc2UgJyonOgpwdXNoKG9wMSAqIG9wMik7CmJyZWFrOwpjYXNlICcvJzoKcHVzaChvcDEgLyBvcDIpOwpicmVhazsKY2FzZSAnJSc6CnB1c2gob3AxICUgb3AyKTsKYnJlYWs7CmNhc2UgJyQnOgpjYXNlICdeJzoKcHVzaChwb3cob3AxLCBvcDIpKTsKYnJlYWs7CmRlZmF1bHQ6CnB1c2goMCk7Cn0KfQp9CnJlcyA9IHBvcCgpOwpwcmludGYoIlxuIFJlc3VsdCA9ICVkIiwgcmVzKTsKfQoKCg==
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
int i, top = -1;
int op1, op2, res, s[20];
char postfix[90], symb;
void push(int item)
{
top = top + 1;
s[top] = item;
}
int pop()
{
int item;
item = s[top];
top = top - 1;
return item;
}
void main()
{
printf("\nEnter a valid postfix expression:\n");
scanf("%s", postfix);
for (i = 0; postfix[i] != '\0'; i++)
{
symb = postfix[i];
if (isdigit(symb))
{
push(symb - '0');
}
else
{
op2 = pop();
op1 = pop();
switch (symb)
{
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
case '$':
case '^':
push(pow(op1, op2));
break;
default:
push(0);
}
}
}
res = pop();
printf("\n Result = %d", res);
}