C Source Codes List | Source Code Home
Program : Program for infix to postfix conversion.
#include
# define MAXVAL 100
# define MAXOP 100
char val[MAXVAL];
char op[MAXOP];
int a=0,b=0,c=0,d=0;
void main()
{
char popval();
char popop();
int i=0,j,k,x,y;
char string[MAXVAL + MAXOP];
char mid[MAXVAL+MAXOP];
printf("\nEnter an infix expression");
scanf("%[^\n]",string);
while (string[i]!='\0')
{
if(string[i]=='*'||string[i]=='/'||string[i]=='-'||string[i]=='+')
pushop(string[i]);
else if((string[i]>=65&&string[i]<=90)||(string[i]>=97&&string[i]<=122))
pushval(string[i]);
else if(string[i]>=48&&string[i]<=57||string[i]=='.')
pushval(string[i]);
x=strlen(val);
y=strlen(op);
if(string[i-1]=='*'||string[i-1]=='/'||string[i+1]=='\0')
{ for(j=1;j<=x;j++)
mid[j]=popval();
for(k=x;k>=1;k--)
printf("%c",mid[k]);
for(j=x+1;j<=(x+y);j++)
mid[j]=popop();
for (k=x+1;k<=j-1;k++)
printf("%c",mid[k]);
} //end of if
i++;
} //end of while
getch();
}//end of main()
int pushval(char s)
{
val[a++]=s;
return(0);
}
int pushop(char s)
{
op[b++]=s;
return(0);
}
char popval()
{
c=val[--a];
val[a]=NULL;
return(c);
}
char popop()
{
d=op[--b];
op[b]=NULL;
return(d);
}
C Source Codes List | Source Code Home
|
|