Description: This program performs the conversion of infix expression into postfix expression.
/*program performs the infix to postfix conversion*/
#include
#include
#define max 30
/*prototype declaration of a fuction*/
void pre(char x);
struct stack
{
char arr1[max],arr2[max];
}s;
int top1=0;
int top2=-1;
void push(char x)
{
if((x=='+')||(x=='-')||(x=='*')||(x=='/')||(x=='^')||(x=='(')||(x==')'))
pre(x);
else
{
top2++;
s.arr2[top2]=x;
}
}
void pre(char x)
{
if((s.arr1[top1]=='(')||(x=='('))
{
s.arr1[++top1]=x;
}
else if(x==')')
{
while(s.arr1[top1]!='(')
{
s.arr2[++top2]=s.arr1[top1--];
}
top1--;
}
else if((x=='+')||(x=='-'))
{
while(s.arr1[top1]!='(')
s.arr2[++top2]=s.arr1[top1--];
s.arr1[++top1]=x;
}
else if((x=='*')||(x=='/'))
{
while((s.arr1[top1]!='+')&&(s.arr1[top1]!='-')&&(s.arr1[top1]!='('))
s.arr2[++top2]=s.arr1[top1--];
s.arr1[++top1]=x;
}
else if(x=='^')
{
if(s.arr1[top1]=='^')
s.arr2[++top2]=s.arr1[top1--];
s.arr1[++top1]=x;
}
}
void main()
{
char exp[20];
int i=0;
clrscr();
printf("enter the exp. to be convert into postfix\n");
while((exp[i++]=getchar())!='\n');
exp[--i]=')';
exp[++i]='\0';
s.arr1[top1]='(';
for(i=0;exp[i]!='\0';i++)
{
push(exp[i]);
}
printf("\n%s",s.arr2);
getch();
}
I think this site has some real great information for everyone :D.