로또 (3개짜리)
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>
# define _SIZE_ 45
# define _CHOICE_ 3
typedef struct _Lotto {
int lotto[_SIZE_]; // [1, 45]
int TodayLotto [_CHOICE_]; // 3
int MeLotto [_CHOICE_]; // 3
}Lotto, *ptrLotto;
// function prototype ++++++++++++++++++++++++++++++++++
void Err_Function(char*); // ---------------------> [0]
void Sett_Function(Lotto**); // ------------------> [1]
void Ball_Function(Lotto**); // ------------------> [2]
void RndChoice_Function(Lotto**); // -------------> [3]
void Ctl_Function(Lotto**, int, int); // ---------> [4]
void BubbleSort_Function(Lotto**); // ------------> [5]
void Menu(); // ----------------------------------> [6]
void TodayLotto_Function(Lotto**); // ------------> [7]
void Win_Function(Lotto**); // -------------------> [8]
// +++++++++++++++++++++++++++++++++++++++++++++++++++++
int main (void) {
srand((unsigned)time(NULL));
system("cls");
ptrLotto Lotto = NULL;
Sett_Function(&Lotto);
Ball_Function(&Lotto);
RndChoice_Function(&Lotto);
BubbleSort_Function(&Lotto);
TodayLotto_Function(&Lotto);
Win_Function(&Lotto);
return 0;
} // end of main function
//++++++++++++++++++++++++++++++++++++++++++++++++
void Err_Function (char* errMessage) {
fprintf(stderr, "%s", errMessage);
exit(0);
} // end of Err_Function
void Sett_Function(Lotto** stu) {
(*stu) = (ptrLotto)malloc(sizeof(Lotto));
if ( (*stu) == NULL) {
Err_Function("memory allocation error");
} else { // (*stu) != NULL
memset( (*stu), 0, sizeof(Lotto));
}
} // end of Sett_Function
void Ball_Function(Lotto** stu) {
int i; // index
for (i = 0; i <_SIZE_; i++) { // 1,2,3,4, ..., 45
(** stu).lotto[i] = (i+1);
}// end of for
// comment
for (i = 0; i <_SIZE_; i++) { // 1,2,3,4, ..., 45
printf("%d ", (** stu).lotto[i]);
} printf("\n");
// end of for
} // end of Ball_Function
void Ctl_Function(Lotto** stu, int index, int size) {
int i; // index
for (i= index; i< size-1; i++) {
(** stu).lotto[i] = (** stu).lotto[i+1];
}
} // end of Ctl_Function
void RndChoice_Function(Lotto** stu) {
int i, j; // index
int tmpSize = _SIZE_;
int chNumber = 0;
int index = 0;
for(i = 1; i<=3; i++) {
chNumber = rand()%tmpSize; // 0, 1, 2, 3, 4, ..., 44
Ctl_Function(stu, chNumber, tmpSize);
tmpSize -=1;
(** stu).MeLotto[index++] = (** stu).lotto[chNumber];
} // end of for
printf("Before> ");
for(i = 0; i<3; i++) {
printf("%d ", (** stu).MeLotto[i]);
} printf("\n");
// end of for
} // end of RndChoice_Function
void BubbleSort_Function(Lotto** stu) {
int i, j; // index
int tmp = 0;
int result;
for (i = 0; i<2; i++) {
result = 0;
for (j = 0; j<2; j++) {
if ( (** stu).MeLotto[j] > (** stu).MeLotto[j+1] ) {
tmp = (** stu).MeLotto[j];
(** stu).MeLotto[j] = (** stu).MeLotto[j+1];
(** stu).MeLotto[j+1] = tmp;
result = 1;
}
}
if (result == 0) {
break;
}
}
printf("after> ");
for(i = 0; i<3; i++) {
printf("%d ", (** stu).MeLotto[i]);
} printf("\n");
// end of for
} // end of BubbleSort_Function
void Menu() {
printf(" Range : 1 ~ 45 \n");
} // end of Menu Function
void TodayLotto_Function(Lotto** stu) {
int number = 0;
int count = 0;
int i;// index
Menu();
while(1) {
fprintf(stdout, "%s", "Plz number(integer) input: ");
scanf("%d", &number);
fflush(stdin);
if ( (number>=1) && (number<=45) ) {
(**stu).TodayLotto[count] = number; // 0, 1
count += 1;
printf("insert success!!! \n");
} else {
printf("wrong!!! \n");
}
if (count == 3) {// 1
break;
}
}
for(i = 0; i<3; i++) {
printf("%d ", (** stu).TodayLotto[i]);
} printf("\n");
} // end of TodayLotto Function
void Win_Function(Lotto** stu) {
int count = 0; // 맞는것에 갯수를 카운팅 할 것이다.
int i, j; // index
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if ( (** stu).TodayLotto[i] == (** stu).MeLotto[j] ) {
count ++;
}
}
}
if (count == 3) {
printf("win !!! \n");
} else { // count != 3
printf("not win !!! \n");
}
} // end of Win_Function