[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