언어/c언어

중복없이 랜덤 하게 숫자 뽑기

파아랑새 2016. 7. 28. 07:57

# include <stdio.h>

# include <stdlib.h>

# include <string.h>


# define SIZE 101

# define TRUE 1


typedef struct _randomNumber {

int* array;

int* sortArray;

int count;


}randomNumber, *pRandomNumber;


// function prototype

// [1] =====================================

void Error(char * ErrorMessage);

// [2] =====================================

void Initiallize(randomNumber** stu);

// [3] =====================================

void testPrintf(randomNumber** stu);

// [4] =====================================

void RandomSorting(randomNumber** stu);

// [5] =====================================

void BubbleSort(randomNumber** stu);

// [6] =====================================

void dataPrintf(randomNumber** stu);

// =========================================


int main (void) {

srand((unsigned)time(NULL));

pRandomNumber stu = NULL;

Initiallize(&stu);

testPrintf(&stu);

RandomSorting(&stu);

BubbleSort(&stu);

dataPrintf(&stu);

return 0;

}


// [1] =====================================

void Error(char * ErrorMessage) {

fprintf(stdout, "%s \n", ErrorMessage);

exit(0);

} // end of Error function


// [2] =====================================

void Initiallize(randomNumber** stu) {

int i; // index

(*stu) = (pRandomNumber)malloc(sizeof(randomNumber));

if ( (*stu) == NULL ) {

Error("mallo fail !!!");

} else { // (*stu) != NULL

(**stu).array = (int*)malloc(sizeof(int)*SIZE);

if ((**stu).array == NULL) {

Error("mallo fail !!!");

} else {

(**stu).count = 0;

memset((**stu).array, 0, sizeof(int)*SIZE);

for (i = 0; i < SIZE-1; i++) {

(**stu).array[i] = i+1;

(**stu).sortArray = NULL;

}

}

}

} // end of Initiallize function


// [3] =====================================

void testPrintf(randomNumber** stu) {

int i;

for (i = 0; i < SIZE; i++) {

printf("%d ", (**stu).array[i]);

}

printf("\n");

} // end of testPrintf function


// [4] =====================================

void RandomSorting(randomNumber** stu) {

int count_1 = 0;

int start = 0;

int randomNumber = 0;

int TempNumber = SIZE-1; // 100 

int TempSize = SIZE;

int i = 0; 

int j; // index

printf("count : ");

scanf("%d", &((**stu).count));

(**stu).sortArray = (int *)malloc(sizeof(int)* (**stu).count);

memset((**stu).sortArray, 0, sizeof(int)* (**stu).count);

do {

randomNumber = rand()%TempNumber;

(**stu).sortArray[i] = (**stu).array[randomNumber];

for(j = randomNumber; j<TempSize ; j++) {

(**stu).array[j] = (**stu).array[j+1]; 

  }

  TempSize--;

TempNumber--;

i++;

count_1++ ; 

} while (count_1 != (**stu).count);

} // end of RandomSorting function


// [5] =====================================

void BubbleSort(randomNumber** stu) {

int i, j; // index

int tempNumber = 0;

for (i = 0; i < (**stu).count -1; i++) {

for (j = 0; j < (**stu).count -1; j++) {

if ( (** stu).sortArray[j] > (** stu).sortArray[j+1] ) {

tempNumber = (** stu).sortArray[j] ;

(** stu).sortArray[j] = (** stu).sortArray[j+1];

(** stu).sortArray[j+1] = tempNumber;

}

}

}


} // end of BubbleSort function


// [6] =====================================

void dataPrintf(randomNumber** stu) {

int i;

for (i = 0; i < (**stu).count; i++) {

printf("%d ", (**stu).sortArray[i]);

}

printf("\n");

} // end of dataPrintf function