Convert Prefix To Post Fix
Convert Prefix To Post Fix
11
53
Lab Report no.11
int main(){
char ch;
int i, k, p = 0;
for(i=0; i<SIZE; i++){
stack[i] = '\0';}
printf("\nEnter an infix expression: ");
scanf("%s",infix);
for(i=0; infix[i] != '\0'; i++){
ch = infix[i];
if (isalnum(ch)){
postfix[p++] = ch;}
else if (ch == '('){
push(ch);}
else if (ch == ')'){
while(stack[top] != '('){
postfix[p++] = pop();}
pop();}
else if (ch == '/' || ch == '*' || ch == '+' || ch == '-'){
if (preced(ch) > preced(stack[top])){
push(ch);}
else{
while (preced(ch) <= preced(stack[top])){
postfix[p++] = pop();}}}
postfix[p] = '\0';
while(top > -1)
postfix[p++] = pop();
printf("\nThe postfix expression is: %s\n",postfix);
return 0;}
void push(char el){
if (top < SIZE-1){
stack[++top] = el;}
else{
printf("\nError! Stack is full.\n");
exit(-1);}}
char pop(){
char ch;
if (top > -1){
ch = stack[top];
stack[top--] = '\0';
return ch;}
else{
printf("\nError! Stack is empty.\n");
exit(-1);}}
54
Lab Report no.11
55