diff options
| author | 2023-11-25 16:24:10 +0000 | |
|---|---|---|
| committer | 2026-11-07 02:12:00 +0000 | |
| commit | bca75f071153ddcaa5ceee0722e2152bacfedc78 (patch) | |
| tree | 7c42e4aa83103aa9813151e4251d2ffb4023ffbe /src/main.c | |
| download | lab-01-master.tar.gz | |
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 307 |
1 files changed, 307 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..4a0d36f --- /dev/null +++ b/src/main.c | |||
| @@ -0,0 +1,307 @@ | |||
| 1 | /* | ||
| 2 | * Лабораторная работа №1 | ||
| 3 | * | ||
| 4 | * Реализовать программу генерации бинарного кода Грея для заполнения | ||
| 5 | * универсума (заданной пользователем разрядности). На основе универсума | ||
| 6 | * формируются два мультимножества двумя способами заполнения: вручную и | ||
| 7 | * автоматически (выбирает пользователь). Мощность множеств также задает | ||
| 8 | * пользователь. В результате на экран выводятся результаты всех действий | ||
| 9 | * над множествами. Кроме того, требуется реализовать защиту от | ||
| 10 | * некорректного пользовательского ввода. | ||
| 11 | * | ||
| 12 | * trie (radix 32 -> 32 indirections + a lot of memory for pointers + a lot of memory + "fast" insert/delete) | ||
| 13 | * array + binary search (log(32)/log(2) -> max 32 jumps + low amount of memory + "slow" insert/delete) | ||
| 14 | * | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <stdio.h> | ||
| 18 | #include <stdlib.h> | ||
| 19 | #include <string.h> | ||
| 20 | #include <time.h> | ||
| 21 | #include <assert.h> | ||
| 22 | |||
| 23 | #include "utils.h" | ||
| 24 | #include "multiset.h" | ||
| 25 | |||
| 26 | #define MULTISET_ELEMENT_MAX_COUNT 99 | ||
| 27 | |||
| 28 | uint | ||
| 29 | bin_to_gray (uint num) | ||
| 30 | { | ||
| 31 | return num ^ (num >> 1); | ||
| 32 | } | ||
| 33 | |||
| 34 | uint | ||
| 35 | gray_to_bin (uint num) | ||
| 36 | { | ||
| 37 | num ^= num >> 16; | ||
| 38 | num ^= num >> 8; | ||
| 39 | num ^= num >> 4; | ||
| 40 | num ^= num >> 2; | ||
| 41 | num ^= num >> 1; | ||
| 42 | return num; | ||
| 43 | } | ||
| 44 | |||
| 45 | void | ||
| 46 | generate_universum (MULTISET u) | ||
| 47 | { | ||
| 48 | for (uint i = 0; i < u->power; ++i) { | ||
| 49 | u->elements[i].value = i; | ||
| 50 | u->elements[i].count = (rand () % MULTISET_ELEMENT_MAX_COUNT) + 1; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | void | ||
| 55 | generate_random_multiset (MULTISET ms, MULTISET u) | ||
| 56 | { | ||
| 57 | assert (ms); | ||
| 58 | assert (u); | ||
| 59 | |||
| 60 | uint pd = u->power/ms->power; | ||
| 61 | for (uint i = 0; i < ms->power; ++i) { | ||
| 62 | ms->elements[i] = u->elements[i*pd + (rand () % pd)]; | ||
| 63 | ms->elements[i].count = (rand () % ms->elements[i].count) + 1; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | void | ||
| 68 | print_multiset (MULTISET ms, MULTISET u) | ||
| 69 | { | ||
| 70 | assert (u); | ||
| 71 | |||
| 72 | if (!ms || ms->power == 0) { | ||
| 73 | printf ("Пустое множество!\n"); | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | for (uint i = 0; i < ms->power; ++i) { | ||
| 78 | printf ("[%0*d] = %0*b x %*d\n", | ||
| 79 | log10ui (ms->power) + 1, i, | ||
| 80 | CTZ (u->power), bin_to_gray (ms->elements[i].value), | ||
| 81 | log10ui (multiset_element_max_count (u)) + 1, ms->elements[i].count); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | uint | ||
| 86 | request_universum_bitdepth_from_user () | ||
| 87 | { | ||
| 88 | uint bitdepth = 0; | ||
| 89 | |||
| 90 | for (;;) { | ||
| 91 | while (!input ("Задайте разрядность универсума U", "^[[:digit:]]+$", "%u", &bitdepth)) | ||
| 92 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 93 | if (bitdepth <= 24) | ||
| 94 | break; | ||
| 95 | puts ("Разрядность должна быть <= 24"); | ||
| 96 | } | ||
| 97 | |||
| 98 | return bitdepth; | ||
| 99 | } | ||
| 100 | |||
| 101 | uint | ||
| 102 | request_multiset_power_from_user (const char *name, MULTISET u) | ||
| 103 | { | ||
| 104 | if (!u) return 0; | ||
| 105 | |||
| 106 | uint power = 0; | ||
| 107 | |||
| 108 | for (;;) { | ||
| 109 | while (!input ("Зададайте мощность мультимножества %s", "^[[:digit:]]+$", "%u", name, &power)) | ||
| 110 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 111 | if (0 < power && power <= u->power) | ||
| 112 | return power; | ||
| 113 | printf ("Мощность мультимножества должна принадлежать (0; %d]!\n", u->power); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | MULTISET | ||
| 118 | request_universum_from_user (void) | ||
| 119 | { | ||
| 120 | MULTISET u; | ||
| 121 | |||
| 122 | for (;;) { | ||
| 123 | uint bitdepth = request_universum_bitdepth_from_user (); | ||
| 124 | if (bitdepth == 0) return NULL; | ||
| 125 | u = init_multiset (1 << bitdepth); | ||
| 126 | if (u) break; | ||
| 127 | else puts ("Недостаточно памяти для универсумма данной разрядности!"); | ||
| 128 | } | ||
| 129 | |||
| 130 | generate_universum (u); | ||
| 131 | return u; | ||
| 132 | } | ||
| 133 | |||
| 134 | struct multiset_element | ||
| 135 | request_multiset_element_from_user (MULTISET ms, MULTISET u, int i) | ||
| 136 | { | ||
| 137 | struct multiset_element mse; | ||
| 138 | |||
| 139 | for (;;) { | ||
| 140 | input: | ||
| 141 | char *bstr; | ||
| 142 | while (!input ("Задайте код Грея %d элемента мультимножества", "^[01]+$", "%ms", | ||
| 143 | i + 1, &bstr)) | ||
| 144 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 145 | mse.value = gray_to_bin (strtoul (bstr, NULL, 2)); | ||
| 146 | free (bstr); | ||
| 147 | |||
| 148 | if (mse.value > (uint) 1 << u->power) { | ||
| 149 | printf ("Код Грея должен принадлежать [%0*d; %0*d]!\n", | ||
| 150 | log10ui (ms->power) + 1, 0, | ||
| 151 | log10ui (ms->power) + 1, bin_to_gray (ms->power)); | ||
| 152 | continue; | ||
| 153 | } | ||
| 154 | for (int j = 0; j < i; ++j) { | ||
| 155 | if (ms->elements[j].value == mse.value) { | ||
| 156 | printf ("Элемент под данным кодом Грея уже в мультимножестве!\n"); | ||
| 157 | goto input; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | break; | ||
| 162 | } | ||
| 163 | |||
| 164 | for (;;) { | ||
| 165 | while (!input ("Задайте кратность %0*b", "^[[:digit:]]+$", "%d", | ||
| 166 | CTZ (u->power), bin_to_gray (mse.value), &mse.count)) | ||
| 167 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 168 | if (0 < mse.count && mse.count <= u->elements[mse.value].count) | ||
| 169 | break; | ||
| 170 | printf ("Введённая кратность должна входить в 1..%d!\n", u->elements[mse.value].count); | ||
| 171 | } | ||
| 172 | |||
| 173 | return mse; | ||
| 174 | } | ||
| 175 | |||
| 176 | MULTISET | ||
| 177 | request_multiset_from_user (const char *name, MULTISET u) | ||
| 178 | { | ||
| 179 | MULTISET ms; | ||
| 180 | |||
| 181 | for (;;) { | ||
| 182 | uint power = request_multiset_power_from_user (name, u); | ||
| 183 | ms = init_multiset (power); | ||
| 184 | if (!ms) return NULL; | ||
| 185 | // if (!ms) { | ||
| 186 | // printf ("Недостаточно памяти для мультимножества данной мощности!\n"); | ||
| 187 | // continue; | ||
| 188 | // } | ||
| 189 | |||
| 190 | for (;;) { | ||
| 191 | int a; | ||
| 192 | while (!input ("Заполнить мультимножество автоматически (1) или вручную (2)", "^[12]$", "%d", &a)) | ||
| 193 | printf ("Ввод не соответствует регулярному выражению!\n"); | ||
| 194 | switch (a) { | ||
| 195 | case 1: | ||
| 196 | generate_random_multiset (ms, u); | ||
| 197 | return ms; | ||
| 198 | case 2: | ||
| 199 | for (uint i = 0; i < ms->power; ++i) | ||
| 200 | ms->elements[i] = request_multiset_element_from_user (ms, u, i); | ||
| 201 | qsort (ms->elements, ms->power, sizeof (struct multiset_element), compare_multiset_elements); | ||
| 202 | return ms; | ||
| 203 | default: | ||
| 204 | printf ("Неверный выбор!\n"); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | int | ||
| 211 | main (void) | ||
| 212 | { | ||
| 213 | srand (time (NULL)); | ||
| 214 | |||
| 215 | MULTISET u = request_universum_from_user (); | ||
| 216 | print_multiset (u, u); | ||
| 217 | PAGEBREAK (); | ||
| 218 | |||
| 219 | MULTISET a = request_multiset_from_user ("A", u); | ||
| 220 | print_multiset (a, u); | ||
| 221 | PAGEBREAK (); | ||
| 222 | |||
| 223 | MULTISET b = request_multiset_from_user ("B", u); | ||
| 224 | print_multiset (b, u); | ||
| 225 | PAGEBREAK (); | ||
| 226 | |||
| 227 | MULTISET c; | ||
| 228 | |||
| 229 | puts ("Объединение A и B:"); | ||
| 230 | c = multiset_union (a, b); | ||
| 231 | print_multiset (c, u); | ||
| 232 | PAGEBREAK (); | ||
| 233 | free (c); | ||
| 234 | |||
| 235 | puts ("Пересечение A и B:"); | ||
| 236 | c = multiset_intersection (a, b); | ||
| 237 | print_multiset (c, u); | ||
| 238 | PAGEBREAK (); | ||
| 239 | free (c); | ||
| 240 | |||
| 241 | puts ("Дополнение A:"); | ||
| 242 | c = multiset_complement (a, u); | ||
| 243 | print_multiset (c, u); | ||
| 244 | PAGEBREAK (); | ||
| 245 | free (c); | ||
| 246 | |||
| 247 | puts ("Дополнение B:"); | ||
| 248 | c = multiset_complement (b, u); | ||
| 249 | print_multiset (c, u); | ||
| 250 | PAGEBREAK (); | ||
| 251 | free (c); | ||
| 252 | |||
| 253 | puts ("Разность A\\B:"); | ||
| 254 | c = multiset_difference (a, b); | ||
| 255 | print_multiset (c, u); | ||
| 256 | PAGEBREAK (); | ||
| 257 | free (c); | ||
| 258 | |||
| 259 | puts ("Разность B\\A:"); | ||
| 260 | c = multiset_difference (b, a); | ||
| 261 | print_multiset (c, u); | ||
| 262 | PAGEBREAK (); | ||
| 263 | free (c); | ||
| 264 | |||
| 265 | puts ("Арифметическое сложение A и B:"); | ||
| 266 | c = multiset_arithmetic_sum (a, b, u); | ||
| 267 | print_multiset (c, u); | ||
| 268 | PAGEBREAK (); | ||
| 269 | free (c); | ||
| 270 | |||
| 271 | puts ("Арифметическая разность A - B:"); | ||
| 272 | c = multiset_arithmetic_difference (a, b); | ||
| 273 | print_multiset (c, u); | ||
| 274 | PAGEBREAK (); | ||
| 275 | free (c); | ||
| 276 | |||
| 277 | puts ("Арифметическая разность B - A:"); | ||
| 278 | c = multiset_arithmetic_difference (b, a); | ||
| 279 | print_multiset (c, u); | ||
| 280 | PAGEBREAK (); | ||
| 281 | free (c); | ||
| 282 | |||
| 283 | puts ("Арифметическое произведение A и B:"); | ||
| 284 | c = multiset_arithmetic_multiplication (a, b, u); | ||
| 285 | print_multiset (c, u); | ||
| 286 | PAGEBREAK (); | ||
| 287 | free (c); | ||
| 288 | |||
| 289 | puts ("Арифметическое деление A на B:"); | ||
| 290 | c = multiset_arithmetic_division (a, b, u); | ||
| 291 | print_multiset (c, u); | ||
| 292 | PAGEBREAK (); | ||
| 293 | free (c); | ||
| 294 | |||
| 295 | puts ("Арифметическое деление B на A:"); | ||
| 296 | c = multiset_arithmetic_division (b, a, u); | ||
| 297 | print_multiset (c, u); | ||
| 298 | PAGEBREAK (); | ||
| 299 | free (c); | ||
| 300 | |||
| 301 | puts ("Симетрическая разность A и B:"); | ||
| 302 | c = multiset_symmetrical_difference (a, b); | ||
| 303 | print_multiset (c, u); | ||
| 304 | free (c); | ||
| 305 | |||
| 306 | return EXIT_SUCCESS; | ||
| 307 | } | ||
