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> |
<lang java5> |
||
import java.util.LinkedList; |
|||
public class RPN{ |
public class RPN{ |
||
public static void |
public static void main(String[] args) { |
||
⚫ | |||
String cleanExpr = cleanExpr(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: |
for (String token : expr.split("\\s")){ |
||
System.out.print(token+"\t"); |
System.out.print(token + "\t"); |
||
⚫ | |||
Double tokenNum = null; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
if(tokenNum != null){ |
|||
System.out.print("Push\t\t"); |
|||
stack.push(Double.parseDouble(token+"")); |
|||
⚫ | |||
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 |
} else { |
||
System.out. |
System.out.print("Push\t\t"); |
||
try { |
|||
⚫ | |||
⚫ | |||
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()); |
||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
//remove all non-operators, non-whitespace, and non digit chars |
|||
return expr.replaceAll("[^\\^\\*\\+\\-\\d/\\s]", ""); |
|||
⚫ | |||
public static void main(String[] args){ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
{{out}} |
{{out}} |
||
<pre>Input Operation Stack after |
<pre>Input Operation Stack after |