In this instructional exercise you will find out about program and
calculation for infix to postfix transformation in C with a model.
In infix documentation or articulation administrators are written in the middle of the
operands while in postfix documentation each administrator pursues the entirety of its operands.
Example:
Infix Expression: 5+3*2
Postfix Expression: 5 3 2*+.
Infix to Postfix Conversion Algorithm
Give Q a chance to be any infix articulation and we need to change over it to postfix articulation P.
For this, the accompanying technique will be pursued.
Push left bracket onto STACK and include right enclosure toward the finish of Q.
Output Q from left to right and rehash stage 3 to 6 for every component of Q until the STACK is unfilled.
In the event that an operand is experienced add it to P.
In the event that a left bracket is experienced push it onto the STACK.
On the off chance that an administrator is experienced, at that point
Over and over fly from STACK and add to P every administrator
which has same priority as or higher priority than the administrator
experienced.
Push the experienced administrator onto the STACK.
On the off chance that a correct enclosure is experienced, at that point
More than once fly from the STACK and add to P every administrator
until a left bracket is experienced.
Evacuate the left enclosure; don’t add it to P.
Exit
Program
// Operator supported: +,-,*,/,%,^,(,)
// Operands supported: all single character operands
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 50
typedef struct stack
{
int data[MAX];
int top;
}stack;
int precedence(char);
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int top(stack *); //value of the top element
void infix_to_postfix(char infix[],char postfix[]);
void main()
{
char infix[30],postfix[30];
printf("Enter an infix expression(eg: 5+2*4): ");
gets(infix);
infix_to_postfix(infix,postfix);
printf("\nPostfix expression: %s",postfix);
}
void infix_to_postfix(char infix[],char postfix[])
{
stack s;
char x,token;
int i,j; //i-index of infix,j-index of postfix
init(&s);
j=0;
for(i=0;infix[i]!='\0';i++)
{
token=infix[i];
if(isalnum(token))
postfix[j++]=token;
else
if(token=='(')
push(&s,'(');
else
if(token==')')
while((x=pop(&s))!='(')
postfix[j++]=x;
else
{
while(precedence(token)<=precedence(top(&s))&&!empty(&s))
{
x=pop(&s);
postfix[j++]=x;
}
push(&s,token);
}
}
while(!empty(&s))
{
x=pop(&s);
postfix[j++]=x;
}
postfix[j]='\0';
}
int precedence(char x)
{
if(x=='(')
return(0);
if(x=='+'||x=='-')
return(1);
if(x=='*'||x=='/'||x=='%')
return(2);
return(3);
}
void init(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
return(0);
}
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
return(0);
}
void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
int top(stack *p)
{
return (p->data[p->top]);
}
Output
A case of changing over infix articulation into postfix structure, indicating stack status after each step is given underneath.
Here RPN represents turn around clean documentation (postfix documentation).
In the event that you have any questions with respect to above infix to postfix change in C instructional
exercise at that point doesn’t hesitate to remark underneath.