Amazon.com Widgets Melinda's Answer Exercise 6.4
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 19, 2014, 03:11:30 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
| | | |-+ Melinda's Answer Exercise 6.4
Pages: [1] Go Down
Print
Author Topic: Melinda's Answer Exercise 6.4 (Read 857 times)
melj247
Newbie
*
Posts: 5






on: September 10, 2009, 06:35:02 PM

Calculator.h:
Code: (Objective-C)
@interface Calculator: NSObject
{
double accumulator;
}

// acumulator methods

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


// arithmetic methods

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

@end



Calculator.m:
Code: (Objective-C)
#import "Calculator.h"


@implementation Calculator

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

-(void) clear
{
accumulator = 0;
}

-(double) accumulator
{
return accumulator;
}

-(void) S: (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
{
if (value == 0)
NSLog(@"Cannot divide by 0");
else accumulator /= value;
}

-(void) E: (double) value
{
NSLog(@" = %i", accumulator);
}


@end



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

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

double value1 = 0;
char operator = 0;
Calculator *deskCalc = [[Calculator alloc] init];



NSLog(@"Begin Calculations ");

while (operator != 'e')
{

NSLog(@"Enter Number and Operator:");
scanf("%lf %c", &value1, &operator);


switch (operator)
{
case 'S':
case 's':
[deskCalc setAccumulator: value1];
break;

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

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

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

case '/':
[deskCalc divide: value1];
break;

case 'e':
case 'E':
NSLog(@"Program End");
break;

default:
NSLog(@"That is not a valid operator, please re-enter:");
break;
}

NSLog(@" = %f", [deskCalc accumulator]);
}



[deskCalc release];

    [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.