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