home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STATS.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  63 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /****************************************************************/
  4. /*                                                              */
  5. /*      Collect file statistics                                 */
  6. /*                                                              */
  7. /*      Public domain demo program for analyzing encrypted      */
  8. /*      files. By: Bob Stout                                    */
  9. /*                                                              */
  10. /****************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include "errors.h"           /* For cant()     */
  16.  
  17. #ifdef __WATCOMC__
  18.  #pragma off (unreferenced);
  19. #endif
  20. #ifdef __TURBOC__
  21.  #pragma argsused
  22. #endif
  23.  
  24. main(int argc, char *argv[])
  25. {
  26.       int i, ch, hist = 0;
  27.       long n = 0L;
  28.       double mean = 0., stdev = 0., ftmp;
  29.       static unsigned bins[256];
  30.       FILE *infile;
  31.  
  32.       infile = cant(argv[1], "rb");
  33.       while (!feof(infile))
  34.       {
  35.             if (EOF == (ch = fgetc(infile)))
  36.                   break;
  37.             bins[ch] += 1;
  38.             ++n;
  39.       }
  40.       fclose(infile);
  41.       for (i = 0; i < 256; ++i)
  42.       {
  43.             mean += (double)(bins[i]);
  44.             if (bins[i])
  45.                   ++hist;
  46.       }
  47.       mean /= 256.;
  48.       for (i = 0; i < 256; ++i)
  49.       {
  50.             ftmp = (double)(bins[i]) - mean;
  51.             stdev += (ftmp * ftmp);
  52.       }
  53.       ftmp  = stdev / 255.;
  54.       stdev = sqrt(ftmp);
  55.       printf("%ld Characters were read from %s\n"
  56.             "There are an average of %f occurrences of each character\n"
  57.             "%d Characters out of 256 possible were used\n"
  58.             "The standard deviation is %f\n"
  59.             "The coefficient of variation is %f%%\n",
  60.             n, argv[1], mean, hist, stdev, (100. * stdev) / mean);
  61.       return EXIT_SUCCESS;
  62. }
  63.