Amazon.com Widgets Stuart's Answer
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 28, 2014, 11:44:22 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
| | | |-+ Stuart's Answer
Pages: [1] Go Down
Print
Author Topic: Stuart's Answer (Read 934 times)
Hutch
Newbie
*
Posts: 7






on: September 14, 2009, 11:29:21 PM

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

@interface Accumulator : NSObject
{
double accumulated;
}

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

@end

@implementation Accumulator

-(void) add: (double) value
{
accumulated += value;
}

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

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

-(void) divide: (double) value
{
accumulated /= value;
}
-(void) setAccumulator: (double) value
{
accumulated = value;
}
-(double) accumulated
{
return accumulated;
}

@end


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

Accumulator *myAccumulator = [[Accumulator alloc]init];
char op;
double number;

scanf("%lf %c", &number, &op);

while (op != 'E') {
scanf("%lf %c", &number, &op);
switch (op) {
case '+':
[myAccumulator add: number];
break;

case '-':
[myAccumulator subtract: number];
break;

case 'x':
case 'X':
case '*':
[myAccumulator multiply: number];
break;

case '/':
if (number == 0)
NSLog(@"Can not divide by zero.");
else
[myAccumulator divide: number];
break;

case 's':
case 'S':
[myAccumulator setAccumulator: number];

break;

default:
NSLog(@"Unknown operator.");
break;
}
NSLog(@"= %g", [myAccumulator accumulated]);
}
NSLog(@"End of program");

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

Thoughts on where I could improve?
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.