Code 4
Code 4
2
Aim :- Convert an Infix expression to Pos ix expression using stack ADT
Program : Sarang R. Shinde
#include<stdio.h> ID no.:VU1F2223060
#include<ctype.h> Class : SE/A Batch : C
#define max 100
char stack [max];
int top=-1;
void push (char x)
{
stack[++top]=x;
}
char pop ()
{
if (top==-1)
return -1;
else
return stack[top--];
}
int priority (char x)
{
if(x=='(')
return 0;
else if (x=='+'||x=='-')
return 1;
else if (x=='*'||x=='/'||x=='%')
return 2;
else if(x=='^')
return 3;
return 0;
}
int main ()
{
char exp [max];
char *e ,x;
prin ("Enter the expression");
scanf ("%s",&exp);
prin ("\n");
e=exp;
while (*e!='\0')
{
if(isalnum(*e))
prin ("%c",*e);
else if (*e=='(')
push(*e);
else if (*e==')')
{
while((x=pop())!='(')
prin ("%c",x);
}
else
{
while(priority(stack[top])>=priority(*e))
prin ("%c",pop());
push(*e);
}
e++;
}
while (top!=-1)
{
prin ("%c",pop());
}
return 0;
}
OUTPUT :
Enter the expression A+(B-C)*D%E^(F/G)+H
ABC-D*EFG/^%+H+