This file contains the main functions of my tool, rac. In particular, I care a lot about the Shannon function.
Would you please give some hints to make the file cleaner? I have a perversion for concise and good indented code.
#include <math.h>
#include <stdio.h>
#include "headers/utils.h"
double ccount(char * foo)
{
FILE * file = fopen(foo, "r");
int a = 0, b;
while ((b = getc(file)) != EOF)
a++;
fclose(file);
return a;
}
int cmp(double foo, double bar)
{
if (foo >= bar)
return 1;
else
return 0;
}
double ratio(double foo, double bar)
{
return (100*(foo - bar)/ foo);
}
void stat(char * file, double vector[256])
{
int c;
FILE * ftopen = fopen(file, "r");
while ((c = getc(ftopen)) != EOF)
{
if (c >= 0 && c <= 256)
vector[c]++;
}
fclose(ftopen);
}
void freq(char * foo, double vector[256])
{
int i;
for (i = 0; i < 256; i++)
{
if (vector[i] == 0)
continue;
else
{
vector[i] = (vector[i] / ccount(foo));
}
}
}
double shannon(double vector[256])
{
int i;
double shan = 0.0;
for (i = 0; i < 256; i++)
{
if (vector[i] == 0.0)
continue;
else
{
shan = shan + (log2(pow(vector[i], vector[i])));
}
}
return (shan * -1.0); //entropy of the file
}