Saturday, 12 April 2014

Infix to Postfix Conversion

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define SIZE 50
void push(char);
char pop();
int priority(char);
char s[SIZE];
int top=-1;
void main()
{
                char infix[50],postfix[50],ch,item;
                int i=0,k=0,len;
                clrscr();
                printf("\n\n $$$ INFIX TO POSTFIX CONVERSION $$$ \n\n");
                printf("Enter infix expression:\t");
                scanf("%s",infix);
                push('#');
                len=strlen(infix);
                for(i=0;i<len;i++)
                {
                                ch=infix[i];
                                if(isalnum(ch))
                                {
                                                postfix[k]=ch;
                                                k++;
                                }
                                else if(ch=='(')
                                {
                                                push(ch);
                                }
                                else if(ch==')')
                                {
                                                while(s[top]!='(')
                                                {
                                                                postfix[k]=pop();
                                                                k++;
                                                }
                                                item=pop();
                                }
                                else
                                {
                                                while(priority(s[top])>=priority(ch))
                                                {
                                                                postfix[k]=pop();
                                                                k++;
                                                }
                                                push(ch);
                                }
                }
                while(s[top]!='#')
                {
                                postfix[k]=pop();
                                k++;
                }
                postfix[k]='\0';
                printf("Postfix expression is: %s",postfix);
                getch();
}
void push(char a)
{
                top=top+1;
                s[top]=a;
}
char pop()
{
                char a;
                a=s[top];
                top=top-1;
                return a;
}
int priority(char a)
{
                switch(a)
                {
                                case '#':return 0;
                                case '(':return 1;
                                case '+':return 2;
                                case '-':return 2;
                                case '*':return 3;
                                case '/':return 3;
                                case '^':return 4;
                }
}

OUTPUT:
$$$ INFIX TO POSTFIX CONVERSION $$$
Enter infix expression: (A+B^C)*D+E^5

Postfix expression is: ABC^+D*E5^+

No comments:

Post a Comment