Feb 10, 2012

C Program to convert from infix expression into prefix expression

C Program to convert from infix expression into prefix expression.
We use the infix expressions for the mathematical expressions in a program, these expressions will converted into equivalent machine instructions by the compiler using stacks.
Using stacks we can efficiently convert the expressions from infix to postfix, infix to prefix, postfix to infix, and postfix to prefix.
Example: infix to prefix:
infix: x^y^z-m+n+p/q,
postfix: ++-^x^yzmn/pq. Read more about C Programming Language.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include<stdio.h>

#include<conio.h>

//Stack precedence function

 int F(char symbol)

  {

      switch(symbol)

               {

                   case ‘+’ :

                   case ‘-‘ :

                          return 1;

                   case ‘*’:

                  case ‘^’:

                         return 6:

                case ‘)’:

                       return 0:;

                case ‘#’:

                       return -1;

                  default:

                       return 8;

                       }

      }

//Input precedence function

int G(char symbol)

    {

      switch(symbol)

            {

               case ‘+’ :

               case ‘-‘ :

                      return 2;

               case ‘*’:

                     return 4;

               case ‘^’:

                     return  5:

                case ‘(‘:

                    return 0;

                 case ‘)’:

                   return 9:;

                 case ‘#’:

                 return -1;

               default:

                  return 7;

              }

}

Void infix_prefix(char infix[], char prefix[])

{

      int top, j, i;

     char symbol, s[40];

      top = -1;

      s[++top] = ‘#’;

      J = 0;

      strrev(infix);

     for(i = 0;i < strlen(infix); i++)

          {

              symbol= infix[i];

              while(F(s[top]) > G(symbol))

                     {

                             prefix[j] = s[top--];

                             j++;

                      }

              if(F(s[top]) != G(symbol))

              s[++top] = symbol;

             else

                       top--;

             }

  while(s[top != ‘#’)

      {

           prefix[j++] = s[top--];

       }

 prefix[j] = ‘\0’;

 strrev(prefix);

}

void main()

   {

     char infix[20];

     char prefix[20];

    printf(“/nEnter a valid infix expression\n”);

    scanf(“%s”,infix);

    infix_prefix(infix, prefix);

    printf(“\n\nThe prefix expression is\n”);

   printf(“%s\n”,prefix);

  }


Read more Similar C Programs
C Basic
C Data Structure
Search Algorithms.
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link