C Program to Convert Infix Expression to Prefix Expression

Infix Expression:

When the operators are in-between the operands just like a + b then the expression is known as Infix Expression.

Prefix Expression:

When the operators are placed before the operands by some rules just like +ab then the expression is known as Prefix Expression.

Algorithm:

1. Create an empty Stack.

2. Reverse the Infix expression.

3. Read all elements from the Infix expression.

3.1 If the element is Left parenthesis then PUSH it into the Stack.

3.2 If the element is Right parenthesis then POP all the elements from the Stack and display with Prefix string until left matching opening parenthesis found.

3.3 If the element is Operand then display with Prefix string.

3.4 If the element is Operator

3.4.1 Check if the Stack is empty, push onto the Stack.

3.4.2 Otherwise, check the priority level from the top of the Stack. If the priority of the operator is greater than the priority level of the operator placed at the top of the stack, PUSH onto stack otherwise POP from Stack and repeat 3.4 step again.

4. At the end of the expression Stack must be empty if not then POP and display with Prefix string.

5. Reverse the Prefix expression.

Code:


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include<stdio.h>

struct stack{

    int top;

    char arr[20];

}Stack;

int checkPriority(char operator){

    if(Stack.top==-1){

        return 1;

    }else if(Stack.arr[Stack.top]=='(' || Stack.arr[Stack.top]=='{' || Stack.arr[Stack.top]=='['){

        return 1;

    }else if(operator==Stack.arr[Stack.top]){

       return 0;

    }else if(operator=='(' || operator=='{' || operator=='['){

        return 1;

    }else if((Stack.arr[Stack.top]=='+' || Stack.arr[Stack.top]=='-') && (operator=='/' || operator=='*' || operator=='/' || operator=='^')){

        return 1;

    }

  return 0;

}

void push(char operator){

    if(Stack.top==19){

        printf("Stack Overflow\n");

    }else{

        Stack.top++;

        Stack.arr[Stack.top]=operator;

    }

}

char pop(){

    char operator;

    if(Stack.top==-1){

        printf("Stack UnderFlow\n");

    }else{

        operator=Stack.arr[Stack.top];

        Stack.top--;

    }

    return operator;

}

int isMatching(char opening,char closing){

    if(opening=='(' && closing==')'){

        return 1;

    }else if(opening=='{' && closing=='}'){

        return 1;

    }else if(opening=='[' && closing==']'){

        return 1;

    }



    return 0;

}



int main(){

    char infix[]="A^B*C-D+E/F/(G+H)",symbol;

    char prefix[20]="";

    int i=0,j=0,l=0;

    Stack.top=-1;

    strrev(infix);



    while(infix[l]){

        if(infix[l]=='('){ infix[l]=')'; }

        else if(infix[l]==')'){ infix[l]='('; }

        else if(infix[l]=='{'){ infix[l]='}'; }

        else if(infix[l]=='}'){ infix[l]='{'; }

        else if(infix[l]=='['){ infix[l]=']'; }

        else if(infix[l]==']'){ infix[l]='['; }

        l++;

    }

    while(infix[i]){

       if(infix[i]=='+' || infix[i]=='-' || infix[i]=='*' || infix[i]=='/' || infix[i]=='^' || infix[i]=='(' || infix[i]=='{' || infix[i]=='['){

            if(checkPriority(infix[i])){

                push(infix[i]);

                i++;

            }

            else{

                prefix[j]=pop();

                j++;

            }

       }else if(infix[i]==')' || infix[i]=='}' || infix[i]==']'){

            while(1){

                symbol=pop();

                if(isMatching(symbol,infix[i])){

                   break;

                }else{

                    prefix[j]=symbol;

                    j++;

                }

            }

            i++;

       }else{

            prefix[j]=infix[i];

            j++;

            i++;

       }

    }

    for(i=j;Stack.top!=-1;i++){

        prefix[i]=pop();

    }

    strrev(prefix);

    printf("Result : %s\n",prefix);

return 0;

}

No comments