#include <stdio.h> #include <stdlib.h> #include<strings.h> struct Node { char data; struct Node *next; }*top=NULL; void push(char x) { struct Node *t; t=(struct Node*)malloc(sizeof(struct Node)); if(t==NULL) printf("stack is full\n"); else { t->data=x; t->next=top; top=t; } } char pop() { struct Node *t; char x=-1; if(top==NULL) printf("Stack is Empty\n"); else { t=top; top=top->next; x=t->data; free(t); } return x; } int pre(char x) { if(x=='+'||x=='-') return 1; else if(x=='/' || x=='*') return 2; else if(x=='%' || x=='^') return 3; else return 0; } int isOperand(char x) { if(x=='+' || x=='-' || x=='(' || x==')'|| x=='*'||x=='/'||x=='^') return 0; else return 1; } void intopost(char infix[]) { int i=0; while(infix[i]!='\0') { if(infix[i]=='(') push(infix[i++]); else if(isOperand(infix[i])) { scanf("%c",infix[i]); i++; } else if(infix[i]==')') { char x=pop(); while(x!='(') { printf("%c",x);; x=pop(); } i++; } else{ if(top==NULL) push(infix[i++]); else if(pre(infix[i])>pre(top->data)) push(infix[i++]); else printf("%c",pop()); } } while(top!=NULL) printf("%c",pop()); } int main() { char *infix; infix="a+(b*c)"; intopost(infix); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter