mirror of https://github.com/dirtbags/fluffy.git
27 lines
345 B
C
27 lines
345 B
C
|
#include <stdio.h>
|
||
|
#include <math.h>
|
||
|
|
||
|
int
|
||
|
main(int argc, char *argv[])
|
||
|
{
|
||
|
size_t len = 0;
|
||
|
size_t hist[256] = {0};
|
||
|
int c;
|
||
|
|
||
|
while ((c = getchar()) != EOF) {
|
||
|
hist[c] += 1;
|
||
|
len += 1;
|
||
|
}
|
||
|
|
||
|
float H = 0;
|
||
|
for (int i = 0; i < 256; i += 1) {
|
||
|
if (hist[i]) {
|
||
|
float Hi = (float)hist[i]/len;
|
||
|
H -= Hi * log2(Hi);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
printf("%f\n", H);
|
||
|
}
|
||
|
|