Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

wf1.c

Go to the documentation of this file.
00001 /* wf1 - print word frequencies; uses structures */
00002 
00003 struct node {
00004     int count;      /* frequency count */
00005     struct node *left;  /* left subtree */
00006     struct node *right; /* right subtree */
00007     char *word;     /* word itself */
00008 } words[2000];
00009 int next;       /* index of next free entry in words */
00010 
00011 struct node *lookup();
00012 
00013 main() {
00014     struct node *root;
00015     char word[20];
00016 
00017     root = 0;
00018     next = 0;
00019     while (getword(word))
00020         lookup(word, &root)->count++;
00021     tprint(root);
00022     return 0;
00023 }
00024 
00025 /* err - print error message s and die  */
00026 err(s) char *s; {
00027     printf("? %s\n", s);
00028     exit(1);
00029 }
00030 
00031 /* getword - get next input word into buf, return 0 on EOF */
00032 int getword(buf) char *buf; {
00033     char *s;
00034     int c;
00035 
00036     while ((c = getchar()) != -1 && isletter(c) == 0)
00037         ;
00038     for (s = buf; c = isletter(c); c = getchar())
00039         *s++ = c;
00040     *s = 0;
00041     if (s > buf)
00042         return (1);
00043     return (0);
00044 }
00045 
00046 /* isletter - return folded version of c if it is a letter, 0 otherwise */
00047 int isletter(c) {
00048     if (c >= 'A' && c <= 'Z')
00049         c += 'a' - 'A';
00050     if (c >= 'a' && c <= 'z')
00051         return (c);
00052     return (0);
00053 }
00054 
00055 /* lookup - lookup word in tree; install if necessary */
00056 struct node *lookup(word, p) char *word; struct node **p; {
00057     int cond;
00058     char *malloc();
00059 
00060     if (*p) {
00061         cond = strcmp(word, (*p)->word);
00062         if (cond < 0)
00063             return lookup(word, &(*p)->left);
00064         else if (cond > 0)
00065             return lookup(word, &(*p)->right);
00066         else
00067             return *p;
00068     }
00069     if (next >= 2000)
00070         err("out of node storage");
00071     words[next].count = 0;
00072     words[next].left = words[next].right = 0;
00073     words[next].word = malloc(strlen(word) + 1);
00074     if (words[next].word == 0)
00075         err("out of word storage");
00076     strcpy(words[next].word, word);
00077     return *p = &words[next++];
00078 }
00079 
00080 /* tprint - print tree */
00081 tprint(tree) struct node *tree; {
00082     if (tree) {
00083         tprint(tree->left);
00084         printf("%d\t%s\n", tree->count, tree->word);
00085         tprint(tree->right);
00086     }
00087 }
00088 
00089 /* strcmp - compare s1 and s2, return <0, 0, or >0 */
00090 int strcmp(s1, s2) char *s1, *s2; {
00091     while (*s1 == *s2) {
00092         if (*s1++ == 0)
00093             return 0;
00094         ++s2;
00095     }
00096     if (*s1 == 0)
00097         return -1;
00098     else if (*s2 == 0)
00099         return 1;
00100     return *s1 - *s2;
00101 }

Generated on Thu Aug 25 12:38:17 2005 for Quake III Arena by  doxygen 1.3.9.1