CS 202 Fall 2013 > In-Class Challenge for Tuesday, November 26, 2013 |
CS 202 Fall 2013
In-Class Challenge for Tuesday, November 26, 2013
Our Goal
A simple calculator program that evaluates Reverse Polish Notation.
What to Do
Do as many of the following as you can before class ends. After each, please show me your work.
- Finish program
rpncalc.cpp
by writing functionrpn
.- This function takes a
string
and astack
ofint
s. - If the string represents a number, then it should push the number
on the stack.
- You may find function
toInt
(included in the posted version ofrpncalc.cpp
) useful. - For now, do not worry about allowing for negative numbers.
Thus, a string represents a number if it begins with a digit
(
0
–9
).
- You may find function
- If the string represents an arithmetic operation
(
+
,-
,*
, or/
) then it should perform the operation on the top two elements of the stack, and push the result.
- This function takes a
- Write function
rpn
so that it can handle both subtraction (“-
”) and negative numbers (e.g., “-15
”). - What about error handling? What can you do to prevent the stack from getting messed up?
CS 202 Fall 2013: In-Class Challenge for Tuesday, November 26, 2013 /
Updated: 26 Nov 2013 /
Glenn G. Chappell