Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2018년 9월 27일 목요일

enumeration switch


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
#include <stdio.h>
 
enum LuxSkill {
    
    LightBinding = 1,
    PrismaticBarrier,
    LucentSigularity,
    FinalSpark
};
 
int main (void) {
    
    enum LuxSkill skill;
    
    skill = LightBinding;
    
    switch (skill) {
        
        case LightBinding:
            printf("LightBinding\n");
            break;
        case PrismaticBarrier:
            printf("PrismaticBarrier\n");
            break;
        case LucentSigularity:
            printf("LucentSigularity\n");
            break;
        case FinalSpark:
            printf("FinalSpark\n");
            break;
        default:
            break;    
    }
    
    return 0;
}
cs

struct alloc memory


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
#include <stdio.h>
#include <stdlib.h>
 
struct Phone {
    
    int areacode;
    unsigned long long int number;
};
 
struct Person {
    
    char name[20];
    int age;
    struct Phone *phone;
};
 
int main (void) {
    
    struct Person *p1 = calloc (1sizeof (struct Person));
    
    p1->phone = calloc (1sizeof (struct Phone));
    
    p1->phone->areacode = 82;
    p1->phone->number = 30545671234;
    
    printf("%d %llu\n", p1->phone->areacode, p1->phone->number);
    
    free (p1->phone);
    free (p1);
    
    return 0;
}
cs

2018년 9월 26일 수요일

struct initialize


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
#include <stdio.h>
 
struct Phone {
    
    int areacode;
    unsigned long long int number;
};
 
struct Person {
    
    char* name;
    int age;
    struct Phone phone;
};
 
int main(void) {
    
    struct Person person1 = {
    person1.name = "Karma", person1.age = 30
        {
            person1.phone.areacode = 82, person1.phone.number = 3045671234
        }
    };
    
    printf("%d %llu\n", person1.phone.areacode, person1.phone.number);
    
    struct Person person2 = {
    person2.name = "mike", person2.age = 35
        {
            person2.phone.areacode = 82, person2.phone.number = 3012341596
        }
    };
    
    printf("%d %llu\n", person2.phone.areacode, person2.phone.number);
    
    person1.name = "Ari";
    person1.age = 20;
    person1.phone.areacode = 82;
    person1.phone.number = 4575164285;
    
    printf("%s %d %d %llu\n", person1.name, person1.age, person1.phone.areacode, person1.phone.number);
    
    return 0;
}
cs

2018년 9월 24일 월요일

struct


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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
unsigned short int MAXIMUM = 1;
 
typedef struct _PRIVACY {
    
    char name[20];
    int age;
    char adress[100];    
} Privacy;
 
int main (void) {
    
    Privacy* man = calloc (MAXIMUM, sizeof (struct _PRIVACY));
    
    strcpy (man->name, "Stive");
    man->age = 23;
    strcpy (man->adress, "America");
    
    printf("name : %s\n", man->name);
    printf("age : %d\n", man->age);
    printf("adress : %s\n", man->adress);
    
    free (man);
    
    return 0;
}
cs

String storage


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
#include <stdio.h>
#include <string.h>
 
int main(void) {
    
    char arrayString[30= "The little prince";
    char* arrayStorage[10= { NULL, };
    char* pointer = strtok (arrayString, " ");
    unsigned short int loop = 0, temporary = 0;
    
    for (loop = 0; pointer != NULL; loop++) {
        
        arrayStorage[loop] = pointer;        
        pointer = strtok (NULL" ");
    }
    
    for (loop = 0; loop < 10; loop++) {
        if (arrayStorage[loop] != NULL) {
            
            printf("%s\n", arrayStorage[loop]);
        }
    }
    
    return 0;
}
cs

input string


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <stdlib.h>
 
int main(void) {
    
    char* inputString = calloc ( 10sizeof (char) );
    
    printf("input : ");
    scanf("%s", inputString);
    
    printf("result : %s\n", inputString);
    
    free (inputString);
    
    return 0;
}
cs

Minesweeper


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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
unsigned const short int _HORIZON = 30 + 1;
unsigned const short int _VERTICAL = 20 + 1;
unsigned const short int _MINE = 800// distribution
 
void examine_boom (int** , int** );
void print_space (int** , unsigned short intunsigned short int);
 
int main(void) {
    
    /* dynamic allocation */
    int** space = calloc (_VERTICAL, sizeof (int*)); // dynamic allocation, vertical
    int** emptySpace = calloc (_VERTICAL, sizeof (int*)); // dynamic allocation, vertical
    unsigned short int horizon = 0, vertical = 0;
    
    srand ((unsigned) time (NULL)); // random
    
    for (vertical = 0; vertical < _VERTICAL; vertical++) {
        
        space[vertical] = calloc (_HORIZON, sizeof (int)); // dynamic allocation, horizon
        emptySpace[vertical] = calloc (_HORIZON, sizeof (int)); // dynamic allocation, horizon
    }
    
    /* create mine */
    for (vertical = 0; vertical < _VERTICAL; vertical++) {
        for (horizon = 0; horizon < _HORIZON; horizon++) {
            if (vertical == 0 || horizon == 0 ||
                    vertical == _VERTICAL - 1 || horizon == _HORIZON - 1// except edge
                continue;
            if ((rand() % (_HORIZON * _VERTICAL * 10/ _MINE) == 0)            
                space[vertical][horizon] = -1// mine
            else
                space[vertical][horizon] = 0;
        }
    }
    /*examine boom*/
    examine_boom(space, emptySpace);
    
    /* print */
    print_space (emptySpace, horizon, vertical);
    
    /* initialize dynamic allocation  */
    for (vertical = 0; vertical < _VERTICAL; vertical++) {
        
        free (space[vertical]); // initialize horizon
    }
    free (space); // initialize vertical
    
    return 0;
}
 
void examine_boom (int** space, int** emptySpace) {
    
    unsigned short int horizon = 0, vertical = 0;
    unsigned short int row = 0, column = 0, countMine = 0;
    unsigned short int loop = 0;
    signed short int mine = -1;
    
    for (vertical = 0; vertical < _VERTICAL; vertical++) {
        for (horizon = 0; horizon < _HORIZON; horizon++) {
            if ( space[vertical][horizon] == mine ) {
                
                emptySpace[vertical][horizon] = -1// mine
                continue;
            }
            if ( vertical == 0 || horizon == 0 ||
                    vertical == _VERTICAL - 1 || horizon == _HORIZON - 1// except edge
                continue;
            
            countMine = 0// initialize mine
            row = horizon - 1;
            column = vertical - 1;
            
            for (loop = 0; loop < 3; loop++) { //   1     2     3
                if (space[column][row++== mine) // 4.4   5.4   6.4
                    countMine++;
                if (space[column][row++== mine) // 4.5   5.5   6.5
                    countMine++;
                if (space[column++][row] == mine) // 4.6   5.6   6.6
                    countMine++;
                row -= 2;
            }
            emptySpace[vertical][horizon] = countMine;
        }
    }
    
    return;
}
 
void print_space (int** space, unsigned short int horizon, unsigned short int vertical) {
    
    for (vertical = 0; vertical < _VERTICAL; vertical++) {
        for (horizon = 0; horizon < _HORIZON; horizon++) {
            if (vertical == 0 || horizon == 0 ||
                    vertical == _VERTICAL - 1 || horizon == _HORIZON - 1// except edge
                continue;
            if (space[vertical][horizon] == -1)
                printf("%c "42);
            else
                printf("%d ", space[vertical][horizon]);
        }
        printf("\n");
    }
    
    return;
}
cs