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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* Exercise 6-3
* Write a cross-referencer that prints a list of all words in a document, and,
* for each word, a list of the line numbers on which it occurs.
* Remove noise words like "the," "and," and so on.
*/
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define MAXWORDSIZE 100
#define BLACKWORDS (sizeof(blacklist) / sizeof(blacklist[0]))
// Sorted word blacklist
char *blacklist[] = { "a", "and", "of", "the" };
bool checkchar(char c)
{
//return isalpha(c) || c == '\'';
return isalpha(c);
}
int getword(char *dest, size_t max)
{
char c;
char *w = dest;
while (!checkchar(c = getchar()) && c != EOF) {
if (c == '\n') {
goto exit;
}
}
if (c == EOF) {
goto exit;
}
*w++ = tolower(c);
for (; --max; w++) {
if (!checkchar(c = getchar())) {
break;
}
*w = tolower(c);
}
exit:
*w = '\0';
return c;
}
bool checkword(char *w)
{
int l = 0;
int r = BLACKWORDS - 1;
while (l <= r) {
int m = (l + r) / 2;
int compare = strcmp(w, blacklist[m]);
if (compare == 0) {
return true;
}
if (compare < 0) {
r = m - 1;
} else {
l = m + 1;
}
}
return false;
}
// Tree stuff
typedef struct occur_list {
unsigned line;
struct occur_list *next;
} occur_t;
typedef struct tree_node {
char word[MAXWORDSIZE];
occur_t *occurs;
struct tree_node *left;
struct tree_node *right;
} treenode_t;
occur_t *occur_insert(occur_t *l, unsigned line)
{
occur_t *new = (occur_t *)malloc(sizeof(occur_t));
new->next = l ? l->next : new;
new->line = line;
if (l) {
l->next = new;
}
return new;
}
void occur_print(occur_t *l)
{
if (!l) {
return;
}
occur_t *n = l->next;
for (; n; n = n->next) {
printf("%d ", n->line);
if (n == l) {
break;
}
}
}
treenode_t *tree_insert(treenode_t *t, char *word, unsigned line)
{
if (!t) {
treenode_t *node = (treenode_t *)malloc(sizeof(treenode_t));
strcpy(node->word, word);
node->left = node->right = NULL;
node->occurs = occur_insert(node->occurs, line);
return node;
}
int compare = strcmp(t->word, word);
if (compare == 0) {
t->occurs = occur_insert(t->occurs, line);
return t;
}
treenode_t **branch = compare > 0 ? &t->left : &t->right;
*branch = tree_insert(*branch, word, line);
return t;
}
void tree_print_words(treenode_t *t, unsigned longest)
{
if (!t) {
return;
}
tree_print_words(t->left, longest);
printf("%*.*s: ", -longest, longest, t->word);
occur_print(t->occurs);
putchar('\n');
tree_print_words(t->right, longest);
}
int main()
{
char word[MAXWORDSIZE];
char c;
treenode_t *root = NULL;
unsigned line = 1;
size_t longest = 0;
unsigned i = 0;
while ((c = getword(word, MAXWORDSIZE)) != EOF) {
if (word[0] == '\0' || checkword(word)) {
goto next_word;
}
size_t wlen = strlen(word);
if (wlen > longest) {
longest = wlen;
}
root = tree_insert(root, word, line);
// This goto can be replaced by a for loop
next_word:
if (c == '\n') {
line++;
}
}
tree_print_words(root, longest);
}
|