home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 195_01 / frqncy.c < prev    next >
Text File  |  1987-10-05  |  2KB  |  114 lines

  1. /* [FRQNCY.C of JUGPDS Vol.18]
  2. *****************************************************************
  3. *                                *
  4. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  5. *            49-114 Kawauchi-Sanjuunin-machi        *
  6. *            Sendai, Miyagi 980                          *
  7. *            Phone: 0222-61-3219                *
  8. *                                *
  9. *    Edited & tested by Y. Monma (JUG-C/M Disk Editor)       * 
  10. *                                *
  11. *****************************************************************
  12. */
  13.  
  14. /* term - produce word frequency list for a text
  15.             K&P Exercise 4-24 in p.126 */
  16.  
  17. #include "stdio.h"
  18. #include "def.h"
  19. #include <dio.h>
  20.  
  21. #define MAXWORD 64
  22.  
  23. struct tnode {
  24.     char  *word;
  25.     int   count;
  26.     struct tnode *left;
  27.     struct tnode *right;
  28. };
  29.  
  30. char    opt_f;        /* fold order        */
  31.  
  32.  
  33. main(argc, argv)
  34. int    argc;
  35. char *argv[];
  36.  
  37. {
  38.     struct tnode *root, *tree();
  39.     char word[MAXWORD], *ap;
  40.     int  t;
  41.  
  42.     dioinit(&argc, argv);
  43.     if (argc < 2) {
  44.         error("Usage: frqncy <infile >outfile ^Z");
  45.         exit();
  46.         }
  47.     else
  48.     opt_f = OFF;
  49.     while (--argc > 0) {
  50.         ap = *++argv;
  51.         if (*ap++ == '-')
  52.             if (tolower(*ap) == 'f')
  53.                 opt_f = ON;
  54.         }
  55.     root = NULL;
  56.     while ((t = getword(word, MAXWORD)) != EOF) {
  57.         if (t == LETTER) {
  58.             if ((root = tree(root, word)) == NULL)
  59.                 exit(puts("alloc overflow."));
  60.             }
  61.         }
  62.     treeprint(root);
  63.     dioflush();
  64. }
  65.  
  66.  
  67. struct tnode *tree(p, w)
  68. struct tnode *p;
  69. char *w;
  70.  
  71. {
  72.     struct tnode *talloc();
  73.     char *strsave();
  74.     int  cond;
  75.  
  76.     if (p == NULL)
  77.         if (((p = talloc()) == NULL) || (p->word = strsave(w)) == NULL)
  78.             return NULL;
  79.         else {
  80.             p->count = 1;
  81.             p->left = p->right = NULL;
  82.             }
  83.     else if ((cond = (opt_f == ON) ? strfcmp(w, p->word)
  84.                      : strcmp(w, p->word)) == 0)
  85.         p->count++;
  86.     else if (cond < 0)
  87.         p->left = tree(p->left, w);
  88.     else
  89.         p->right = tree(p->right, w);
  90.     return(p);
  91. }
  92.  
  93.  
  94. treeprint(p)
  95. struct tnode *p;
  96.  
  97. {
  98.     if (p != NULL) {
  99.         treeprint(p->left);
  100.         printf("%5d: %s\n", p->count, p->word);
  101.         treeprint(p->right);
  102.     }
  103. }
  104.  
  105.  
  106. struct tnode *talloc()
  107.  
  108. {
  109.     char *alloc();
  110.     struct tnode *p;
  111.  
  112.     return(p = alloc(sizeof(*p)));
  113. }
  114.