구조체를 클래스로 써보기
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdbool.h>
# include <time.h>
# define LENGTH 15
typedef struct _Sort
{
int numBer[LENGTH];
void(* ptrBubble)(struct _Sort**); // * Oh .... !!!!!!!!!!!!!!!!!!!!!
}Sort, *ptrSort;
ptrSort create_node();
void __init__(Sort** param);
void do_sorting_by_bubble(Sort** param);
int main(void)
{
ptrSort MySort = NULL;
MySort = create_node();
__init__(&MySort);
MySort->ptrBubble = do_sorting_by_bubble;
printf("[1] => %#x \n", (unsigned int)do_sorting_by_bubble);
printf("[2] => %#x \n", (unsigned int)MySort->ptrBubble);
MySort->ptrBubble(&MySort);
return 0;
} // end of main function
ptrSort create_node()
{
ptrSort p = (ptrSort)malloc(sizeof(Sort));
return p;
} // end of create_node function
void __init__(Sort** param)
{
srand((unsigned)time(NULL));
if ((*param) == NULL)
{
fprintf(stderr, "malloc error ... !!!\n");
exit(1);
}
else
{ // (*param) != NULL
for (int i = 0; i < LENGTH; i++)
{
(*param)->numBer[i] = rand() % 1000 + 1;
}
(*param)->ptrBubble = NULL;
}
} // end of __init__ function
void do_sorting_by_bubble(Sort** param)
{
// printf("test ... \n");
// [before]
for (int i = 0; i < LENGTH; i++)
{
printf("%d ", (** param).numBer[i]);
}
printf("\n"); // 개행
int i, j, k; // index
int temp = 0;
bool flag;
for (j = 0; j < LENGTH; j++)
{
flag = true;
for (k = 0; k < LENGTH-1; k++)
{
if ((** param).numBer[k] >(** param).numBer[k+1])
{
temp = (** param).numBer[k];
(** param).numBer[k] = (** param).numBer[k + 1];
(** param).numBer[k + 1] = temp;
flag = false;
}
}
if (flag == true)
{
break;
}
}
// [after]
for (int i = 0; i < LENGTH; i++)
{
printf("%d ", (** param).numBer[i]);
}
printf("\n"); // 개행
} // end of do_sorting_by_bubble function
'언어 > c언어' 카테고리의 다른 글
#네이버 문제 (0) | 2017.10.19 |
---|---|
학생들 자리 랜덤 코드 (0) | 2017.10.16 |
# 네이버 문제 풀이 (0) | 2017.10.14 |
#네이버 c언어 문제풀이 (0) | 2017.10.14 |
네이버 풀이 나 (0) | 2017.10.13 |