티스토리 뷰

16진수 자릿수별 빈도 계산 in 64bit 환경

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
 
typedef struct number{
    unsigned int zero;
    unsigned int one;
    unsigned int two;
    unsigned int three;
    unsigned int four;
    unsigned int five;
    unsigned int six;
    unsigned int seven;
    unsigned int eight;
    unsigned int nine;
    unsigned int a;
    unsigned int b;
    unsigned int c;
    unsigned int d;
    unsigned int e;
    unsigned int f;
} num;
 
num ot;
 
int count(unsigned char n)
{
    switch(n){
    case 0:
        ot.zero++;
        return 0;
    case 1:
        ot.one++;
        return 1;
    case 2:
        ot.two++;
        return 2;
    case 3:
        ot.three++;
        return 3;
    case 4:
        ot.four++;
        return 4;
    case 5:
        ot.five++;
        return 5;
    case 6:
        ot.six++;
        return 6;
    case 7:
        ot.seven++;
        return 7;
    case 8:
        ot.eight++;
        return 8;
    case 9:
        ot.nine++;
        return 9;
    case 0xa:
        ot.a++;
        return 0xa;
    case 0xb:
        ot.b++;
        return 0xb;
    case 0xc:
        ot.c++;
        return 0xc;
    case 0xd:
        ot.d++;
        return 0xd;
    case 0xe:
        ot.e++;
        return 0xe;
    case 0xf:
        ot.f++;
        return 0xf;
    default:
        puts("error!");
        return -1;
    }
}
 
void calculation_square(unsigned long long number, unsigned int base)
{
    if(number)
    { 
        if(count(number % base) == -1)
            exit(-1);
 
        number /= base;
 
        calculation_square(number, base);
    }
}
 
void hex_result(void)
{
    puts("================================================");
    printf("zero : \t%u\n", ot.zero);
    printf("one : \t%u\n", ot.one);
    printf("two : \t%u\n", ot.two);
    printf("three : %u\n", ot.three);
    printf("four : \t%u\n", ot.four);
    printf("five : \t%u\n", ot.five);
    printf("six : \t%u\n", ot.six);
    printf("seven : %u\n", ot.seven);
    printf("eight : %u\n", ot.eight);
    printf("nine : \t%u\n", ot.nine);
    printf("A : \t%u\n", ot.a);
    printf("B : \t%u\n", ot.b);
    printf("C : \t%u\n", ot.c);
    printf("D : \t%u\n", ot.d);
    printf("E : \t%u\n", ot.e);
    printf("F : \t%u\n", ot.f);    
    puts("================================================");
}
 
void decimal_result(void)
{
    puts("================================================");
    printf("zero : \t%u\n", ot.zero);
    printf("one : \t%u\n", ot.one);
    printf("two : \t%u\n", ot.two);
    printf("three : %u\n", ot.three);
    printf("four : \t%u\n", ot.four);
    printf("five : \t%u\n", ot.five);
    printf("six : \t%u\n", ot.six);
    printf("seven : %u\n", ot.seven);
    printf("eight : %u\n", ot.eight);
    printf("nine : \t%u\n", ot.nine);   
    puts("================================================");
}
 
void octal_result(void)
{
    puts("================================================");
    printf("zero : \t%u\n", ot.zero);
    printf("one : \t%u\n", ot.one);
    printf("two : \t%u\n", ot.two);
    printf("three : %u\n", ot.three);
    printf("four : \t%u\n", ot.four);
    printf("five : \t%u\n", ot.five);
    printf("six : \t%u\n", ot.six);
    printf("seven : %u\n", ot.seven);  
    puts("================================================");
}
 
int main(int argc, char* argv[]){
    unsigned long long random = 0;
    unsigned int size, count_num, base;
    int i, fd;
 
    if(argc != 4){
        printf("usage : ./filename [size] [count] [base]\n");
        return 0;
    }
 
    size = strtoul(argv[1], 010);
    base = strtoul(argv[3], 010);
    count_num = strtoul(argv[2], 010);
 
    if(size == 0) exit(-1);
    if((base != 8&& (base != 10&& (base != 16)) exit(-1);
 
    for(i=0; i<count_num; i++){
        fd = open("/dev/urandom", O_RDONLY);
        if(fd==-1) exit(-1);
 
        if(read(fd, &random, size)!=size) exit(-1);
        close(fd);
 
        printf("random = %llx\n", random);
 
        calculation_square(random, base);
        random = 0;
    }
 
    switch(base){
    case 8:
        octal_result();
        break;
    case 10:
        decimal_result();
        break;
    case 16:
        hex_result();
        break;
    default:
        break;
    }
 
    return 0;
}
cs

- 난수 값에서 자릿수별로 숫자들이 총 몇 번 사용되었는지 계산하는 프로그램이다. 딱히 사용할 곳이 있긴 한가 싶다...

- base에 8진수, 10진수, 16진수를 입력하면 해당 진수에 맞춰 연산을 해준다. 

- size에는 읽어들일 난수의 바이트 크기를 넣으면 되고, count에는 총 몇 번 읽어들일 것인지이다. 필요한 기능은 추가적으로 변경하면 되겠다.


e.g.


일단은 제대로 돌아가지만, 매우 큰 난수 값의 경우 자료형을 변경해서 사용해야 할 것이다.

댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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