static
char exp[50];
static
int i=0,len,top=1,isp,icp;
static
char x,stack[50];
#include<stdio.h>
void
main()
{
void rec(char c);
static
char yn;
char ch;
clrscr();
stack[1]='#';
top=1;
clrscr();
printf("\n\t\tINFIX
TO POSTFIX CONVERSION");
printf("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~
");
printf("\n\n\tEnter
the Infix expression (! for End): ");
for(len=1;(exp[len]=getchar())!='\n';len++);
if(exp[len-1]=='!')
{
printf("\n\n\tEqvalent
Postfix Expression => ");
rec(exp[0]);
}
else
printf("\n\n\t\t
You must type ! on end");
getch();
}
void
rec(char x)
{
i++;
if(x=='!')
{
while(top>1)
{
printf("%c",stack[top]);
top--;
}
return;
}
if(((x>=65)&&(x<=90))||((x>=97)&&(x<=122)))
printf("%c",x);
if(x==')')
{
while(stack[top]!='(')
{
printf("%c",stack[top]);
top--;
}
top--;
}
if((x=='^')||(x=='*')||(x=='/')||(x=='+')||(x=='-')||(x=='('))
{
do
{
icp=isp=0;
if((x=='+')||(x=='-'))
icp=1;
if((stack[top]=='+')||(stack[top]=='-'))
isp=1;
if((x=='*')||(x=='/'))
icp=2;
if((stack[top]=='*')||(stack[top]=='/'))
isp=2;
if(x=='(')
icp=4;
if(stack[top]=='(')
isp=0;
if(stack[top]=='^')
isp=3;
if(x=='^')
icp=4;
if(isp>=icp)
{
printf("%c",stack[top]);
top--;
}
if(top==1)
break;
}while(isp>=icp);
top++;
stack[top]=x;
}
while(i<len)
{
rec(exp[i]);
}
}
No comments:
Post a Comment