Вы находитесь на странице: 1из 4

INFIX TO POSTFIX

// INFIX TO POSTFIX

#include<stdio.h>
#include<conio.h>
#include<string.h>

push(char s[],int *top,char ch)


{
*top=*top+1;
s[*top]=ch;

char pop(char s[],int *top)


{
char k;
k=s[*top];
*top=*top-1;
return k;

case '*':

return 2;

break;

case '/':

return 2;

break;

case '+':

return 1;

break;

case '-':

return 1;

break;

default : return 0;
}
}

int main()
{
char inf[100],pos[100],s[100];char x,y;
int i=0,j=0,top=-1;
printf("\nEnter the Infix Notation: ");
gets(inf);
for(i=0;inf[i]!='\0';i++)
{

if (isalnum(inf[i]))

pos[j]=inf[i];

j++;

else if(inf[i]=='(')

push(s,&top,inf[i]);

else if(inf[i]==')')

y=pop(s,&top);

while(y!='(')

pos[j]=y;

j++;

y=pop(s,&top);

else

if( (top==-1 ) || (s[top]== '(' ) || ( (priority(s[top])) < priority(inf[i])))

push(s,&top,inf[i]);

else

while((priority(s[top]))>=(priority(inf[i])))

pos[j]=pop(s,&top);

j++;

push(s,&top,inf[i]);

}
}
while(top>-1)
{

pos[j]=pop(s,&top);

j++;
}
pos[j]='\0';
printf("\nThe Postfx Notation is: ");
puts(pos);

getch();
}

Вам также может понравиться