Amazon.com Widgets Answer to Ch 6 Q3 - using switch.
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 31, 2014, 02:00:16 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 to Ch 6 Q3 - using switch.
Pages: [1] Go Down
Print
Author Topic: Answer to Ch 6 Q3 - using switch. (Read 861 times)
yeaweeyum
Newbie
*
Posts: 9


Email




on: September 10, 2009, 12:46:41 PM

Code: (Objective-C)
#import <Foundation/Foundation.h>

//interface section

@interface Calculator : NSObject
{
double accumulator;
}

-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;
-(void) endOfCalc;

-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;

@end

//implementation section

@implementation Calculator

-(void) setAccumulator: (double) value
{
accumulator = value;
}

-(void) clear
{
accumulator = 0;
}

-(double) accumulator
{
return accumulator;
}

-(void) endOfCalc
{
NSLog(@"Job Done! Final answer is = %f", accumulator);
}

-(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

//program section

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
double value;
char op;
Calculator *deskCalc =[[Calculator alloc] init];

do
{
NSLog (@"Type value:");
scanf ("%lf", &value);

NSLog (@"Type operator:");
scanf (" %c", &op);

switch (op)
{
case 'S':
[deskCalc setAccumulator: value];
break;

case '+':
[deskCalc add: value];
break;

case '-':
[deskCalc subtract: value];
break;

case '*':
[deskCalc multiply: value];
break;

case '/':
if (value !=0)
{
[deskCalc divide: value];
}
else {
NSLog(@"DO NOT DIVIDE BY ZERO!! COME ON!!");
}

break;

case 'E':
[deskCalc endOfCalc];
break;

default:
NSLog(@"Unknown Operator");
break;
}
}
while (op !='E');



    [pool drain];
    return 0;
}
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.