#define MAXWORDS 250
+FILE *in;
+#define getchar() fgetc(in)
+
struct node
{
- int count; /* frequency count */
- struct node *left; /* left subtree */
- struct node *right; /* right subtree */
- char *word; /* word itself */
+ int count; /* frequency count */
+ struct node *left; /* left subtree */
+ struct node *right; /* right subtree */
+ char *word; /* word itself */
} words[MAXWORDS];
-int next; /* index of next free entry in words */
+int next; /* index of next free entry in words */
/*struct node *lookup();*/
int isletter(char c);
-/* err - print error message s and die */
+/* err - print error message s and die */
#ifndef NO_OLD_FUNC_DECL
err(s) char *s; {
#else
int err(char *s) {
#endif
- printf("? %s\n", s);
- exit(1);
+ printf("? %s\n", s);
+ exit(1);
}
/* getword - get next input word into buf, return 0 on EOF */
int getword(char *buf)
#endif
{
- char *s;
- int c;
+ char *s;
+ int c;
while (((c = getchar()) != -1) && (isletter(c) == 0))
- ;
+ ;
for (s = buf; (c = isletter(c)); c = getchar())
- *s++ = c;
- *s = 0;
- if (s > buf)
- return 1;
- return 0;
+ *s++ = c;
+ *s = 0;
+ if (s > buf)
+ return 1;
+ return 0;
}
/* isletter - return folded version of c if it is a letter, 0 otherwise */
int isletter(char c)
{
- if ((c >= 'A') && (c <= 'Z')) c += 'a' - 'A';
- if ((c >= 'a') && (c <= 'z')) return c;
- return 0;
+ if ((c >= 'A') && (c <= 'Z')) c += 'a' - 'A';
+ if ((c >= 'a') && (c <= 'z')) return c;
+ return 0;
}
/* lookup - lookup word in tree; install if necessary */
struct node *lookup(char *word, struct node **p)
#endif
{
- int cond;
-/* char *malloc(); */
-
- if (*p) {
- cond = strcmp(word, (*p)->word);
- if (cond < 0)
- return lookup(word, &(*p)->left);
- else if (cond > 0)
- return lookup(word, &(*p)->right);
- else
- return *p;
- }
- if (next >= MAXWORDS)
- err("out of node storage");
- words[next].count = 0;
- words[next].left = words[next].right = 0;
- words[next].word = malloc(strlen(word) + 1);
- if (words[next].word == 0)
- err("out of word storage");
- strcpy(words[next].word, word);
- return *p = &words[next++];
+ int cond;
+/* char *malloc(); */
+
+ if (*p) {
+ cond = strcmp(word, (*p)->word);
+ if (cond < 0)
+ return lookup(word, &(*p)->left);
+ else if (cond > 0)
+ return lookup(word, &(*p)->right);
+ else
+ return *p;
+ }
+ if (next >= MAXWORDS)
+ err("out of node storage");
+ words[next].count = 0;
+ words[next].left = words[next].right = 0;
+ words[next].word = malloc(strlen(word) + 1);
+ if (words[next].word == 0)
+ err("out of word storage");
+ strcpy(words[next].word, word);
+ return *p = &words[next++];
}
/* tprint - print tree */
#else
void tprint(struct node *tree) {
#endif
- if (tree) {
- tprint(tree->left);
- printf("%d:%s\n", tree->count, tree->word);
- tprint(tree->right);
- }
+ if (tree) {
+ tprint(tree->left);
+ printf("%d:%s\n", tree->count, tree->word);
+ tprint(tree->right);
+ }
}
int main(void)
{
- struct node *root;
- char word[20];
+ struct node *root;
+ char word[20];
+
+ in = fopen("wf1.in","rb");
+ if (in == NULL) {
+ return EXIT_FAILURE;
+ }
- root = 0;
- next = 0;
- while (getword(word))
- lookup(word, &root)->count++;
- tprint(root);
+ root = 0;
+ next = 0;
+ while (getword(word))
+ lookup(word, &root)->count++;
+ tprint(root);
+ fclose(in);
return 0;
}