Parsing/RPN calculator algorithm: Difference between revisions

Content added Content deleted
Line 2,148: Line 2,148:
{{works with|Java|1.5+}}
{{works with|Java|1.5+}}
Supports multi-digit numbers and negative numbers.
Supports multi-digit numbers and negative numbers.
<lang java5>import java.util.LinkedList;
<lang java5>
import java.util.LinkedList;


public class RPN{
public class RPN{
public static void evalRPN(String expr){
public static void main(String[] args) {
evalRPN("3 4 2 * 1 5 - 2 3 ^ ^ / +");
String cleanExpr = cleanExpr(expr);
}

private static void evalRPN(String expr){
LinkedList<Double> stack = new LinkedList<Double>();
LinkedList<Double> stack = new LinkedList<Double>();
System.out.println("Input\tOperation\tStack after");
System.out.println("Input\tOperation\tStack after");
for(String token:cleanExpr.split("\\s")){
for (String token : expr.split("\\s")){
System.out.print(token+"\t");
System.out.print(token + "\t");
if (token.equals("*")) {
Double tokenNum = null;
try{
tokenNum = Double.parseDouble(token);
}catch(NumberFormatException e){}
if(tokenNum != null){
System.out.print("Push\t\t");
stack.push(Double.parseDouble(token+""));
}else if(token.equals("*")){
System.out.print("Operate\t\t");
System.out.print("Operate\t\t");
double secondOperand = stack.pop();
double secondOperand = stack.pop();
double firstOperand = stack.pop();
double firstOperand = stack.pop();
stack.push(firstOperand * secondOperand);
stack.push(firstOperand * secondOperand);
}else if(token.equals("/")){
} else if (token.equals("/")) {
System.out.print("Operate\t\t");
System.out.print("Operate\t\t");
double secondOperand = stack.pop();
double secondOperand = stack.pop();
double firstOperand = stack.pop();
double firstOperand = stack.pop();
stack.push(firstOperand / secondOperand);
stack.push(firstOperand / secondOperand);
}else if(token.equals("-")){
} else if (token.equals("-")) {
System.out.print("Operate\t\t");
System.out.print("Operate\t\t");
double secondOperand = stack.pop();
double secondOperand = stack.pop();
double firstOperand = stack.pop();
double firstOperand = stack.pop();
stack.push(firstOperand - secondOperand);
stack.push(firstOperand - secondOperand);
}else if(token.equals("+")){
} else if (token.equals("+")) {
System.out.print("Operate\t\t");
System.out.print("Operate\t\t");
double secondOperand = stack.pop();
double secondOperand = stack.pop();
double firstOperand = stack.pop();
double firstOperand = stack.pop();
stack.push(firstOperand + secondOperand);
stack.push(firstOperand + secondOperand);
}else if(token.equals("^")){
} else if (token.equals("^")) {
System.out.print("Operate\t\t");
System.out.print("Operate\t\t");
double secondOperand = stack.pop();
double secondOperand = stack.pop();
double firstOperand = stack.pop();
double firstOperand = stack.pop();
stack.push(Math.pow(firstOperand, secondOperand));
stack.push(Math.pow(firstOperand, secondOperand));
}else{//just in case
} else {
System.out.println("Error");
System.out.print("Push\t\t");
return;
try {
stack.push(Double.parseDouble(token+""));
} catch (NumberFormatException e) {
System.out.println("\nError: invalid token " + token);
return;
}
}
}
System.out.println(stack);
System.out.println(stack);
}
if (stack.size() > 1) {
System.out.println("Error, too many operands: " + stack);
return;
}
}
System.out.println("Final answer: " + stack.pop());
System.out.println("Final answer: " + stack.pop());
}
}
}
</lang>
private static String cleanExpr(String expr){
//remove all non-operators, non-whitespace, and non digit chars
return expr.replaceAll("[^\\^\\*\\+\\-\\d/\\s]", "");
}
public static void main(String[] args){
evalRPN("3 4 2 * 1 5 - 2 3 ^ ^ / +");
}
}</lang>
{{out}}
{{out}}
<pre>Input Operation Stack after
<pre>Input Operation Stack after