C program to convert infix to postfix expression and evaluation.
Program:
#include<stdio.h>
#include<string.h>
int pr(char op)
{
switch(op)
{
case '*': return 2;
case '/': return 2;
case '+': return 1;
case '-': return 1;
case '^': return 3;
}
}
int push(char st[],int top,char c)
{
top=top+1;
st[top]=c;
return top;
}
char pop(char st[],int *top)
{
char k;
k=st[*top];
*top=*top-1;
return k;
}
float exp(float f,float c)
{
float ans=f;
int k=(int)c,i=1;
while(i<k)
{
ans=ans*f;
i++;
}
return ans;
}
int push2(float ist[],int top2,float item)
{
top2+=1;
ist[top2]=item;
return top2;
}
float pop2(float ist[],int *top2)
{
float i;
i=ist[*top2];
*top2=*top2-1;
return i;
}
void main()
{
char st[100],in[100],po[100],y='#';
float ist[100],tem,o,t;
int i=0,j=0,top=-1,top2=-1;
printf("\nEnter infix expression: ");
gets(in);
while(in[i]!='\0')
{
if((in[i]!='+')&&(in[i]!='-')&&(in[i]!='*')&&(in[i]!='/')&&(in[i]!='^'))
{
if((in[i]=='(')||(in[i]==')'))
{
if(in[i]=='(')
{
top=push(st,top,in[i]);
}
else
{
y=pop(st,&top);
while(y!='(')
{
po[j]=y;
j++;
y=pop(st,&top);
}
}
}
else
{
po[j]=in[i];
j++;
}
}
else
{
if(top==-1)
{
top=push(st,top,in[i]);
}
else
{
if((st[top]=='(')||pr(in[i])>pr(st[top]))
{
top=push(st,top,in[i]);
}
else
{
while(((pr(in[i])<=pr(st[top]))&&((top!=-1)&&(st[top]!='('))))
{
y=pop(st,&top);
po[j]=y;
j++;
}
top=push(st,top,in[i]);
}
}
}
i++;
}
while(top>-1)
{
y=pop(st,&top);
po[j]=y;
j++;
}
po[j]='\0';
printf("\nCorresponding postfix expression is: %s",po);
i=0;
while(po[i]!='\0')
{
if(po[i]=='+'||po[i]=='^'||po[i]=='/'||po[i]=='*'||po[i]=='-')
{
o=pop2(ist,&top2);
t=pop2(ist,&top2);
switch(po[i])
{
case'*': top2=push2(ist,top2,t*o);
break;
case '^': top2=push2(ist,top2,exp(t,o));
break;
case '/': top2=push2(ist,top2,t/o);
break;
case '+': top2=push2(ist,top2,t+o);
break;
case '-': top2=push2(ist,top2,t-o);
break;
}
}
else
{
printf("\nEnter value for %c: ",po[i]);
scanf("%f",&tem);
top2=push2(ist,top2,tem);
}
i++;
}
printf("\nResult is %f: ",ist[top2]);
}
Output:
Enter infix expression: (a+d)-c*d
Corresponding postfix expression is: ad+cd*-
Enter value for a: 10 Enter value for d: 6
Enter value for c: 2
Enter value for d: 4
Result is 8.000000
#include<stdio.h>
#include<string.h>
int pr(char op)
{
switch(op)
{
case '*': return 2;
case '/': return 2;
case '+': return 1;
case '-': return 1;
case '^': return 3;
}
}
int push(char st[],int top,char c)
{
top=top+1;
st[top]=c;
return top;
}
char pop(char st[],int *top)
{
char k;
k=st[*top];
*top=*top-1;
return k;
}
float exp(float f,float c)
{
float ans=f;
int k=(int)c,i=1;
while(i<k)
{
ans=ans*f;
i++;
}
return ans;
}
int push2(float ist[],int top2,float item)
{
top2+=1;
ist[top2]=item;
return top2;
}
float pop2(float ist[],int *top2)
{
float i;
i=ist[*top2];
*top2=*top2-1;
return i;
}
void main()
{
char st[100],in[100],po[100],y='#';
float ist[100],tem,o,t;
int i=0,j=0,top=-1,top2=-1;
printf("\nEnter infix expression: ");
gets(in);
while(in[i]!='\0')
{
if((in[i]!='+')&&(in[i]!='-')&&(in[i]!='*')&&(in[i]!='/')&&(in[i]!='^'))
{
if((in[i]=='(')||(in[i]==')'))
{
if(in[i]=='(')
{
top=push(st,top,in[i]);
}
else
{
y=pop(st,&top);
while(y!='(')
{
po[j]=y;
j++;
y=pop(st,&top);
}
}
}
else
{
po[j]=in[i];
j++;
}
}
else
{
if(top==-1)
{
top=push(st,top,in[i]);
}
else
{
if((st[top]=='(')||pr(in[i])>pr(st[top]))
{
top=push(st,top,in[i]);
}
else
{
while(((pr(in[i])<=pr(st[top]))&&((top!=-1)&&(st[top]!='('))))
{
y=pop(st,&top);
po[j]=y;
j++;
}
top=push(st,top,in[i]);
}
}
}
i++;
}
while(top>-1)
{
y=pop(st,&top);
po[j]=y;
j++;
}
po[j]='\0';
printf("\nCorresponding postfix expression is: %s",po);
i=0;
while(po[i]!='\0')
{
if(po[i]=='+'||po[i]=='^'||po[i]=='/'||po[i]=='*'||po[i]=='-')
{
o=pop2(ist,&top2);
t=pop2(ist,&top2);
switch(po[i])
{
case'*': top2=push2(ist,top2,t*o);
break;
case '^': top2=push2(ist,top2,exp(t,o));
break;
case '/': top2=push2(ist,top2,t/o);
break;
case '+': top2=push2(ist,top2,t+o);
break;
case '-': top2=push2(ist,top2,t-o);
break;
}
}
else
{
printf("\nEnter value for %c: ",po[i]);
scanf("%f",&tem);
top2=push2(ist,top2,tem);
}
i++;
}
printf("\nResult is %f: ",ist[top2]);
}
Output:
Enter infix expression: (a+d)-c*d
Corresponding postfix expression is: ad+cd*-
Enter value for a: 10 Enter value for d: 6
Enter value for c: 2
Enter value for d: 4
Result is 8.000000
No comments