Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
September 24, 2017, 07:01:24 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Stuart's Answer  (Read 1525 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:  

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 ゥ 2009 classroomM.com. All rights reserved.