Datei: p8-2.c


    1   /*
    2    *      p8-2.c
    3    *      Beispielprogramm 2, Abschnitt 8
    4    *      Haeufigkeit von Woertern zaehlen
    5    */
    6   
    7   #include <stdio.h>
    8   #include <string.h>
    9   #include <stdlib.h>
   10   
   11   #define MAXWORD 32
   12   #define LETTER  'a'
   13   #define DIGIT   '0'
   14   
   15   /* binaerer Baum */
   16   struct tnode {
   17     char *word;           /* zeigt auf den Text */
   18     int count;            /* Haeufigkeit */
   19     struct tnode *left;   /* linker Nachfolger */
   20     struct tnode *right;  /* rechter Nachfolger */
   21   };
   22         
   23   struct tnode *tree(struct tnode *, char *);
   24   void treeprint(struct tnode *p);
   25   struct tnode *talloc(void);
   26   char *strsave(char *);
   27   int getword(char *, int);
   28   int type(int);
   29   
   30   void main(void)
   31   {
   32     struct tnode *root;
   33     char word[MAXWORD];
   34     int t;
   35     
   36     root = NULL;
   37     while ((t = getword(word, MAXWORD)) != EOF)
   38       if (t == LETTER)
   39         root = tree(root, word);
   40         
   41     treeprint(root);
   42   
   43   } /* main() */
   44   
   45   /* w bei oder nach p einfuegen */
   46   struct tnode *tree(struct tnode *p, char *w)
   47   {
   48     int cond;
   49     
   50     if (p == NULL) {      /* ein neues Wort */
   51       p = talloc();       /* einen neuen Knoten anlegen */
   52       p->word = strsave(w);
   53       p->count = 1;
   54       p->left = p->right = NULL;
   55     } 
   56     else if ((cond = strcmp(w, p->word)) == 0)
   57       p->count++;         /* Wort wird wiederholt */
   58     else if (cond < 0)    /* kleiner, links darunter */
   59       p->left = tree(p->left, w);
   60     else                  /* groesser, rechts darunter */
   61       p->right = tree(p->right, w);
   62     return(p);
   63     
   64   } /* tree() */
   65   
   66   /* Baum p rekursiv ausgeben */
   67   void treeprint(struct tnode *p)
   68   {
   69     if (p != NULL) {
   70       treeprint(p->left);
   71       printf("%4d %s\n",p->count, p->word);
   72       treeprint(p->right);
   73     }
   74     
   75   } /* treeprint() */
   76   
   77   /* Platz für eine Struktur besorgen */
   78   struct tnode *talloc(void)
   79   {
   80     return((struct tnode *) calloc(1, sizeof(struct tnode)));
   81     
   82   } /* talloc() */
   83   
   84   /* Zeichenkette s in Sicherheit bringen */
   85   char *strsave(char *s)
   86   {
   87     char *p;
   88     
   89     if ((p = (char *) malloc(strlen(s) + 1)) != NULL)
   90       strcpy(p, s);
   91     return (p);
   92     
   93   } /* strsave() */
   94   
   95   #define getch() getc(stdin)
   96   #define ungetch(c) ungetc(c, stdin)
   97    
   98   /* naechstes Wort aus der Eingabe holen */
   99   int getword(char *w, int lim)
  100   {
  101     int c, t;
  102     
  103     if (type(c = *w++ = getch()) != LETTER) {
  104       *w = '\0';
  105       return(c);
  106     }
  107     while (--lim > 0) {
  108       t = type(c= *w++ = getch());
  109       if (t != LETTER && t != DIGIT) {
  110         ungetch(c);
  111         break;
  112       }
  113     }
  114     *(w-1) = '\0';
  115     return(LETTER);
  116   
  117   } /* getword() */
  118   
  119   /* Typ eines ASCII Zeichens liefern */
  120   int type(int c)
  121   {
  122     if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
  123       return(LETTER);
  124     else if (c >= '0' && c <= '9')
  125       return(DIGIT);
  126     else
  127       return(c);
  128   
  129   } /* type() */


Erzeugt von c2html 1.01