Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.44 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <assert.h>
  7. #include <progbase.h>
  8. #include <progbase/console.h>
  9.  
  10. struct Word
  11. {
  12.     char word[100];
  13.     int len;
  14.     int Row;
  15.     int Char;
  16.     int index;
  17. };
  18.  
  19. struct WordArray
  20. {
  21.     struct Word *wordAr;
  22.     int len;
  23. };
  24. // Я вирішив, що доречно порівнювати
  25. // кожну складову структури окремо,
  26. // маючи при цьому загальний висновок порівняння
  27. // Тому створено окремі структори для цього
  28. struct WordCompare
  29. {
  30.     int word;
  31.     int len;
  32.     int Row;
  33.     int Char;
  34.     int index;
  35.     int resOfCompare;
  36. };
  37.  
  38. struct WordArrayCompare
  39. {
  40.     int wordAr;
  41.     int len;
  42.     int resOfCompare;
  43. };
  44.  
  45. struct WordCompare wordcom(struct Word word1, struct Word word2);
  46. struct WordArrayCompare wordarcom(struct WordArray wordar1, struct WordArray wordar2);
  47. struct Word WordRewriter2(const char *string);
  48. int secondWordSearch(char *string);
  49. char *WordRewriter1(const char *string, char *buffer, int buflen);
  50. struct WordArray *getAllWordsArrayNew(const char *str, struct WordArray *arr);
  51. void arrayFree(struct WordArray *array);
  52. void task1(void);
  53. void task2(void);
  54. void task3(void);
  55.  
  56. int main(void)
  57. {
  58.     task1();
  59.     task2();
  60.     task3();
  61.     return 0;
  62. }
  63.  
  64. void task1(void)
  65. {
  66.     char *nullAr = NULL;
  67.     assert(secondWordSearch("122=-|[1-1-") == -1);
  68.     assert(secondWordSearch("") == -1);
  69.     assert(secondWordSearch("wqe") == -1);
  70.     assert(secondWordSearch(nullAr) == -1);
  71.     assert(secondWordSearch("12qwqwpoewio -wqo") == 14);
  72.     assert(secondWordSearch("aa-b-c--ss") == 8);
  73.     assert(secondWordSearch("aa-b\'-c--ss") == 6);
  74.  
  75.     char buffer[100];
  76.     assert(WordRewriter1(nullAr, buffer, 100) == NULL);
  77.     assert(WordRewriter1("", buffer, 100) == NULL);
  78.     assert(WordRewriter1("201\'-@#$", buffer, 100) == NULL);
  79.     char expected[] = "abc-abc's";
  80.     char *actual = WordRewriter1("00-abc-abc's", buffer, 10);
  81.     for (int i = 0; i < 10; i++)
  82.     {
  83.         assert(expected[i] == actual[i]);
  84.     }
  85. }
  86.  
  87. int secondWordSearch(char *string)
  88. {
  89.     if (string == NULL)
  90.     {
  91.         return -1;
  92.     }
  93.  
  94.     int firstWord = 0;
  95.     int apostrophe = 0;
  96.     int i = 0;
  97.     for (i = 0;
  98.          (firstWord != 1 ||
  99.           isalpha(string[i]) ||
  100.           (i != 0 && isalpha(string[i - 1]) && apostrophe == 0 && string[i] == '\'') ||
  101.           (i != 0 && string[i] == '-' && isalpha(string[i - 1]) && isalpha(string[i + 1]))) &&
  102.          string[i] != '\0';
  103.          i++)
  104.     {
  105.         if (string[i] == '\'')
  106.         {
  107.             apostrophe = 1;
  108.         }
  109.         if (isalpha(string[i]))
  110.         {
  111.             firstWord = 1;
  112.         }
  113.     }
  114.     while ((!(isalpha(string[i])) && string[i] != '\0'))
  115.     {
  116.         i++;
  117.     }
  118.     if (string[i] == '\0')
  119.     {
  120.         return -1;
  121.     }
  122.     else
  123.     {
  124.         return i;
  125.     }
  126. }
  127.  
  128. char *WordRewriter1(const char *string, char *buffer, int bufLen)
  129. {
  130.     if (string == NULL)
  131.     {
  132.         return NULL;
  133.     }
  134.     int apostrophe = 0;
  135.     int wordStart = 0;
  136.     int wordLen = 0;
  137.     int i = 0;
  138.     for (i = 0;
  139.          (wordStart != 1 ||
  140.           isalpha(string[i]) ||
  141.           (i != 0 && isalpha(string[i - 1]) && apostrophe == 0 && string[i] == '\'') ||
  142.           (i != 0 && string[i] == '-' && isalpha(string[i - 1]) && isalpha(string[i + 1]))) &&
  143.          string[i] != '\0';
  144.          i++)
  145.     {
  146.         if (string[i] == '\'')
  147.         {
  148.             apostrophe = 1;
  149.         }
  150.         if (isalpha(string[i]))
  151.         {
  152.             wordStart = 1;
  153.         }
  154.         if (wordStart == 1)
  155.         {
  156.             wordLen++;
  157.         }
  158.     }
  159.     if (wordLen > bufLen || (string[i] == '\0' && wordStart != 1))
  160.     {
  161.         return NULL;
  162.     }
  163.     int delta = bufLen - wordLen;
  164.  
  165.     while (wordLen > 0)
  166.     {
  167.         buffer[bufLen - delta - wordLen] = string[i - wordLen];
  168.         wordLen--;
  169.     }
  170.  
  171.     buffer[bufLen - delta] = '\0';
  172.     return buffer;
  173. }
  174.  
  175. struct WordCompare wordcom(struct Word word1, struct Word word2)
  176. {
  177.     struct WordCompare result = {0, 0, 0, 0, 0, 0};
  178.     if (word1.Row == word2.Row)
  179.     {
  180.         result.Row = 1;
  181.     }
  182.     if (word1.Char == word2.Char)
  183.     {
  184.         result.Char = 1;
  185.     }
  186.     if (word1.index == word2.index)
  187.     {
  188.         result.index = 1;
  189.     }
  190.     if (word1.len == word2.len)
  191.     {
  192.         result.len = 1;
  193.     }
  194.     if (!(strcmp(word1.word, word2.word)))
  195.     {
  196.         result.word = 1;
  197.     }
  198.     if (result.Row && result.Char && result.index && result.len && result.word)
  199.     {
  200.         result.resOfCompare = 1;
  201.     }
  202.     return result;
  203. }
  204.  
  205. struct WordArrayCompare wordarcom(struct WordArray wordar1, struct WordArray wordar2)
  206. {
  207.     struct WordArrayCompare result = {0, 0, 0};
  208.     if (wordar1.wordAr == wordar2.wordAr)
  209.     {
  210.         result.wordAr = 1;
  211.     }
  212.     if (wordar1.len == wordar2.len)
  213.     {
  214.         result.len = 1;
  215.     }
  216.     if (result.len && result.wordAr)
  217.     {
  218.         result.resOfCompare = 1;
  219.     }
  220.     return result;
  221. }
  222.  
  223. struct Word WordRewriter2(const char *string)
  224. {
  225.  
  226.     struct Word result = {{'\0'}, 0, 1, 1, 0};
  227.     if (string == NULL)
  228.     {
  229.         return result;
  230.     }
  231.     while (!(isalpha(string[result.index])) && string[result.index] != '\0')
  232.     {
  233.         result.Char++;
  234.         if (string[result.index] == '\n')
  235.         {
  236.             result.Row++;
  237.             result.Char = 1;
  238.         }
  239.         result.index++;
  240.     }
  241.     if (string[result.index] != '\0')
  242.     {
  243.         char *buffer = WordRewriter1(string + result.index, result.word, 100);
  244.         result.len = strlen(buffer);
  245.         for (int i = 0; i < result.len; i++)
  246.         {
  247.             result.word[i] = buffer[i];
  248.         }
  249.     }
  250.     return result;
  251. }
  252.  
  253. void task2(void)
  254. {
  255.     struct Word word1 = {"Sugoi Desu!", 11, 5, 24, 100};
  256.     struct Word word2 = {"Sugoi Desu!", 11, 5, 24, 100};
  257.     struct Word word3 = {"Senpai!", 7, 6, 21, 100};
  258.     struct Word word4 = {"Sempai!", 7, 6, 21, 100};
  259.  
  260.     struct Word Ar1[4] = {word1, word2, word3, word4};
  261.     struct Word Ar2[4] = {word1, word1, word2, word2};
  262.  
  263.     struct WordArray wordar1 = {Ar1, 4};
  264.     struct WordArray wordar2 = {Ar2, 4};
  265.     struct WordArray wordar3 = {Ar1, 3};
  266.  
  267.     struct WordCompare zeroCompair = wordcom(word1, word1);
  268.     struct WordCompare firstCompair = wordcom(word1, word2);
  269.     struct WordCompare secondCompair = wordcom(word3, word2);
  270.     struct WordCompare thirdCompair = wordcom(word3, word4);
  271.  
  272.     assert(zeroCompair.resOfCompare == 1);
  273.     assert(firstCompair.word == 1);
  274.     assert(firstCompair.resOfCompare == 1);
  275.     assert(secondCompair.word == 0);
  276.     assert(secondCompair.len == 0);
  277.     assert(secondCompair.Row == 0);
  278.     assert(secondCompair.Char == 0);
  279.     assert(secondCompair.index == 1);
  280.     assert(thirdCompair.word == 0);
  281.     assert(thirdCompair.len == 1);
  282.     assert(thirdCompair.Row == 1);
  283.     assert(thirdCompair.Char == 1);
  284.     assert(thirdCompair.index == 1);
  285.  
  286.     struct WordArrayCompare fourthCompair = wordarcom(wordar3, wordar3);
  287.     struct WordArrayCompare fivethCompair = wordarcom(wordar1, wordar3);
  288.     struct WordArrayCompare sixthCompair = wordarcom(wordar2, wordar3);
  289.     struct WordArrayCompare seventhCompair = wordarcom(wordar1, wordar2);
  290.  
  291.     assert(fourthCompair.resOfCompare == 1);
  292.     assert(fivethCompair.resOfCompare == 0);
  293.     assert(fivethCompair.wordAr == 1);
  294.     assert(fivethCompair.len == 0);
  295.     assert(sixthCompair.resOfCompare == 0);
  296.     assert(seventhCompair.resOfCompare == 0);
  297.     assert(seventhCompair.len == 1);
  298.     assert(seventhCompair.wordAr == 0);
  299.  
  300.     char Text1[] = "\n\n---\n(OMAE-WA\'MOE-SINGEIRU)---";
  301.     char Text2[] = "\n-@\n#__(+__NANI__+)__#@-";
  302.  
  303.     struct Word fromText1 = WordRewriter2(Text1);
  304.     struct Word fromText2 = WordRewriter2(Text2);
  305.  
  306.     struct Word expected1 = {"OMAE-WA\'MOE-SINGEIRU", 20, 4, 2, 7};
  307.     struct Word expected2 = {"NANI", 4, 3, 8, 11};
  308.  
  309.     struct WordCompare eighthCompair = wordcom(fromText1, expected1);
  310.     struct WordCompare ninethCompair = wordcom(fromText2, expected2);
  311.     assert(eighthCompair.resOfCompare == 1);
  312.     assert(ninethCompair.resOfCompare == 1);
  313. }
  314.  
  315. struct WordArray *getAllWordsArrayNew(const char *str, struct WordArray *arr)
  316. {
  317.     struct Word empty = {{'\0'}, 0, 1, 1, 0};
  318.     arr->len = 0;
  319.     arr->wordAr = malloc(sizeof(struct Word) * strlen(str));
  320.     arr->wordAr[0] = empty;
  321.     do
  322.     {
  323.  
  324.         if (arr->len != 0)
  325.         {
  326.  
  327.             arr->wordAr[arr->len] = WordRewriter2(str + arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len);
  328.  
  329.             arr->wordAr[arr->len].index += arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len;
  330.  
  331.             arr->wordAr[arr->len].Row += arr->wordAr[arr->len - 1].Row - 1;
  332.  
  333.             if (arr->wordAr[arr->len].Row == arr->wordAr[arr->len - 1].Row)
  334.             {
  335.                 arr->wordAr[arr->len].Char += arr->wordAr[arr->len - 1].Char + arr->wordAr[arr->len - 1].len - 1;
  336.             }
  337.         }
  338.         else
  339.         {
  340.             arr->wordAr[arr->len] = WordRewriter2(str);
  341.         }
  342.         arr->len++;
  343.     } while (!(wordcom(arr->wordAr[arr->len - 1], empty).resOfCompare) &&
  344.              *(str + arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len) != '\0');
  345.     return arr;
  346. }
  347.  
  348. void arrayFree(struct WordArray *array)
  349. {
  350.     if (array->wordAr != NULL)
  351.     {
  352.         free(array->wordAr);
  353.     }
  354. }
  355.  
  356. void task3(void)
  357. {
  358.     char *str = "993Simba\'s ___Tale \n is shook\nback-and-forth";
  359.     struct Word word1 = {{'S', 'i', 'm', 'b', 'a', '\'', 's'}, 7, 1, 4, 3};
  360.     struct Word word2 = {{'T', 'a', 'l', 'e'}, 4, 1, 15, 14};
  361.     struct Word word3 = {{'i', 's'}, 2, 2, 2, 21};
  362.     struct Word word4 = {{'s', 'h', 'o', 'o', 'k'}, 5, 2, 5, 24};
  363.     struct Word word5 = {{'b', 'a', 'c', 'k', '-', 'a', 'n', 'd', '-', 'f', 'o', 'r', 't', 'h'}, 14, 3, 1, 30};
  364.     struct Word empty = {{'\0'}, 0, 1, 1, 0};
  365.     struct WordArray array1 = {NULL, 0};
  366.     struct WordArray *array = &array1;
  367.     array = getAllWordsArrayNew(str, array);
  368.     assert(wordcom(word1, array->wordAr[0]).resOfCompare == 1);
  369.     assert(wordcom(word2, array->wordAr[1]).resOfCompare == 1);
  370.     assert(wordcom(word3, array->wordAr[2]).resOfCompare == 1);
  371.     assert(wordcom(word4, array->wordAr[3]).resOfCompare == 1);
  372.     assert(wordcom(word5, array->wordAr[4]).resOfCompare == 1);
  373.     arrayFree(array);
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement