-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.c
380 lines (341 loc) · 9.64 KB
/
snippets.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <stdio.h>
#include <stdlib.h>
// конфиги
#define NORMAL 0
#define REVERSE 1
#define ALPHABET 0
#define LENGTH 1
#define VOWELS 2
#define CONSNTS 3
typedef struct file {
char *str;
int size;
} file;
size_t file_size(FILE *file) {
fseek(file, 0, SEEK_END);
size_t fsize = ftell(file);
fseek(file, 0, SEEK_SET);
return fsize;
}
file *read_file(char *path) {
FILE *f = fopen(path, "r");
if (f == NULL)
perror("Error opening file");
// read string from file
size_t fsize = file_size(f);
char *str = (char*) malloc(fsize * sizeof(char));
for (size_t i = 0; i < fsize; i++)
str[i] = fgetc(f);
fclose(f);
// create file structure
file *temp_file = (file*) malloc(sizeof(file));
temp_file -> str = str;
temp_file -> size = fsize;
return temp_file;
}
// list -> содержит указатель на начало связанного списка node
// node -> содержит ссылку на word и ссылки на пред/след элемент
// word -> содержит массив char и длину/кол-во глассных в слове
typedef struct word {
char *arr;
int size;
int capacity;
int vowels;
int consnts;
} word;
typedef struct node {
struct word *word;
struct node *prev;
struct node *next;
} node;
typedef struct list {
node *head;
size_t size;
} list;
void init(list *list) {
list -> head = NULL;
list -> size = 0;
}
void destroy_word(word *word) {
free(word -> arr);
free(word);
}
void destroy_node(node *node) {
destroy_word(node -> word);
free(node);
}
void destroy(list *list) {
node *curr = list -> head;
node *prev = NULL;
while (curr != NULL) {
prev = curr;
curr = curr -> next;
destroy_node(prev);
}
}
int strlen(char *str) {
int count = 0;
for (int i = 0; str[i] != '\0'; i++) count++;
return count;
}
int vowels_count(char *str) {
int count = 0;
for (int i = 0; str[i] != '\0'; i++)
if ((str[i] == 'a')
|| (str[i] == 'e')
|| (str[i] == 'i')
|| (str[i] == 'o')
|| (str[i] == 'u')) count++;
return count;
}
void upd_word(word *w) {
w -> vowels = vowels_count(w -> arr);
w -> consnts = w -> size - w -> vowels;
}
struct word *make_word(char *str, int capacity) {
word *temp = (word*)malloc(sizeof(word));
temp -> arr = str;
temp -> size = strlen(str);
temp -> capacity = capacity;
upd_word(temp);
return temp;
}
char lower(int ch) {
return ch > 64 && ch < 91 ? ch + 32 : ch;
}
void push_back(list *list, char *str, int capacity) {
word *w = make_word(str, capacity);
node *n = (node*) malloc(sizeof(node));
node *curr;
n -> word = w;
n -> next = NULL;
if(list -> head == NULL) {
n -> prev = NULL;
list -> head = n;
} else {
curr = list -> head;
while (curr -> next != NULL)
curr = curr -> next;
curr -> next = n;
n -> prev = curr;
}
list -> size++;
}
void merge_lists(list *source, list *destination) {
node *curr = source -> head;
for (int i = 0; i < source -> size; i++) {
word *temp_word = curr -> word;
char *temp_str = (char*) malloc(temp_word -> size * sizeof(char));
for (size_t i = 0; i < temp_word -> size; i++)
temp_str[i] = temp_word -> arr[i];
push_back(destination, temp_str, temp_word -> capacity);
curr = curr -> next;
}
}
struct word *get_word(list *list, int index) {
int count = 0;
node *curr = list -> head;
word *result = curr -> word;
while (count++ != index) {
curr = curr -> next;
result = curr -> word;
}
return result;
};
struct node *get_node(list *list, int index) {
int count = 0;
node *curr = list -> head;
while (count++ != index)
curr = curr -> next;
return curr;
};
void print_list(list *list) {
printf("---------------------\n");
for (size_t i = 0; i < list -> size; i++) {
word *temp = get_word(list, i);
printf("%d -- %s\n", i, temp -> arr);
printf("---------------------\n");
}
}
int is_char(int ch) {
return (int)
((97 <= ch && ch <= 122) ||
(128 <= ch && ch <= 175) ||
(224 <= ch && ch <= 239) ||
(65 <= ch && ch <= 90));
}
int strcmp(char *s1, char *s2) {
char c1, c2;
while ((c1 == *s1++) == (c2 = *s2++))
if (c1 == '\0')
return 0;
return c1 - c2;
}
void edit_word(list *list, int index, char *str, int cap) {
node *temp = get_node(list, index);
word *word = temp -> word;
word -> arr = str;
word -> size = strlen(str);
word -> capacity = cap;
upd_word(word);
}
void swap_nodes(node *n1, node *n2) {
struct word *temp = n2 -> word;
n2 -> word = n1 -> word;
n1 -> word = temp;
}
void parse_str(char *temp, size_t size, list *list) {
int capacity = 1, len = 0;
char *str = (char*) malloc(sizeof(char));
for (size_t i = 0; i < size; i++) {
if (is_char(temp[i])) {
str[len++] = temp[i];
if (len >= capacity)
str = (char*) realloc(str, (capacity *= 2) * sizeof(char));
if (size - i == 1)
goto word_end;
} else {
word_end:
if (len) {
str[len] = '\0';
push_back(list, str, capacity);
capacity = 1; len = 0;
str = (char*) malloc(sizeof(char));
}
}
}
}
list *parse_file(file *file) {
list *temp_list = (list*) malloc(sizeof(list));
init(temp_list);
parse_str(file -> str, file -> size, temp_list);
free(file -> str);
free(file);
return temp_list;
}
// новая функция сравнения слов
int compare_words(word *w1, word *w2, int order) {
// 1 - порядок правильный
// 0 - слова идентичны
// -1 - порядок неправильный
int min_size = w1 -> size < w2 -> size ? w1 -> size : w2 -> size;
for (int i = 0; i < min_size; i++)
if (w1 -> arr[i] != w2 -> arr[i])
return order ^ (w1 -> arr[i] > w2 -> arr[i]) ? -1 : 1;
if (w1 -> size == w2 -> size) return 0;
else return order ^ (w1 -> size > w2 -> size) ? -1 : 1;
}
void insert(list *list, node *curr, char *str, int cap) {
node *temp_node = (node*) malloc(sizeof(node));
word *temp_word = make_word(str, cap);
temp_node -> word = temp_word;
temp_node -> next = curr -> next;
temp_node -> prev = curr;
curr -> next -> prev = temp_node;
curr -> next = temp_node;
list -> size++;
}
// поиск слова (с начала / с конца / с оффсетом)
int find_word(list *list, char *str, int offset, int order) {
word *tmp = make_word(str, 0);
switch (order) {
case NORMAL:
for (int i = offset; i < list -> size; i++)
if (compare_words(get_word(list, i), tmp, NORMAL) == 0) {
destroy_word(tmp);
return i;
}
break;
case REVERSE:
for (int i = list -> size - offset - 1; i >= 0; i--)
if (compare_words(get_word(list, i), tmp, NORMAL) == 0) {
destroy_word(tmp);
return i;
}
break;
}
destroy_word(tmp);
return -1;
}
void remove_node(list *list, int index) {
node *curr = get_node(list, index);
if (curr -> prev != NULL) curr -> prev -> next = curr -> next;
if (curr -> next != NULL) curr -> next -> prev = curr -> prev;
if (index == 0)
if (list -> head -> next != NULL)
list -> head = list -> head -> next;
else
list -> head = NULL;
destroy_node(curr);
list->size--;
}
void remove_nodes(list *list, int start, int end) {
for (int i = start; i < end; i++) // [start, end)
remove_node(list, i);
list -> size -= (end - start);
}
void remove_duplicates(list *list) {
for (int i = 0; i < list -> size - 1; i++) {
int index;
node *curr = get_node(list, i);
while ((index = find_word(list, curr -> word -> arr, i + 1, NORMAL)) != -1)
remove_node(list, index);
}
}
// проверяет, нужно ли менять слова местами
int need_swap(word *w1, word *w2, int sort_type, int reverse) {
// 1 - порядок правильный
// 0 - слова идентичны
// -1 - порядок неправильный
switch (sort_type) {
case ALPHABET:
return compare_words(w1, w2, reverse);
case LENGTH:
if (w1 -> size == w2 -> size) return 0;
else return (reverse ^ (w1 -> size > w2 -> size)) ? -1 : 1;
case VOWELS:
if (w1 -> vowels == w2 -> vowels) return 0;
else return (reverse ^ (w1 -> vowels > w2 -> vowels)) ? -1 : 1;
case CONSNTS:
if (w1 -> consnts == w2 -> consnts) return 0;
else return (reverse ^ (w1 -> consnts > w2 -> consnts)) ? -1 : 1;
default:
printf("Invalid sorting configuration!");
}
}
// обычная сортировка
void sort_list(list *list, int sort_type, int reverse) {
for (int i = 0; i < list -> size; i++) {
for (int j = i+1; j < list -> size; j++) {
struct node *node1 = get_node(list, i);
struct node *node2 = get_node(list, j);
if (need_swap(node1 -> word, node2 -> word, sort_type, reverse) < 0)
swap_nodes(node1, node2);
}
}
}
// двойная сортировка
void double_sort(list *list, int sort_1, int sort_2, int order) {
for (int i = 0; i < list -> size; i++) {
for (int j = i+1; j < list -> size; j++) {
struct node *node1 = get_node(list, i);
struct node *node2 = get_node(list, j);
int swap = need_swap(node1 -> word, node2 -> word, sort_1, order);
switch (sort_1) { // оптимизировать
case ALPHABET:
if (swap >= 0)
swap = need_swap(node1 -> word, node2 -> word, sort_2, order);
break;
case LENGTH:
case VOWELS:
case CONSNTS:
if (!swap)
swap = need_swap(node1 -> word, node2 -> word, sort_2, order);
break;
default:
printf("Invalid sorting configuration!");
}
if (swap < 0) swap_nodes(node1, node2);
}
}
}