postfix evaluation
postfix evaluation
h>
#include <string.h>
char infix[50],postfix[50],s[50];
int top=-1;
void push(char symb);
char pop();
int precedence(char symb);
int isempty ();
void convertexp(char infix[20]);
case '*' :
case '/' :
return 2;
case '^' :
case '$' :
return 3;
// break;
case '(' :
//case ')' :
return 0;
// break;
default:
return 0;
}
char scannedsymb,symb;
for (i=0;i<strlen(infix);i++)
{
symb=infix[i];
switch(symb)
{
case '(': push(symb);
break;
case ')':
while((scannedsymb=pop())!='(')
{
postfix[j++]=scannedsymb;
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
while(!isempty() && precedence(s[top])>=precedence(symb))
postfix[j++]=pop();
push(symb);
break;
default:
postfix[j++]=symb;
}
}
while(!isempty())
{
postfix[j++]=pop();
}
puts (postfix);
//printf("postfix expression is %s",postfix);
}
int isempty ()
{
if (top==-1)
return 1;
else
return 0;
}
void main()
{
printf("enter infix expression:=");
scanf("%s",infix);
convertexp(infix) ;
getch();
}