-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path6-4.c
169 lines (133 loc) · 4.3 KB
/
6-4.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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX_WORDS 100
#define MAX_LENGTH 100
#define BUFFER_SIZE 100
int buffer_index = 0;
char buffer[100];
void unget_character(int character) {
if (buffer_index >= BUFFER_SIZE)
printf("\nUngetch : Too many characters");
else
buffer[buffer_index++] = character;
}
int get_character() {
if (buffer_index > 0)
return buffer[--buffer_index];
else
return getchar();
}
int get_word(char *word, int limit) {
char *word_ptr = word;
char character;
char temp_character;
while (isspace(character = get_character()));
if (character != EOF && isalnum(character))
*word_ptr++ = character;
if (!isalpha(character)) {
if (character == '"')
for (character = get_character(); character != '"'; character = get_character());
else if (character == '\"')
for (character = get_character(); character != '\"'; character = get_character());
else if (character == '#')
for (character = get_character(); character != '\n'; character = get_character());
else if (character == '/') {
if ((character = get_character()) == '/')
for (character = get_character(); character != '\n'; character = get_character());
else if (character == '*') {
for (character = get_character(), temp_character = get_character(); character != '*' && temp_character != '/'; character = get_character(), temp_character = get_character())
unget_character(temp_character);
} else
unget_character(character);
} else if (character != '(' && character != '*' && character != '[' && character != '!' && character != '&' && character != '-' && character != '+')
for ( ; !isspace(character) && character != EOF; character = get_character());
*word_ptr = '\0';
return character;
}
for ( ; --limit > 0; ) {
char check_character = get_character();
if (!isalnum(check_character) && check_character != '_') {
if (!isspace(check_character)) {
unget_character(check_character);
return check_character;
} else {
unget_character(check_character);
break;
}
} else
*word_ptr++ = check_character;
}
*word_ptr = '\0';
return word[0];
}
struct tnode {
char *word;
int count;
struct tnode *left;
struct tnode *right;
};
struct tnode *talloc(void) {
return (struct tnode *) malloc(sizeof(struct tnode));
}
char *string_duplicate(char *string) {
char *ptr;
ptr = (char *) malloc(strlen(string) + 1);
if (ptr != NULL)
strcpy(ptr, string);
return ptr;
}
int addtree(struct tnode **ptr, char *word) {
int max_count = 1;
if (*ptr == NULL) {
*ptr = talloc();
(*ptr)->word = string_duplicate(word);
(*ptr)->count = 1;
(*ptr)->left = (*ptr)->right = NULL;
} else if (strcmp(word, (*ptr)->word) == 0) {
(*ptr)->count++;
if ((*ptr)->count > max_count)
max_count = (*ptr)->count;
} else if (strcmp(word, (*ptr)->word) < 0) {
int count = addtree(&(*ptr)->left, word);
if (count > max_count)
max_count = count;
} else {
int count = addtree(&(*ptr)->right, word);
if (count > max_count)
max_count = count;
}
return max_count;
}
void treeprint(struct tnode *ptr, int count) {
if (ptr != NULL) {
treeprint(ptr->left, count);
if (ptr->count == count)
printf("%4d %s\n", ptr->count, ptr->word);
treeprint(ptr->right, count);
}
}
void clear_string(char string[MAX_LENGTH]) {
for (int index = strlen(string); index > 0; index--)
string[index] = 0;
}
int main() {
struct tnode *root;
char word[MAX_WORDS];
int max_count;
root = NULL;
while (get_word(word, MAX_WORDS) != EOF) {
if (isalpha(word[0])) {
int count = addtree(&root, word);
if (count > max_count)
max_count = count;
}
clear_string(word);
}
while (max_count) {
treeprint(root, max_count);
max_count--;
}
return 0;
}