Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2018년 11월 12일 월요일

linked list


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct Student {
 
    int identity;
    char* name;
    int Korean;
    int English;
    int Math;
 
    struct Student* next;
};
 
struct Student* Create_Node(void);
void Add_First(struct Student* target, int _identity, char* _name, int _Korean, int _English, int _Math);
void Remove_First(struct Student* target);
 
struct Student* Search_Node(struct Student* target, int Key);
 
int main(void) {
 
    struct Student* information = NULL;
    information = Create_Node();
    information->next = NULL;
 
    Add_First(information, 1"Stive"909585);
    Add_First(information, 2"Elris"859080);
    Add_First(information, 3"Mike"808575);
    Add_First(information, 4"Aris"758070);
    Add_First(information, 5"Ava"707555);
    
    struct Student* Search_Target = Search_Node(information, 2);
 
    if (Search_Target == NULL) {
 
        printf("Error!, Not Find :(\n");
    }
    else {
 
        printf("ID : %d\n", Search_Target->identity);
        printf("Name : %s\n", Search_Target->name);
        printf("Korean score : %d\n", Search_Target->Korean);
        printf("English score : %d\n", Search_Target->English);
        printf("Math score : %d\n", Search_Target->Math);
        
        int total = Search_Target->Korean + Search_Target->English + Search_Target->Math;
        printf ("\nTotal score : %d\n", total);
        printf ("Average score : %.2f\n", total / 3.f); 
    }
 
    Search_Target = information->next;
    while (Search_Target != NULL) {
 
        struct Student* next = Search_Target->next;
        free(Search_Target);
        Search_Target = next;
    }
    free(information);
 
    return 0;
}
 
struct Student* Search_Node(struct Student* head, int Key) {
 
    struct Student* current = head->next;
    while (current != NULL) {
 
        if (current->identity == Key) return current;
        current = current->next;
    }
 
    return NULL;
}
 
void Remove_First(struct Student* target) {
 
    struct Student* Remove_Node = target->next;
    target->next;
 
    free(Remove_Node);
 
    return;
}
 
void Add_First(struct Student* target, int _identity, char* _name, int _Korean, int _English, int _Math) {
 
    struct Student* New_Node = (struct Student*calloc(1sizeof(struct Student));
    New_Node->next = target->next;
 
    // data
    New_Node->identity = _identity; // ID
    New_Node->name = _name;
    New_Node->Korean = _Korean;
    New_Node->English = _English;
    New_Node->Math = _Math;
 
    target->next = New_Node;
 
    return;
}
 
struct Student* Create_Node(void) {
 
    struct Student* Pointer_Return = NULL;
    Pointer_Return = (struct Student*calloc(1sizeof(struct Student));
 
    if (Pointer_Return != NULL) {
 
        (void)memset(Pointer_Return, 0sizeof(struct Student));
    }
    else {
 
        printf("Error!, Memory allocation Create_Node()\n");
        return NULL;
    }
 
    return Pointer_Return;
}
cs

2018년 11월 10일 토요일

factorization


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
 
#define BOX_SIZE 10
 
int Is_Prime(int Natural_Number);
int factorization(int factor, int Prime_Factor);
 
int main(void) {
 
    int Array_Prime_Factor_Box[BOX_SIZE] = { 0, };
    int Prime_Factor = 2;
    float factor = 36.f;
    int remainder = 0;
 
    int index = 0;
    for (index = 0; index < BOX_SIZE; index++) {
        if (Is_Prime(Prime_Factor)) {
 
            int temporary = factorization(factor, Prime_Factor);
            if (temporary == 0) {
 
                Array_Prime_Factor_Box[index] = Prime_Factor;
                factor /= Prime_Factor;
            }
            else if (temporary > 0) {
 
                Prime_Factor++;
                index--;
            }
            else break;
            remainder++;
        }
        else {
 
            Prime_Factor++;
            index--;
        }
    }
    if (factor != 1)
        Array_Prime_Factor_Box[remainder - 1 - 1= (int)factor;
 
    for (index = 0; Array_Prime_Factor_Box[index] != 0; index++) {
 
        printf("%d\t", Array_Prime_Factor_Box[index]);
    }
 
    return 0;
}
 
int factorization(int factor, int Prime_Factor) {
 
    if (factor < Prime_Factor) return -1;
    if ((factor / Prime_Factor) == 0return -2;
    return factor % Prime_Factor;
}
 
int Is_Prime(int Natural_Number) {
 
    if (Natural_Number < 2return -1;
 
    int Prime_Check = 0;
    for (Prime_Check = 2; Prime_Check < Natural_Number; Prime_Check++) {
 
        if (Natural_Number % Prime_Check == 0return 0;
    }
    return Natural_Number;
}
cs

2018년 10월 17일 수요일

bubble sort animation


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXIMUM 20 + 1
 
struct Chart {
 
    int Data[MAXIMUM];
    int Space[MAXIMUM][MAXIMUM];
 
};
 
/* Sort */
void Bubble_Sort(struct Chart* _chart, int Array_Size);
 
void Swap(int* current, int* next);
//////////
 
int Insert_Random_Numbers_In_A_Chart(struct Chart* _chart, int The_Number_Of);
int Draw_chart(struct Chart* _chart);
 
int Print_Space(struct Chart* _chart, int limit_horizon);
 
int main(void) {
 
    struct Chart chart = { .Data = { 0, },.Space = { 0, } };
 
    Insert_Random_Numbers_In_A_Chart(&chart, 20); // max = 20, MAXIMUM
 
    Bubble_Sort(&chart, 20); // max = 20, MAXIMUM
 
    //Draw_chart(&chart);
    //Print_Space(&chart, 50);
 
    return 0;
}
 
void Bubble_Sort(struct Chart* _chart, int Array_Size) {
 
    int All_Element = 0, Partial_Element = 0;
    for (All_Element = 0; All_Element < Array_Size; All_Element++) {
        for (Partial_Element = 0; Partial_Element < Array_Size - 1; Partial_Element++) {
 
            if (_chart->Data[Partial_Element] <= _chart->Data[Partial_Element + 1]) {
 
                /* animation */
                Sleep(10);
                system("cls");
 
                Draw_chart(_chart);
                Print_Space(_chart, Array_Size);
                ///////////////
 
                Swap(&_chart->Data[Partial_Element], &_chart->Data[Partial_Element + 1]);
            }
        }
    }
 
    return;
}
int Draw_chart(struct Chart* _chart) {
 
    // clear
    int row = 0, column = 0;
    for (column = 0; column < MAXIMUM; column++) {
        for (row = 0; row < MAXIMUM; row++) {
 
            _chart->Space[column][row] = 0;
        }
    }
 
    // draw
    int index = 0;
    for (index = 0; _chart->Data[index] != 0; index++) {
 
        _chart->Space[_chart->Data[index] - 1][index] = _chart->Data[index];
    }
 
    return 0;
}
int Insert_Random_Numbers_In_A_Chart(struct Chart* _chart, int The_Number_Of) {
 
    srand((unsigned)time(NULL));
 
    int loop = 0, Random_Number = 0, MAX = MAXIMUM;
    for (loop = 0; loop < The_Number_Of; loop++) {
 
        Random_Number = (rand() % (MAX - ((MAX / 3- ((MAX / 3) % 10)))) + ((MAX / 3- ((MAX / 3) % 10));
        _chart->Data[loop] = Random_Number;
    }
 
    return 0;
}
int Print_Space(struct Chart* _chart, int limit_horizon) {
 
    int row = 0, column = 0;
    for (column = MAXIMUM - 1 - 1; column > 0; column--) {
        for (row = limit_horizon; row > 0; row--) {
 
            if (_chart->Space[column][row] == 0) {
 
                printf("   ");
                continue;
            }
            printf("%3d", _chart->Space[column][row]);
        }
        printf("\n");
    }
 
    return 0;
}
void Swap(int* current, int* next) {
 
    int temporary = 0;
 
    temporary = *current;
    *current = *next;
    *next = temporary;
 
    return;
}
cs

2018년 10월 16일 화요일

drawing sign


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <math.h>
#define MAXIMUM 1000
#define PI 3.14159265359
 
struct Trigonometric_Function {
    
    float sign[MAXIMUM];
    float cosign[MAXIMUM];
    float tangent[MAXIMUM];
};
 
int Function_Sign (struct Trigonometric_Function* _trigonometric, int Angle);
 
int Drawing_Sign (struct Trigonometric_Function* _trigonometric, char space[][MAXIMUM], int the_number_of_sign);
 
void Space_Print (char space[][MAXIMUM], int limit_horizon);
void Drawing_Graticule (char space[][MAXIMUM]);
 
void print (struct Trigonometric_Function* _trigonometric); //
 
int main (void) {
    
    struct Trigonometric_Function trigonometric = { .sign = { 0, }, .cosign = { 0, }, .tangent = { 0, } };
    char Space[40 + 1][MAXIMUM] = { (char0, };
    int radian = 360;
    
    int the_number_of_sign = Function_Sign (&trigonometric, radian);
    
    Drawing_Graticule (Space);
    Drawing_Sign (&trigonometric, Space, the_number_of_sign);
    
    Space_Print (Space, 51);
    //print (&trigonometric); // testing
    
    return 0;
}
 
int Drawing_Sign (struct Trigonometric_Function* _trigonometric, char space[][MAXIMUM], int the_number_of_sign) {
    
    int index = 0;
    for (index = 0; index < the_number_of_sign; index++) {
        
        _trigonometric->sign[index] *= 20;
        space[(40 / 2- (int)_trigonometric->sign[index]][index] = 's';
    }
    
    return 0;
}
int Function_Sign (struct Trigonometric_Function* _trigonometric, int Angle) {
    
    int The_Number_Of_Sign = 0;
    int loop = 0, index = 0, unit = 10;
    for (loop = 0, index = 0; loop < Angle + 1; loop += unit, index++) {
        
        _trigonometric->sign[index] = sin(PI * loop / 180.f);
        The_Number_Of_Sign++;
    }
    
    return The_Number_Of_Sign;
}
void Space_Print (char space[][MAXIMUM], int limit_horizon) {
    
    int row = 0, column = 0;
    for (column = 0; column < 40 + 1; column++) {
        for (row = 0; row < limit_horizon; row++) {
            
            printf ("%2c", space[column][row]);
        }
        printf ("\n");
    }
}
void Drawing_Graticule (char space[][MAXIMUM]) {
    
    int loop = 0;
    for (loop = 0; loop < 40 + 1; loop++) {
        
        space[loop][0= '[';
    }
    for (loop = 0; loop < MAXIMUM; loop++) {
        
        space[(40 + 1 - 1/ 2][loop] = '-';
    }
    
    return;
}
 
void print (struct Trigonometric_Function* _trigonometric) { // testing
    
    int loop = 0;
    for (loop = 0; loop < 19; loop++) {
        
        printf ("%f\n", _trigonometric->sign[loop]);
    }
}
cs