MENU











Sunday, 10 November 2013

Ds Week3 Programs

Exercise 3

3c)C Program to convert infix expression into postfix expression

Aim::/*To convert infix expression into postfix expression */

Program::

#include< stdio.h> //Standard i/p,o/p header file
#include< string.h>
#include< ctype.h>
#define size 20    //The variable 'size' is defined as macro for ease initialisation & to avoid repeated referencing.
int top=-1,Stack[size];      //Global variables to work in all the functions.
char infixexp[size],postfixexp[size];

void push(char ch)      //push() fuction.....
{
    Stack[++top]=ch;
}

char pop()                                //pop() function.....
{
    retun(Stack[top--]);
}

int precedence(char op)       //To know the precedence of operators.....
{
    if(op=='+'||op=='-')
        retun 1;
    else if(op=='*'||op=='/'||op=='%')
        retun 2;
    else if(op=='(')
        retun 3;
    else
        retun 0;
}

int main()                                //main() function 
{
    int i=0,j=0,l;
    char ch;
    printf("\n\nPlease enter Infix Expression to convert into Postfix: ");
    scanf("%s",infixexp);
    l=strlen(infixexp);
    for(i=0;i<=l;i++)
    {
        if(isalpha(infixexp[i]))        //Checking alphabets.....
        {
            postfixexp[j++]=infixexp[i];
        }
        else if(precedence(infixexp[i]))    //Checking operators either to push into the Stack or to add it to Postfix Expression.....
             {
                while(Stack[top]!='(' && precedence(infixexp[i])<=precedence(Stack[top]))
                {
                    postfixexp[j++]=pop();
                }
                push(infixexp[i]);
            }
            else if(infixexp[i]==')')        //Operator is closed paranthesis pop all the operators until open paranthesis.....
                 { 
                    while(Stack[top]!='(')
                    {
                        postfixexp[j++]=pop();
                    }    
                    if(top<0)                
                    {
                        printf("\n\nDue to wrong paranthesis.....Infix is wrong......");
                    }    
                    pop();
                }
    }
    
    while(top>-1)
    {
        ch=pop();
        if(ch!='(')
        {
            postfixexp[j++]=ch;
        }
        else
        {
            printf("\n\nforget to place closed paranthesis in Infix Expresssion......");
            break;
        }
    
    }
    
    postfixexp[j]='\0';
    
    printf("\n\nConverted Postfix Expression is : ");
    for(i=0;iint.....
        printf("%c ",postfixexp[i]);
    print>f("\n\n");
}


output::

gcc postfix.c -o postfix
./postfix

Please enter Infix Expression to convert into Postfix: a+b*c


Converted Postfix Expression is : a b c * + 


./postfix


Please enter Infix Expression to convert into Postfix: a*b+c


Converted Postfix Expression is : a b * c + 


./postfix

Please enter Infix Expression to convert into Postfix: a+b*c/(d-e)


Converted Postfix Expression is : a b c * d e - / + 



No comments:

Post a Comment