|
| 1 | +#include <stdio.h> |
| 2 | +#include <ctype.h> |
| 3 | +#include <string.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +#define MAXWORD 100 |
| 7 | +#define MAX_LINES 100 |
| 8 | + |
| 9 | +struct tnode { |
| 10 | + char *word; |
| 11 | + int count; |
| 12 | + int lines[MAX_LINES]; |
| 13 | + struct tnode *left; |
| 14 | + struct tnode *right; |
| 15 | +}; |
| 16 | + |
| 17 | +struct tnode *addtree(struct tnode *, char *); |
| 18 | +void treeprint(struct tnode *); |
| 19 | +int getword(char *, int); |
| 20 | + |
| 21 | +static int current_line = 1; |
| 22 | + |
| 23 | +// gcc 7.words-appear-lines.c getch.c |
| 24 | +main() |
| 25 | +{ |
| 26 | + struct tnode *root; |
| 27 | + char word[MAXWORD]; |
| 28 | + |
| 29 | + root = NULL; |
| 30 | + while (getword(word, MAXWORD) != EOF) |
| 31 | + if (isalpha(word[0])) |
| 32 | + root = addtree(root, word); |
| 33 | + treeprint(root); |
| 34 | + |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +int getword(char *word, int lim) |
| 39 | +{ |
| 40 | + int c, getch(void); |
| 41 | + void ungetch(int); |
| 42 | + char *w = word; |
| 43 | + |
| 44 | + do { |
| 45 | + c = getch(); |
| 46 | + if (c == '\n') |
| 47 | + ++current_line; |
| 48 | + } while (isspace(c)); |
| 49 | + |
| 50 | + if (c != EOF) |
| 51 | + *w++ = c; |
| 52 | + if (!isalpha(c)) { |
| 53 | + *w = '\0'; |
| 54 | + return c; |
| 55 | + } |
| 56 | + for ( ; --lim > 0; w++) |
| 57 | + if (!isalnum(*w = getch()) && *w != '_') { |
| 58 | + ungetch(*w); |
| 59 | + break; |
| 60 | + } |
| 61 | + *w = '\0'; |
| 62 | + return word[0]; |
| 63 | +} |
| 64 | + |
| 65 | +struct tnode *talloc(void); |
| 66 | +char *mystrdup(char *s); |
| 67 | + |
| 68 | +struct tnode *addtree(struct tnode *p, char *w) |
| 69 | +{ |
| 70 | + int cond; |
| 71 | + int i; |
| 72 | + |
| 73 | + if (p == NULL) { |
| 74 | + p = talloc(); |
| 75 | + p->word = mystrdup(w); |
| 76 | + p->count = 1; |
| 77 | + p->lines[0] = current_line; |
| 78 | + p->left = p-> right = NULL; |
| 79 | + } else if ((cond = strcmp(w, p->word)) == 0) { |
| 80 | + p->count++; |
| 81 | + i = 0; |
| 82 | + while (p->lines[i] != 0) |
| 83 | + ++i; |
| 84 | + //printf("i: %d\n", i); |
| 85 | + p->lines[i] = current_line; |
| 86 | + } else if (cond < 0) |
| 87 | + p->left = addtree(p->left, w); |
| 88 | + else |
| 89 | + p->right = addtree(p->right, w); |
| 90 | + return p; |
| 91 | +} |
| 92 | + |
| 93 | +void treeprint(struct tnode *p) |
| 94 | +{ |
| 95 | + int i = 0; |
| 96 | + if (p != NULL) { |
| 97 | + treeprint(p->left); |
| 98 | + printf("%4d %s ", p->count, p->word); |
| 99 | + while (p->lines[i] != 0 && i < MAX_LINES) |
| 100 | + printf("%d ", p->lines[i++]); |
| 101 | + printf("\n"); |
| 102 | + treeprint(p->right); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +struct tnode *talloc(void) |
| 107 | +{ |
| 108 | + return (struct tnode *) malloc(sizeof(struct tnode)); |
| 109 | +} |
| 110 | + |
| 111 | +char *mystrdup(char *s) |
| 112 | +{ |
| 113 | + char *p; |
| 114 | + |
| 115 | + p = (char *) malloc(strlen(s)+1); |
| 116 | + if (p != NULL) |
| 117 | + strcpy(p, s); |
| 118 | + |
| 119 | + return p; |
| 120 | +} |
| 121 | + |
0 commit comments