-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex6_2.c
278 lines (252 loc) · 5.14 KB
/
ex6_2.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Exercise 6-2. Write a program that reads a C program and prints in
* alphabetical order each group of variable names that are identical
* in the first 6 characters, but different somewhere thereafter.
* Don't count words within strings and comments.
* Make 6 a parameter that can be set from the command line.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAXWORD 100
#define COMP_LEN 6
/* the tree node: */
struct tnode {
char *word; /* points to the text */
int count; /* number of occurrences */
int group; /* group id */
struct tnode *left; /* left child */
struct tnode *right; /* right child */
};
struct tnode *addtree(struct tnode *, char *);
void treeprint(struct tnode *);
int getword(char *, int);
int lg = 0; /* last group */
int currgroup = 0; /* current group */
int n; /* comparison length */
main(int argc, char *argv [])
{
struct tnode *root;
char word[MAXWORD];
if (argc > 1)
{
n = atoi(*++argv);
n = n <= 0 ? COMP_LEN : n;
}
else
n = COMP_LEN;
root = NULL;
while (getword(word, MAXWORD) != EOF)
{
if (isalpha(word[0]) && (int) strlen(word) >= n)
{
currgroup = 0;
root = addtree(root, word);
}
}
lg = 0;
treeprint(root);
getchar();
return 0;
}
struct tnode *talloc(void);
char *str_dup(char *);
void str_cpy(char *, char *);
int str_cmp(char *, char *);
int str_n_cmp(char *, char *, int);
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
int cond;
if (p == NULL) {
/* a new word has arrived */
p = talloc(); /* make a new node */
p->word = str_dup(w);
p->count = 1;
p->group = currgroup;
p->left = p->right = NULL;
return p;
}
if (str_n_cmp(w, p->word, n) == 0) /* group matching */
{
if (!p->group) /* if it doesn't has group set a new group for it */
p->group = ++lg;
currgroup = p->group;
}
if ((cond = str_cmp(w, p->word)) == 0)
p->count++; /* repeated word */
else if (cond < 0) /* less than into left subtree */
p->left = addtree(p->left, w);
else /* greater than into right subtree */
p->right = addtree(p->right, w);
return p;
}
/* treeprint: in-order print of tree p */
void treeprint(struct tnode *p)
{
if (p != NULL) {
treeprint(p->left);
if (p->group)
{
if (lg != p->group)
printf("------------------(%d)------------------\n", p->group);
printf("\t%4d %s\n", p->count, p->word);
lg = p->group;
}
treeprint(p->right);
}
}
#include <stdlib.h>
/* talloc: make a tnode */
struct tnode *talloc(void)
{
return (struct tnode *) malloc(sizeof(struct tnode));
}
/* make a duplicate of s */
char *str_dup(char *s)
{
char *p;
p = (char *) malloc(strlen(s) + 1); /* +1 for '\0' */
if (p != NULL)
str_cpy(p, s);
return p;
}
/* strcpy: copy t to s; pointer version 3 */
void str_cpy(char *s, char *t)
{
while (*s++ = *t++)
;
}
/* strcmp: returns <0 if s<t, 0 if s==t, >0 if s>t */
int str_cmp(char *s, char *t)
{
for (; *s == *t; s++, t++)
if (*s == '\0')
return 0;
return *s - *t;
}
/* strncmp: compares most n characters of t to s.*/
int str_n_cmp(char *s, char *t, int n)
{
while (n-- && *s++ == *t && s[-1] && *t++);
return n > 0 ? *--s - *t : 0;
}
int isbeginvalid(int c)
{
return isalpha(c) || c == '_';
}
int isvalid(int c)
{
return isalnum(c) || c == '_';
}
int getch(void);
void ungetch(int);
/* consumes spaces and returns the next character. */
int consume_space()
{
int c;
while (isspace(c = getch()));
return c;
}
/* consumes the string constant. */
int consume_double_quote()
{
int c;
while ((c = getch()) != EOF)
{
if (c == '\\') /* go through \" */
getch();
else if (c == '\"')
break;
}
return c == EOF ? c : getch();
}
int consume_single_quote()
{
int c;
while ((c = getch()) != EOF)
{
if (c == '\\') /* go through \" */
getch();
else if (c == '\'')
break;
}
return c == EOF ? c : getch();
}
/* consumes the comment. */
int consume_comment()
{
int c;
while ((c = getch()) != EOF)
{
if (c == '*')
{
c = getch();
if (c == '/')
break;
else
ungetch(c);
}
}
return c == EOF ? c : getch();
}
/* getword: get next word or character from input */
int getword(char *word, int lim)
{
int c;
char *w = word;
while (isspace(c = getch()))
;
/*------------------------------------*/
while (c == '\"' || c == '\'' || c == '/')
{
if (c == '\"')
{
c = consume_double_quote(); /* either \" or EOF */
}
else if (c == '\'')
{
c = consume_single_quote(); /* either \' or EOF */
}
else if (c == '/')
{
if ((c = getch()) == '*')
c = consume_comment();
}
if (isspace(c))
c = consume_space();
}
/*------------------------------------*/
if (c != EOF)
*w++ = c;
if (!isbeginvalid(c)) {
*w = '\0';
return c;
}
for (; --lim > 0; w++)
{
if (!isvalid(*w = getch())) /* isalnum has been relaced by isvalid */
{
ungetch(*w);
break;
}
}
*w = '\0';
return word[0];
}
/*------------------------------------------------------------------*/
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch()
{
return bufp > 0 ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}