Amazon.com Widgets Answer from Stephen F.
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 24, 2014, 12:19:18 PM
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
Print
Author Topic: Answer from Stephen F. (Read 900 times)
Stephen F.
Newbie
*
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;
@end

@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;}
@end


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];
            break;
         case '-':
            [deskCalc subtract: enteredValue];
            break;
         case '*':
         case 'x':
            [deskCalc multiply: enteredValue];
            break;
         case '/':
            if (enteredValue == 0)
               NSLog (@"I refuse to divide by zero.");
            else
               [deskCalc divide: enteredValue];
            break;
         case 'S':
         case 's':
            [deskCalc set: enteredValue];
            break;
         case 'e':
         case 'E':
            break;
         default:
            NSLog (@"I'm sorry Dave, but I'm afraid I can't do that.");
            break;
      }
      NSLog(@"= %.6f", [deskCalc accumulator]);
      
   } while (operator != 'E' && operator != 'e');
      
   NSLog(@"End of Calculations");
   
   [deskCalc release];
    [pool drain];
    return 0;
}
Logged
skochan
Administrator
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.

Cheers,

Steve Kochan
Logged
Pages: [1] Go Up
Print
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.