home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CBUFF09.ZIP / SRC.ZIP / STATS.C < prev    next >
C/C++ Source or Header  |  1993-11-16  |  5KB  |  202 lines

  1. /*  $Id$
  2.  *  
  3.  *  File    stats.c
  4.  *  Part of    ChessBase utilities file format (CBUFF)
  5.  *  Author    Anjo Anjewierden, anjo@swi.psy.uva.nl
  6.  *  Purpose    Statistics utility
  7.  *  Works with    GNU CC 2.4.5
  8.  *  
  9.  *  Notice    Copyright (c) 1993  Anjo Anjewierden
  10.  *  
  11.  *  History    15/10/93  (Created)
  12.  *          15/10/93  (Last modified)
  13.  */ 
  14.  
  15.  
  16. /*------------------------------------------------------------
  17.  *  Directives
  18.  *------------------------------------------------------------*/
  19.  
  20. #include "cbuff.h"
  21.  
  22. char *        UTILITY_NAME = "Statistics utility";
  23. char *        UTILITY_VERSION = "1.1.0";
  24.  
  25. void        helpUtility(FILE *fd);
  26.  
  27. static void    statisticsUtility(Option);
  28. static void    statisticsGame(Game);
  29.  
  30. static void    resetStatistics(void);
  31. static void    reportStatistics(FILE *);
  32.  
  33.  
  34. /*------------------------------------------------------------
  35.  *  Definitions
  36.  *------------------------------------------------------------*/
  37.  
  38. long        NoGames;        /* Total number of games */
  39. long        NoWhiteWins;        /* Games won by White */
  40. long        NoBlackWins;        /* Games won by Black */
  41. long        NoDraws;        /* Games drawn */
  42. long        NoLines;        /* Games with no known result */
  43.  
  44. long        NoYear[2100];        /* Games per year */
  45.  
  46.  
  47. /*------------------------------------------------------------
  48.  *  Statistics utility
  49.  *------------------------------------------------------------*/
  50.  
  51. static void
  52. statisticsUtility(Option opt)
  53. { Game g = newGame();
  54.   CBase cb = opt->database;
  55.   long from = opt->from;
  56.   long to = (opt->to < getNoGamesCBase(cb) ? opt->to : getNoGamesCBase(cb));
  57.   long n;
  58.  
  59.   reportCBase(cb, stderr);
  60.  
  61.   resetStatistics();
  62.   
  63.   for (n=from; n<=to; n++)
  64.   { environmentError(cb, g, n);
  65.     initialiseGame(g, n, cb);
  66.     if (foundError())
  67.     { reportError(stderr);
  68.       continue;
  69.     }
  70.     statisticsGame(g);
  71.     if (foundError())
  72.       reportError(stderr);
  73.   }
  74.  
  75.   reportStatistics(opt->output);
  76.  
  77.   freeGame(g);
  78. }
  79.  
  80.  
  81. /*------------------------------------------------------------
  82.  *  Statistics of a single game
  83.  *------------------------------------------------------------*/
  84.  
  85. static void
  86. statisticsGame(Game g)
  87. { NoGames++;
  88.  
  89.   switch (getResultGame(g))
  90.   { case WHITE_WINS:    NoWhiteWins++; break;
  91.     case DRAW:        NoDraws++; break;
  92.     case BLACK_WINS:    NoBlackWins++; break;
  93.     default:        NoLines++;
  94.   }
  95.   NoYear[getYearGame(g)]++;
  96. }
  97.  
  98.  
  99. /*------------------------------------------------------------
  100.  *  Functions
  101.  *------------------------------------------------------------*/
  102.  
  103. static float    pct(long, long);
  104.  
  105.  
  106. static float
  107. pct(long n, long m)
  108. { return 100.0 * ((float) n / (float) m);
  109. }
  110.  
  111.  
  112. static void
  113. resetStatistics()
  114. { NoWhiteWins = 0;
  115.   NoBlackWins = 0;
  116.   NoDraws = 0;
  117.   NoLines = 0;
  118.   NoGames = 0;
  119. }
  120.  
  121.  
  122. static void
  123. reportStatistics(FILE *fd)
  124. { int year;
  125.  
  126.   fprintf(fd, "\n");
  127.   fprintf(fd, "Total games     %6ld\n", NoGames);
  128.   fprintf(fd, "======================\n");
  129.   fprintf(fd, "Won by White    %6ld  (%4.1f%%)\n",
  130.       NoWhiteWins, pct(NoWhiteWins,NoGames));
  131.   fprintf(fd, "Won by Black    %6ld  (%4.1f%%)\n",
  132.       NoBlackWins, pct(NoBlackWins,NoGames));
  133.   fprintf(fd, "Drawn           %6ld  (%4.1f%%)\n",
  134.       NoDraws, pct(NoDraws, NoGames));
  135.   fprintf(fd, "Lines           %6ld  (%4.1f%%)\n",
  136.       NoLines, pct(NoLines, NoGames));
  137.  
  138.   fprintf(fd, "\n");
  139.   fprintf(fd, "Distribution by year\n");
  140.   fprintf(fd, "====================\n");
  141.   if (NoYear[0] == 0)
  142.     fprintf(fd, "Unknown %6ld  (%4.1f%%)\n",
  143.         NoYear[0], pct(NoYear[0], NoGames));
  144.   for (year=1; year<2100; year++)
  145.     if (NoYear[year])
  146.       fprintf(fd, "%4d     %6ld  (%4.1f%%)\n",
  147.           year, NoYear[year], pct(NoYear[year], NoGames));
  148. }
  149.  
  150.  
  151. /*------------------------------------------------------------
  152.  *  Main
  153.  *------------------------------------------------------------*/
  154.  
  155. int
  156. main(int argc, char *argv[])
  157. { int i;
  158.   Option options = newOption();
  159.  
  160.   initChessSymbols();
  161.  
  162.   for (i=1; i<argc; i++)
  163.   {
  164.     if (strhead(argv[i], "-"))
  165.     { int n;
  166.  
  167.       n = genericOption(options, argv, argc, i);
  168.       if (n == 0)
  169.       { fprintf(stderr, "Fatal: Unknown command %s\n", argv[i]);
  170.     fprintf(stderr, "Do ``%s -help'' or see the documentation\n", argv[0]);
  171.     exit(1);
  172.       }
  173.       i = n;
  174.       continue;
  175.     }
  176.  
  177.     setCurrentCBase(argv[i], "-database", argc, i);
  178.     options->database = CurrentBase;
  179.     statisticsUtility(options);
  180.     freeCBase(options->database);
  181.     options->database = (CBase) NULL;
  182.   }
  183.  
  184.   if (options->database)
  185.   { statisticsUtility(options);
  186.     freeCBase(options->database);
  187.   }
  188.  
  189.   exit(0);
  190. }
  191.  
  192.  
  193. /*------------------------------------------------------------
  194.  *  Help
  195.  *------------------------------------------------------------*/
  196.  
  197. void
  198. helpUtility(FILE *fd)
  199. { helpCBUFF(fd);
  200.   fprintf(fd, "%s options:\n", UTILITY_NAME);
  201. }
  202.