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

Evalpostfix

Data structure Experiment

Uploaded by

batharva24comp
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)
14 views2 pages

Evalpostfix

Data structure Experiment

Uploaded by

batharva24comp
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/ 2

#include <stdio.

h>
#include <stdlib.h>

#define max 50
int stack[max];
int top = -1;

void push(int x)
{
if (top == max - 1)
{
printf("Stack Overflow");
return;
}
top++;
stack[top] = x;
}

int pop()
{
if (top == -1)
{
printf("\nStack Underflow");
return -1;
}
int ele = stack[top];
top--;
return ele;
}

int is_digit(char ch)


{
if (ch >= '0' && ch <= '9')
{
return 1;
}
else
return 0;
}
int main()
{
char exp[20];
char *e;
int n1, n2, n3, num;
printf("Enter the expression: ");
scanf("%s", exp);
e = exp;
while (*e != '\0')
{
if (is_digit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch (*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression is %d", pop());
return 0;
}

Output:-

Enter the expression: 34+2*

The result of expression is 14

You might also like