본문 바로가기

C.O.M.P.U.T.E.R/C.+.+

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

#include "Csort.h"


void
cSort::_init()

{

    Sort.Count = 0;

    Sort.UpDown = 0;

    cout << "입력 배열 개수 : ";

    cin >> Sort.Count;

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

    int i = 0;

    while(i < Sort.Count)

    {

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

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

        i++;

    }

}

 

void cSort::_sort()

{

    cout << "[오름 차순 : 1] [내림차순 : 2] : ";

    cin >> Sort.UpDown;

    switch(Sort.UpDown)

    {

        case 1:

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

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

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

                        _swap(&Sort.Num[x], &Sort.Num[y]);

            break;

        case 2:

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

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

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

                        _swap(&Sort.Num[x], &Sort.Num[y]);

            break;

    }

}

 

void cSort::_swap(int *n1, int *n2)

{

    int temp = *n1;

    *n1 = *n2;

    *n2 = temp;

}

 

void cSort::_print()

{

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

        printf("%d\n", Sort.Num[i]);

    free(Sort.Num);

}


요즘 프로그램 공부로 인해 블로그가 많이 소홀해 졌네요. 올릴 글은 많아도 올릴 시간이 주말밖에 없으니... 누가 시간 좀 팔아줬으면 좋곘네요.