Igorochenka

3AA

Apr 13th, 2021 (edited)
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.52 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7.  
  8. typedef int KeyType1;
  9. typedef char KeyType2;
  10. typedef int IndexType1;
  11. typedef int IndexType2;
  12. typedef short int BusyType1;
  13. typedef short int BusyType2;
  14. typedef short int RelType1;
  15.  
  16. typedef struct InfoType {
  17. int a;
  18. int b;
  19. char* string;
  20. } InfoType;
  21.  
  22. typedef struct Item {
  23. InfoType* info;
  24. KeyType1 key1;
  25. KeyType2* key2;
  26.  
  27. } Item;
  28.  
  29. typedef struct KeySpace1 {
  30. BusyType1 busy;
  31. KeyType1 key;
  32. RelType1 release;
  33. Item* info;
  34. } KeySpace1;
  35.  
  36. typedef struct KeySpace2 {
  37. BusyType2 busy;
  38. KeyType2* key;
  39. Item* info;
  40. } KeySpace2;
  41.  
  42. typedef struct Table {
  43. KeySpace1* ks1;
  44. KeySpace2* ks2;
  45.  
  46. IndexType1 msize1;
  47. IndexType2 msize2;
  48.  
  49. IndexType1 size1;
  50. IndexType2 size2;
  51. } Table;
  52.  
  53.  
  54. Table* table_new(int msize1, int msize2) {
  55. Table* t = (Table*)malloc(sizeof(Table));
  56. t->msize1 = msize1;
  57. t->size1 = 0;
  58. t->ks1 = (KeySpace1*)malloc(msize1 * sizeof(KeySpace1));
  59. for (int i = 0; i < msize1; i++) {
  60. t->ks1[i].info = NULL;
  61. t->ks1[i].busy = 0;
  62. t->ks1[i].key = 0;
  63. t->ks1[i].release = 0;
  64. }
  65.  
  66. t->msize2 = msize2;
  67. t->size2 = 0;
  68. t->ks2 = (KeySpace2*)malloc(msize2 * sizeof(KeySpace2));
  69. for (int i = 0; i < msize2; i++) {
  70. t->ks2[i].info = NULL;
  71. t->ks2[i].busy = 0;
  72. t->ks2[i].key = NULL;
  73. }
  74.  
  75. return t;
  76. }
  77.  
  78. int dialog(const char* msgs[], int n) {
  79. char* error = (char*)"";
  80. int choice;
  81. do {
  82. puts(error);
  83. error = (char*)"You're wrong. Try again!";
  84. for (int i = 0; i < n; ++i) {
  85. puts(msgs[i]);
  86. }
  87. puts("Make your choice: ");
  88. choice = get_int();
  89. while (getchar() != '\n') {}
  90. } while (choice < 0 || choice >= n);
  91. return choice;
  92. }
  93.  
  94. int get_int() {
  95. int n = 0;
  96. int res;
  97. do {
  98. n = scanf("%d", &res);
  99. if (n < 0) {
  100. exit(-1);
  101. return -1;
  102. }
  103. else if (n == 0) {
  104. while (getchar() != '\n');
  105. printf("Please enter integer:");
  106. }
  107. } while (n == 0);
  108. return res;
  109. }
  110.  
  111. char* get_str() {
  112. char buf[81] = { 0 };
  113. char* res = NULL;
  114. int len = 0;
  115. int n = 0;
  116. do {
  117. n = scanf("%s", buf);
  118.  
  119. if (n < 0) {
  120. if (!res) {
  121. return NULL;
  122. }
  123. }
  124. else if (n > 0) {
  125. int chunk_len = strlen(buf);
  126. int str_len = len + chunk_len;
  127. res = (char*)realloc(res, str_len + 1);
  128. memcpy(res + len, buf, chunk_len);
  129. len = str_len;
  130. }
  131. else {
  132. scanf("%*c");
  133. }
  134. } while (n == 0);
  135.  
  136. if (len > 0) {
  137. res[len] = '\0';
  138. }
  139. else {
  140. res = (char*)calloc(1, sizeof(char));
  141. }
  142.  
  143. return res;
  144. }
  145.  
  146. void include_element(Table* t) {
  147. InfoType info;
  148. printf("Input INFO:\n\ta: ");
  149. info.a = get_int();
  150. printf("\tb: ");
  151. info.b = get_int();
  152. printf("\tstr: ");
  153. info.string = get_str();
  154.  
  155. printf("Input Key 1: ");
  156. KeyType1 key1 = get_int();
  157.  
  158. printf("Input Key 2: ");
  159. KeyType2* key2 = (KeyType2*)calloc(100, sizeof(KeyType2));
  160. scanf("%s", key2);
  161.  
  162. int res = table_include(t, info, key1, key2);
  163.  
  164. if (res == 1)
  165. printf("Success!\n");
  166. else if (res == -1)
  167. printf("Error: Table is full!\n");
  168. else if (res == 0)
  169. printf("Error: key is already exists\n");
  170. }
  171.  
  172. void correct_versions(KeySpace1* ks,KeyType1 key1,int size) {
  173. int i = 0;
  174. int version = 0;
  175. for (i = 0; i < size; i++) {
  176. if (ks[i].key == key1 && ks[i].busy == 1) {
  177. ks[i].release = version;
  178. version += 1;
  179. }
  180. }
  181. }
  182.  
  183. Item* keyspace1_find_key(KeySpace1* ks, KeyType1 key, int size) {
  184. int i = 0;
  185. int ind = size;
  186.  
  187. for (i = 0; i < ind; i++) {
  188. if (ks[i].key == key && ks[i].busy == 1) {
  189. return ks[i].info;
  190. }
  191. }
  192. return 0;
  193. }
  194.  
  195. Item* keyspace1_find_key_versions(KeySpace1* ks, KeyType1 key, int size, int version) {
  196. int i = 0, m = 0;
  197. int ind = size;
  198. int s = version;
  199.  
  200. for (i = 0; i < ind; i++) {
  201. if (ks[i].key == key && ks[i].busy == 1) {
  202. if (ks[i].release == version) {
  203. return ks[i].info;
  204. }
  205.  
  206. }
  207. }
  208. return 0;
  209. }
  210.  
  211. int keyspace2_hash(KeyType2* key, int msize) {
  212. int lkey, skey = 0;
  213. lkey = strlen(key);
  214. for (int i = 0; i < lkey; i++) {
  215. skey += (int)key[i];
  216. }
  217.  
  218. return skey % msize;
  219. }
  220.  
  221. Item* keyspace2_find_key(KeySpace2* ks, KeyType2* key, int msize) {
  222. int ind = abs(keyspace2_hash(key, msize));
  223. if (ks[ind].busy == 1 && strcmp(ks[ind].key, key) == 0)
  224. return ks[ind].info;
  225.  
  226. for (int i = ind + 1; i != ind; i = (i + 1) % msize) {
  227. if (ks[i].busy == 1 && strcmp(ks[i].key, key) == 0)
  228. return ks[i].info;
  229. }
  230.  
  231. return 0;
  232. }
  233.  
  234. void keyspace1_include(KeySpace1* ks, Item* info, KeyType1 key, int size) {
  235. int i = 0, j = 0;
  236. int ind = size;
  237.  
  238. for (i = 0; i < ind; i++) {
  239. if (ks[i].key == info->key1 && ks[i].busy == 1) {
  240. j += 1;
  241. }
  242. }
  243.  
  244.  
  245. ks[ind].key = key;
  246. ks[ind].info = info;
  247. ks[ind].busy = 1;
  248. ks[ind].release = j;
  249. return;
  250. }
  251.  
  252. void keyspace2_include(KeySpace2* ks, Item* info, KeyType2* key, int msize) {
  253. int ind = abs(keyspace2_hash(key, msize));
  254. if (ks[ind].busy == 0 && ks[ind].key != key) {
  255. ks[ind].busy = 1;
  256. ks[ind].info = info;
  257. ks[ind].key = key;
  258. return;
  259. }
  260.  
  261. for (int i = ind + 1; i != ind; i = (i + 1) % msize) {
  262. if (ks[i].busy == 0 && ks->key != key) {
  263. ks[i].busy = 1;
  264. ks[i].info = info;
  265. ks[i].key = key;
  266. return;
  267. }
  268. }
  269.  
  270. return;
  271. }
  272.  
  273. Item* item_new(InfoType info, KeyType1 key1, KeyType2* key2) {
  274. Item* item = (Item*)malloc(sizeof(Item));
  275. item->info = (InfoType*)malloc(sizeof(InfoType));
  276. (*item->info) = info;
  277. item->key1 = key1;
  278. item->key2 = key2;
  279.  
  280. return item;
  281. }
  282.  
  283. int table_include(Table* t, InfoType info, KeyType1 key1, KeyType2* key2) {
  284. if (t->msize1 == t->size1 || t->msize2 == t->size2)
  285. return -1;
  286.  
  287. if (keyspace2_find_key(t->ks2, key2, t->msize2))
  288. return 0;
  289.  
  290. Item* item = item_new(info, key1, key2);
  291.  
  292.  
  293. keyspace1_include(t->ks1, item, key1, t->size1);
  294. keyspace2_include(t->ks2, item, key2, t->msize2);
  295.  
  296. t->size1++;
  297. t->size2++;
  298.  
  299. return 1;
  300. }
  301.  
  302. void print_table(Table* t) {
  303. printf("1: \n");
  304. for (int i = 0; i < t->msize1; i++) {
  305. if (t->ks1[i].info)
  306. printf(" Busy: %d Key1: %d Int A:%d Int B:%d String:'%s' Version: %d\n", t->ks1[i].busy, t->ks1[i].key, t->ks1[i].info->info->a, t->ks1[i].info->info->b, t->ks1[i].info->info->string, t->ks1[i].release);
  307. else
  308. printf("-\n");
  309. }
  310. printf("2:\n");
  311. for (int i = 0; i < t->msize2; i++) {
  312. if (t->ks2[i].info)
  313. printf(" Busy: %d Key2: %s Int A:%d Int B:%d String:'%s'\n", t->ks2[i].busy, t->ks2[i].key, t->ks2[i].info->info->a, t->ks2[i].info->info->b, t->ks2[i].info->info->string);
  314. else
  315. printf("-\n");
  316. }
  317. }
  318.  
  319. void find_element_with_key(Table* t) {
  320. printf("From key space 1 or key space 2? (1/2): ");
  321. int space = get_int();
  322. while (space < 1 || space > 2) {
  323. printf("Please choose between 1 and 2\n");
  324. printf("From key space 1 or key space 2? (1/2): ");
  325. space = get_int();
  326. }
  327.  
  328. Item* res = 0;
  329. if (space == 1) {
  330. printf("Input Key 1: ");
  331. KeyType1 key1 = get_int();
  332. printf("Input Version: ");
  333. int version = get_int();
  334. res = keyspace1_find_key_versions(t->ks1, key1, t->size1, version);
  335. }
  336. else {
  337. printf("Input Key 2: ");
  338. KeyType2* key2 = (KeyType2*)calloc(100, sizeof(KeyType2));
  339. scanf("%s", key2);
  340. res = keyspace2_find_key(t->ks2, key2, t->msize2);
  341. }
  342.  
  343. if (res != NULL)
  344. printf("Success: %d %s %d %d %s\n", res->key1, res->key2, res->info->a, res->info->b, res->info->string);
  345. else
  346. printf("Error: Not Exist!\n");
  347. }
  348.  
  349. Item* table_find_composite(Table* t, KeyType1 key1, KeyType2* key2) {
  350. int i = 0, j = 0;
  351. for (i = 0; (i < t->size1) || (i < t->msize2); i++) {
  352. if ((t->ks1[i].key == key1) && (t->ks1[i].busy == 1)) {
  353. if ((strcmp(t->ks1[i].info->key2, key2) == 0)) {
  354. return t->ks1[i].info;
  355. }
  356. }
  357. }
  358. return 0;
  359. }
  360.  
  361. void find_element_with_composite(Table* t) {
  362. printf("Input Key 1: ");
  363. KeyType1 key1 = get_int();
  364. printf("Input Key 2: ");
  365. KeyType2* key2 = (KeyType2*)calloc(100, sizeof(KeyType2));
  366. scanf("%s", key2);
  367.  
  368. Item* res = table_find_composite(t, key1, key2);
  369. if (res)
  370. printf("Success: %d %s %d %d %s\n", res->key1, res->key2, res->info->a, res->info->b, res->info->string);
  371. else
  372. printf("Error: Not Exist!\n");
  373. }
  374.  
  375.  
  376. void table_delete_item1(Table* t, Item* item) {
  377. int i = 0, j = 0;
  378. for (i = 0; i < t->size1; i++) {
  379. if (t->ks1[i].info == item && t->ks1[i].busy == 1) {
  380. t->ks1[i].busy = 0;
  381. for (j = 0; j < t->msize2 - 1; j++) {
  382. if (t->ks2[j].info == item && t->ks2[j].busy == 1) {
  383. t->ks2[j].busy = 0;
  384. t->size2 -= 1;
  385. correct_versions(t->ks1, t->ks1[i].key, t->size1);
  386. }
  387. }
  388. }
  389. }
  390. }
  391.  
  392. void table_delete_item123(Table* t, Item* item) {
  393. int i = 0, j = 0;
  394. for (i = 0; i < t->size1; i++) {
  395. if (t->ks1[i].key == item->key1 && t->ks1[i].busy == 1) {
  396. t->ks1[i].busy = 0;
  397. for (j = 0; j < t->msize2 - 1; j++) {
  398. if (t->ks2[j].info == t->ks1[i].info && t->ks2[j].busy == 1) {
  399. t->ks2[j].busy = 0;
  400. t->size2 -= 1;
  401. correct_versions(t->ks1, t->ks1[i].key, t->size1);
  402. }
  403. }
  404. }
  405. }
  406. }
  407.  
  408. void table_delete_item12(Table* t, Item* item, int version) {
  409. int i = 0, j = 0;
  410. for (i = 0; i < t->size1; i++) {
  411. if (t->ks1[i].info == item && t->ks1[i].busy == 1 && t->ks1[i].release == version) {
  412. t->ks1[i].busy = 0;
  413. for (j = 0; j < t->msize2; j++) {
  414. if (t->ks1[i].info == t->ks2[j].info && t->ks2[j].busy == 1) {
  415. t->ks2[j].busy = 0;
  416. t->size2 -= 1;
  417. correct_versions(t->ks1, t->ks1[i].key, t->size1);
  418. }
  419. }
  420. }
  421. }
  422. }
  423.  
  424. void table_delete_item2(Table* t, Item* item) {
  425. int i = 0, j = 0;
  426. for (i = 0; i < t->msize2 - 1; i++) {
  427. if (t->ks2[i].info == item && t->ks2[i].busy == 1) {
  428. t->ks2[i].busy = 0;
  429. for (j = 0; j < t->size1; j++) {
  430. if (t->ks1[i].info == t->ks2[j].info && t->ks1[j].busy == 1) {
  431. t->ks1[j].busy = 0;
  432. t->size2 -= 1;
  433. correct_versions(t->ks1, t->ks1[i].key, t->size1);
  434. }
  435. }
  436. }
  437. }
  438. }
  439.  
  440. void table_delete_item_version(Table* t, Item* item, int version) {
  441. int i = 0, j = 0;
  442. for (i = 0; i < t->size1; i++) {
  443. if (t->ks1[i].info == item && t->ks1[i].release == version) {
  444. t->ks1[i].busy = 0;
  445. for (j = 0; j < t->msize2; j++) {
  446. if (t->ks1[i].info == t->ks2[j].info) {
  447. t->ks2[j].busy = 0;
  448. t->size2 -= 1;
  449. correct_versions(t->ks1, t->ks1[i].key, t->size1);
  450. }
  451. }
  452. }
  453. }
  454. }
  455.  
  456. // 1 - deleted , -1 - not found
  457. int table_delete_composite(Table* t, KeyType1 key1, KeyType2* key2) {
  458. Item* item = table_find_composite(t, key1, key2);
  459. if (item) {
  460. table_delete_item1(t, item);
  461. return 1;
  462. }
  463. return -1;
  464. }
  465.  
  466. void delete_element_with_composite(Table* t) {
  467. printf("Input Key 1: ");
  468. KeyType1 key1 = get_int();
  469. printf("Input Key 2: ");
  470. KeyType2* key2 = (KeyType2*)calloc(100, sizeof(KeyType2));
  471. scanf("%s", key2);
  472. int res = table_delete_composite(t, key1, key2);
  473. if (res == 1)
  474. printf("Success!");
  475. else
  476. printf("Error: Not Exist!\n");
  477. }
  478.  
  479. void delete_element_with_key(Table* t) {
  480. printf("From key space 1 or key space 2? (1/2): ");
  481. int space = get_int();
  482. while (space < 1 || space > 2) {
  483. printf("Please choose between 1 and 2\n");
  484. printf("From key space 1 or key space 2? (1/2): ");
  485. space = get_int();
  486. }
  487.  
  488. Item* res;
  489. if (space == 1) {
  490. printf("Input Key 1: ");
  491. KeyType1 key1 = get_int();
  492. printf("Input Version: ");
  493. int version = get_int();
  494. res = keyspace1_find_key_versions(t->ks1, key1, t->size1, version);
  495. if (res) {
  496. table_delete_item12(t, res, version);
  497. printf("Success!");
  498. }
  499. else {
  500. printf("Error: Not Exist!\n");
  501. }
  502. }
  503. else {
  504. printf("Input Key 2: ");
  505. KeyType2* key2 = (KeyType2*)calloc(100, sizeof(KeyType2));
  506. scanf("%s", key2);
  507. res = keyspace2_find_key(t->ks2, key2, t->msize2);
  508. if (res) {
  509. table_delete_item2(t, res);
  510. printf("Success!");
  511. }
  512. else {
  513. printf("Error: Not Exist!\n");
  514. }
  515. }
  516. }
  517.  
  518. void reorganize_first_key(Table* t) {
  519. KeySpace1 tmp1;
  520. int zero_num = 0;
  521. int i = 0, j = 0;
  522. for (i = 0; i < t->msize1; i++) {
  523. if (t->ks1[i].busy == 0) {
  524. zero_num += 1;
  525. }
  526. else {
  527. if (zero_num > 0) {
  528. tmp1 = t->ks1[i];
  529. t->ks1[i] = t->ks1[i - zero_num];
  530. t->ks1[i - zero_num] = tmp1;
  531. t->size1 -= 1;
  532. }
  533. }
  534.  
  535. }
  536. }
  537.  
  538. void work_with_versions(Table* t) {
  539. printf("Delete every versions (1) or one version (2)?: ");
  540. int space = get_int();
  541. while (space < 1 || space > 2) {
  542. printf("Please choose between 1 and 2\n");
  543. printf("Delete every versions (1) or one version (2)?: ");
  544. space = get_int();
  545. }
  546.  
  547. Item* res;
  548.  
  549. if (space == 1) {
  550. printf("Input Key 1: ");
  551. KeyType1 key1 = get_int();
  552. res = keyspace1_find_key(t->ks1, key1, t->size1);
  553. if (res) {
  554. table_delete_item123(t, res);
  555. printf("Success!");
  556. }
  557. else {
  558. printf("Error: Not Exist!\n");
  559. }
  560. }
  561. else {
  562. printf("Input Key 1: ");
  563. KeyType1 key1 = get_int();;
  564. printf("Input Version: ");
  565. int version = get_int();
  566. res = keyspace1_find_key_versions(t->ks1, key1, t->size1, version);
  567. if (res) {
  568. table_delete_item_version(t, res, version);
  569. printf("Success!");
  570. }
  571. else {
  572. printf("Error: Not Exist!\n");
  573. }
  574. }
  575. }
  576.  
  577. int main() {
  578. const char* MSGS[] = { "0. Quit", "1. Including new element", "2. Finding element with composite key", "3. Deleting element with composite key", "4. Finding element with one key", "5. Deleting element with one key", "6. Print table", "7. Reorganize First Keys", "8. Work with versions" };
  579. const int MSGS_SIZE = sizeof(MSGS) / sizeof(MSGS[0]);
  580. int c = 0;
  581. printf("Enter msize1: ");
  582. int msize1 = get_int();
  583. printf("Enter msize2: ");
  584. int msize2 = get_int();
  585. Table* t = table_new(msize1, msize2);
  586. do {
  587. c = dialog(MSGS, MSGS_SIZE);
  588. switch (c) {
  589. case 0: {
  590. free(t);
  591. break;
  592. } case 1: {
  593. include_element(t);
  594. break;
  595. } case 2: {
  596. find_element_with_composite(t);
  597. break;
  598. } case 3: {
  599. delete_element_with_composite(t);
  600. break;
  601. } case 4: {
  602. find_element_with_key(t);
  603. break;
  604. } case 5: {
  605. delete_element_with_key(t);
  606. break;
  607. } case 6: {
  608. print_table(t);
  609. break;
  610. } case 7: {
  611. reorganize_first_key(t);
  612. break;
  613. } case 8: {
  614. work_with_versions(t);
  615. break;
  616. }
  617. }
  618. } while (c != 0);
  619. return 0;
  620. }
  621.  
Add Comment
Please, Sign In to add comment