RSS

Convert Infix Expression to Postfix form

08 Mar
The following C program converts infix expression to postfix expression. Note that the input infix expression must have opening and closing parenthesis enclosing each operation.
For eg. for converting a+b/c*d to postfix form give the input as ((a+b)/(c*d))

#include<stdio.h>
#include<string.h>
void main()
{
char s[100],str[100],oparr[100];
int l,i,j=0,top=0;
printf(“Enter a string in INFIX\n”);
scanf(“%s”,&s);
l=strlen(s);
for(i=0;i<l;i++)
{if(s[i]==’+’||s[i]==’-‘||s[i]==’*’||s[i]==’/’||s[i]==’%’)
{oparr[j++]=s[i];}
if(s[i]!='(‘&& s[i]!=’+’ && s[i]!=’-‘ && s[i]!=’*’ && s[i]!=’/’ && s[i]!=’%’)
{if(s[i]==’)’)
printf(“%c”,oparr[–j]);
else
printf(“%c”,s[i]);}}
}
Advertisements
 
Leave a comment

Posted by on March 8, 2011 in C/C++

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: