0% found this document useful (0 votes)
17 views

Infix To Postfix

This document contains code to convert an infix notation mathematical expression to postfix notation. It defines functions to push and pop characters onto a stack, compares the priority of operators, and uses the stack to evaluate the expression from left to right, outputting the postfix form. The main function gets the infix input, iterates through each character, pushes operands and opening parentheses to the stack and pops to output higher priority operators, and converts the expression to postfix notation which is printed.

Uploaded by

Hog Rider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Infix To Postfix

This document contains code to convert an infix notation mathematical expression to postfix notation. It defines functions to push and pop characters onto a stack, compares the priority of operators, and uses the stack to evaluate the expression from left to right, outputting the postfix form. The main function gets the infix input, iterates through each character, pushes operands and opening parentheses to the stack and pops to output higher priority operators, and converts the expression to postfix notation which is printed.

Uploaded by

Hog Rider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 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();
}

You might also like