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.

  1. Finish program rpncalc.cpp by writing function rpn.
    • This function takes a string and a stack of ints.
    • 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 of rpncalc.cpp) useful.
      • For now, do not worry about allowing for negative numbers. Thus, a string represents a number if it begins with a digit (09).
    • 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.
  2. Write function rpn so that it can handle both subtraction (“-”) and negative numbers (e.g., “-15”).
  3. 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