2 !!DESCRIPTION!! print word frequencies; uses structures
3 !!ORIGIN!! LCC 4.1 Testsuite
4 !!LICENCE!! own, freely distributeable for non-profit. read CPYRIGHT.LCC
16 int count; /* frequency count */
17 struct node *left; /* left subtree */
18 struct node *right; /* right subtree */
19 char *word; /* word itself */
21 int next; /* index of next free entry in words */
23 /*struct node *lookup();*/
25 #if defined(NO_NEW_PROTOTYPES_FOR_OLD_FUNC_DECL) && !defined(NO_OLD_FUNC_DECL)
30 int getword(char *buf);
31 void tprint(struct node *tree);
32 struct node *lookup(char *word, struct node **p);
38 /* err - print error message s and die */
39 #ifndef NO_OLD_FUNC_DECL
48 /* getword - get next input word into buf, return 0 on EOF */
49 #ifndef NO_OLD_FUNC_DECL
50 int getword(buf) char *buf;
52 int getword(char *buf)
58 while (((c = getchar()) != -1) && (isletter(c) == 0))
60 for (s = buf; (c = isletter(c)); c = getchar())
68 /* isletter - return folded version of c if it is a letter, 0 otherwise */
71 if ((c >= 'A') && (c <= 'Z')) c += 'a' - 'A';
72 if ((c >= 'a') && (c <= 'z')) return c;
76 /* lookup - lookup word in tree; install if necessary */
77 #ifndef NO_OLD_FUNC_DECL
78 struct node *lookup(word, p)
79 char *word; struct node **p;
81 struct node *lookup(char *word, struct node **p)
88 cond = strcmp(word, (*p)->word);
90 return lookup(word, &(*p)->left);
92 return lookup(word, &(*p)->right);
97 err("out of node storage");
98 words[next].count = 0;
99 words[next].left = words[next].right = 0;
100 words[next].word = malloc(strlen(word) + 1);
101 if (words[next].word == 0)
102 err("out of word storage");
103 strcpy(words[next].word, word);
104 return *p = &words[next++];
107 /* tprint - print tree */
108 #ifndef NO_OLD_FUNC_DECL
109 void tprint(tree) struct node *tree; {
111 void tprint(struct node *tree) {
115 printf("%d:%s\n", tree->count, tree->word);
127 while (getword(word))
128 lookup(word, &root)->count++;