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 to Controller.m. You can also type the three-key combination Command-Option-up-arrow to rapidly switch between a class’s .h and .m files.

    - (IBAction)enterOp:(id)sender 
    {
      if (yFlag) {           // Something is stored in Y
                             switch (operation) {
                                case PLUS:
                                  X = Y + X;
                                  break;
    
                                case SUBTRACT:
                                  X = Y - X;
                                  break;
    
                                case MULTIPLY:
                                  X = Y * X;
                                  break;
    
                                case DIVIDE:
                                  X = Y / X;
                                  break;
                              }
                           }
    
                            Y    = X;
                            yFlag = YES;
    
                           operation = [ [sender selectedCell] tag];
                           enterFlag = YES;
    
                           [self displayX];
    }

The enterOp: method is the computational engine of our Calculator application. It performs the arithmetic operation that was stored in the operation instance variable, sets up the registers and flags for another operation or another button click, and then displays the contents of the X register in the window display area.

  1. Activate IB and create an NSMatrix with six buttons, as shown in Figure 5-28. (If you don’t remember how to do this, go back and review how the digit buttons were set up.)

Calculator window with operations in IB

Figure 5-28. Calculator window with operations in IB

As you try to place these buttons, you may want to resize or rearrange the existing buttons on the current Calculator interface. Feel free — one of the most powerful things about IB is that you move around an interface after you have created it.

  1. Set the title of each button to correspond with one of the six basic functions, as shown in Figure 5-28. You may want to use a larger font for some of the titles to make them more readable.

  2. Set the tag of each button (except “+/-”) to correspond with the enum defined in the Controller.h file above.

    To set the tag of a button, select the button, type Command-1 to display the NSButtonCell Info dialog, change the value of the tag, and press Return. Don’t worry about the unary minus “+/-” button for now. Again, double-check your tags to make sure they are correct.

  3. Connect the new NSMatrix to the Controller by Control-dragging from the NSMatrix to the Controller instance icon and double-clicking the enterOp: action in the NSMatrix Info dialog. This connection is similar to the one we made for the numeric keypad.

  4. Choose IB’s File Save (or Save All) command to save the nib and Controller class files.

  5. Compile your program and run it. If you use PB, all you have to do is click the build and run button, and PB will do all the rest (including prompting for file saving).

All of the Calculator’s buttons should now work properly, except for the unary minus button.

  1. Choose Calculator Quit or click PB’s Stop button to exit the Calculator application.

For easy access, we recommend that you keep the PB, IB, and Terminal application icons in your Dock while developing applications. Also, when you want to switch applications, use Mac OS X’s Hide command rather than Quit. This keeps your screen clear and avoids the wait of having applications start up again when you need them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.195.34