0% found this document useful (0 votes)
49 views2 pages

Exp No 3 Evaluate Postfix Expression Using ADt

Uploaded by

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

Exp No 3 Evaluate Postfix Expression Using ADt

Uploaded by

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

#include<stdio.

h>
#include<ctype.h>
# define MAX 100
# define POSTFIXSIZE 100

int stack[MAX];
int top = -1 ;

void push(int n)
{
if(top >= MAX-1)
{
printf("stack overflow");
return;
}
else
{
top = top + 1 ;
stack[top]=n;
}
}

int pop()
{
int n;
if(top <0)
{
printf("stack underflow");
}
else
{
n = stack[top];
top = top - 1;
return n;
}
}

void EvalPostfix(char postfix[])


{
int i ;
char ch;
int val;
int A, B ;

for (i = 0 ; postfix[i] != ')'; i++)


{
ch = postfix[i];
if (isdigit(ch))
{

push(ch - '0');
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{

A = pop();
B = pop();
switch (ch)
{
case '*':
val = B * A;
break;

case '/':
val = B / A;
break;

case '+':
val = B + A;
break;

case '-':
val = B - A;
break;
}

push(val);
}
}
printf( " \n Result of expression evaluation : %d \n", pop()) ;
}

int main()
{

int i ;
char postfix[POSTFIXSIZE];
printf("ASSUMPTION: There are only four operators(*, /, +, -) in an
expression and operand is single digit only.\n");
printf( " \nEnter postfix expression,\n press right parenthesis ')' for end
expression : ");

for (i = 0 ;i<= POSTFIXSIZE - 1 ; i++)


{
scanf("%c", &postfix[i]);
if ( postfix[i] == ')' )
{ break; }
}

EvalPostfix(postfix);
return 0;
}

You might also like