### Write a C program to Convert infix expression to postfix expression using stack
#define MAX 50 #include #include #include #include char stack[MAX],ch ;
int top=-1; void push(char ch) { if(top==MAX-1) return ; stack[++top]=ch; } char pop() { if (top==-1) return '\0'; else return stack[top--]; } int prece(char ch) { if(ch=='*'||ch=='/') return 2; else if(ch=='+'||ch=='-') return 1; else return 0; } void main() { clrscr(); char in[50],post[50]; int i=0,j=0,len; printf("\n Enter Experission in INFIX notation"); gets(in); len=strlen(in);
in[len]=')'; in[len+1]='\0'; push('('); while(in[i]!='\0') { if(isalpha(in[i])) post[j++]=in[i]; else if(in[i]=='(') push('('); else if(in[i]==')') { while((ch=pop())!='(') post[j++]=ch; } else if(in[i]!=32) { while(prece(ch=pop())>=prece(in[i])) post[j++]=ch; push(ch); push(in[i]); } i++; } post[j]='\0'; printf("post fix notation is %s",post); getch(); }
|
No feedbacks found. Be the first to respond...
|