1
0
mirror of https://github.com/cc65/cc65.git synced 2024-11-19 06:31:31 +00:00
cc65/test/ref/cf.c

192 lines
3.9 KiB
C
Raw Normal View History

2014-09-24 14:45:10 +00:00
/*
!!DESCRIPTION!! print character frequencies
!!ORIGIN!! LCC 4.1 Testsuite
!!LICENCE!! own, freely distributeable for non-profit. read CPYRIGHT.LCC
*/
/*
cf - print character frequencies
*/
#include "common.h"
2014-09-24 14:45:10 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
FILE *in;
2014-09-24 14:45:10 +00:00
#define INFILE "cf.in"
#define GETCHAR() fgetc(in)
2014-09-24 14:45:10 +00:00
#ifndef NO_FLOATS
float f[0x100];
#else
signed f[0x100];
#endif
#ifdef NO_OLD_FUNC_DECL
int main(int argc,char **argv)
#else
main(argc, argv)
int argc;
char *argv[];
#endif
{
2019-02-12 21:50:49 +00:00
int i, c, nc;
2014-09-24 14:45:10 +00:00
#ifndef NO_FLOATS
2019-02-12 21:50:49 +00:00
float cutoff, atof();
2014-09-24 14:45:10 +00:00
#else
signed cutoff;
#endif
in = fopen(INFILE, "rb");
if (in == NULL) {
return EXIT_FAILURE;
}
2019-02-12 21:50:49 +00:00
if (argc <= 1)
2014-09-24 14:45:10 +00:00
#ifndef NO_FLOATS
2019-02-12 21:50:49 +00:00
cutoff = 0.0;
2014-09-24 14:45:10 +00:00
#else
cutoff = 0;
#endif
2019-02-12 21:50:49 +00:00
else
2014-09-24 14:45:10 +00:00
#ifndef NO_FLOATS
2019-02-12 21:50:49 +00:00
cutoff = atof(argv[1])/100;
2014-09-24 14:45:10 +00:00
#else
cutoff = atoi(argv[1])/100;
#endif
2019-02-12 21:50:49 +00:00
for (i = 0; i < 0x100; )
2014-09-24 14:45:10 +00:00
{
#ifndef NO_FLOATS
2019-02-12 21:50:49 +00:00
f[i++] = 0.0;
2014-09-24 14:45:10 +00:00
#else
f[i++] = 0;
#endif
}
printf("input:\n\n");
2022-04-17 14:07:52 +00:00
2014-09-24 14:45:10 +00:00
nc = 0;
while ((c = GETCHAR()) != -1)
{
/* printf("[%02x]",c); */
printf("%c",c);
f[c] += 1;
nc++;
}
printf("\n\ncount: %d\n\n",nc);
/*
now try to print a report in a way so that
the order is somewhat independent from the
target character set
*/
printf("a-z char:freq\n\n");
/* first round ... lowercase characters */
2019-02-12 21:50:49 +00:00
for (i = 0; i < 0x100; ++i)
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if ((f[i]) && ((f[i]/nc) >= cutoff))
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if ((i >= 'a') && (i <= 'z'))
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
printf("%c", i);
2014-09-24 14:45:10 +00:00
#ifndef NO_FLOATS
printf(":%.1f\n", 100*f[i]/nc);
#else
printf(":%d\n", 100*f[i]/nc);
#endif
f[i]=0;
}
2019-02-12 21:50:49 +00:00
}
2014-09-24 14:45:10 +00:00
}
printf("A-Z char:freq\n\n");
/* second round ... uppercase characters */
2019-02-12 21:50:49 +00:00
for (i = 0; i < 0x100; ++i)
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if ((f[i]) && ((f[i]/nc) >= cutoff))
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if ((i >= 'A') && (i <= 'Z'))
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
printf("%c", i);
2014-09-24 14:45:10 +00:00
#ifndef NO_FLOATS
printf(":%.1f\n", 100*f[i]/nc);
#else
printf(":%d\n", 100*f[i]/nc);
#endif
f[i]=0;
}
2019-02-12 21:50:49 +00:00
}
2014-09-24 14:45:10 +00:00
}
printf("0-9 char:freq\n\n");
/* third round ... numbers */
2019-02-12 21:50:49 +00:00
for (i = 0; i < 0x100; ++i)
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if ((f[i]) && ((f[i]/nc) >= cutoff))
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if ((i >= '0') && (i <= '9'))
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
printf("%c", i);
2014-09-24 14:45:10 +00:00
#ifndef NO_FLOATS
printf(":%.1f\n", 100*f[i]/nc);
#else
printf(":%d\n", 100*f[i]/nc);
#endif
f[i]=0;
}
2019-02-12 21:50:49 +00:00
}
2014-09-24 14:45:10 +00:00
}
printf("isprint char:freq\n\n");
/* second last round ... remaining printable characters */
2019-02-12 21:50:49 +00:00
for (i = 0; i < 0x100; ++i)
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if ((f[i]) && ((f[i]/nc) >= cutoff))
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if(isprint(i))
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
printf("%c", i);
2014-09-24 14:45:10 +00:00
#ifndef NO_FLOATS
printf(":%.1f\n", 100*f[i]/nc);
#else
printf(":%d\n", 100*f[i]/nc);
#endif
f[i]=0;
}
2019-02-12 21:50:49 +00:00
}
2014-09-24 14:45:10 +00:00
}
printf("rest char:freq\n\n");
/* last round ... remaining non printable characters */
2019-02-12 21:50:49 +00:00
for (i = 0; i < 0x100; ++i)
2014-09-24 14:45:10 +00:00
{
2019-02-12 21:50:49 +00:00
if ((f[i]) && ((f[i]/nc) >= cutoff))
2014-09-24 14:45:10 +00:00
{
if(i=='\n')
{
2019-02-12 21:50:49 +00:00
printf("newline");
2014-09-24 14:45:10 +00:00
}
else
{
2019-02-12 21:50:49 +00:00
printf("%03o", i);
2014-09-24 14:45:10 +00:00
}
#ifndef NO_FLOATS
printf(":%.1f\n", 100*f[i]/nc);
#else
printf(":%d\n", 100*f[i]/nc);
#endif
2019-02-12 21:50:49 +00:00
}
2014-09-24 14:45:10 +00:00
}
fclose(in);
2014-09-24 14:45:10 +00:00
return 0;
}