Courses‎ > ‎AP Computer Science 2‎ > ‎Konstantinovich‎ > ‎

2017-04-20 Post Fix

posted Apr 20, 2017, 7:18 AM by Samuel Konstantinovich   [ updated Apr 20, 2017, 9:21 AM ]
What is/isn't on the AP:

Quick Reference: (1st page only!)


GOAL: Evaluate a post fix expression

Evaluate an arbitrary postfix expression. Assume all results are doubles (do not have to preserve int when you run an operation on two ints)

valid numbers:
any int or double.

valid operators: (all binary)
+
-
*
/
%

All values and operators are separated by a single space:
"10 2.0 +"
"11 3 - 4 + 2.5 *"
"8 2 + 99 9 - * 2 + 9 -"

Write the eval(String) method, that will correctly evaluate a post-fix expression.

eval("10 2.0 +") is 12.0
eval("11 3 - 4 + 2.5 *") is 30.0
eval("8 2 + 99 9 - * 2 + 9 -") is 893.0


1. Convert your string into tokens. (A list of values and operators)
1b. Test this by printing each one!
2. Instead of printing each one, decide what to do with them... using a stack!



Comments