Welcome, Guest. Please login or register.
Did you miss your activation email?
September 09, 2010, 09:43:33 AM
Home Help Search chat Login Register   
News:   If you like the book and forum please  write a review on Amazon   Recorded lessons available for iPad viewing and download....in process! Consulting and onsite training available!   If you're running Xcode 3.2 (or later), see this post   Follow me on Twitter  @skochan  Forum Guests: register to download (or see) attachments.    Join our mailing list.


+  Official Forum for Programming in Objective-C 2.0 (the iPhone Programming Language) - Stephen Kochan
|-+  Introduction to iPhone Programming Webcast Series (March 2 - May 27, 2010)
| |-+  Part I - Programming in Objective-C 2.0
| | |-+  Chapter 6, Exercise 4
| | | |-+  Chapter 6, Exercise 4
« previous next »
Pages: [1] Go Down Print
Author Topic: Chapter 6, Exercise 4  (Read 134 times)
Shawn
Newbie
*
Posts: 19


« on: March 09, 2010, 06:14:54 PM »

I worked ahead on this over the weekend. Here's my solution:

Calculator.h

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

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) add: (double) value
{
accumulator += value;
}

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

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

-(void) divide: (double) value
{
accumulator /= value;
}
@end

Ex6-4.m

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

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

do {
NSLog (@"Type in your expression in the format: number operator");
scanf ("%lf %c", &value, &operator);

switch (operator) {
case '+':
[deskCalc add: value];
NSLog (@"= %f", [deskCalc accumulator]);
break;
case '-':
[deskCalc subtract: value];
NSLog (@"= %f", [deskCalc accumulator]);
break;
case '*':
[deskCalc multiply: value];
NSLog (@"= %f", [deskCalc accumulator]);
break;
case '/':
[deskCalc divide: value];
NSLog (@"= %f", [deskCalc accumulator]);
break;
case 'S':
[deskCalc setAccumulator: value];
NSLog (@"= %f", [deskCalc accumulator]);
break;
case 'E':
NSLog (@"End of program.");
NSLog (@"= %f", [deskCalc accumulator]);
break;
default:
NSLog (@"Unknown operator.");
break;
}
}
while (operator != 'E');

[deskCalc release];
    [pool drain];
    return 0;
}

Results

2010-03-09 20:13:22.075 Ex6-4[1062:a0f] Type in your expression in the format: number operator
10 S
2010-03-09 20:13:59.321 Ex6-4[1062:a0f] = 10.000000
2010-03-09 20:13:59.322 Ex6-4[1062:a0f] Type in your expression in the format: number operator
2 /
2010-03-09 20:14:06.929 Ex6-4[1062:a0f] = 5.000000
2010-03-09 20:14:06.929 Ex6-4[1062:a0f] Type in your expression in the format: number operator
55 -
2010-03-09 20:14:11.513 Ex6-4[1062:a0f] = -50.000000
2010-03-09 20:14:11.513 Ex6-4[1062:a0f] Type in your expression in the format: number operator
100.25 S
2010-03-09 20:14:18.832 Ex6-4[1062:a0f] = 100.250000
2010-03-09 20:14:18.833 Ex6-4[1062:a0f] Type in your expression in the format: number operator
4 *
2010-03-09 20:14:22.944 Ex6-4[1062:a0f] = 401.000000
2010-03-09 20:14:22.945 Ex6-4[1062:a0f] Type in your expression in the format: number operator
0 E
2010-03-09 20:14:27.936 Ex6-4[1062:a0f] End of program.
2010-03-09 20:14:27.937 Ex6-4[1062:a0f] = 401.000000

Logged
skochan
Administrator
Hero Member
*****
Posts: 2250



« Reply #1 on: March 12, 2010, 03:50:22 PM »

Good solution!  Nice and clean.

Cheers,

Steve
Logged
Pages: [1] Go Up Print 
« previous next »
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.