Tuesday 28 August 2012

Infix to prefix and infix to postfix in C


/*Infix to prefix and infix to postfix*/
/*Only for positive numbers*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 100
/*Stack Implementation*/
typedef struct stck
{
    char stack[max];
    int top;
}Stack;

void push(Stack*s,char val)
{
    if(s->top==max-1)
    {
        printf("Overflow\n");
        getche();
        exit(0);
    }
    s->top++;
    s->stack[s->top]=val;
}
char pop(Stack*s)
{
    char val;
    if(s->top<0)
    {
        printf("Underflow\n");
        getche();
        exit(0);
    }
    val=s->stack[s->top];
    s->top--;
    return val;
}
/*--------------------------------------------*/
char priority[3][2]={{'-','+'},{'*','/'},{'^','\0'}};

int priority_scanner(char opr,char tos,int prepost_bit) //prepost_bit=1  then  post
{
    int p_opr,p_tos,i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<2;j++)
        {
            if(opr==priority[i][j])
                p_opr=i;
            if(tos==priority[i][j])
                p_tos=i;
        }
    }
    return(prepost_bit==1?((p_tos>=p_opr)?1:0):((p_tos>p_opr)?1:0));
}

void in2post(char strng[max])
{
    int i=0;
    char val;
    Stack s;
    s.top=-1;
    while(i<strlen(strng))
    {
        if(strng[i]=='(')
            push(&s,'(');
        else if(strng[i]=='+'||strng[i]=='-'||strng[i]=='/'||strng[i]=='*'|strng[i]=='^')
        {
            if(s.top==-1)
                push(&s,strng[i]);
            else if(s.stack[s.top]=='(')
                push(&s,strng[i]);
            else
            {
                while(s.top!=-1 && s.stack[s.top]!='(' && priority_scanner(strng[i],s.stack[s.top],1))
                    printf("%c",pop(&s));
                push(&s,strng[i]);
            }
        }
        else if(strng[i]==')')
        {
            while((val=pop(&s))!='(')
            {
                printf("%c",val);   
            }
        }
        else
        {
            printf("%c",strng[i]);
        }
        i++;
    }
    while(s.top!=-1 && s.stack[s.top]!='(')
        printf("%c",pop(&s));    
}

void in2pre(char strng[max])
{
    int i=0,k=0;
    char val,f_strng[max];
    Stack s;
    s.top=-1;
    strrev(strng);
    while(i<strlen(strng))
    {
        if(strng[i]==')')
            push(&s,')');
        else if(strng[i]=='+'||strng[i]=='-'||strng[i]=='/'||strng[i]=='*'|strng[i]=='^')
        {
            if(s.top==-1)
                push(&s,strng[i]);
            else if(s.stack[s.top]==')')
                push(&s,strng[i]);
            else
            {
                while(s.top!=-1 && s.stack[s.top]!=')' && priority_scanner(strng[i],s.stack[s.top],0))
                      f_strng[k++]=pop(&s);
                push(&s,strng[i]);
            }
        }
        else if(strng[i]=='(')
        {
            while((val=pop(&s))!=')')
                f_strng[k++]=val;
        }
        else
        {
            f_strng[k++]=strng[i];
        }
        i++;
    }
    while(s.top!=-1 && s.stack[s.top]!=')')
          f_strng[k++]=pop(&s);
    f_strng[k]='\0';
    strrev(f_strng);
    printf("%s",f_strng);
}

int main()
{
    char infix_expr[max];
    printf("Enter an infix expression.\n");
    scanf("%s",infix_expr);
    printf("PostFix\n");
    in2post(infix_expr);
    printf("\nPreFix\n");
    in2pre(infix_expr);
    getche();
    return 0;
}

1 comments:

Unknown said...

sir i need favour to make prefix to infix and postfix program in c c++ with step and flexibility, its urgent plz if any body have this program plz mail me this program , i will heartly thankful to u .

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
@Gnosioware Solutions