-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4.c
221 lines (195 loc) · 4.75 KB
/
lab4.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
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mylib.h"
#define MAXLENGTH 100
#define KEYLENGTH 10
#define WORDLENGTH 8
typedef struct ring {
char string[MAXLENGTH];
struct ring *next;
} ring_t;
typedef struct tree {
char key[MAXLENGTH];
ring_t *ring;
struct tree *left;
struct tree *right;
} tree_t;
char myget_arg(int, char **);
int create_tree(tree_t *);
int attach_tree(tree_t *, char *);
int create_ring(ring_t **);
int attach_elem(ring_t **, char *);
int walk_through_the_tree(tree_t *, char *);
int walk_through_the_ring(ring_t *, char *);
int find_word(char *, char *);
int input_and_check_word(tree_t *);
// ------------------------------------------
int main(int argc, char **argv)
{
tree_t *root;
if(myget_arg(argc, argv) == 'h') {
print_manual();
return 0;
}
root = (tree_t *)malloc(sizeof(*root));
printf("Please, enter the root key: ");
mygets(root->key, MAXLENGTH);
create_ring(&(root->ring));
while(create_tree(root));
while(input_and_check_word(root));
printf("Exiting...\n");
return 0;
}
// ------------------------------------------
char myget_arg(int argc, char **argv)
{
if(argc == 2) {
if(!(strcmp(argv[1], "-h"))) {
return 'h';
}
}
return '0';
}
// ------------------------------------------
int create_ring(ring_t **ring)
{
char buffer[MAXLENGTH];
printf("Enter strings ('end' to stop reading):\n");
while(1) {
printf("----> ");
mygets(buffer, MAXLENGTH);
if(!strncmp(buffer, "end", 3)) {
return 0;
}
attach_elem(ring, buffer);
}
return 0;
}
// ------------------------------------------
int attach_elem(ring_t **ring, char *string)
{
ring_t *new_elem;
new_elem = (ring_t *)malloc(sizeof(*new_elem));
strncpy(new_elem->string, string, MAXLENGTH);
if(!*ring) {
*ring = new_elem;
(*ring)->next = *ring;
} else {
new_elem->next = (*ring)->next;
(*ring)->next = new_elem;
}
return 0;
}
// ------------------------------------------
int create_tree(tree_t *root)
{
char key[MAXLENGTH];
printf("Enter the key ('end' to stop input): ");
mygets(key, MAXLENGTH);
if(!strncmp(key, "end", 3)) {
return 0;
}
attach_tree(root, key);
return 1;
}
// ------------------------------------------
int attach_tree(tree_t *root, char *key)
{
tree_t *tmp;
tmp = root;
while(1) {
if(strncmp(key, tmp->key, MAXLENGTH) > 0) {
if(tmp->left) {
tmp = tmp->left;
} else {
tmp->left = (tree_t *)malloc(sizeof(*tmp->left));
tmp = tmp->left;
break;
}
} else if(tmp->right) {
tmp = tmp->right;
} else {
tmp->right = (tree_t *)malloc(sizeof(*tmp->right));
tmp = tmp->right;
break;
}
}
strncpy(tmp->key, key, MAXLENGTH);
create_ring(&(tmp->ring));
return 0;
}
// ------------------------------------------
int walk_through_the_ring(ring_t *ring, char *word)
{
int counter = 0, buffer;
ring_t *tmp;
if(!ring) {
return 0;
}
tmp = ring;
do {
if((buffer = find_word(tmp->string, word)) != 0) {
printf("%s\n", tmp->string);
counter += buffer;
}
tmp = tmp->next;
}
while(tmp != ring);
return counter;
}
// ------------------------------------------
int find_word(char *str, char *word)
{
int counter = 0;
char *p;
p = str;
while(p) {
p = strstr(p, word);
if(p) {
counter++;
} else {
break;
}
while(*p != ' ' && *p)
p++;
}
return counter;
}
// ------------------------------------------
int walk_through_the_tree(tree_t *root, char *word)
{
int counter = 0;
if(!root) {
return 0;
}
counter += walk_through_the_ring(root->ring, word);
counter += walk_through_the_tree(root->right, word);
counter += walk_through_the_tree(root->left, word);
return counter;
}
// ------------------------------------------
int input_and_check_word(tree_t *root)
{
int counter = 0;
char buffer[WORDLENGTH];
printf("Enter the requied word to start search (end, to exit): ");
mygets(buffer, WORDLENGTH);
if(!(strncmp(buffer, "end", 3))) {
return 0;
} else if(!(strlen(buffer))) {
printf("You don't have typed anything, try again\n");
return 1;
}
printf("The word occurs at:\n\n");
counter = walk_through_the_tree(root, buffer);
printf("\n%d times\n", counter);
return 1;
}
#ifdef __cplusplus
}
#endif