C examples for Data Structure:Algorithm
Convert an Infix Expression to a Postfix Expression
#include <stdio.h> #include <string.h> char postfixExp[60]; char infixExp[60]; char operatorStack[60]; int i=0, j=0, intTop=0; int lowPriority(char opr, char oprStack) { int k, p1, p2;//from w ww .j a v a 2 s . c om char oprList[] = {'+', '-', '*', '/', '%', '^', '('}; int prioList[] = {0,0,1,1,2,3,4}; if( oprStack == '(' ) return 0; for(k = 0; k < 6; k ++) { if(opr == oprList[k]) p1 = prioList[k]; } for(k = 0; k < 6; k ++) { if(oprStack == oprList[k]) p2 = prioList[k]; } if(p1 < p2) return 1; else return 0; } void pushOpr(char opr) { if(intTop == 0) { operatorStack[intTop] = opr; intTop++; } else { if(opr != '(' ) { while(lowPriority(opr, operatorStack[intTop-1]) == 1 && intTop > 0) { postfixExp[j] = operatorStack[--intTop]; j++; } } operatorStack[intTop] = opr; intTop++; } } void popOpr() { while(operatorStack[--intTop] != '(' ) { postfixExp[j] = operatorStack[intTop]; j++; } } int main() { char k; printf("\n Enter Infix Expression : "); gets_s(infixExp); while( (k=infixExp[i++]) != '\0') { /* while statement begins. */ switch(k) { /* switch statement begins. */ case ' ' : break; case '(' : case '+' : case '-' : case '*' : case '/' : case '^' : case '%' : pushOpr(k); break; case ')' : popOpr(); break; default : postfixExp[j] = k; j++; } /* switch statement ends. */ } /* while state ment ends. */ while(intTop >= 0) { /* while statement begins. */ postfixExp[j] = operatorStack[--intTop]; j++; } /* while statement ends. */ postfixExp[j] = '\0'; printf("\n Infix Expression : %s ", infixExp); printf("\n Postfix Expression : %s ", postfixExp); }