Convert Infix Expression To Postfix Converter Code Example


Example: infix to postfix conversion


Begin
initially push some special character say # into the stack
for each character ch from infix expression, do
if ch is alphanumeric character, then
add ch to postfix expression
else if ch = opening parenthesis (, then
push ( into stack
else if ch = ^, then //exponential operator of higher precedence
push ^ into the stack
else if ch = closing parenthesis ), then
while stack is not empty and stack top ≠ (,
do pop and add item from stack to postfix expression
done

pop ( also from the stack
else
while stack is not empty AND precedence of ch <= precedence of stack top element, do
pop and add into postfix expression
done

push the newly coming character.
done

while the stack contains some remaining characters, do
pop and add to the postfix expression
done
return postfix
End

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable