DATA STRUCTURE C
Convert following infix expression into postfix expression
Expression: (A+B+C)/(D*E/F)-G$H-K
Answers
Answer:
#include <stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int order(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[20];
char *e;
char x;
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c",x);
}
else
{
while(order(stack[top]) >= order(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
return 0;
}