rendered paste bodyboolean postfixExpression()
{
stk.clear();
Scanner scan = new Scanner(expression);
char current;
while (scan.hasNext())
{
String token = scan.next();
if (isNumber(token))
postfix = postfix + token + " ";
else
{
current = token.charAt(0);
if (isParentheses(current))
{
if(stk.empty() || current == Constants.LEFT_NORMAL)
{
stk.push(new Character(current));
}
else if (current == Constants.RIGHT_NORMAL)
{
try
{
Character ch = (Character)stk.pop();
char top = ch.charValue();
while (top != Constants.LEFT_NORMAL)
{
postfix = postfix + top + " ";
ch = (Character)stk.pop();
top = ch.charValue();
}
}
catch(EmptyStackException e)
{
return false;
}
}
}
else if (isOperator(current))
{
if(stk.empty())
stk.push(new Character(current));
else
{
try
{
char top = (Character)stk.peek();
boolean higher = hasHigherPrecedence(top, current);
while(top != Constants.LEFT_NORMAL && higher)
{
postfix = postfix + stk.pop() + " ";
top = (Character)stk.peek();
}
stk.push(new Character(current));
}
catch(EmptyStackException e)
{
stk.push(new Character(current));
}
}
}
}
}
try
{
while(!stk.empty())
{
postfix = postfix + stk.pop() + " ";
}
}
catch(EmptyStackException e)
{
}
return true;
}