Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.84 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. int fileExists(const char *fileName);
  53. long getFileSize(const char *fileName);
  54. int readFileToBuffer(const char *fileName, char *buffer, int bufferLength);
  55. void task1(void);
  56. void task2(void);
  57. void task3(void);
  58. void task4(char *file);
  59.  
  60. int main(int argc, char *argv[])
  61. {
  62.     if (argc == 1)
  63.     {
  64.         return EXIT_FAILURE;
  65.     }
  66.     if (argc == 2)
  67.     {
  68.         if (!strcmp(argv[1], "task1"))
  69.         {
  70.             task1();
  71.             return EXIT_SUCCESS;
  72.         }
  73.         else if (!strcmp(argv[1], "task2"))
  74.         {
  75.             task2();
  76.             return EXIT_SUCCESS;
  77.         }
  78.         else if (!strcmp(argv[1], "task3"))
  79.         {
  80.             task3();
  81.             return EXIT_SUCCESS;
  82.         }
  83.         else
  84.         {
  85.             return EXIT_FAILURE;
  86.         }
  87.     }
  88.     else if (argc == 3 && !strcmp(argv[1], "task4") && fileExists(argv[2]))
  89.     {
  90.         task4(argv[2]);/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  91.         +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  92.         +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==
  93.         ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  94.         return EXIT_SUCCESS;
  95.     }
  96.     else
  97.     {
  98.         return EXIT_FAILURE;
  99.     }
  100.     return 0;
  101. }
  102.  
  103. void task1(void)
  104. {
  105.     char *nullAr = NULL;
  106.     assert(secondWordSearch("122=-|[1-1-") == -1);
  107.     assert(secondWordSearch("") == -1);
  108.     assert(secondWordSearch("wqe") == -1);
  109.     assert(secondWordSearch(nullAr) == -1);
  110.     assert(secondWordSearch("12qwqwpoewio -wqo") == 14);
  111.     assert(secondWordSearch("aa-b-c--ss") == 8);
  112.     assert(secondWordSearch("aa-b\'-c--ss") == 6);
  113.  
  114.     char buffer[100];
  115.     assert(WordRewriter1(nullAr, buffer, 100) == NULL);
  116.     assert(WordRewriter1("", buffer, 100) == NULL);
  117.     assert(WordRewriter1("201\'-@#$", buffer, 100) == NULL);
  118.     char expected[] = "abc-abc's";
  119.     char *actual = WordRewriter1("00-abc-abc's", buffer, 10);
  120.     for (int i = 0; i < 10; i++)
  121.     {
  122.         assert(expected[i] == actual[i]);
  123.     }
  124. }
  125.  
  126. int secondWordSearch(char *string)
  127. {
  128.     if (string == NULL)
  129.     {
  130.         return -1;
  131.     }
  132.  
  133.     int firstWord = 0;
  134.     int apostrophe = 0;
  135.     int i = 0;
  136.     for (i = 0;
  137.          (firstWord != 1 ||
  138.           isalpha(string[i]) ||
  139.           (i != 0 && isalpha(string[i - 1]) && apostrophe == 0 && string[i] == '\'') ||
  140.           (i != 0 && string[i] == '-' && isalpha(string[i - 1]) && isalpha(string[i + 1]))) &&
  141.          string[i] != '\0';
  142.          i++)
  143.     {
  144.         if (string[i] == '\'')
  145.         {
  146.             apostrophe = 1;
  147.         }
  148.         if (isalpha(string[i]))
  149.         {
  150.             firstWord = 1;
  151.         }
  152.     }
  153.     while ((!(isalpha(string[i])) && string[i] != '\0'))
  154.     {
  155.         i++;
  156.     }
  157.     if (string[i] == '\0')
  158.     {
  159.         return -1;
  160.     }
  161.     else
  162.     {
  163.         return i;
  164.     }
  165. }
  166.  
  167. char *WordRewriter1(const char *string, char *buffer, int bufLen)
  168. {
  169.     if (string == NULL)
  170.     {
  171.         return NULL;
  172.     }
  173.     int apostrophe = 0;
  174.     int wordStart = 0;
  175.     int wordLen = 0;
  176.     int i = 0;
  177.     for (i = 0;
  178.          (wordStart != 1 ||
  179.           isalpha(string[i]) ||
  180.           (i != 0 && isalpha(string[i - 1]) && apostrophe == 0 && string[i] == '\'') ||
  181.           (i != 0 && string[i] == '-' && isalpha(string[i - 1]) && isalpha(string[i + 1]))) &&
  182.          string[i] != '\0';
  183.          i++)
  184.     {
  185.         if (string[i] == '\'')
  186.         {
  187.             apostrophe = 1;
  188.         }
  189.         if (isalpha(string[i]))
  190.         {
  191.             wordStart = 1;
  192.         }
  193.         if (wordStart == 1)
  194.         {
  195.             wordLen++;
  196.         }
  197.     }
  198.     if (wordLen > bufLen || (string[i] == '\0' && wordStart != 1))
  199.     {
  200.         return NULL;
  201.     }
  202.     int delta = bufLen - wordLen;
  203.  
  204.     while (wordLen > 0)
  205.     {
  206.         buffer[bufLen - delta - wordLen] = string[i - wordLen];
  207.         wordLen--;
  208.     }
  209.  
  210.     buffer[bufLen - delta] = '\0';
  211.     return buffer;
  212. }
  213.  
  214. struct WordCompare wordcom(struct Word word1, struct Word word2)
  215. {
  216.     struct WordCompare result = {0, 0, 0, 0, 0, 0};
  217.     if (word1.Row == word2.Row)
  218.     {
  219.         result.Row = 1;
  220.     }
  221.     if (word1.Char == word2.Char)
  222.     {
  223.         result.Char = 1;
  224.     }
  225.     if (word1.index == word2.index)
  226.     {
  227.         result.index = 1;
  228.     }
  229.     if (word1.len == word2.len)
  230.     {
  231.         result.len = 1;
  232.     }
  233.     if (!(strcmp(word1.word, word2.word)))
  234.     {
  235.         result.word = 1;
  236.     }
  237.     if (result.Row && result.Char && result.index && result.len && result.word)
  238.     {
  239.         result.resOfCompare = 1;
  240.     }
  241.     return result;
  242. }
  243.  
  244. struct WordArrayCompare wordarcom(struct WordArray wordar1, struct WordArray wordar2)
  245. {
  246.     struct WordArrayCompare result = {0, 0, 0};
  247.     if (wordar1.wordAr == wordar2.wordAr)
  248.     {
  249.         result.wordAr = 1;
  250.     }
  251.     if (wordar1.len == wordar2.len)
  252.     {
  253.         result.len = 1;
  254.     }
  255.     if (result.len && result.wordAr)
  256.     {
  257.         result.resOfCompare = 1;
  258.     }
  259.     return result;
  260. }
  261.  
  262. struct Word WordRewriter2(const char *string)
  263. {
  264.  
  265.     struct Word result = {{'\0'}, 0, 1, 1, 0};
  266.     if (string == NULL)
  267.     {
  268.         return result;
  269.     }
  270.     while (!(isalpha(string[result.index])) && string[result.index] != '\0')
  271.     {
  272.         result.Char++;
  273.         if (string[result.index] == '\n')
  274.         {
  275.             result.Row++;
  276.             result.Char = 1;
  277.         }
  278.         result.index++;
  279.     }
  280.     if (string[result.index] != '\0')
  281.     {
  282.         char *buffer = WordRewriter1(string + result.index, result.word, 100);
  283.         result.len = strlen(buffer);
  284.         for (int i = 0; i < result.len; i++)
  285.         {
  286.             result.word[i] = buffer[i];
  287.         }
  288.     }
  289.     return result;
  290. }
  291.  
  292. void task2(void)
  293. {
  294.     struct Word word1 = {"Sugoi Desu!", 11, 5, 24, 100};
  295.     struct Word word2 = {"Sugoi Desu!", 11, 5, 24, 100};
  296.     struct Word word3 = {"Senpai!", 7, 6, 21, 100};
  297.     struct Word word4 = {"Sempai!", 7, 6, 21, 100};
  298.  
  299.     struct Word Ar1[4] = {word1, word2, word3, word4};
  300.     struct Word Ar2[4] = {word1, word1, word2, word2};
  301.  
  302.     struct WordArray wordar1 = {Ar1, 4};
  303.     struct WordArray wordar2 = {Ar2, 4};
  304.     struct WordArray wordar3 = {Ar1, 3};
  305.  
  306.     struct WordCompare zeroCompair = wordcom(word1, word1);
  307.     struct WordCompare firstCompair = wordcom(word1, word2);
  308.     struct WordCompare secondCompair = wordcom(word3, word2);
  309.     struct WordCompare thirdCompair = wordcom(word3, word4);
  310.  
  311.     assert(zeroCompair.resOfCompare == 1);
  312.     assert(firstCompair.word == 1);
  313.     assert(firstCompair.resOfCompare == 1);
  314.     assert(secondCompair.word == 0);
  315.     assert(secondCompair.len == 0);
  316.     assert(secondCompair.Row == 0);
  317.     assert(secondCompair.Char == 0);
  318.     assert(secondCompair.index == 1);
  319.     assert(thirdCompair.word == 0);
  320.     assert(thirdCompair.len == 1);
  321.     assert(thirdCompair.Row == 1);
  322.     assert(thirdCompair.Char == 1);
  323.     assert(thirdCompair.index == 1);
  324.  
  325.     struct WordArrayCompare fourthCompair = wordarcom(wordar3, wordar3);
  326.     struct WordArrayCompare fivethCompair = wordarcom(wordar1, wordar3);
  327.     struct WordArrayCompare sixthCompair = wordarcom(wordar2, wordar3);
  328.     struct WordArrayCompare seventhCompair = wordarcom(wordar1, wordar2);
  329.  
  330.     assert(fourthCompair.resOfCompare == 1);
  331.     assert(fivethCompair.resOfCompare == 0);
  332.     assert(fivethCompair.wordAr == 1);
  333.     assert(fivethCompair.len == 0);
  334.     assert(sixthCompair.resOfCompare == 0);
  335.     assert(seventhCompair.resOfCompare == 0);
  336.     assert(seventhCompair.len == 1);
  337.     assert(seventhCompair.wordAr == 0);
  338.  
  339.     char Text1[] = "\n\n---\n(OMAE-WA\'MOE-SINGEIRU)---";
  340.     char Text2[] = "\n-@\n#__(+__NANI__+)__#@-";
  341.  
  342.     struct Word fromText1 = WordRewriter2(Text1);
  343.     struct Word fromText2 = WordRewriter2(Text2);
  344.  
  345.     struct Word expected1 = {"OMAE-WA\'MOE-SINGEIRU", 20, 4, 2, 7};
  346.     struct Word expected2 = {"NANI", 4, 3, 8, 11};
  347.  
  348.     struct WordCompare eighthCompair = wordcom(fromText1, expected1);
  349.     struct WordCompare ninethCompair = wordcom(fromText2, expected2);
  350.     assert(eighthCompair.resOfCompare == 1);
  351.     assert(ninethCompair.resOfCompare == 1);
  352. }
  353.  
  354. struct WordArray *getAllWordsArrayNew(const char *str, struct WordArray *arr)
  355. {
  356.  
  357.     struct Word empty = {{'\0'}, 0, 1, 1, 0};
  358.     arr->len = 0;
  359.     arr->wordAr = malloc(sizeof(struct Word) * strlen(str) / 2);
  360.     arr->wordAr[0] = empty;
  361.     do
  362.     {
  363.  
  364.         if (arr->len != 0)
  365.         {
  366.  
  367.             arr->wordAr[arr->len] = WordRewriter2(str + arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len);
  368.  
  369.             arr->wordAr[arr->len].index += arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len;
  370.  
  371.             arr->wordAr[arr->len].Row += arr->wordAr[arr->len - 1].Row - 1;
  372.  
  373.             if (arr->wordAr[arr->len].Row == arr->wordAr[arr->len - 1].Row)
  374.             {
  375.                 arr->wordAr[arr->len].Char += arr->wordAr[arr->len - 1].Char + arr->wordAr[arr->len - 1].len - 1;
  376.             }
  377.         }
  378.         else
  379.         {
  380.             arr->wordAr[arr->len] = WordRewriter2(str);
  381.         }
  382.         arr->len++;
  383.     } while (!(wordcom(arr->wordAr[arr->len - 1], empty).resOfCompare) &&
  384.              *(str + arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len) != '\0');
  385.     return arr;
  386. }
  387.  
  388. void arrayFree(struct WordArray *array)
  389. {
  390.     if (array->wordAr != NULL)
  391.     {
  392.         free(array->wordAr);
  393.     }
  394. }
  395.  
  396. void task3(void)
  397. {
  398.     char *str = "993Simba\'s ___Tale \n is shook\nback-and-forth";
  399.     struct Word word1 = {{'S', 'i', 'm', 'b', 'a', '\'', 's'}, 7, 1, 4, 3};
  400.     struct Word word2 = {{'T', 'a', 'l', 'e'}, 4, 1, 15, 14};
  401.     struct Word word3 = {{'i', 's'}, 2, 2, 2, 21};
  402.     struct Word word4 = {{'s', 'h', 'o', 'o', 'k'}, 5, 2, 5, 24};
  403.     struct Word word5 = {{'b', 'a', 'c', 'k', '-', 'a', 'n', 'd', '-', 'f', 'o', 'r', 't', 'h'}, 14, 3, 1, 30};
  404.     struct WordArray arr1 = {NULL, 0};
  405.     struct WordArray *array = &arr1;
  406.     array = getAllWordsArrayNew(str, array);
  407.     assert(wordcom(word1, array->wordAr[0]).resOfCompare == 1);
  408.     assert(wordcom(word2, array->wordAr[1]).resOfCompare == 1);
  409.     assert(wordcom(word3, array->wordAr[2]).resOfCompare == 1);
  410.     assert(wordcom(word4, array->wordAr[3]).resOfCompare == 1);
  411.     assert(wordcom(word5, array->wordAr[4]).resOfCompare == 1);
  412.     arrayFree(array);
  413. }
  414.  
  415. int fileExists(const char *fileName)
  416. {
  417.     FILE *f = fopen(fileName, "rb");
  418.     if (!f)
  419.         return 0; // false: not exists
  420.     fclose(f);
  421.     return 1; // true: exists
  422. }
  423.  
  424. long getFileSize(const char *fileName)
  425. {
  426.     FILE *f = fopen(fileName, "rb");
  427.     if (!f)
  428.         return -1;         // error opening file
  429.     fseek(f, 0, SEEK_END); // rewind cursor to the end of file
  430.     long fsize = ftell(f); // get file size in bytes
  431.     fclose(f);
  432.     return fsize;
  433. }
  434.  
  435. int readFileToBuffer(const char *fileName, char *buffer, int bufferLength)
  436. {
  437.     FILE *f = fopen(fileName, "rb");
  438.     if (!f)
  439.         return 0; // read 0 bytes from file
  440.     long readBytes = fread(buffer, 1, bufferLength, f);
  441.     fclose(f);
  442.     return readBytes; // number of bytes read
  443. }
  444.  
  445. void task4(char *file)
  446. {
  447.     long bites = getFileSize(file);
  448.     char *buffer = calloc(2 * bites, sizeof(char));
  449.     bites = readFileToBuffer(file, buffer, bites) / sizeof(char);
  450.     buffer[bites] = '\0';
  451.     puts(buffer);
  452.     struct WordArray arr1 = {NULL, 0};
  453.     struct WordArray *array = &arr1;
  454.     array = getAllWordsArrayNew(buffer, array);
  455.     puts("Variant 7\n---------------------------------------------");
  456.     puts("Illumination");
  457.     char vow[] = {'a', 'e', 'y', 'u', 'i', 'o', 'A', 'E', 'Y', 'U', 'I', 'O'};
  458.     int count = 0;
  459.     for(int i = 0; i < bites; i++){
  460.         if(isalpha(buffer[i])){
  461.             if(array->wordAr[count].len < 5){
  462.                 Console_setCursorAttribute(BG_RED);
  463.                 Console_setCursorAttribute(FG_WHITE);
  464.             }
  465.             else if(strchr(vow, array->wordAr[count].word[0])){
  466.                 Console_setCursorAttribute(BG_BLUE);
  467.                 Console_setCursorAttribute(FG_WHITE);
  468.             }
  469.             printf("%s", array->wordAr[count].word);
  470.             i = i + array->wordAr[count].len - 1;
  471.             count++;
  472.             Console_setCursorAttribute(BG_DEFAULT);
  473.             Console_setCursorAttribute(FG_DEFAULT);
  474.         }
  475.         else{
  476.             printf("%c", buffer[i]);
  477.         }
  478.     }
  479.     puts("\nSpecial words");
  480.     for(int i = 0; i < array->len; i++){
  481.         if(strchr(vow, array->wordAr[i].word[0])){
  482.             puts(array->wordAr[i].word);
  483.         }
  484.     }
  485.     arrayFree(array);
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement