Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 18, 2018, 09:25:35 PM *
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: Exercise 4  (Read 1526 times)
kotyara
Newbie
*
Posts: 15


« on: December 14, 2014, 01:26:48 PM »

//  Calculator.h

#import <Foundation/Foundation.h>

@interface Calculator : NSObject
@property double accumulator;
-(void) clear;
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end

//  Calculator.m

#import "Calculator.h"

@implementation Calculator
@synthesize accumulator;
-(void)clear{
    accumulator = 0;
}
-(void)add:(double)value{
    accumulator += value;
    NSLog(@"%g", accumulator);
}
-(void)subtract:(double)value{
    accumulator -= value;
    NSLog(@"%g", accumulator);
}
-(void)multiply:(double)value{
    accumulator *= value;
    NSLog(@"%g", accumulator);
}
-(void)divide:(double)value{
    accumulator /= value;
    NSLog(@"%g", accumulator);
}
@end

//  Calculator+Trig.h

#import "Calculator.h"

@interface Calculator (Trig)
-(double) sin;
-(double) cos;
-(double) tan;
@end

//  Calculator+Trig.m

#import "Calculator+Trig.h"

@implementation Calculator (Trig)
-(double)sin{
    double result;
    result = sin(self.accumulator);
    NSLog(@"%g", result);
    return result;
}
-(double)cos{
    double result;
    result = cos(self.accumulator);
    NSLog(@"%g", result);
    return result;
}
-(double)tan{
    double result;
    result = tan(self.accumulator);
    NSLog(@"%g", result);
    return result;
}
@end

//  main.m

#import <Foundation/Foundation.h>
#import "Calculator.h"
#import "Calculator+Trig.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Calculator * myCalc = [[Calculator alloc]init];
        [myCalc add: 12];
        [myCalc sin];
        [myCalc cos];
        [myCalc tan];
    }
    return 0;
}

output:

2014-12-14 23:21:38.233 q04[8479:378729] -0.536573
2014-12-14 23:21:38.233 q04[8479:378729] 0.843854
2014-12-14 23:21:38.233 q04[8479:378729] -0.63586
Program ended with exit code: 0
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.