336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
#import <Foundation/Foundation.h>
// 간단한 복소수 클래스
@interface TapitoComplex : NSObject
{
float fImaginary; // 허수부
float fReal; // 실수부
}
- (id) init;
- (oneway void) release;
- (float) getImaginary;
- (void) setImaginary : (float) value;
- (float) getReal;
- (void) setReal : (float) value;
- (TapitoComplex *) addComplex : (TapitoComplex *) value;
- (TapitoComplex *) mulComplex : (TapitoComplex *) value;
- (NSString *) toString;
@end
@implementation TapitoComplex
// 인스턴스 초기화
- (id) init
{
self = [super init]; // 상위 클래스의 메서드 호출
if(self)
{
[self setImaginary : 0.0f]; // 현재 클래스의 필드 초기 값 지정
[self setReal : 0.0f];
}
return self;
}
// 인스턴스 해제
- (oneway void) release
{
NSLog(@"TapitoComplex: released.");
[super release]; // 상위 클래스의 메서드 호출
}
// 허수부 가져오기
- (float) getImaginary
{
return fImaginary;
}
// 허수부 설정하기
- (void) setImaginary : (float) value
{
fImaginary = value;
}
// 실수부 가져오기
- (float) getReal
{
return fReal;
}
// 실수부 설정하기
- (void) setReal : (float) value
{
fReal = value;
}
// 복소수 덧셈 결과를 가져온다.
- (TapitoComplex *) addComplex : (TapitoComplex *) value
{
TapitoComplex * ret = [[TapitoComplex alloc] init];
[ret setReal : ([self getReal] + [value getReal])];
[ret setImaginary : ([self getImaginary] + [value getImaginary])];
return ret;
}
// 복소수 뺄셈 결과를 가져온다.
- (TapitoComplex *) mulComplex : (TapitoComplex *) value
{
TapitoComplex * ret = [[TapitoComplex alloc] init];
[ret setReal : ([self getReal] * [value getReal] + [self getImaginary] * [value getImaginary] * -1)];
[ret setImaginary : ([self getImaginary] * [value getReal] + [self getReal] * [value getImaginary])];
return ret;
}
// 이 인스턴스가 갖는 복소수를 문자열로 변환
- (NSString *) toString
{
// NSString initWithFormat는 C언어의 sprintf함수와 같은 역할을 한다.
return [[NSString alloc] initWithFormat: @"%c%f%c%fi", ([self getReal] < 0) ? '-' : '+', [self getReal], ([self getImaginary] < 0) ? '-' : '+', [self getImaginary]];
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// 인스턴스의 할당과 초기화를 동시에 해결. alloc와 init는 NSObject에 기본적으로 구현되어 있음.
TapitoComplex * tc1 = [[TapitoComplex alloc] init];
TapitoComplex * tc2 = [[TapitoComplex alloc] init];
TapitoComplex * tc3 = [[TapitoComplex alloc] init];
// printf 대신에 NSLog를 사용. %@은 NSString형 문자열을 치환함.
NSLog(@"Initialized ... \n");
NSLog(@"\ntc1 : %@, tc2 : %@, tc3 : %@", [tc1 toString], [tc2 toString], [tc3 toString]);
[tc1 setReal: 3.14];
[tc1 setImaginary: 1.41];
[tc2 setReal: 5.11];
[tc2 setImaginary: 99.99];
NSLog(@"\ntc1 : %@, tc2 : %@, tc3 : %@", [tc1 toString], [tc2 toString], [tc3 toString]);
tc3 = [tc1 addComplex: tc2];
NSLog(@"tc1 + tc2 == tc3");
NSLog(@"\ntc1 : %@, tc2 : %@, tc3 : %@", [tc1 toString], [tc2 toString], [tc3 toString]);
tc3 = [tc1 mulComplex: tc2];
NSLog(@"tc1 * tc2 == tc3");
NSLog(@"\ntc1 : %@, tc2 : %@, tc3 : %@", [tc1 toString], [tc2 toString], [tc3 toString]);
[tc1 release];
[tc2 release];
[tc3 release];
[pool drain];
return 0;
}
C++도 아닌것이 C도 아닌 것이 여전히 낯설기만 하다... Objective-C
물론 해킨토시에서 실행해 본 화면.
'자작 프로그램' 카테고리의 다른 글
| Hell, World! 버전 1.0 (0) | 2014.09.14 |
|---|---|
| 맥 OS용 만년달력 프로그램 (0) | 2014.05.12 |
| 자바로 구현한 만년달력 프로그램 (8) | 2012.02.14 |
| 자바로 구현한 로또 번호 생성 프로그램 (0) | 2012.02.03 |
| 완전 심플 메모장 프로그램 (옴니아 Windows CE용) (0) | 2011.10.09 |