C Program to Convert Infix Expression to Postfix Expression
Infix Expression:
When the operators are in-between the operands just like a + b then the expression is known as Infix Expression.Postfix Expression:
When the operators are placed after the operands by following some rules just like ab+ then the expression is known as Postfix Expression.Algorithm:
1. Create an empty Stack.
2. Read all elements from the Infix expression.
2.1 If the element is Left parenthesis then PUSH it into the Stack.
2.2 If the element is Right parenthesis then POP all the elements from the Stack and display with Postfix string until left matching opening parenthesis found.
2.3 If the element is Operand then display with Postfix string.
2.4 If the element is Operator
2.4.1 Check if the Stack is empty, PUSH onto the Stack.
2.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 2.4 step again.
3. At the end of the expression Stack must be empty if not then POP and display with Postfix string.
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 | #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 postfix[20]=""; int i=0,j=0; Stack.top=-1; 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{ postfix[j]=pop(); j++; } }else if(infix[i]==')' || infix[i]=='}' || infix[i]==']'){ while(1){ symbol=pop(); if(isMatching(symbol,infix[i])){ break; }else{ postfix[j]=symbol; j++; } } i++; }else{ postfix[j]=infix[i]; j++; i++; } } for(i=j;Stack.top!=-1;i++){ postfix[i]=pop(); } printf("Result : %s\n",postfix); return 0; } |
No comments