Saturday, May 18, 2013

Implement Infix to postfix conversion using Stack C source codes

We can implement a valid infix expression to a valid postfix expression using stacks in c as shown:





//C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20

char stack[MAX];
int istack[MAX];
int top = -1;
char pop();
void push(char item);
int prcd(char symbol)
{

    switch(symbol)

{
    case '+':
    case '-':return 2;break;
    case '*':
    case '/':return 4;break;
    case '$':return 6;break;
    case '(':
    case ')':
    case '#':return 1;break;
}

}

int isoperator(char symbol)

{
    if(symbol=='+'||symbol=='-'||symbol=='*'||symbol=='/'||symbol=='$')

         return 1;
     else if(symbol==')'||symbol=='(')
          return -1;
     else
           return 0;
}

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

{

      int i,symbol,j=0;
      stack[++top]='#';
      for(i=0;i<strlen(infix);i++)
     {
        symbol=infix[i];
        if(isoperator(symbol)==0)

       {
           postfix[j]=symbol;
           j++;
       }

      else{

         if(symbol=='(')
            push(symbol);
         else if(symbol==')')
         {
             while(stack[top]!='(')
        {
            postfix[j]=pop();
            j++;
        }
        pop();//pop out (.

    }

        else{
            if(prcd(symbol)>prcd(stack[top]))
                  push(symbol);
            else{
                while(prcd(symbol)<=prcd(stack[top]))
            {
                postfix[j]=pop();
                j++;
            }
            push(symbol);

           }//end of else.

         }//end of else.

     }//end of else.

   }//end of for.

       while(stack[top]!='#')
       {
           postfix[j]=pop();
           j++;
        }
        postfix[j]='\0';//null terminate string.

}
long int oper(char c,int op1,int op2)
{
    switch(c)
    {
        case '+':return op2+op1+48;
        case '-':return op2-op1+48;
        case '*':return op2*op1+48;
        case '/':return op2/op1+48;
        case '$':return pow(op2,op1)+48;
        default: printf("Invalid operation!!");exit(1);
    }
}

int eval(char postfix[])
{
   int i;
   char c;
   float value ,op1,op2;
   for(i=0;(c=postfix[i])!='\0';i++)
   {
       if(isoperator(postfix[i])==0)
         push(postfix[i]);
       else
       {
           op1=pop();
           op2=pop();
           op1-=48;
           op2-=48;
           value = oper(c,op1,op2) ;
           push(value);
       }
   }

   return (pop()-48);
}

int main()

{
    char infix[20],postfix[20];
    int i;
    char res;
    do
    {

    printf("\nEnter the valid infix string:  ");
    fflush(stdin);
    gets(infix);
    for(i=0;infix[i]!='\0';i++)
    {
        if(isoperator(infix[i])==1 && isoperator(infix[i+1])==1)
           {

            printf("\n\tINPUT ERROR : Invalid Infix Expression !!");
            getch();
            exit(0);

           }

    }
    convertip(infix,postfix);
    printf("The corresponding postfix string is:  ");
    puts(postfix);
    printf("\nThe value of postfix expression : %d",eval(postfix));
    printf("\n\n\tDo you want to continue -[y/n]? ");
    scanf("%c",&res);

    }while(res =='y'||res=='Y');

    return 0;

}

void push(char item)

{
    top++;
    stack[top]=item;
}

char pop()

{
    char a;
    a=stack[top];
    top--;
    return a;
}



11 comments :

  1. check out for more expression conversion codes
    http://purecodes.blogspot.in/search/label/Stack

    ReplyDelete
    Replies
    1. Great Article IoT Projects for Students

      Deep Learning Projects for Final Year

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Hey there, You have done a fantastic job. I will certainly digg it and personally suggest to my friends. I’m confident they’ll be benefited from this website.
    C Programming Courses in Chennai

    ReplyDelete
  3. Aluminium Composite Panel or ACP Sheet is used for building exteriors, interior applications, and signage. They are durable, easy to maintain & cost-effective with different colour variants.

    ReplyDelete
  4. I feel satisfied to read your blog, you have been delivering a useful & unique information to our vision.keep blogging.
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata

    ReplyDelete
  5. Hello! I could have sworn I’ve visited this blog before but after going through some of the articles I realized it’s new to me. Anyways, I’m certainly happy views I found it and I’ll be book-marking it and checking back regularly!

    ReplyDelete
  6. RekordBox DJ 6.6.5 Crack 2023 is a music management application for Pioneer D products. It can transfer music files from Windows. Rekordbox Dj License Key

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...