Sunday 7 April 2013

Infix to Postfix Conversion program in c with output

CONVERSION OF INFIX TO POSTFIX EXPRESSION

#include<stdio.h>
#include<conio.h>
#include<string.h>
int prece(char j);
void main()
{
char infix[10],stack[5],output[10],j;
int k=0,l=0,i,a,b;
stack[k]=0;
printf("write the infix expression to be converted to postfix");
scanf("%s",infix);
for(i=0;i<10;i++)
{
a=prece(infix[i]);
b=prece(stack[k]);
if(a<=b)
{
output[l]=infix[i];
l++;
}
else
{
stack[k]=infix[i];
k++;
}
}
for(i=k;i>0;i--)
{
output[l]=stack[i];
l++;
}
printf("%s",output);
getch();
}
int prece(char j)
{
if(j=='*'||j=='/')
{
j=3;
}
else
{
if(j=='+'||j=='-')
{
j=2;
}
else
{
j=1;
}
}
return j;
}
 
Output:

write the infix expression to be converted to postfix(2*4)+5
 
(24)5*+

No comments:

Post a Comment