[go: up one dir, main page]

0% found this document useful (0 votes)
44 views2 pages

Infix to Postfix Conversion Code

Infix postfix

Uploaded by

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

Infix to Postfix Conversion Code

Infix postfix

Uploaded by

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

#define SIZE 50 /* Size of Stack */

#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */

void push(char item)


{ /* Function for PUSH operation */
s[++top] = item;
}

char pop()
{ /* Function for POP operation */
return (s[top--]);
}

int pr(char ch)


{ /* Function for precedence */
switch (ch)
{
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/':
case '%': return 3;
case '^': return 4;
}
}

void infixtopostfix(char infix[],char postfix[])


{
int i,k=0;
char ch;
for(i=0;infix[i]!=0;i++)
{
ch = infix[i];
if (ch == '(')
push(ch);
else if (isalnum(ch))
postfix[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
postfix[k++] = pop();
pop(); /* Remove ( */
}
else
{ /* Operator */
while (top!=-1 && pr(s[top]) >= pr(ch) && ch!='^')
postfix[k++] = pop();
push(ch);
} /*end if*/
} /*end for*/

while (top != -1) /* Pop from stack till empty */


postfix[k++] = pop();

postfix[k] = '\0'; /* Make pofx as valid string */


}/* end postfix*/
main()
{ /* Main Program */
char infix[50], postfix[50];
clrscr();
printf("\n\nEnter the Infix Expression \n ");
scanf("%s", infix);
infixtopostfix(infix,postfix);
printf("\n\nGiven Infix Expression is: %s\n",infix);
printf("Postfix Expression is: %s\n",postfix);
getch();
}

You might also like