Adding the Four Calculator Functions

We still need to add the functions that perform the calculations to our Calculator application. We’ll add six new buttons in yet another NSMatrix. The Controller object will need to differentiate between the buttons somehow, so we’ll assign them different integer tags.

Our first step in handling the four functions is to equip our Controller class with definitions for the mathematical operations that we want our Calculator to be able to handle. Then we’ll add these functions to our Calculator’s user interface.

  1. Using PB’s (or another) editor, insert the following enumerated data type after the #import directive in the Controller.h file (remember that bold code should be typed into class files):

                         enum {
                           PLUS      = 1001,
                           SUBTRACT  = 1002,
                           MULTIPLY  = 1003,
                           DIVIDE    = 1004,
                           EQUALS    = 1005
                         };

These codes will correspond to the tags that we will give the arithmetic buttons in the NSMatrix (we don’t want to confuse these tags with tags that we set previously). The Controller object will determine the tag of the button that sends it the action message and use that tag to figure out which function button the user has clicked. This is similar to what we did with the NSMatrix of digit buttons.

  1. Using an editor, insert the lines shown here in bold into the enterOp: method in the Controller.m file.

    You may be able to use the same PB code pane (or separate window) that you used for Controller.h by pressing the up-down “stepper” arrows next to the filename and dragging ...

Get Building Cocoa Applications: A Step by Step Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.