#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();
}