home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / TDEXMPL.ZIP / BCDEMOB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.6 KB  |  156 lines

  1. /*    file <tcdemob.c>
  2.  *
  3.  *    Broken demo program to show debugging techniques
  4.  *    Reads words from standard input, analyzes letter and word frequency
  5.  *      Copyright (c) 1988, 1989 - Borland Intl.
  6.  */
  7.  
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <ctype.h>
  11.  
  12. #define BUFSIZE            128    /* length of line buffer */
  13. #define LETTERSINALPHABET       26      /* number of letters in the alphabet */
  14. #define MAXWORDLENGTH        10      /* maximum word length allowed */
  15.          
  16. static struct linfo {
  17.         unsigned int count;         /* number of occurrences of this letter */
  18.         unsigned int firstletter;       /* number of times as first letter of a word */
  19. } letterinfo[26];               /* info for each letter */;
  20.  
  21. static unsigned int wordcounts[MAXWORDLENGTH];  /* count of words of each length */
  22. static char buffer[BUFSIZE];            /* line buffer */
  23.  
  24. static int makeintowords(char *bufp);
  25. static long analyzewords(char *bufp);
  26. static void printstatistics(int nlines, int nwords, long charcount);
  27. static int readaline(void);
  28. static void showargs(int argc, char *argv[]);
  29.  
  30. /* program entry point
  31.  */
  32. int main(int argc, char **argv) {
  33.         unsigned int  nlines, nwords, wordcount;
  34.         unsigned long totalcharacters;
  35.  
  36.         nlines = 0;
  37.     nwords = 0;
  38.     totalcharacters = 0;
  39.         showargs(argc, argv);
  40.         while (readaline() != 0) {
  41.                 wordcount = makeintowords(buffer);
  42.                 nwords += wordcount;
  43.                 totalcharacters += analyzewords(buffer);
  44.                 nlines++;
  45.         }
  46.         printstatistics(nwords, nlines, totalcharacters);    /* BUG #2 HERE */
  47.         return(0);
  48. }
  49.  
  50. /* make the buffer into a list of null-terminated words that end in
  51.  * in two nulls, squish out white space
  52.  */
  53. static int makeintowords(char *bufp) {
  54.         unsigned int nwords;
  55.         char *writep;
  56.  
  57.         nwords = 0;
  58.         writep = bufp;
  59.         do {
  60.                 while (*bufp == ' ')
  61.                         bufp++; /* skip white space between words */
  62.                 if (*bufp != 0)
  63.                         nwords++;       /* got the start of a word */
  64.                 while (*bufp && *bufp != ' ')
  65.                         *writep++ = *bufp++;
  66.                 if (*bufp != 0)
  67.                         bufp++; /* skip over space after word */
  68.                 *writep++ = 0;  /* word is null terminated */
  69.         }
  70.         while (*bufp);
  71.         *writep++ = 0;
  72.         return(nwords);
  73. }
  74.  
  75. /* analyze the words in the buffer
  76.  * there is a null between each word and two nulls at the end
  77.  */
  78. static long analyzewords(char *bufp) {
  79.         unsigned long charcount;
  80.     unsigned int  letterindex;
  81.  
  82.         charcount = 0;
  83.         while (*bufp != 0) {
  84.                 char first = 1;
  85.                 int wordlen = 0;
  86.                 while (*bufp != 0) {
  87.                         letterindex = toupper(*bufp) - 'A'; /* 0-based index */
  88.                         if (first) {
  89.                                 letterinfo[letterindex].firstletter++;
  90.                                 first = 0;
  91.                         }
  92.                         letterinfo[letterindex].count++;        /* count the letter */
  93.                         charcount++;
  94.                         wordlen++;
  95.                         bufp++;
  96.         }    /* BUG #1 AFTER THIS LINE */
  97.                 wordcounts[wordlen - 1]++;      /* count a word of this length */
  98.         }
  99.         return(charcount);
  100. }
  101.  
  102. /* display all the statistics
  103.  */
  104. static void printstatistics(int nlines, int nwords, long charcount) {
  105.         unsigned int n, count;
  106.         float    averagelength;
  107.  
  108.         if (nlines == 0)
  109.                 printf("No lines entered\n");
  110.         else {
  111.                 averagelength = (float)nwords / nlines;
  112.                 printf("Total number of letters = %ld\n", charcount);
  113.                 printf("Total number of lines = %d\n", nlines);
  114.                 printf("Total word count = %d\n", nwords);
  115.                 printf("Average number of words per line = %g\n", averagelength);
  116.                 for (n = 0; n < LETTERSINALPHABET; n++) {
  117.                         count = letterinfo[n].count;     
  118.                         if (count > 0)
  119.                                 printf("'%c' occurs %u times, %u times at start of a word\n",
  120.                                  'A' + n, count, letterinfo[n].firstletter);
  121.                 }
  122.                 for (n = 0; n < MAXWORDLENGTH; n++) {
  123.                         count = wordcounts[n];
  124.                         if (count > 0) {
  125.                                 if (count == 1)
  126.                                         printf("There is 1 word");
  127.                                 else
  128.                                         printf("There are %d words", count);
  129.                                 printf(" %d character", n + 1);
  130.                                 if (n > 0)
  131.                                         printf("s");    /* make it plural */
  132.                                 printf(" long\n");
  133.                         }
  134.                 }
  135.         }
  136. }
  137.  
  138. /* read a line from the standard input into "buffer"
  139.  * returns 1 for ok, 0 for end of file or empty line
  140.  */
  141. static int readaline() {
  142.         printf("Enter a line (empty line to end): ");
  143.         if (gets(buffer) == 0)
  144.                 return(0);      /* end of file */
  145.         return(*buffer == 0 ? 0 : 1);   /* empty line is also end */
  146. }
  147.  
  148. /* display the arguments the program was called with
  149.  */
  150. static void showargs(int argc, char *argv[]) {
  151.         printf("Arguments: ");
  152.         while (--argc)
  153.                 printf("%s ", *++argv);
  154.         printf("\n");
  155. }
  156.