<<4장 데이터 형과 표현식>>
Calculator Class
#import <Foundation/Foundation.h>
@interface Calculator : NSObject
{
double Accumulator; // 결과값 or 중간 결과값
}
- (void) setAccumulator; // 입력값
- (void) clear; // 초기화
- (void) accumulator; //결과값
- (void) add : (double) value; // 연산자
- (void) subtract : (double) value;
- (void) multiply : (double) value;
- (void) divide : (double) value;
@end
@implementation Calculator
- (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, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Calculator *deskCalc;
deskCalc = [[Calculator alloc]init];
[deskCalc clear];
[deskCalc setAccumulator : 100.0];
[deskCalc add : 200.];
[deskCalc divide : 15.0];
[deskCalc subtract : 10.0];
[deskCalc multiply : 5];
NSLog(@"The result is %g",[deskCalc accumulator]);
[deskCalc release];
[pool drain];
return 0;
}
------------------------------------------------------------------------------
<<5장 프로그램 반복문>>
<1~8까지 더하는 함수>
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool *pool =[[NsAutoreleasePool alloc]init];
int triangularNumber;
triangularNumber = 1+2+3+4+5+6+7+8;
NSLog(@"The eighth triangular number is %i",triangularNumber);
[pool drain];
return 0;
}
<1~200까지 더하는 함수>
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool *pool =[[NsAutoreleasePool alloc]init];
int triangularNumber, n;
triangularNumber=0;
for(n=1; n <=200; n=n+1) // C언어의 for문과의 차이점 : 중괄호를 쓰지 않는다.
// 200보다 같거나 작을 때까지 연산한다는 의미입니다.
// n<201이라고 써도 무방하다.
triangularNumber +=n;
NSLog(@"The 200th triangular number is %i",triangularNumber);
[pool drain];
return 0;
}
<유사한 예제>
for(n=1; n <=10; ++n){
triangularNumber +=n;
NSLog(@"",변수); //프린트 함수가 있을때는 중괄호가 들어간다.
}
<최대공약수를 찾는 예제>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool =[[NsAutoreleasePool alloc]init];
unsigned int u, v, temp;
NSLog (@"Type in two nonnegative integers");
scanf("%u %u", u,v);
while(v!=0)
{
temp= u%v;
u=v;
v=temp;
}
NSLog(@"Their greatest common divisor is %u", u);
[pool drain];
return 0;
}
그 외에 do~while 문, break문, continue문 등이 있다.
------------------------------------------------------------------------------
<<6장 의사결정하기>>
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
{
int numerator;
int denominator;
}
- (void) print;
- (void) setNumerator : (int) n;
- (void) set Denominator : (int) d;
- (int) numerator;
- (int) denominator;
- (double) convertToNum;
@implementation Fraction
- (void) print
{
NSLog(@"%i%i", numerator, denominator);
}
- (void) setNumerator : (int) n
{
numerator = n;
}
- (void) setDenominator : (int) d
{
denominator = d;
}
- (int) numerator
{
return numerator;
}
- (int) denominator
{
return denominator;
}
- (double) convertToNum
{
if(denominator!=0)
return (double) numerator / denominator;
else
return 0.0;
}
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
Fraction *aFraction = [[Fraction alloc]init];
Fraction *bFraction = [[Fraction alloc]init];
[aFraction setNumerator : 1];
[aFraction setDenominator : 4];
[aFraction print];
NSLog(@"=");
NSLog(@"%g",[aFraction converToNum]);
[bFraction print];
NSLog(@"=");
NSLog(@"%g",[bFraction converToNum]);
[aFraction release];
[bFraction release];
[pool drain];
return 0;
}
// <짝홀수 알아보는 프로그램>
int main(int argc, char *argv[])
{
NSAutoreleasePool = [[NSAutoreleasePool alloc]init];
int number_to_test, remainder;
NSLog(@"Enter the number which you want to test : ");
scanf("%i",&number_to_test);
remainder = number_to_test % 2;
if(remainder==0)
NSLog("The number is even");
if(remainder != 0) // else 문 하나로 대체해도 가능하다.
NSLog("The number is odd");
[pool drain];
return 0;
}
출처 : Programming in Object-C 2.0 / 스티븐 코찬 지음, 박세헌 옮김 / 인사이트 출판