#include #include #include struct stack { char op; } *p; int top; char aa[80]; void push(char a) { (p+top)->op=a; top++; } pop() { top--; return (p+top)->op; } comp(char c) { switch (c) { case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; } /* switch */ return 0; } void main() { int cp,ctp,n,j; char c,ct; printf("give me the number --> "); scanf("%d",&n); p=(struct stack *)malloc(sizeof(stack)*n); j=top=0; printf("give me the infix string --> "); while ((c=getchar()) != '=') { if ((c == '+') || (c == '-') || (c == '*') || (c == '/') || (c == '^')) { cp=comp(c); aa[j++]=' '; while (top > 0) { /* process stack empty ??? */ ct=pop(); ctp=comp(ct); if (ctp < cp) { /* stack < outer */ push(ct); push(c); break; } else if (ctp >= cp) { /* stack >= outer */ aa[j++]=' '; aa[j++]=ct; aa[j++]=' '; } } /* while process stack empty */ if (top == 0) push(c); } /* if operator */ else if (c == '(') push(c); else if (c == ')') while ((ct=pop()) != '(') { aa[j++]=' '; aa[j++]=ct; aa[j++]=' '; } else if ((c >= '0') && (c <= '9')) aa[j++]=c; } /* while */ while (top > 0) { aa[j++]=' '; aa[j++]=pop(); aa[j++]=' '; } /* while */ aa[j++]='='; printf("%s\n",aa); }