KEMBAR78
Program To Evaluate Postfix Expression | PDF
0% found this document useful (0 votes)
80 views3 pages

Program To Evaluate Postfix Expression

This program evaluates a postfix expression. It uses a stack to process the postfix notation. For each element in the expression, if it is a digit, it is pushed onto the stack. If it is an operator, the last two elements are popped from the stack, the operation is performed on them and the result is pushed back onto the stack. Once the full expression is processed, the final result is displayed.

Uploaded by

bolt spark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views3 pages

Program To Evaluate Postfix Expression

This program evaluates a postfix expression. It uses a stack to process the postfix notation. For each element in the expression, if it is a digit, it is pushed onto the stack. If it is an operator, the last two elements are popped from the stack, the operation is performed on them and the result is pushed back onto the stack. Once the full expression is processed, the final result is displayed.

Uploaded by

bolt spark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//program to evaluate postfix expression

#include<stdio.h>
#include<ctype.h>
int stk[20],top=-1;
void push(int a);
int pop();
void main()
{
char exp[20];
int i,v,n1,n2;
printf("\n enter postfix exp");
gets(exp);
for(i=0;exp[i]!='\0';i++)
{
if(isdigit(exp[i]))
{
push(exp[i]-'0');
}
else if(isalpha(exp[i]))
{
printf("\n alphabets not allowed");
}
else
{
n2=pop();
n1=pop();
switch(exp[i])
{
case '+':push(n1+n2);
break;
case '-':push(n1-n2);
break;
case '/':push(n1/n2);
break;
case '*':push(n1*n2);
break;
default: printf("\n enter valid operator(+, -, *, /)");

}
}
}
printf("result value is: %d",stk[top]);
}
void push(int a)
{
stk[++top]=a;
}
int pop()
{
if(top==-1)
return -1;
else
return stk[top--];
}

You might also like