hi, i am not sure how can i read a number like 10 and above for a char array, i am converting from infix to postfix , currently i am able to do so for single digits however when it comes to multiple digits when evaluating the postfix equation it would be a wrong answer. for example 10+13+4 it would give a wrong answer but 1+3+4 it would be correct.
//class main
calcParser calc = new calcParser("13+20+3+4");
calc.toPostfix();
calc.displayPostfix();
calc.evaluatePostfix();
calc.displayResult();
// result for above would be
1320+3+4+
9.0
// class calcParser
public void toPostfix()
{
for(char c : originalExp.toCharArray())
{
switch(c)
{
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
postfixExp.append(c);
break;
case '+': case '-':
if(postfixStack.empty())
{
postfixStack.push(c);
}
else
{
if((postfixStack.peek().equals(c)))
{
postfixExp.append(postfixStack.pop());
postfixStack.push(c);
}
else
{
postfixExp.append(postfixStack.pop());
postfixStack.push(c);
}
}
break;
}
}
while(!postfixStack.empty())
{
postfixExp.append(postfixStack.pop());
}
}
public void evaluatePostfix()
{
String postfix = postfixExp.toString();
for(char c : postfix.toCharArray())
{
switch (c)
{
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
postfixResultStack.push(c);
break;
case '+':
firstOperand = Double.parseDouble(postfixResultStack.pop().toString());
secondOperand = Double.parseDouble(postfixResultStack.pop().toString());
postfixResultStack.push(firstOperand + secondOperand);
break;
case '-':
firstOperand = Double.parseDouble(postfixResultStack.pop().toString());
secondOperand = Double.parseDouble(postfixResultStack.pop().toString());
postfixResultStack.push(firstOperand - secondOperand);
break;
}
}
}