Assignment 5
Assignment 5
ASSIGNMENT-5
Course: BTECH(CSE)
Section: B1
}
}
char oper(int a)
{
switch(a)
{
case 1:return '+';
case -1:return '-';
case 2:return '*';
case -2:return '/';
}
}
int main()
{
int st[100];
char s[100];
char ans[100];
int i,n,top=-1;
int a=0;
printf("ENTER THE EXPRESSION-");
scanf("%s",s);
n=strlen(s);
for(int i=0;i<n;i++)
{
if(s[i]=='(')
{
push(st,0,&top);
}
else if(s[i]==')')
{
while((st[top]!=0)&&(!isEmpty(top))){
int t=st[top];
pop(st,&top);
ans[a++]=oper(t);
}
pop(st,&top);
}
else if(s[i]>='a' && s[i]<='z')
{
ans[a++]=s[i];
}
else{
while(!isEmpty(top)&& (prec(s[i]))<=(prec(st[top])))
{
int t=st[top];
pop(st,&top);
ans[a++]=oper(t);
}
push(st,prec(s[i]),&top);
}
}
while(!isEmpty(top))
{
int t=st[top];
pop(st,&top);
ans[a++]=oper(t);
}
ans[a]='\0';
printf("%s",ans);
return 0;