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 [email protected]
* 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] = ‘’;

strrev(prefix);

}

void main()

{

char infix[20];

char prefix[20];

printf(“/nEnter a valid infix expressionn”);

scanf(“%s”,infix);

infix_prefix(infix, prefix);

printf(“nnThe prefix expression isn”);

printf(“%sn”,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 Follow @c_program

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
(c) www.c-program-example.com

9 comments on “C Program to convert from infix expression into prefix expression

  • Void infix_prefix(char infix[], char prefix[])
    In this function getting an error as "declaration error"
    plz , help me,,,,,,,,,,,,,

    Reply
  • 1) Hi! I checked though its working on precedence between +- against *^ the prefix code of the following code

    infix= "2^2*5" which makes 20 would make a prefix : ^2 * 2 5
    But ^2 * 2 5 is in infix 2^(2*5) that gives 1024

    2) The same problem appears if you implement division as same precedence value as multiplication:

    2/2*5 makes 5 . It would give /2 * 2 5 in prefix
    But /2 * 2 5 is 2/ (2*5) that equals 0.2 not 5;

    3) Is there a workaround? You can of course use brackets, but do I really have to rewrite the expression with brackets if there are precedence functions?

    Reply
  • The good solution for precedence values are:

    int F(char symbol)

    {

    switch(symbol)

    {
    case ‘+’ :
    case ‘-‘ :
    return 1;

    case ‘*’:
    case ‘/’:
    return 3:

    case ‘^’:
    return 5;

    case ‘)’:
    return 0;

    case ‘#’:
    return -1;

    default:

    return 18;

    }

    }

    //Input precedence function

    int G(char symbol)

    {

    switch(symbol)

    {

    case ‘+’ :
    case ‘-‘ :
    return 2;

    case ‘*’:
    case ‘/’:
    return 4;

    case ‘^’:
    return 6:

    case ‘(‘:
    return 0;

    case ‘)’:
    return 19;

    case ‘#’:

    return -1;

    default:
    return 17;

    }

    }

    With this taking the infix
    "1-3*2^2^3 *5*2/10*2/3+1-1*2+5-49/7^2/7*7"
    is in prefix:

    -+-+-1/*/***3^^2 2 3 5 2 10 2 3 1 *1 2 5 *// 49^ 7 2 77 giving -124, and thats right.

    Reply
  • To Arjun : Void infix_prefix(char infix[], char prefix[])
    It gives declaration error for every compiler I know, since "Void" should be written as "void".

    And as well
    s[++top] = ‘#’;
    J = 0;
    Here it was declared as: int j , so change to lowercase j. The rest is fine, see correction on precedence values. Bye!

    Reply

Leave a Reply