#include
#include
#include
#define operand(x)(x>='a'&&x<='z' || x>='A'&&x<='Z' || x>='0'&&x<='9')
char infix[30], postfix[30], stack[30];
int top,i=0;
void init()
{
top=-1;
}
void push(char x)
{
stack[++top]=x;
}
char pop()
{
return( stack[top--] );
}
int isp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?0:-1);
return y;
}
int icp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?4:-1);
return y;
}
void infixtopostfix()
{
int j,l=-1;
char x,y;
stack[++top]='\0';
for (j=0; j<=strlen(infix)-1; j++)
{
x=infix[j];
if (operand(x))
postfix[++l]=x;
else if (top=='\0')
push(x);
else
{
while (isp(stack[top]) >= icp(x) )
postfix[++l]=pop();
push(x);
}
}
while (top!='\0')
postfix[++l]=pop();
postfix[++l]='\0';
}
void main()
{
clrscr();
init();
cout<<"Enter an infix expression :\n";
gets(infix);
infixtopostfix();
cout<<"The resulting postfix expression is: "<
}
No comments:
Post a Comment