언어/c언어

lotto 관련 코드 네이버 답변

파아랑새 2017. 10. 8. 11:29

/*http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=286023141&page=1#answer2*?

/* 작성자 : sleep4725.tistory.com */

# include <stdio.h>

# include <string.h>

# include <stdlib.h>

# include <time.h>

# include <stdbool.h>

# define SIZE 45


typedef struct _Lotto

{

int numBer[45];

int** ptrMyLottoBall;

int count;

}Lotto, *ptrLotto;


// function prototype

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

ptrLotto createNode(); // [1]

void init(Lotto** pParam); // [2]

void choiceBall(Lotto** pParam); // [3]

void numBerCreate(Lotto** pParam); // [4]

void PrintfData(Lotto** pParam); // [5]

void BubbleSort(Lotto** pParam); // [6]

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

int main(void)

{

ptrLotto L = createNode();

init(&L);

choiceBall(&L);

numBerCreate(&L);

BubbleSort(&L);

PrintfData(&L);

free(L);

return 0;

} // end of main function

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

ptrLotto createNode() // [1]

{

ptrLotto n = (ptrLotto)malloc(sizeof(Lotto));

return n;

} // end of createNode function

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

void init(Lotto** pParam) // [2]

{

if ((*pParam) == NULL)

{

printf("malloc error ... !!!\n");

exit(1);

}

else

{ // (*pParam) != NULL

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

{

(*pParam)->numBer[i] = i + 1;

(*pParam)->ptrMyLottoBall = NULL;

(*pParam)->count = 0;

}

}

} // end of init function 

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

void choiceBall(Lotto** pParam) // [3]

{

printf("몇개의 로또번호 생성을 원하십니까? ");

scanf("%d", &((*pParam)->count));


(*pParam)->ptrMyLottoBall = (int**)malloc(sizeof(int*)*(*pParam)->count);

for (int i = 0; i < (*pParam)->count; i++)

{

(*pParam)->ptrMyLottoBall[i] = (int*)malloc(sizeof(int)*6);

}

// 초기화 

for (int i = 0; i < (*pParam)->count; i++)

{

for (int j = 0; j < 6; j++)

{

(*pParam)->ptrMyLottoBall[i][j] = 0;

}

}

} // end of choiceBall function 

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

void numBerCreate(Lotto** pParam) // [4]

{

int ChoiceIndex = 0;

int TempSize = 0;

int Ball = 0;

srand((unsigned)time(NULL));

int TempBall[45] = { 0, };

for (int i = 0; i < (*pParam)->count; i++)

{/* 내가 선택한 로또 갯수 */

for (int k = 0; k < SIZE; k++) 

{ // 1, 2, 3, 4, ..., 41, 42, 43, 44, 45

TempBall[k] = (*pParam)->numBer[k]; 

}

while (1)

{

TempSize = SIZE;

int j = 0;

for (j = 0; j < 6; j++)

{

ChoiceIndex = rand() % TempSize; // mod(45)

Ball = TempBall[ChoiceIndex];

(*pParam)->ptrMyLottoBall[i][j] = Ball;

//printf("TempSize => %d\n", TempSize);


if (ChoiceIndex == SIZE - 1)

{

--TempSize;

}


else // ChoiceIndex != SIZE -1 

{

for (int s = ChoiceIndex; s < SIZE - 1; s++)

{

TempBall[s] = TempBall[s + 1];

}

--TempSize;

}

}

if (j == 6){ break; }

}

}

} // end of numBerCreate function 

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

void PrintfData(Lotto** pParam) // [5]

{

for (int j = 0; j < (** pParam).count; j++)

{

for (int k = 0; k < 6; k++)

{

printf("%02d ", (*pParam)->ptrMyLottoBall[j][k]);

}

printf("\n");

}

} // end of PrintfData function

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

void BubbleSort(Lotto** pParam) // [6]

{

int tempNumber = 0;

int flag;

for (int i = 0; i < (*pParam)->count; i++)

{

flag = 0;

for (int j = 0; j < 6; j++)

{

for (int k = 0; k < 5; k++)

{

if ((**pParam).ptrMyLottoBall[i][k] >(**pParam).ptrMyLottoBall[i][k + 1])

{

tempNumber = (**pParam).ptrMyLottoBall[i][k];

(**pParam).ptrMyLottoBall[i][k] = (**pParam).ptrMyLottoBall[i][k + 1];

(**pParam).ptrMyLottoBall[i][k + 1] = tempNumber;

flag = 1;

}

}

if (flag == 0) { break; }

}

}

} // end of BubbleSort function