Amazon.com Widgets Ben's solution
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 24, 2014, 07:44:07 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
| | | |-+ Ben's solution
Pages: [1] Go Down
Print
Author Topic: Ben's solution (Read 1080 times)
webwrx
Newbie
*
Posts: 41






on: September 09, 2009, 06:45:57 PM

My solution -

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

@interface Calculator: NSObject
{
double accumulator;
}

// accumulator methods
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;

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

@end


@implementation Calculator

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

-(void) clear
{
accumulator = 0;
}

-(double) accumulator
{
return 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


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

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

NSLog(@"Enter Number then Operator (+ - * / S E):");

do {

scanf("%lf %c", &value, &operator);

if (operator == 'S' || operator == 's')
[deskCalc setAccumulator: value];
else if (operator == '+')
[deskCalc add: value];
else if (operator == '-')
[deskCalc subtract: value];
else if (operator == '*')
[deskCalc multiply: value];
else if (operator == '/')
if (value == 0)
NSLog (@"Division by zero! Try Again.");
else
[deskCalc divide: value];
else if (operator == 'E' || operator == 'e')
NSLog (@"End of Calculations.");
else
NSLog (@"Unknown Operator! Try Again.");

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

} while (operator != 'E' || operator != 'e');


[deskCalc release];

    [pool drain];
    return 0;
}


Sample output -

Code: (Objective-C)
2009-09-10 11:41:56.721 exer6-4[47807:10b] Enter Number then Operator (+ - * / S E):
4 S
2009-09-10 11:42:01.535 exer6-4[47807:10b] = 4.000000
23.4 *
2009-09-10 11:42:07.207 exer6-4[47807:10b] = 93.600000
-3 -
2009-09-10 11:42:17.616 exer6-4[47807:10b] = 96.600000
3 -
2009-09-10 11:42:25.688 exer6-4[47807:10b] = 93.600000
33 ^
2009-09-10 11:42:36.058 exer6-4[47807:10b] Unknown Operator! Try Again.
2009-09-10 11:42:36.059 exer6-4[47807:10b] = 93.600000
8 s
2009-09-10 11:42:47.352 exer6-4[47807:10b] = 8.000000
-12 +
2009-09-10 11:42:57.978 exer6-4[47807:10b] = -4.000000
0 e
2009-09-10 11:43:05.785 exer6-4[47807:10b] End of Calculations.
2009-09-10 11:43:05.785 exer6-4[47807:10b] = -4.000000
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: September 10, 2009, 10:59:08 AM

Looks good Ben!  As an extra exercise you might want to change the if statement to a switch.

Cheers,

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