0% found this document useful (0 votes)
10 views3 pages

p12 (Infix To Postfix)

Uploaded by

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

p12 (Infix To Postfix)

Uploaded by

abaiju696
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*INFIX TO POSTFIX CONVERSION*/

#include<stdio.h>

#include<string.h>

#define MAX 20

char s[MAX];

int top=0;

int precedence (char elem);

void push(char elem);

int pop();

int main()

char infix [MAX], postfix [MAX], ch,elem;

int i=0,j=0;

printf("\n\t\tProgram to Convert infix to Postfix Expression.");

printf("\n\t\t~~~~~~~~~~~~~~\n");

printf("\n Enter the infix expression: \n");

scanf("%s", infix);

push('#');

for(i=0;i<strlen(infix); i++)

ch=infix[i];

if(isalnum(ch))

postfix[j++]=ch;

else

if(ch=='(')

push(ch);

else

if(ch==')')

while(s[top]!='(')

postfix[j++] = pop();
elem = pop();

else

while (precedence (s[top])>= precedence(ch))

postfix[j++]=pop();

push(ch);

}/*End of scanning elements */

while(s[top] != '#')

postfix[j++]=pop();

postfix[j]='\0';

printf("\n Postfix Expression conversion is: \n %s \n", postfix);

/* Function for Precedence */

int precedence (char elem)

switch(elem)

case '+':

case '-':

return(1);

case '*':

case '/':

return(2);

case '^': return(3);

case '(':

case '#': return(0);

}
}

/* Function to void push (char elem)*/

void push(char elem)

++top;

s[top]=elem;

/* Function for poping Character */

int pop()

char elem;

elem=s[top];

--top;

return(elem);

OUTPUT

You might also like