2013년 2월 1일 금요일

Objective-C Chapter 4.5


// 4.5 연습문제 8번, 9번, 10번

#import <Foundation/Foundation.h>

@interface Calculator : NSObject
// 누산기 매서드
-(void) setAccumulator : (double) value;
-(void) clear;
-(double) accumulator;
// 산술 연산 매서드
-(double) add : (double) value;
-(double) subtract : (double) value;
-(double) multiply : (double) value;
-(double) divide : (double) value;

// 연습문제 9번 추가 메서드
-(double) changeSign;  // 누산기의 부호를 바꿈
-(double) reciprocal;  // 1 / 누산기
-(double) xSquared;    // 누산기를 제곱

// 연습문제 10번 추가 메서드
-(double) memoryClear;  // 메모리를 저장한다
-(double) memoryStore;  // 누산기 값을 메모리에 설정한다
-(double) memoryRecall;  // 메모리 값을 누산기에 설정한다
-(double) memoryAdd : (double) value;  // 메모리에 value 더한다
-(double) memorySubtract : (double) value;  // value 메모리에서 뺀다
@end

@implementation Calculator
{
    double accumulator, memory; // memory 연습문제 10 추가 변수
}
-(void) setAccumulator : (double) value
{
    accumulator = value;
}
-(void) clear
{
    accumulator = 0;
}
-(double) accumulator
{
    return accumulator;
}
-(double) add : (double) value
{
    accumulator += value;
    return accumulator;
}
-(double) subtract : (double) value
{
    accumulator -= value;
    return accumulator;
}
-(double) multiply : (double) value
{
    accumulator *= value;
    return accumulator;
}
-(double) divide : (double) value
{
    accumulator /= value;
    return accumulator;
}

// 연습문제 9번
-(double) changeSign
{
    return accumulator = - accumulator;
}
-(double) reciprocal
{
    return accumulator = 1.0 / accumulator;
}
-(double) xSquared
{
    return accumulator *= accumulator;
}

// 연습문제 10번
-(double) memoryClear
{
    return memory = 0;
}
-(double) memoryStore
{
    return memory = accumulator;
}
-(double) memoryRecall
{
    accumulator = memory;
    return memory; // 주의! 반환값은 메모리!
}
-(double) memoryAdd : (double) value
{
    return memory += value;
}
-(double) memorySubtract : (double) value
{
    return memory -= value;
}
@end

int main (int argc, char *argv[])
{
    @autoreleasepool {
        Calculator *deskCalc = [[Calculator alloc] init];
        [deskCalc setAccumulator : 100.0];
        NSLog(@"The setAccumulator result is %g", [deskCalc accumulator]);
        [deskCalc add : 200.0];
        NSLog(@"The add result is %g", [deskCalc accumulator]);
        [deskCalc divide : 15.0];
        NSLog(@"The divide result is %g", [deskCalc accumulator]);
        [deskCalc subtract : 10.0];
        NSLog(@"The subtract result is %g", [deskCalc accumulator]);
        [deskCalc multiply : 5];
        NSLog(@"The multiply result is %g", [deskCalc accumulator]);
        
        [deskCalc clear];
        // 연습문제 9번
        [deskCalc setAccumulator : -0.2];
        [deskCalc changeSign];
        NSLog(@"changeSign method makes accumulator %g", [deskCalc accumulator]);
        [deskCalc reciprocal];
        NSLog(@"reciprocal method makes accumulator %g", [deskCalc accumulator]);
        [deskCalc xSquared];
        NSLog(@"xSquared method makes accumulator %g", [deskCalc accumulator]);
        
        [deskCalc clear];
        // 연습문제 10번
        [deskCalc setAccumulator : 10];
        NSLog(@"[deskCalc setAccumulator : 10] method > accumulator : %g, memory : %g", [deskCalc accumulator], [deskCalc memoryClear]);
        NSLog(@"[deskCalc memoryStore] method > accumulator : %g, memory : %g", [deskCalc accumulator], [deskCalc memoryStore]);
        NSLog(@"[deskCalc memoryAdd : 5] method > accumulator : %g, memory : %g", [deskCalc accumulator], [deskCalc memoryAdd : 5]);
        NSLog(@"[deskCalc memorySubtract : 2] method > accumulator : %g, memory : %g", [deskCalc accumulator], [deskCalc memorySubtract : 2]);
        [deskCalc memoryRecall];  // memoryRecall 메서드기 때문에 미리 리콜을 해줘서 accumulator 를 변환해주고 표시해야함.
        NSLog(@"[deskCalc memoryRecall] method > accumulator : %g, memory : %g", [deskCalc accumulator], [deskCalc memoryRecall]);
    }
    return 0;
}

댓글 없음:

댓글 쓰기