9 (B) - Stack Application - Postfix Evaluation
9 (B) - Stack Application - Postfix Evaluation
: 9 - B
STACK APPLICATION –
POSTFIX EVALUATION
DATE: 22 - 10 - 2024
AIM:
To write a C program to evaluate postfix expression using stack.
ALGORITHM:
evaluatePostfix()
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdbool.h>
int s[20];
int t = -1;
void push(int x)
{
s[++t] = x;
}
int pop()
{
if(t == -1)
return -1;
else
return s[t--];
}
bool isdigit(char x)
{
if(x >= '0' && x <= '9')
return true;
return false;
}
int main()
{
char p[25] = {'\0'} , e;
int i , n1 , n2 , ans;
printf("\nEnter Postfix Expression : ");
scanf("%s" , p);
printf("\nPostfix Expression : %s" , p);
i = 0;
while(i <= (strlen(p) - 1))
{
e = p[i];
if(isdigit(e))
{
push(atoi(e));
}
else
OUTPUT:
{
n1 = pop();
n2 = pop();
switch(e)
{
case '^':
ans = pow(n2 , n1);
break;
case '/':
ans = n2 / n1;
break;
case '*':
ans = n2 * n1;
break;
case '-':
ans = n2 - n1;
break;
case '+':
ans = n2 + n1;
break;
}
push(ans);
}
i++;
printf("\nAnswer : %d" , pop());
}
printf(“\n\nProgram done by : “);
printf(“\nName : Gokula Krishnan”);
printf(“\nRegister no. : 2303717710421013);
return 0;
}
PREPARATION 15
OBSERVATION 15
IMPLEMENTATION AND OUTPUT 20
VIVA - VOCE 15
RECORD 10
TOTAL 75
RESULT:
Thus, the C program to evaluate postfix expression using stack is successfully
developed and executed.