Welcome, Guest. Please login or register.
Did you miss your activation email?
July 29, 2016, 12:52:28 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Webcast Series Part I, Sept. 1 - 24
| | |-+ Chapter 6, Exercise 4
| | | |-+ Answer from Stephen F.
Pages: [1] Go Down
Author Topic: Answer from Stephen F. (Read 1267 times)
Stephen F.
Posts: 4

on: September 09, 2009, 07:16:10 AM

Here's mine. Almost identical to Rhode Island Steve's!

#import <Foundation/Foundation.h>

@interface Calculator: NSObject
   double accumulator;
-(void) add:      (double) vaule;
-(void) subtract:   (double) vaule;
-(void) multiply:   (double) vaule;
-(void) divide:      (double) vaule;

@implementation Calculator
-(void)   clear            {accumulator = 0;}
-(double) accumulator      {return accumulator;}
-(void)   setAccumulator:   (double) value {accumulator =  value;}
-(void)   set:            (double) value {accumulator =  value;}
-(void)   add:            (double) value {accumulator += value;}
-(void)   subtract:         (double) value {accumulator -= value;}
-(void)   multiply:         (double) value {accumulator *= value;}
-(void)   divide:         (double) value {accumulator /= value;}

int main (int argc, const char * argv[])
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   double enteredValue;
   char operator;
   Calculator *deskCalc = [[Calculator alloc] init];
    NSLog(@"Begin Calculations");

   do {
      // Ideally, here we could read in a string and
      // convert it to a double, as there is no way
      // to validate that a number was entered.
      scanf("%lf %c", &enteredValue, &operator);
      switch (operator)
         case '+':
            [deskCalc add: enteredValue];
         case '-':
            [deskCalc subtract: enteredValue];
         case '*':
         case 'x':
            [deskCalc multiply: enteredValue];
         case '/':
            if (enteredValue == 0)
               NSLog (@"I refuse to divide by zero.");
               [deskCalc divide: enteredValue];
         case 'S':
         case 's':
            [deskCalc set: enteredValue];
         case 'e':
         case 'E':
            NSLog (@"I'm sorry Dave, but I'm afraid I can't do that.");
      NSLog(@"= %.6f", [deskCalc accumulator]);
   } while (operator != 'E' && operator != 'e');
   NSLog(@"End of Calculations");
   [deskCalc release];
    [pool drain];
    return 0;
Hero Member
Posts: 3114

Reply #1 on: September 10, 2009, 11:03:09 AM

scanf is not a robust function for reading and parsing data; however, it's an easy-to-use function to suit our purposes.  You can test the return value of scanf.  If it's 2, then it read and stored 2 values.


Steve Kochan
Pages: [1] Go Up
Jump to:

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.