2 !!DESCRIPTION!! print word frequencies; uses structures
3 !!ORIGIN!! LCC 4.1 Testsuite
4 !!LICENCE!! own, freely distributeable for non-profit. read CPYRIGHT.LCC
15 #define getchar() fgetc(in)
19 int count; /* frequency count */
20 struct node *left; /* left subtree */
21 struct node *right; /* right subtree */
22 char *word; /* word itself */
24 int next; /* index of next free entry in words */
26 /*struct node *lookup();*/
28 #if defined(NO_NEW_PROTOTYPES_FOR_OLD_FUNC_DECL) && !defined(NO_OLD_FUNC_DECL)
33 int getword(char *buf);
34 void tprint(struct node *tree);
35 struct node *lookup(char *word, struct node **p);
41 /* err - print error message s and die */
42 #ifndef NO_OLD_FUNC_DECL
51 /* getword - get next input word into buf, return 0 on EOF */
52 #ifndef NO_OLD_FUNC_DECL
53 int getword(buf) char *buf;
55 int getword(char *buf)
61 while (((c = getchar()) != -1) && (isletter(c) == 0))
63 for (s = buf; (c = isletter(c)); c = getchar())
71 /* isletter - return folded version of c if it is a letter, 0 otherwise */
74 if ((c >= 'A') && (c <= 'Z')) c += 'a' - 'A';
75 if ((c >= 'a') && (c <= 'z')) return c;
79 /* lookup - lookup word in tree; install if necessary */
80 #ifndef NO_OLD_FUNC_DECL
81 struct node *lookup(word, p)
82 char *word; struct node **p;
84 struct node *lookup(char *word, struct node **p)
91 cond = strcmp(word, (*p)->word);
93 return lookup(word, &(*p)->left);
95 return lookup(word, &(*p)->right);
100 err("out of node storage");
101 words[next].count = 0;
102 words[next].left = words[next].right = 0;
103 words[next].word = malloc(strlen(word) + 1);
104 if (words[next].word == 0)
105 err("out of word storage");
106 strcpy(words[next].word, word);
107 return *p = &words[next++];
110 /* tprint - print tree */
111 #ifndef NO_OLD_FUNC_DECL
112 void tprint(tree) struct node *tree; {
114 void tprint(struct node *tree) {
118 printf("%d:%s\n", tree->count, tree->word);
128 in = fopen("wf1.in","rb");
135 while (getword(word))
136 lookup(word, &root)->count++;