/******************************************************/
/*                                                    */
/* digit.c                                            */ 
/*                                                    */
/* Author: Stefan Spaennare, Lund, Sweden             */
/*                                                    */
/* E-mail: stefan@spaennare.se                        */
/*                                                    */
/* First version: September 1993                      */
/* Latest update: 2007-08-13                          */
/*                                                    */
/* Compilation: >gcc -O3 -o digit digit.c -lm         */
/*                                                    */
/* This program counts sequences of up to 10 equal    */
/* digits in an ascii file. The program can be used   */
/* to check if for example the first digits of Pi     */
/* seems to have a normal distribution.               */ 
/*                                                    */
/* Total = The number of digits in the table.         */
/* TOTAL = The total number of digits in the file.    */
/*                                                    */
/******************************************************/

/********************************************************************************/
/*                                                                              */
/* Notice                                                                       */
/* ======                                                                       */
/*                                                                              */
/* I make no warranties that this program is (1) free of errors, (2) consistent */
/* with any standard merchantability, or (3) meeting the requirements of a      */
/* particular application. This software shall not, partly or as a whole,       */
/* participate in a process, whose outcome can result in injury to a person or  */
/* loss of property. It is solely designed for analytical work. Permission to   */
/* use, copy and distribute is hereby granted without fee, providing that the   */
/* header above including this notice appears in all copies.                    */
/*                                                                              */
/*                                                            Stefan Spaennare  */
/*                                                                              */
/********************************************************************************/


#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>


int main(argc,argv) 
int argc;
char *argv[];
{

   FILE *infile;

   int s[10][21];
   int r[11];
  
   int c,d,e,tot,ttot,ttot2,flag,i,j;


   if (argc != 2) {
      printf("Usage: %s infile\n",argv[0]);
      exit(0);
   } /* if */
   

   if ((infile = fopen(argv[1],"r")) == NULL) {
      printf("File %s not found.\n",argv[1]);
      exit(0);
   } /* if */


   for (i=0; i<=9; i++) {
      r[i]=1;
   } /* for i */

   for (i=0; i<=9; i++) {
       for (j=1; j<=20; j++) {
          s[i][j]=0;
       } /* for j */
   } /* for i */

   ttot2=0;

   flag=0;

   while ((flag==0) && ((c=getc(infile)) != EOF)) {
      if (c>='0' && c<='9') {
         d=c-48;
         e=d;
         ttot2++;
         flag=1;
      }
   } /* while */

   while ((c=getc(infile)) != EOF) {
      if (c>='0' && c<='9') {
         d=c-48;
         if (d==e) {
            r[d]++;
         }
         else {
            if (r[e] <= 20) {
               s[e][r[e]]++;
            } /* if */
            r[e]=1;
         }
         e=d;
         ttot2++;
      } /* if */
   } /* while */

   if (flag==1) {
      if (r[e] <= 20) {
         s[e][r[e]]++;
      } /* if */
   } /* if */

   printf("\n");
   printf("           1         2        3      4     5     6");
   printf("    7    8   9  10");
   printf("        tot\n\n");

   ttot=0;
   for (i=0; i<=9; i++) {
      tot=0;
      printf("%1d:",i);
      printf("%10d %9d %8d %6d %5d %5d %4d %4d %3d %3d",
             s[i][1],s[i][2],s[i][3],s[i][4],s[i][5],
             s[i][6],s[i][7],s[i][8],s[i][9],s[i][10]);
      for (j=1; j<=10; j++) {
         tot=tot+s[i][j]*j;
      } /* for j */
      printf("%11d\n",tot);
      ttot=ttot+tot;
   } /* for i */

   printf("\n");
   printf("Total = %10d;  TOTAL = %10d\n",ttot,ttot2);

   fclose(infile);


} /* End */
