Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <time.h>
- #include <progbase.h>
- #include <progbase/console.h>
- #include <assert.h>
- #define INITIAL_CAPACITY 16
- struct Abbr
- {
- char source[100];
- int len;
- int lineNumber;
- int position;
- int index;
- };
- struct AbbrArray
- {
- struct Abbr *array;
- int size;
- };
- struct Abbr *getMax(struct AbbrArray *array)
- {
- int size = array->size;
- int max = array->array[0].len;
- int imax = 0;
- int i;
- for (i = 0; i < size; i++)
- {
- if (max < array->array[i].len)
- {
- max = array->array[i].len;
- imax = i;
- }
- }
- return &array->array[imax];
- }
- struct Abbr *getMin(struct AbbrArray *array)
- {
- int size = array->size;
- int min = array->array[0].len;
- int imin = 0;
- int i;
- for (i = 0; i < size; i++)
- {
- if (min > array->array[i].len)
- {
- min = array->array[i].len;
- imin = i;
- }
- }
- return &array->array[imin];
- }
- long getFileSize(const char *fileName);
- int fileExists(const char *fileName);
- int IndexOfAbb(char str[]);
- int readFileToBuffer(const char *fileName, char *buffer, int bufferLength);
- int indexOfChar(char str[], char a)
- {
- for (int i = 0; i < strlen(str); i++)
- {
- if (str[i] == a)
- return i;
- }
- return -1;
- }
- void shiftLeft(char str[], int len, int from)
- {
- for (int i = 0; i < len - from; i++)
- {
- str[i] = str[from + i];
- }
- str[len - from] = '\0';
- }
- void copyOfRange(char buffer[], char str[], int to, int from);
- void getAbb(char str[], char buff[], int len);
- struct AbbrArray *getAllAbbrsArrayNew(const char *str);
- void arrayFree(struct AbbrArray *array)
- {
- array->size = 0;
- free(array->array);
- free(array);
- }
- void printAbbrArray(struct AbbrArray *result);
- int abbrcmp(struct Abbr x, struct Abbr y);
- int abbrArraycmp(struct AbbrArray x, struct AbbrArray y);
- int isVowel(char a)
- {
- char vowels[] = "AEIOU";
- for (int i = 0; i < 6; i++)
- {
- if (vowels[i] == a)
- return 1;
- }
- return 0;
- }
- int main(int argc, char *argv[])
- {
- struct Abbr x = {"HELL", 5, 1, 1, 1};
- struct Abbr y = {"HELLO", 5, 2, 2, 2};
- if (argc == 1)
- {
- return EXIT_FAILURE;
- }
- if (argc == 2)
- {
- if (!strcmp(argv[1], "task1"))
- {
- assert(IndexOfAbb("There is no abb right here") == -1);
- assert(IndexOfAbb("ABB is 0") == 0);
- assert(IndexOfAbb("first ABB IS 6") == 6);
- assert(IndexOfAbb("") == -1);
- char buffer[100];
- getAbb("hello WORLD", buffer, 100);
- assert(!strcmp("WORLD", buffer));
- getAbb("first ABB TEST", buffer, 100);
- assert(!strcmp("ABB", buffer));
- getAbb("no abbrs here", buffer, 100);
- assert(!strcmp("", buffer));
- getAbb("", buffer, 100);
- assert(!strcmp("", buffer));
- assert(abbrcmp(x, x));
- assert(!abbrcmp(x, y));
- assert(!abbrcmp(x, y));
- return EXIT_SUCCESS;
- }
- else if (!strcmp(argv[1], "task2"))
- {
- struct Abbr *array = calloc(1, sizeof(struct Abbr));
- array[0] = x;
- struct AbbrArray x1 = {
- .size = 1,
- .array = array};
- assert(abbrArraycmp(x1, x1));
- free(array);
- array = malloc(2 * sizeof(struct Abbr));
- array[0] = x;
- array[1] = y;
- x1.size = 2,
- x1.array = array;
- struct Abbr *array2 = calloc(2, sizeof(struct Abbr));
- array2[0] = y;
- array2[1] = x;
- struct AbbrArray x2 = {
- .size = 2,
- .array = array2};
- assert(!abbrArraycmp(x1, x2));
- free(array);
- free(array2);
- array = calloc(2, sizeof(struct Abbr));
- array[0] = x;
- array[1] = y;
- x1.size = 2;
- x1.array = array;
- array2 = calloc(2, sizeof(struct Abbr));
- x2.size = 2,
- x2.array = array;
- assert(abbrArraycmp(x1, x2));
- free(array);
- free(array2);
- return EXIT_SUCCESS;
- }
- else if (!strcmp(argv[1], "task3"))
- {
- char *txt = "TESTsdasdasdas. asdasdas , MESSAGE HERE";
- struct AbbrArray *result = getAllAbbrsArrayNew(txt);
- assert(abbrArraycmp(*result, *result));
- txt = "ANOTHER MESSAGE";
- struct AbbrArray *result1 = getAllAbbrsArrayNew(txt);
- assert(!abbrArraycmp(*result, *result1));
- arrayFree(result);
- arrayFree(result1);
- return EXIT_SUCCESS;
- }
- else
- {
- return EXIT_FAILURE;
- }
- }
- else if (argc == 3 && !strcmp(argv[1], "task4") && fileExists(argv[2]))
- {
- char *fileName = argv[2];
- int size = getFileSize(fileName) / sizeof(char);
- char *buffer = calloc(2 * size, sizeof(char));
- size = readFileToBuffer(fileName, buffer, size) / sizeof(char);
- buffer[size] = '\0';
- char string[size];
- for (int i = 0; i <= size; i++)
- {
- string[i] = buffer[i];
- }
- string[size] = '\0';
- struct AbbrArray *abbs = getAllAbbrsArrayNew(buffer);
- puts("VARIANT 13");
- printAbbrArray(abbs);
- puts("Min is:");
- struct Abbr *a = getMin(abbs);
- puts(a->source);
- a = getMax(abbs);
- puts("Max is:");
- puts(a->source);
- puts("----------------------------");
- int count = 0;
- for (int i = 0; i < size; i++)
- {
- if (count < abbs->size)
- {
- if (abbs->array[count].index == i)
- {
- count++;
- if (isVowel(string[i]))
- Console_setCursorAttributes(BG_BLUE);
- else
- Console_setCursorAttributes(BG_RED);
- }
- }
- if (!isupper(string[i]))
- Console_setCursorAttributes(BG_DEFAULT);
- putchar(string[i]);
- }
- puts("");
- Console_setCursorAttributes(BG_DEFAULT);
- free(buffer);
- arrayFree(abbs);
- return EXIT_SUCCESS;
- }
- else
- {
- return EXIT_FAILURE;
- }
- }
- int abbrcmp(struct Abbr x, struct Abbr y)
- {
- if (!strcmp(x.source, y.source) && (x.len == y.len) && (x.lineNumber == y.lineNumber) && (x.position == y.position) && (x.index == y.index))
- return 1;
- return 0;
- }
- void printAbbrArray(struct AbbrArray *result)
- {
- puts("----------------------------");
- printf("Size of Array is: %i\n", result->size);
- for (int i = 0; i < result->size; i++)
- {
- printf("(%i), length: %i ", result->array[i].index, result->array[i].len);
- puts(result->array[i].source);
- }
- puts("\n----------------------------");
- }
- int abbrArraycmp(struct AbbrArray x, struct AbbrArray y)
- {
- if (x.size != y.size)
- return 0;
- for (int i = 0; i < x.size; i++)
- {
- if (!abbrcmp(x.array[i], y.array[i]))
- return 0;
- }
- return 1;
- }
- int IndexOfAbb(char str[])
- {
- int index = -1;
- int size = strlen(str);
- if (isupper(str[0]) && isupper(str[1]))
- return 0;
- for (int i = 0; i < size - 1; i++)
- {
- if (isupper(str[i]) && isupper(str[i + 1]) && !isupper(str[i - 1]))
- {
- index = i;
- break;
- }
- }
- return index;
- }
- void getAbb(char str[], char buff[], int len)
- {
- buff[0] = '\0';
- int size = strlen(str);
- int j = 0;
- for (int i = 0; i < size - 1; i++)
- {
- if (isupper(str[i]) && isupper(str[i + 1]))
- {
- while (isupper(str[i]))
- {
- if (j < len)
- {
- buff[j] = str[i];
- i++;
- j++;
- }
- }
- break;
- }
- }
- buff[j] = '\0';
- }
- struct AbbrArray *getAllAbbrsArrayNew(const char *str)
- {
- int capacity = INITIAL_CAPACITY;
- int len = strlen(str);
- struct AbbrArray *result = calloc(1, sizeof(struct AbbrArray));
- result->array = calloc(capacity, sizeof(struct Abbr));
- result->size = 0;
- char string[len];
- string[0] = '\0';
- for (int i = 0; i <= len; i++)
- {
- string[i] = str[i];
- }
- int index = IndexOfAbb(string);
- int counter = 0;
- int gap = 0;
- int line = 0;
- while (index != -1)
- {
- index = IndexOfAbb(string);
- int indexOfEnter = indexOfChar(string, '\n') != -1 ? indexOfChar(string, '\n') : strlen(str);
- if (index > indexOfEnter)
- line++;
- getAbb(string, result->array[counter].source, 100);
- result->array[counter].index = index + gap;
- result->array[counter].len = strlen(result->array[counter].source);
- result->array[counter].lineNumber = line;
- if (indexOfEnter != strlen(str))
- result->array[counter].position = index - indexOfEnter;
- result->array[counter].position = 0;
- counter++;
- if (counter > capacity)
- {
- capacity *= 2;
- result->array = realloc(result->array, capacity * sizeof(struct Abbr));
- }
- if (index != -1)
- {
- while (isupper(string[index]))
- {
- index++;
- }
- }
- shiftLeft(string, strlen(string), index);
- gap += index;
- }
- result->size = counter - 1;
- return result;
- }
- void copyOfRange(char buffer[], char str[], int to, int from)
- {
- for (int i = 0; i < to - from; i++)
- {
- buffer[i] = str[i + from];
- }
- buffer[to - from] = '\0';
- }
- int fileExists(const char *fileName)
- {
- FILE *f = fopen(fileName, "rb");
- if (!f)
- return 0; // false: not exists
- fclose(f);
- return 1; // true: exists
- }
- long getFileSize(const char *fileName)
- {
- FILE *f = fopen(fileName, "rb");
- if (!f)
- return -1; // error opening file
- fseek(f, 0, SEEK_END); // rewind cursor to the end of file
- long fsize = ftell(f); // get file size in bytes
- fclose(f);
- return fsize;
- }
- int readFileToBuffer(const char *fileName, char *buffer, int bufferLength)
- {
- FILE *f = fopen(fileName, "rb");
- if (!f)
- return 0; // read 0 bytes from file
- long readBytes = fread(buffer, 1, bufferLength, f);
- fclose(f);
- return readBytes; // number of bytes read
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement