You are on page 1of 5

Roll No.

5608017 Date:-

Aim:- Write a program to implement infix expression to


postfix expression using stack.

Algorithms:- POLISH(Q,P)
Q is expression in infix notation.
P is postfix expression.

1. Push (“ onto stack and add”) to the end of Q.


2. Scan Q from left to right and repeat steps 3 to 6 for each
elements of Q until stack is empty.
3. If an operand is encountered,add it to P.
4. If a left parenthesis is encountered,push it onto.
5. If on operator is encountered them ;
(a). Pop from stack and add to P each operator which has
some precedence than .
(b). Add to stack.
6. If a right parenthesis is encountered them.
(a). Pop from stack and add to P each operator until a left
parenthsis is on countered.
(b). Remove left parenthesis.
7. Exit.
Coding:-

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<type.h>
#define Max 50
struct infix
{
char target [Max];
char * s, *t;
char stack [Max];
int.top;
}
void int infix(struct infix x);
void set expr(struct infix x,char);
void push(struct infix *;char);
char pop(struct infix *);
void onvert(struct infix *);
void show(struct infix);
void main( )
{
struct infix P;
char expr[Max];
int infix[i];
clrscr ( );
printf(“\n enter the expression in infix from”);
get (expr);
set expr(& P,expr);
convert(& P);
printf(“the postfix expression is”,show(P);
get ch( );
}
void set infix (struct infix *P)
{
P top-1;
If (*pS))= = ( )
{
push (P *(PS));
PS++;
}
if(*(PS) = “*” = = ‘1’
{
if(Ptop ! = 1)
{
opr = pop (P);
while (priority (opr)>priority(*(PS))
{
*(Pt) = opr
P t++;
opr = pop(P);
}
push (P,opr);
push (P1 *(PS);
}
else
push (P; *(PS);
PS++;
}
if ( *(PS = = ‘j’)
{
opr = pop(P);
while(opr);=’c’
}
*(Pt)=opr;
PS++;
opr = pop(P);
}
Ppop(P);
}
PS++;
}
while(Ppop ! ==-1)
char opr= pop(P);
{
*(Pt)=opr;
Pt++;
}
*(Pt)=’10’;
}
int priority ( char( ))
{
if (c==’S’)
return };
if (i== ‘*’//c=’1’//c==’%’)
return z;
else
{
if (c==’+’//c= =’=’)
return i;
else
return;
}
}
void show (struct infix P)
{
printf(“%d”,P.target);
}
OUTPUT:-

Enter an expression in infix from;


4 $ 2 *] – 3 + 8 / 4 ) ( 1 + 1 )
Stack is empty
The positive expression is ;
4 2 $ 3 *3 – 8 4 / 1 / + 1

You might also like