Friday, August 28, 2009

Infix to Postfix Expression



#include<stdio.h>



#include<string.h>



#include<ctype.h>



main()



{



char
ans;



do



{



char
infix[50],stack[50],x;



int
top=0,i=0;



clrscr();



printf("\n\n\t\t\tCONVERSION
OF INFIX TO POSTFIX EXPRESSION"
);



printf("\n\t\t\t*****************************************");



printf("\n\nENTER
THE INFIX EXPRESSION :->"
);



fflush(stdin);



gets(infix);



printf("\n\nTHE
REQUIRE POSTFIX FORM IS:->"
);



stack[0]='#';



x=infix[0];



while(x!='\0')



{




if
(isalnum(x))



printf("%c",x);




else
if(x==')')



{




while(stack[top]!='(')




printf("%d",stack[top--]);




top--;



}




else



{




while(isp(stack[top])>=icp(x))




printf("%c",stack[top--]);




stack[++top]=x;



}



x=infix[++i];



}



while(top>0)



printf("%c",stack[top--]);



printf("\n\nDO
U CONTINUE (Y/N):->"
);



scanf("%s",&ans);



}while(ans=='y'||ans=='y');



getch();



}




int

isp(char c)



{



if(c=='*'||c=='/')
return 2;



if(c=='+'||c=='-')
return 1;



if(c=='(') return 0;



if(c=='#') return -4;



}









int

icp(char c)



{



if(c=='*'||c=='/')
return 2;



if(c=='+'||c=='-')
return 1;



if(c=='(') return -4;



}





No comments:

Post a Comment