You are on page 1of 4

INFIX EXPRESSION

# include<ctype.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAX 20
int st[MAX],top=-1;
void push(int x)
{
if (top>=MAX)
{
printf("\n overflow");
exit(0);
}
else
{
top++;
st[top]=x;
printf("\n pushed element:%d",st[top]);
}
}
int pop()
{
int t;
if (top==-1)
{
printf("\nunderflow" );
exit(0);
}
else
{
t=st[top];
top--;
}
return (t);
}

void main()
{
char str[10];
int i,x,y,z,a,l,j;
clrscr();
printf("\nenter the expression ..");
scanf("%s",str);
l=strlen (str);
str[l]='$';
i=0;
while(str[i]!='$')
{
if (isdigit(str[i]))
{
a=toascii(str[i])-48;
push(a);
}
else
{
y=pop();
x=pop();
switch(str[i])
{
case '+':z=x+y;
break;
case '-':
z=x-y;
break;
case '*':z=x*y;
break;
case '/':z=x/y;
break;
case '^':z=l;
for (j=l;j<=y;j++)
{
z=z*x;
}
break;
default:printf("invalid");
}
push(z);
}
i++;
}
printf("\n result is %d",st[top]);
getch();
}
OUTPUT
enter the expression ..37+9*
pushed element:3
pushed element:7
pushed element:10
pushed element:9
pushed element:90
result is 90

You might also like