Skip to content

Instantly share code, notes, and snippets.

@B45i
Created March 16, 2016 03:15
Show Gist options
  • Save B45i/c4153c3fbf6eb6615601 to your computer and use it in GitHub Desktop.
Save B45i/c4153c3fbf6eb6615601 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include <ctype.h>
char s[50];
int top = -1;
push(char elem) {
s[++top] = elem;
}
char pop() {
return (s[top--]);
}
int pr(char elem) {
switch (elem) {
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
}
}
pushit(int ele){
s[++top]=ele;
}
int popit(){
return(s[top--]);
}
main() {
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("Enter Infix expression :");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0') {
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')') {
while (s[top] != '(')
pofx[k++] = pop();
elem = pop();
} else {
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#')
pofx[k++] = pop();
pofx[k] = '\0';
printf("PostFix expression : %s",pofx);
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment