Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <progbase.h>
  7. #include <progbase/console.h>
  8. #include <assert.h>
  9. #define INITIAL_CAPACITY 16
  10.  
  11. struct Abbr
  12. {
  13.     char source[100];
  14.     int len;
  15.     int lineNumber;
  16.     int position;
  17.     int index;
  18. };
  19.  
  20. struct AbbrArray
  21. {
  22.     struct Abbr *array;
  23.     int size;
  24. };
  25. struct Abbr *getMax(struct AbbrArray *array)
  26. {
  27.     int size = array->size;
  28.     int max = array->array[0].len;
  29.     int imax = 0;
  30.     int i;
  31.     for (i = 0; i < size; i++)
  32.     {
  33.         if (max < array->array[i].len)
  34.         {
  35.             max = array->array[i].len;
  36.             imax = i;
  37.         }
  38.     }
  39.     return &array->array[imax];
  40. }
  41. struct Abbr *getMin(struct AbbrArray *array)
  42. {
  43.     int size = array->size;
  44.     int min = array->array[0].len;
  45.     int imin = 0;
  46.     int i;
  47.     for (i = 0; i < size; i++)
  48.     {
  49.         if (min > array->array[i].len)
  50.         {
  51.             min = array->array[i].len;
  52.             imin = i;
  53.         }
  54.     }
  55.     return &array->array[imin];
  56. }
  57.  
  58. long getFileSize(const char *fileName);
  59. int fileExists(const char *fileName);
  60. int IndexOfAbb(char str[]);
  61. int readFileToBuffer(const char *fileName, char *buffer, int bufferLength);
  62. int indexOfChar(char str[], char a)
  63. {
  64.     for (int i = 0; i < strlen(str); i++)
  65.     {
  66.         if (str[i] == a)
  67.             return i;
  68.     }
  69.     return -1;
  70. }
  71.  
  72. void shiftLeft(char str[], int len, int from)
  73. {
  74.     for (int i = 0; i < len - from; i++)
  75.     {
  76.         str[i] = str[from + i];
  77.     }
  78.     str[len - from] = '\0';
  79. }
  80. void copyOfRange(char buffer[], char str[], int to, int from);
  81. void getAbb(char str[], char buff[], int len);
  82. struct AbbrArray *getAllAbbrsArrayNew(const char *str);
  83.  
  84. void arrayFree(struct AbbrArray *array)
  85. {
  86.     array->size = 0;
  87.     free(array->array);
  88.     free(array);
  89. }
  90. void printAbbrArray(struct AbbrArray *result);
  91. int abbrcmp(struct Abbr x, struct Abbr y);
  92. int abbrArraycmp(struct AbbrArray x, struct AbbrArray y);
  93. int isVowel(char a)
  94. {
  95.     char vowels[] = "AEIOU";
  96.     for (int i = 0; i < 6; i++)
  97.     {
  98.         if (vowels[i] == a)
  99.             return 1;
  100.     }
  101.     return 0;
  102. }
  103.  
  104. int main(int argc, char *argv[])
  105. {
  106.     struct Abbr x = {"HELL", 5, 1, 1, 1};
  107.     struct Abbr y = {"HELLO", 5, 2, 2, 2};
  108.     if (argc == 1)
  109.     {
  110.         return EXIT_FAILURE;
  111.     }
  112.     if (argc == 2)
  113.     {
  114.         if (!strcmp(argv[1], "task1"))
  115.         {
  116.             assert(IndexOfAbb("There is no abb right here") == -1);
  117.             assert(IndexOfAbb("ABB is 0") == 0);
  118.             assert(IndexOfAbb("first ABB IS 6") == 6);
  119.             assert(IndexOfAbb("") == -1);
  120.             char buffer[100];
  121.             getAbb("hello WORLD", buffer, 100);
  122.             assert(!strcmp("WORLD", buffer));
  123.             getAbb("first ABB TEST", buffer, 100);
  124.             assert(!strcmp("ABB", buffer));
  125.             getAbb("no abbrs here", buffer, 100);
  126.             assert(!strcmp("", buffer));
  127.             getAbb("", buffer, 100);
  128.             assert(!strcmp("", buffer));
  129.             assert(abbrcmp(x, x));
  130.             assert(!abbrcmp(x, y));
  131.             assert(!abbrcmp(x, y));
  132.             return EXIT_SUCCESS;
  133.         }
  134.         else if (!strcmp(argv[1], "task2"))
  135.         {
  136.             struct Abbr *array = calloc(1, sizeof(struct Abbr));
  137.             array[0] = x;
  138.             struct AbbrArray x1 = {
  139.                 .size = 1,
  140.                 .array = array};
  141.             assert(abbrArraycmp(x1, x1));
  142.             free(array);
  143.             array = malloc(2 * sizeof(struct Abbr));
  144.             array[0] = x;
  145.             array[1] = y;
  146.             x1.size = 2,
  147.             x1.array = array;
  148.             struct Abbr *array2 = calloc(2, sizeof(struct Abbr));
  149.             array2[0] = y;
  150.             array2[1] = x;
  151.             struct AbbrArray x2 = {
  152.                 .size = 2,
  153.                 .array = array2};
  154.             assert(!abbrArraycmp(x1, x2));
  155.             free(array);
  156.             free(array2);
  157.  
  158.             array = calloc(2, sizeof(struct Abbr));
  159.             array[0] = x;
  160.             array[1] = y;
  161.             x1.size = 2;
  162.             x1.array = array;
  163.             array2 = calloc(2, sizeof(struct Abbr));
  164.             x2.size = 2,
  165.             x2.array = array;
  166.             assert(abbrArraycmp(x1, x2));
  167.             free(array);
  168.             free(array2);
  169.             return EXIT_SUCCESS;
  170.         }
  171.         else if (!strcmp(argv[1], "task3"))
  172.         {
  173.             char *txt = "TESTsdasdasdas. asdasdas , MESSAGE HERE";
  174.             struct AbbrArray *result = getAllAbbrsArrayNew(txt);
  175.             assert(abbrArraycmp(*result, *result));
  176.             txt = "ANOTHER MESSAGE";
  177.             struct AbbrArray *result1 = getAllAbbrsArrayNew(txt);
  178.             assert(!abbrArraycmp(*result, *result1));
  179.             arrayFree(result);
  180.             arrayFree(result1);
  181.  
  182.             return EXIT_SUCCESS;
  183.         }
  184.         else
  185.         {
  186.             return EXIT_FAILURE;
  187.         }
  188.     }
  189.     else if (argc == 3 && !strcmp(argv[1], "task4") && fileExists(argv[2]))
  190.     {
  191.         char *fileName = argv[2];
  192.         int size = getFileSize(fileName) / sizeof(char);
  193.         char *buffer = calloc(2 * size, sizeof(char));
  194.         size = readFileToBuffer(fileName, buffer, size) / sizeof(char);
  195.         buffer[size] = '\0';
  196.         char string[size];
  197.         for (int i = 0; i <= size; i++)
  198.         {
  199.             string[i] = buffer[i];
  200.         }
  201.         string[size] = '\0';
  202.         struct AbbrArray *abbs = getAllAbbrsArrayNew(buffer);
  203.         puts("VARIANT 13");
  204.         printAbbrArray(abbs);
  205.         puts("Min is:");
  206.         struct Abbr *a = getMin(abbs);
  207.         puts(a->source);
  208.         a = getMax(abbs);
  209.         puts("Max is:");
  210.         puts(a->source);
  211.         puts("----------------------------");
  212.         int count = 0;
  213.         for (int i = 0; i < size; i++)
  214.         {
  215.             if (count < abbs->size)
  216.             {
  217.                 if (abbs->array[count].index == i)
  218.                 {
  219.                     count++;
  220.                     if (isVowel(string[i]))
  221.                         Console_setCursorAttributes(BG_BLUE);
  222.                     else
  223.                         Console_setCursorAttributes(BG_RED);
  224.                 }
  225.             }
  226.             if (!isupper(string[i]))
  227.                 Console_setCursorAttributes(BG_DEFAULT);
  228.  
  229.             putchar(string[i]);
  230.         }
  231.         puts("");
  232.         Console_setCursorAttributes(BG_DEFAULT);
  233.         free(buffer);
  234.         arrayFree(abbs);
  235.         return EXIT_SUCCESS;
  236.     }
  237.     else
  238.     {
  239.         return EXIT_FAILURE;
  240.     }
  241. }
  242. int abbrcmp(struct Abbr x, struct Abbr y)
  243. {
  244.     if (!strcmp(x.source, y.source) && (x.len == y.len) && (x.lineNumber == y.lineNumber) && (x.position == y.position) && (x.index == y.index))
  245.         return 1;
  246.     return 0;
  247. }
  248.  
  249. void printAbbrArray(struct AbbrArray *result)
  250. {
  251.     puts("----------------------------");
  252.     printf("Size of Array is: %i\n", result->size);
  253.     for (int i = 0; i < result->size; i++)
  254.     {
  255.         printf("(%i), length: %i ", result->array[i].index, result->array[i].len);
  256.         puts(result->array[i].source);
  257.     }
  258.  
  259.     puts("\n----------------------------");
  260. }
  261. int abbrArraycmp(struct AbbrArray x, struct AbbrArray y)
  262. {
  263.     if (x.size != y.size)
  264.         return 0;
  265.     for (int i = 0; i < x.size; i++)
  266.     {
  267.         if (!abbrcmp(x.array[i], y.array[i]))
  268.             return 0;
  269.     }
  270.     return 1;
  271. }
  272.  
  273. int IndexOfAbb(char str[])
  274. {
  275.     int index = -1;
  276.     int size = strlen(str);
  277.     if (isupper(str[0]) && isupper(str[1]))
  278.         return 0;
  279.  
  280.     for (int i = 0; i < size - 1; i++)
  281.     {
  282.  
  283.         if (isupper(str[i]) && isupper(str[i + 1]) && !isupper(str[i - 1]))
  284.         {
  285.             index = i;
  286.             break;
  287.         }
  288.     }
  289.     return index;
  290. }
  291. void getAbb(char str[], char buff[], int len)
  292. {
  293.     buff[0] = '\0';
  294.     int size = strlen(str);
  295.     int j = 0;
  296.     for (int i = 0; i < size - 1; i++)
  297.     {
  298.         if (isupper(str[i]) && isupper(str[i + 1]))
  299.         {
  300.             while (isupper(str[i]))
  301.             {
  302.                 if (j < len)
  303.                 {
  304.                     buff[j] = str[i];
  305.                     i++;
  306.                     j++;
  307.                 }
  308.             }
  309.             break;
  310.         }
  311.     }
  312.     buff[j] = '\0';
  313. }
  314. struct AbbrArray *getAllAbbrsArrayNew(const char *str)
  315. {
  316.     int capacity = INITIAL_CAPACITY;
  317.     int len = strlen(str);
  318.     struct AbbrArray *result = calloc(1, sizeof(struct AbbrArray));
  319.     result->array = calloc(capacity, sizeof(struct Abbr));
  320.     result->size = 0;
  321.  
  322.     char string[len];
  323.     string[0] = '\0';
  324.     for (int i = 0; i <= len; i++)
  325.     {
  326.         string[i] = str[i];
  327.     }
  328.     int index = IndexOfAbb(string);
  329.     int counter = 0;
  330.     int gap = 0;
  331.     int line = 0;
  332.     while (index != -1)
  333.     {
  334.  
  335.         index = IndexOfAbb(string);
  336.         int indexOfEnter = indexOfChar(string, '\n') != -1 ? indexOfChar(string, '\n') : strlen(str);
  337.         if (index > indexOfEnter)
  338.             line++;
  339.         getAbb(string, result->array[counter].source, 100);
  340.         result->array[counter].index = index + gap;
  341.         result->array[counter].len = strlen(result->array[counter].source);
  342.         result->array[counter].lineNumber = line;
  343.         if (indexOfEnter != strlen(str))
  344.             result->array[counter].position = index - indexOfEnter;
  345.         result->array[counter].position = 0;
  346.         counter++;
  347.         if (counter > capacity)
  348.         {
  349.             capacity *= 2;
  350.             result->array = realloc(result->array, capacity * sizeof(struct Abbr));
  351.         }
  352.         if (index != -1)
  353.         {
  354.             while (isupper(string[index]))
  355.             {
  356.                 index++;
  357.             }
  358.         }
  359.  
  360.         shiftLeft(string, strlen(string), index);
  361.         gap += index;
  362.     }
  363.     result->size = counter - 1;
  364.     return result;
  365. }
  366. void copyOfRange(char buffer[], char str[], int to, int from)
  367. {
  368.     for (int i = 0; i < to - from; i++)
  369.     {
  370.         buffer[i] = str[i + from];
  371.     }
  372.     buffer[to - from] = '\0';
  373. }
  374.  
  375. int fileExists(const char *fileName)
  376. {
  377.     FILE *f = fopen(fileName, "rb");
  378.     if (!f)
  379.         return 0; // false: not exists
  380.     fclose(f);
  381.     return 1; // true: exists
  382. }
  383.  
  384. long getFileSize(const char *fileName)
  385. {
  386.     FILE *f = fopen(fileName, "rb");
  387.     if (!f)
  388.         return -1;         // error opening file
  389.     fseek(f, 0, SEEK_END); // rewind cursor to the end of file
  390.     long fsize = ftell(f); // get file size in bytes
  391.     fclose(f);
  392.     return fsize;
  393. }
  394. int readFileToBuffer(const char *fileName, char *buffer, int bufferLength)
  395. {
  396.     FILE *f = fopen(fileName, "rb");
  397.     if (!f)
  398.         return 0; // read 0 bytes from file
  399.     long readBytes = fread(buffer, 1, bufferLength, f);
  400.     fclose(f);
  401.     return readBytes; // number of bytes read
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement