Skip to content

Instantly share code, notes, and snippets.

@voxxit
Created July 17, 2012 19:50
Show Gist options
  • Save voxxit/3131588 to your computer and use it in GitHub Desktop.
Save voxxit/3131588 to your computer and use it in GitHub Desktop.
Objective-C app #2
// Implement a Calculator class
#import <Foundation/Foundation.h>
@interface Calculator : NSObject
// 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 {
double accumulator;
}
-(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[])
{
@autoreleasepool {
Calculator *calc = [[Calculator alloc] init];
[calc setAccumulator: 100.0];
[calc add: 200];
[calc divide: 15];
[calc subtract: 10];
[calc multiply: 5];
NSLog(@"The result is: %g", [calc accumulator]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment