aboutsummaryrefslogtreecommitdiff
path: root/ch6/6-04_top-words.c
blob: 796dac1cf6d11002040d67c25cd6c6fb4cb94a62 (plain)
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

/* The C Programming Language: 2nd Edition
 *
 * Exercise 6-4: Write a program that prints the distinct words in its input
 * sorted into decreasing order of frequency of occurrence. Precede each word
 * by its count.
 *
 * Notes: The approach I took builds a separate sorted tree instead of
 * modifying the tree in-place, like a lot of sorting functions tend to do.
 * This meant doubling the amount of RAM needed to solve the exercise. There
 * is probably a more elegant way to do this -- even within Category-0
 * standards -- but I couldn't seem to figure it out. I'm disappointed that
 * freq_sort and addsorted couldn't seem to be combined.
 */

#define BUFSIZE 1000
#define MAXWORD 100
#define DEF_PRE_LEN 4

struct tnode {
	char *word;
	int count;
	struct tnode *left;
	struct tnode *right;
};

char buf[BUFSIZE];
int bufp = 0;
int minlen;

int getch(void);
void ungetch(int);
int getword(char *, int);
struct tnode *addtree(struct tnode *, char *);
struct tnode *freq_sort(struct tnode *, struct tnode *);
struct tnode *addsorted(struct tnode *, struct tnode *);
void treeprint(struct tnode *);
struct tnode *talloc(void);
char *mystrdup(char *);

int main(int argc, char *argv[]) {
	switch (argc) {
		case 1:
			minlen = DEF_PRE_LEN;
			break;
		case 2:
			minlen = strtol(argv[1], NULL, 10);
			break;
		default:
			printf("Too many arguments.\n");
			return 1;
	}
	struct tnode *root;
	struct tnode *sorted;
	char word[MAXWORD];
	root = NULL;
	sorted = NULL;
	while (getword(word, MAXWORD) != EOF) {
		/* Only add the words we need to the tree */
		if (isalpha(word[0]) && strlen(word) > minlen) {
			root = addtree(root, word);
		}
	}
	/* Sort them in order of occurrence */
	if (root == NULL) {
		printf("No words at that length or greater were found.\n");
		return 1;
	} else {
		sorted = freq_sort(root, sorted);
		treeprint(sorted);
		return 0;
	}
}

/* Iterate over the "tree" and create a new, sorted tree */
struct tnode *freq_sort(struct tnode *p, struct tnode *s) {
	for (; p != NULL; p = p->right) {
		s = addsorted(p, s);
	}
	return s;
}

/* Add entries to the new tree, ordered by word frequency. I feel
 * that this function and freq_sort() could somehow be consolidated
 * and that this could be done better. Patches or PRs welcome.
 */
struct tnode *addsorted(struct tnode *p, struct tnode *s) {
	if (s != NULL) {
		if (p->count > s->count) {
			s->left = addsorted(p, s->left);
		} else {
			s->right = addsorted(p, s->right);
		}
	} else {
		s = talloc();
		s->word = mystrdup(p->word);
		s->count = p->count;
	}
	return s;
}

int getword(char *word, int lim) {
	int c;
	char *w = word;
	while (isspace(c = getch())) {
	}
	if (c == EOF) {
		return EOF;
	}
	if (c != EOF) {
		*w++ = c;
	}
	for ( ; --lim > 0; w++) {
		if (!isalpha(*w = getch())) {
			ungetch(*w);
			break;
		}
	}
	*w = '\0';
	return word[0];
}

int getch(void) {
	return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) {
	if (bufp >= BUFSIZE) {
		printf("ungetch: Too many characters.\n");
	} else {
		buf[bufp++] = c;
	}
}

struct tnode *addtree(struct tnode *p, char *w) {
	if (p != NULL) {
		/* We're putting all new things to the right side to dumb our tree struct
		 * down to a list. It's technically one address larger than a linked list,
		 * but there's no harm in re-purposing a structure.
		 */
		if (strcasecmp(p->word, w) == 0) {
			p->count++;
		} else {
			p->right = addtree(p->right, w);
		}
	} else {
		p = talloc();
		p->word = mystrdup(w);
		p->count = 1;
	}
	return p;
}

void treeprint(struct tnode *p) {
	if (p != NULL) {
		treeprint(p->left);
		printf("%4d  %-20s\n", p->count,  p->word);
		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;
}