You are on page 1of 2

char infix[max], postfix[max];

long int stack[max];


int top=0;

infixtopostfix()
{
int i,p=0,typr, precedence,len;
char next;
stack[top]='#';
len=strlen(infix);
infix[len]='#';

for(i=0;infix[i]!='#';i++)
{
if(!whitespace(infix[i])
{
switch(infix[i])
{
case '(': push(infix[i]);
break;

case ')': while((next=pop())!='(') postfix[p+


+]=next;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
precedence=prec(infix[i]);
while (stack[top]!
='#'&&precedence<prec(stack[top])) postfix[p++]=pop();
push(infix[i]);
break;
default: in case of operand
postfix[p++]=infix[i];
}
}
}
prec(char symbol)
{ return 0 for (, 1 for + and -, 2 for *,/ and % and 3 for ^
}

push(long int symbol))


{ top=top+1 and stack[top]=symbol }

long int pop()


{ return stack[top--]; }

whitespace(char symbol)
{ return 1 for Blank, Tab nd '\0' else 0; }

long int evalpost()


{
len=strlen(postfix);
postfix[len]='#';
for(i=0;postfix[i]!='#';i++)
{
if(postfix[i]<='9'&&postfix[i]>=’0’) push(postfix[i]-48);
else
{
a=pop();
b=pop();

switch(postfix[i])
{
case +,-,*,/,%,^, temp=b(operator)a, for ^, use
pow(); }
push(temp);
}
}
result=pop();
return result;
}

You might also like