본문 바로가기

C.O.M.P.U.T.E.R/O.b.j.e.c.t.i.v.e.-.C

[0] |Objective-C| 숫자정렬 프로그램 .m 파일 (2/2)

#import "cSort.h"

@implementation cSort

- (id)init

{

    self = [super init];

    Count = 0;

    UpDown = 0;

    NSLog(@"입력 배열 개수 : ");

    scanf("%d", &Count);

    Num = (int *)malloc(sizeof(int)*Count);

    int i = 0;

    while(i < Count)

    {

        NSLog(@"%d 번째 숫자 : ", i + 1);

        scanf("%d", &Num[i]); //num + loop);

        i++;

    }

    return self;

}

-(void) _sort

{

    NSLog(@"[오름 차순 : 1] [내림차순 : 2] : ");

    scanf("%d", &UpDown);

    switch(UpDown)

    {

        case 1:

            for(int x = 0; x < Count - 1 ; x++)

            {

                for(int y = x + 1 ; y < Count ; y++)

                {

                    if(Num[x] > Num[y])

                    {

                        [self _swap:&Num[x]:&Num[y]];

                    }

                }

            }

            break;

        case 2:

            for(int x = 0; x < Count - 1 ; x++)

            {

                for(int y = x + 1 ; y < Count ; y++)

                {

                    if(Num[x] < Num[y])

                    {

                        [self _swap:&Num[x]:&Num[y]];

                    }

                }

            }

            break;

    }

}

-(void) _swap:(int *)n1:(int *)n2

{

    int temp = *n1;

    *n1 = *n2;

    *n2 = temp;

}

-(void) _print

{

    for(int i = 0 ; i < Count ; i++)

        NSLog(@"%d\n", Num[i]);

}

- (void)dealloc

{

    free(Num);    

    [super dealloc];

}

@end


주석이 없어서 보기 힘들 수도 있습니다. 만... 짧으니까 참고 정도만 해주세요. 저도 초보입니다... 주석보다 타자치기 바빠서... 주석도 넣는 연습을 해야 겠네요.