Annotation of researchv10no/cmd/lcc/tst/wf.0, revision 1.1

1.1     ! root        1: /* wf - print word frequencies */
        !             2: 
        !             3: int count[2000];       /* frequency count */
        !             4: int left[2000];                /* left subtrees */
        !             5: int right[2000];       /* right subtrees */
        !             6: char *words[2000];     /* words */
        !             7: int next;              /* index of next free entry in count, left, etc. */
        !             8: 
        !             9: main()
        !            10: {
        !            11:        int root, lookup(), getword();
        !            12:        char word[20];
        !            13: 
        !            14:        root = 0;
        !            15:        next = 1;
        !            16:        while (getword(word))
        !            17:                ++count[lookup(word, &root)];
        !            18:        tprint(root);
        !            19:        return 0;
        !            20: }
        !            21: 
        !            22: /* err - print error message s and die */
        !            23: err(s)
        !            24: char *s;
        !            25: {
        !            26:        printf("? %s\n", s);
        !            27:        exit(1);
        !            28: }
        !            29: 
        !            30: /* getword - get next input word into buf, return 0 on EOF */
        !            31: int getword(buf)
        !            32: char *buf;
        !            33: {
        !            34:        char *s;
        !            35:        int c;
        !            36: 
        !            37:        while ((c = getchar()) != -1 && isletter(c) == 0)
        !            38:                ;
        !            39:        for (s = buf; c = isletter(c); c = getchar())
        !            40:                *s++ = c;
        !            41:        *s = 0;
        !            42:        if (s > buf)
        !            43:                return 1;
        !            44:        return 0;
        !            45: }
        !            46: 
        !            47: /* isletter - return folded version of c if it is a letter, 0 otherwise */
        !            48: int isletter(c)
        !            49: int c;
        !            50: {
        !            51:        if (c >= 'A' && c <= 'Z')
        !            52:                c += 'a' - 'A';
        !            53:        if (c >= 'a' && c <= 'z')
        !            54:                return c;
        !            55:        return 0;
        !            56: }
        !            57: 
        !            58: /* lookup - lookup word in tree; install if necessary */
        !            59: int lookup(word, p)
        !            60: char *word;
        !            61: int *p;
        !            62: {
        !            63:        int cond;
        !            64:        char *malloc();
        !            65: 
        !            66:        if (*p) {
        !            67:                cond = strcmp(word, words[*p]);
        !            68:                if (cond < 0)
        !            69:                        return lookup(word, &left[*p]);
        !            70:                else if (cond > 0)
        !            71:                        return lookup(word, &right[*p]);
        !            72:                else
        !            73:                        return *p;
        !            74:        }
        !            75:        if (next >= 2000)
        !            76:                err("out of node storage");
        !            77:        count[next] = left[next] = right[next] = 0;     
        !            78:        words[next] = malloc(strlen(word) + 1);
        !            79:        if (words[next] == 0)
        !            80:                err("out of word storage");
        !            81:        strcpy(words[next], word);
        !            82:        return *p = next++;
        !            83: }
        !            84: 
        !            85: /* tprint - print tree */
        !            86: tprint(tree)
        !            87: int tree;
        !            88: {
        !            89:        if (tree) {
        !            90:                tprint(left[tree]);
        !            91:                printf("%d\t%s\n", count[tree], words[tree]);
        !            92:                tprint(right[tree]);
        !            93:        }
        !            94: }
        !            95: 
        !            96: /* strcmp - compare s1 and s2, return <0, 0, or >0 */
        !            97: int strcmp(s1, s2)
        !            98: char *s1, *s2;
        !            99: {
        !           100:        while (*s1 == *s2) {
        !           101:                if (*s1++ == 0)
        !           102:                        return 0;
        !           103:                ++s2;
        !           104:        }
        !           105:        if (*s1 == 0)
        !           106:                return -1;
        !           107:        else if (*s2 == 0)
        !           108:                return 1;
        !           109:        return *s1 - *s2;
        !           110: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.