Skip to content

Commit 6b3a269

Browse files
committed
lab 6 now finished + additional task done!!
1 parent 2181ecc commit 6b3a269

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

Semester_1/Lab_6/Lab_6.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
#define ORDER NORMAL
77

88
void main() {
9-
file *file = read_file("input/medium.txt");
9+
file *file = read_file("input/large.txt");
1010
list *words = parse_file(file);
1111

12+
printf("\n---- NOT SORTED -----\n");
13+
print_list(words);
14+
1215
printf("\n--- ALPHABET SORT ---\n");
1316
sort_list(words, ALPHABET, ORDER);
1417
print_list(words);

Semester_1/Lab_6/Lab_6_dop.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// По символьному файлу составить два линейных списка слов,
2+
// упорядоченных по алфавиту и по количеству гласных букв
3+
// вывести оба списка
4+
5+
// доп: сделать управление функциями
6+
// (0 - выход из цикла, 1 - удаление элемента, 2 - вставка элемента)
7+
8+
#include "snippets.c"
9+
10+
void main() {
11+
file *file = read_file("input/test.txt");
12+
list *words = parse_file(file);
13+
14+
int running = 1;
15+
while (running) {
16+
17+
int option, id, id2, order;
18+
printf("Enter option >> ");
19+
scanf("%d", &option);
20+
21+
switch (option) {
22+
case HELP: // working!
23+
printf("\nAvailable commands:\n");
24+
printf("0 - EXIT\n1 - HELP\n2 - REMOVE\n3 - INSERT\n4 - SORT\n5 - SORTx2\n9 - PRINT\n\n");
25+
break;
26+
27+
case EXIT: // working!
28+
running = 0;
29+
break;
30+
31+
case REMOVE: // working!
32+
printf("Enter node index to delete: ");
33+
scanf("%d\n", &id);
34+
remove_node(words, id);
35+
break;
36+
37+
case INSERT: // working!
38+
printf("Enter index: ");
39+
scanf("%d", &id);
40+
printf("Enter word: ");
41+
char *str = read_str();
42+
node *curr = get_node(words, id);
43+
insert(words, curr, str);
44+
break;
45+
46+
case SORT: // working!
47+
printf("Enter sort type and order: "); // see type in snippets
48+
scanf("%d %d", &id, &order);
49+
sort_list(words, id, order);
50+
break;
51+
52+
case SORTx2: // working!
53+
printf("Enter sort types and order: ");
54+
scanf("%d %d %d", &id, &id2, &order);
55+
double_sort(words, id, id2, order);
56+
break;
57+
58+
case PRINT: // working!
59+
print_list(words);
60+
break;
61+
62+
default:
63+
printf("Unknown command: %d !!!\n", option);
64+
printf("Enter 1 to get help\n");
65+
break;
66+
}
67+
}
68+
69+
destroy(words);
70+
}

Semester_1/Lab_6/input/test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
aaad ac cab aa aac aaaa bab abc
1+
lorem ipsum dolor lol sit amet consectetur adipiscing elit

Semester_1/Lab_6/snippets.c

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44
// конфиги
55
#define NORMAL 0
66
#define REVERSE 1
7+
78
#define ALPHABET 0
89
#define LENGTH 1
910
#define VOWELS 2
1011
#define CONSNTS 3
1112

13+
#define EXIT 0
14+
#define HELP 1
15+
#define REMOVE 2
16+
#define INSERT 3
17+
#define SORT 4
18+
#define SORTx2 5
19+
#define PRINT 9
20+
21+
1222
typedef struct file {
1323
char *str;
1424
int size;
@@ -86,6 +96,26 @@ void destroy(list *list) {
8696
}
8797
}
8898

99+
int len = 0;
100+
char *read_str() {
101+
len = 0;
102+
int capacity = 1;
103+
char *s = (char *)malloc(sizeof(char));
104+
char c = getchar();
105+
while (c == '\n') c = getchar();
106+
107+
while (c != '\n') {
108+
s[(len)++] = c;
109+
if (len >= capacity) {
110+
capacity *= 2;
111+
s = (char *)realloc(s, capacity * sizeof(char));
112+
}
113+
c = getchar();
114+
}
115+
s[len] = '\0';
116+
return s;
117+
}
118+
89119
int strlen(char *str) {
90120
int count = 0;
91121
for (int i = 0; str[i] != '\0'; i++) count++;
@@ -257,9 +287,10 @@ int compare_words(word *w1, word *w2, int order) {
257287
else return order ^ (w1 -> size > w2 -> size) ? -1 : 1;
258288
}
259289

260-
void insert(list *list, node *curr, char *str, int cap) {
290+
void insert(list *list, node *curr, char *str) {
291+
261292
node *temp_node = (node*) malloc(sizeof(node));
262-
word *temp_word = make_word(str, cap);
293+
word *temp_word = make_word(str, strlen(str));
263294
temp_node -> word = temp_word;
264295
temp_node -> next = curr -> next;
265296
temp_node -> prev = curr;
@@ -376,4 +407,18 @@ void double_sort(list *list, int sort_1, int sort_2, int order) {
376407
if (swap < 0) swap_nodes(node1, node2);
377408
}
378409
}
410+
}
411+
412+
// новая версия двойной сортировки
413+
void double_sort_2(list *list, int sort_1, int sort_2, int order) {
414+
for (int i = 0; i < list -> size; i++) {
415+
for (int j = i+1; j < list -> size; j++) {
416+
struct node *node1 = get_node(list, i);
417+
struct node *node2 = get_node(list, j);
418+
int swap = need_swap(node1 -> word, node2 -> word, sort_1, order);
419+
if ((sort_1 == ALPHABET && swap >= 0) || (sort_1 != ALPHABET && !swap))
420+
swap = need_swap(node1 -> word, node2 -> word, sort_2, order);
421+
if (swap < 0) swap_nodes(node1, node2);
422+
}
423+
}
379424
}

0 commit comments

Comments
 (0)