home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / PWBTUTOR / COUNT.C$ / COUNT
Encoding:
Text File  |  1991-08-12  |  2.7 KB  |  110 lines

  1. // COUNT.C - Generate text statistics for text file.
  2. // Multimodule example program used in the PWB tutorial.
  3. //
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "count.h"
  8.  
  9. // Conditional operator prevents divide by zero
  10. #define NONZERO(val) (val ? val : 1)
  11.  
  12. // Vowels per syllable in typical English text
  13. #define FACTOR 1.1
  14.  
  15. char Buffer[BUFFSIZE];
  16. long Bytes      = 0;
  17. long Characters = 0;
  18. long Words      = 0;
  19. long Lines      = 0;
  20. long Letters    = 0;
  21. long Vowels     = 0;
  22. long Consonants = 0;
  23. long Sentences  = 0;
  24.  
  25. int main( int argc, char *argv[] );
  26. int CountFile( char *name );
  27. float Syllables( long cVowels, long cWords );
  28.  
  29. int main( int argc, char *argv[] )
  30. {
  31.     char FileName[65];
  32.     int curArg;
  33.  
  34.     if( argc > 1 )
  35.     {
  36.         for( curArg = 1; curArg < argc; curArg++ )
  37.         {
  38.             if( CountFile( argv[curArg] ) )
  39.                 return( 1 );
  40.  
  41.             // Reinitialize counters
  42.             Bytes      = 0;
  43.             Characters = 0;
  44.             Words      = 0;
  45.             Lines      = 0;
  46.             Letters    = 0;
  47.             Vowels     = 0;
  48.             Sentences  = 0;
  49.         }
  50.     }
  51.     else
  52.     {
  53.         // Get a file when one was not specified as an argument.
  54.         printf( "\n\nEnter file name: " );
  55.         if( gets( FileName ) != NULL )
  56.             return( CountFile( FileName ) );
  57.         else
  58.             return 1;
  59.     }
  60.  
  61.  
  62. }
  63.  
  64. int CountFile( char *name )
  65. {
  66.     FILE *File;
  67.     int nMax;
  68.     FLAG InWord;
  69.  
  70.     // Open file in binary mode.
  71.     if( (File = fopen( name, "rb" )) == NULL )
  72.     {
  73.         printf( "\nCan't open %s\n", name );
  74.         return ( 1 );
  75.     }
  76.  
  77.     InWord = FALSE;
  78.     // Read file buffers
  79.     while( ( nMax = fread( Buffer, 1, BUFFSIZE, File ) ) != 0 )
  80.         InWord = CountWords( InWord, nMax );
  81.  
  82.     // Calculate and print the results.
  83.     printf( "\n\nFile statistics for %s\n\n", name );
  84.     printf( "\tBytes:      %6ld\n", Bytes );
  85.     printf( "\tCharacters: %6ld\n", Characters );
  86.     printf( "\tLetters:    %6ld\n", Letters );
  87.     printf( "\tVowels:     %6ld\n", Vowels );
  88.     printf( "\tConsonants: %6ld\n", Letters - Vowels );
  89.     printf( "\tWords:      %6ld\n", Words );
  90.     printf( "\tLines:      %6ld\n", Lines ? Lines : 1 );
  91.     printf( "\tSentences:  %6ld\n\n", Sentences );
  92.  
  93.     printf( "\tWords per sentence:           %4.1f\n",
  94.         (float)Words / NONZERO(Sentences) );
  95.  
  96.     printf( "\tLetters per word:             %4.1f\n",
  97.         (float)Letters / NONZERO(Words) );
  98.  
  99.     printf( "\tEstimated syllables per word: %4.1f\n\n",
  100.         (Vowels * FACTOR) / NONZERO(Words) );
  101.  
  102.     return fclose( File );
  103.  
  104. }
  105.  
  106. float Syllables( long cVowels, long cWords )
  107. {
  108.     return (float)((cVowels * FACTOR) / NONZERO(cWords));
  109. }
  110.