긴 자리 정수(BigInt) 게산이 가능한 ObjectiveC 언어로 작성된 소스를 작성하고 컴파일한다.
컴파일은 Mac OS X 10.6.x 의 명령줄(커맨드 라인)에서 gcc 로 한다.
우선 http://www.santsys.com/code/display?folder=Objective-C&file=BigInt.h 에서 헤더 파일 BigInt.h 및구현 파일 BigInt.m 을 구해야 한다.
그리고 BigInt.h 의 째 줄
main.m 파일의 내용
/*
[x initWithInt: 100];
컴파일하기
$ gcc -fobjc-gc-only -framework Foundation BigInt.m main.m -o main -std=gnu99
실행하기
$ ./main
컴파일은 Mac OS X 10.6.x 의 명령줄(커맨드 라인)에서 gcc 로 한다.
우선 http://www.santsys.com/code/display?folder=Objective-C&file=BigInt.h 에서 헤더 파일 BigInt.h 및구현 파일 BigInt.m 을 구해야 한다.
그리고 BigInt.h 의 째 줄
#define MAX_LENGTH 70
을
#define MAX_LENGTH 640
로 수정한다.
아래의 소스는 300^2048 (300의 2048승) 까지 계산이 가능함을 보여준다.
아래의 소스는 300^2048 (300의 2048승) 까지 계산이 가능함을 보여준다.
main.m 파일의 내용
/*
* Filename: main.m
*
* Compile: gcc -fobjc-gc-only -framework Foundation BigInt.m main.m -o main -std=c99
* Or gcc -fobjc-gc-only -framework Foundation BigInt.m main.m -o main -std=gnu99
*
* Date: 2011. 8. 4.
* Author: phk
*/
#import <Foundation/Foundation.h>
#import <Foundation/NSString.h>
#import <stdio.h>
#import "BigInt.h"
int main( int argc, const char *argv[] ) {
// create a new instance
BigInt *x = [[BigInt alloc] initWithInt: 100];
BigInt *y = [[BigInt alloc] initWithInt: 200];
BigInt *z = [[BigInt alloc] init];
[x initWithInt: 100];
[y initWithInt: 200];
x = [x add: y];
z = [z add: x];
z = [z multiply: z]; // z = 300^2
z = [z multiply: z]; // z = 300^4
z = [z multiply: z]; // z = 300^8
z = [z multiply: z]; // z = 300^16
z = [z multiply: z]; // z = 300^32
z = [z multiply: z]; // z = 300^64
z = [z multiply: z]; // z = 300^128
z = [z multiply: z]; // z = 300^256
z = [z multiply: z]; // z = 300^512
z = [z multiply: z]; // z = 300^1024
z = [z multiply: z]; // z = 300^2048
// z = [z multiply: z]; // z = 300^4096
// print it
NSLog( @"x = %@\n", [x toStringWithRadix: 10] );
NSLog( @"z = %@\n", [z toStringWithRadix: 10] );
NSLog( @"The digits of z are %d\n", [[z toStringWithRadix: 10] length] );
// free memory
[z release];
[y release];
[x release];
return 0;
}
컴파일하기
컴파일은 세개의 파일 BigInt.h, BigInt.m, main.m 을 모두 동일한 폴더에 저장하고, 그 폴더에서 다음의 gcc 컴파일 명령을 내린다.
$ gcc -fobjc-gc-only -framework Foundation BigInt.m main.m -o main -std=c99
또는$ gcc -fobjc-gc-only -framework Foundation BigInt.m main.m -o main -std=gnu99
위에서 옵션 -std=c99 (또는 -std=gnu99)를 사용한 이유는 구현 파일 BigInt.m 에
for (int i = 0; ... ; ... ) {
..........
}
처럼 int i 의 선언이 구문 중간 중간에 있기 때문이다.
int i;
for (i = 0; ... ; ... ) {
..........
}
처럼 int i 선언을 밖으로 뽑아내도 되지만 그러자면 구현 파일 BigInt.m 을 여러 곳 수정해야 하기 때문에 번거롭다. 이런 때는 컴파일 옵션 -std=c99(또는 -std-gnu99) 을 사용하는 것이 편리하다.
만일 이 옵션을 생략하여 명령
$ gcc -fobjc-gc-only -framework Foundation BigInt.m main.m -o main
으로 컴파일하면 에러 메세지
으로 컴파일하면 에러 메세지
BigInt.m: In function ‘-[BigInt initWithBigInt:]’:
BigInt.m:129: error: ‘for’ loop initial declaration used outside C99 mode
...........................
.........................
.........................
를 만난다.
실행하기
$ ./main
2011-08-04 20:42:51.809 main[5267:903] x = 300
2011-08-04 20:42:52.077 main[5267:903] z = 13942147270623679146873528796701570723260623211399818675976228428320324826739932742348
50024760182945480272076581245800069562691222247613756153589976812373494337854015847291
06693395255281491169142017082115307947771801954704567433211464560639140944673635364155
01669338920161359403898468691617862370257835219063625177603974210138485147431108373510
28779931312121969626954646544698167192029706192523430266460141324855483079358472609004
41555694943498191955672798596377459777392636575523873495086339994375985976543710570091
43823325722842052959455745735960972363978751956762143499995484338210048940092499596190
40232593243010479659177831242751628805145427355281522289274838413728448102554703994718
41348890379437250393401562476664019972812975660227449988177768002661436897984534184331
07995039930675681806131094808453498537466120327843659034923143107156940238942976892218
04858389687649685235712612880380643104425852839332564045727486066401513073768002507254
27496845607662821346676098703361000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The digits of z are 5074
50024760182945480272076581245800069562691222247613756153589976812373494337854015847291
06693395255281491169142017082115307947771801954704567433211464560639140944673635364155
01669338920161359403898468691617862370257835219063625177603974210138485147431108373510
28779931312121969626954646544698167192029706192523430266460141324855483079358472609004
41555694943498191955672798596377459777392636575523873495086339994375985976543710570091
43823325722842052959455745735960972363978751956762143499995484338210048940092499596190
40232593243010479659177831242751628805145427355281522289274838413728448102554703994718
41348890379437250393401562476664019972812975660227449988177768002661436897984534184331
07995039930675681806131094808453498537466120327843659034923143107156940238942976892218
04858389687649685235712612880380643104425852839332564045727486066401513073768002507254
27496845607662821346676098703361000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The digits of z are 5074
'프로그래밍 > Objective-C' 카테고리의 다른 글
80컬럼 컨솔에 19단표 출력하기 예제 for Objective-C (0) | 2012.04.30 |
---|---|
(최대공약수 구하기) while... 반복문 예제 for Objective-C (0) | 2012.04.30 |
if...else... 조건문 사용 예제 for Objective-C (0) | 2012.04.29 |
명령행 인자 처리 예제 for Objective-C (0) | 2012.04.29 |
윈도우의 Cygwin 에서 Objective-C 언어로 작성된 HelloWorld 예제 (0) | 2011.08.04 |
Posted by Scripter