Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <assert.h>
- #include <progbase.h>
- #include <progbase/console.h>
- struct Word
- {
- char word[100];
- int len;
- int Row;
- int Char;
- int index;
- };
- struct WordArray
- {
- struct Word *wordAr;
- int len;
- };
- // Я вирішив, що доречно порівнювати
- // кожну складову структури окремо,
- // маючи при цьому загальний висновок порівняння
- // Тому створено окремі структори для цього
- struct WordCompare
- {
- int word;
- int len;
- int Row;
- int Char;
- int index;
- int resOfCompare;
- };
- struct WordArrayCompare
- {
- int wordAr;
- int len;
- int resOfCompare;
- };
- struct WordCompare wordcom(struct Word word1, struct Word word2);
- struct WordArrayCompare wordarcom(struct WordArray wordar1, struct WordArray wordar2);
- struct Word WordRewriter2(const char *string);
- int secondWordSearch(char *string);
- char *WordRewriter1(const char *string, char *buffer, int buflen);
- struct WordArray *getAllWordsArrayNew(const char *str, struct WordArray *arr);
- void arrayFree(struct WordArray *array);
- int fileExists(const char *fileName);
- long getFileSize(const char *fileName);
- int readFileToBuffer(const char *fileName, char *buffer, int bufferLength);
- void task1(void);
- void task2(void);
- void task3(void);
- void task4(char *file);
- int main(int argc, char *argv[])
- {
- if (argc == 1)
- {
- return EXIT_FAILURE;
- }
- if (argc == 2)
- {
- if (!strcmp(argv[1], "task1"))
- {
- task1();
- return EXIT_SUCCESS;
- }
- else if (!strcmp(argv[1], "task2"))
- {
- task2();
- return EXIT_SUCCESS;
- }
- else if (!strcmp(argv[1], "task3"))
- {
- task3();
- return EXIT_SUCCESS;
- }
- else
- {
- return EXIT_FAILURE;
- }
- }
- else if (argc == 3 && !strcmp(argv[1], "task4") && fileExists(argv[2]))
- {
- task4(argv[2]);/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
- return EXIT_SUCCESS;
- }
- else
- {
- return EXIT_FAILURE;
- }
- return 0;
- }
- void task1(void)
- {
- char *nullAr = NULL;
- assert(secondWordSearch("122=-|[1-1-") == -1);
- assert(secondWordSearch("") == -1);
- assert(secondWordSearch("wqe") == -1);
- assert(secondWordSearch(nullAr) == -1);
- assert(secondWordSearch("12qwqwpoewio -wqo") == 14);
- assert(secondWordSearch("aa-b-c--ss") == 8);
- assert(secondWordSearch("aa-b\'-c--ss") == 6);
- char buffer[100];
- assert(WordRewriter1(nullAr, buffer, 100) == NULL);
- assert(WordRewriter1("", buffer, 100) == NULL);
- assert(WordRewriter1("201\'-@#$", buffer, 100) == NULL);
- char expected[] = "abc-abc's";
- char *actual = WordRewriter1("00-abc-abc's", buffer, 10);
- for (int i = 0; i < 10; i++)
- {
- assert(expected[i] == actual[i]);
- }
- }
- int secondWordSearch(char *string)
- {
- if (string == NULL)
- {
- return -1;
- }
- int firstWord = 0;
- int apostrophe = 0;
- int i = 0;
- for (i = 0;
- (firstWord != 1 ||
- isalpha(string[i]) ||
- (i != 0 && isalpha(string[i - 1]) && apostrophe == 0 && string[i] == '\'') ||
- (i != 0 && string[i] == '-' && isalpha(string[i - 1]) && isalpha(string[i + 1]))) &&
- string[i] != '\0';
- i++)
- {
- if (string[i] == '\'')
- {
- apostrophe = 1;
- }
- if (isalpha(string[i]))
- {
- firstWord = 1;
- }
- }
- while ((!(isalpha(string[i])) && string[i] != '\0'))
- {
- i++;
- }
- if (string[i] == '\0')
- {
- return -1;
- }
- else
- {
- return i;
- }
- }
- char *WordRewriter1(const char *string, char *buffer, int bufLen)
- {
- if (string == NULL)
- {
- return NULL;
- }
- int apostrophe = 0;
- int wordStart = 0;
- int wordLen = 0;
- int i = 0;
- for (i = 0;
- (wordStart != 1 ||
- isalpha(string[i]) ||
- (i != 0 && isalpha(string[i - 1]) && apostrophe == 0 && string[i] == '\'') ||
- (i != 0 && string[i] == '-' && isalpha(string[i - 1]) && isalpha(string[i + 1]))) &&
- string[i] != '\0';
- i++)
- {
- if (string[i] == '\'')
- {
- apostrophe = 1;
- }
- if (isalpha(string[i]))
- {
- wordStart = 1;
- }
- if (wordStart == 1)
- {
- wordLen++;
- }
- }
- if (wordLen > bufLen || (string[i] == '\0' && wordStart != 1))
- {
- return NULL;
- }
- int delta = bufLen - wordLen;
- while (wordLen > 0)
- {
- buffer[bufLen - delta - wordLen] = string[i - wordLen];
- wordLen--;
- }
- buffer[bufLen - delta] = '\0';
- return buffer;
- }
- struct WordCompare wordcom(struct Word word1, struct Word word2)
- {
- struct WordCompare result = {0, 0, 0, 0, 0, 0};
- if (word1.Row == word2.Row)
- {
- result.Row = 1;
- }
- if (word1.Char == word2.Char)
- {
- result.Char = 1;
- }
- if (word1.index == word2.index)
- {
- result.index = 1;
- }
- if (word1.len == word2.len)
- {
- result.len = 1;
- }
- if (!(strcmp(word1.word, word2.word)))
- {
- result.word = 1;
- }
- if (result.Row && result.Char && result.index && result.len && result.word)
- {
- result.resOfCompare = 1;
- }
- return result;
- }
- struct WordArrayCompare wordarcom(struct WordArray wordar1, struct WordArray wordar2)
- {
- struct WordArrayCompare result = {0, 0, 0};
- if (wordar1.wordAr == wordar2.wordAr)
- {
- result.wordAr = 1;
- }
- if (wordar1.len == wordar2.len)
- {
- result.len = 1;
- }
- if (result.len && result.wordAr)
- {
- result.resOfCompare = 1;
- }
- return result;
- }
- struct Word WordRewriter2(const char *string)
- {
- struct Word result = {{'\0'}, 0, 1, 1, 0};
- if (string == NULL)
- {
- return result;
- }
- while (!(isalpha(string[result.index])) && string[result.index] != '\0')
- {
- result.Char++;
- if (string[result.index] == '\n')
- {
- result.Row++;
- result.Char = 1;
- }
- result.index++;
- }
- if (string[result.index] != '\0')
- {
- char *buffer = WordRewriter1(string + result.index, result.word, 100);
- result.len = strlen(buffer);
- for (int i = 0; i < result.len; i++)
- {
- result.word[i] = buffer[i];
- }
- }
- return result;
- }
- void task2(void)
- {
- struct Word word1 = {"Sugoi Desu!", 11, 5, 24, 100};
- struct Word word2 = {"Sugoi Desu!", 11, 5, 24, 100};
- struct Word word3 = {"Senpai!", 7, 6, 21, 100};
- struct Word word4 = {"Sempai!", 7, 6, 21, 100};
- struct Word Ar1[4] = {word1, word2, word3, word4};
- struct Word Ar2[4] = {word1, word1, word2, word2};
- struct WordArray wordar1 = {Ar1, 4};
- struct WordArray wordar2 = {Ar2, 4};
- struct WordArray wordar3 = {Ar1, 3};
- struct WordCompare zeroCompair = wordcom(word1, word1);
- struct WordCompare firstCompair = wordcom(word1, word2);
- struct WordCompare secondCompair = wordcom(word3, word2);
- struct WordCompare thirdCompair = wordcom(word3, word4);
- assert(zeroCompair.resOfCompare == 1);
- assert(firstCompair.word == 1);
- assert(firstCompair.resOfCompare == 1);
- assert(secondCompair.word == 0);
- assert(secondCompair.len == 0);
- assert(secondCompair.Row == 0);
- assert(secondCompair.Char == 0);
- assert(secondCompair.index == 1);
- assert(thirdCompair.word == 0);
- assert(thirdCompair.len == 1);
- assert(thirdCompair.Row == 1);
- assert(thirdCompair.Char == 1);
- assert(thirdCompair.index == 1);
- struct WordArrayCompare fourthCompair = wordarcom(wordar3, wordar3);
- struct WordArrayCompare fivethCompair = wordarcom(wordar1, wordar3);
- struct WordArrayCompare sixthCompair = wordarcom(wordar2, wordar3);
- struct WordArrayCompare seventhCompair = wordarcom(wordar1, wordar2);
- assert(fourthCompair.resOfCompare == 1);
- assert(fivethCompair.resOfCompare == 0);
- assert(fivethCompair.wordAr == 1);
- assert(fivethCompair.len == 0);
- assert(sixthCompair.resOfCompare == 0);
- assert(seventhCompair.resOfCompare == 0);
- assert(seventhCompair.len == 1);
- assert(seventhCompair.wordAr == 0);
- char Text1[] = "\n\n---\n(OMAE-WA\'MOE-SINGEIRU)---";
- char Text2[] = "\n-@\n#__(+__NANI__+)__#@-";
- struct Word fromText1 = WordRewriter2(Text1);
- struct Word fromText2 = WordRewriter2(Text2);
- struct Word expected1 = {"OMAE-WA\'MOE-SINGEIRU", 20, 4, 2, 7};
- struct Word expected2 = {"NANI", 4, 3, 8, 11};
- struct WordCompare eighthCompair = wordcom(fromText1, expected1);
- struct WordCompare ninethCompair = wordcom(fromText2, expected2);
- assert(eighthCompair.resOfCompare == 1);
- assert(ninethCompair.resOfCompare == 1);
- }
- struct WordArray *getAllWordsArrayNew(const char *str, struct WordArray *arr)
- {
- struct Word empty = {{'\0'}, 0, 1, 1, 0};
- arr->len = 0;
- arr->wordAr = malloc(sizeof(struct Word) * strlen(str) / 2);
- arr->wordAr[0] = empty;
- do
- {
- if (arr->len != 0)
- {
- arr->wordAr[arr->len] = WordRewriter2(str + arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len);
- arr->wordAr[arr->len].index += arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len;
- arr->wordAr[arr->len].Row += arr->wordAr[arr->len - 1].Row - 1;
- if (arr->wordAr[arr->len].Row == arr->wordAr[arr->len - 1].Row)
- {
- arr->wordAr[arr->len].Char += arr->wordAr[arr->len - 1].Char + arr->wordAr[arr->len - 1].len - 1;
- }
- }
- else
- {
- arr->wordAr[arr->len] = WordRewriter2(str);
- }
- arr->len++;
- } while (!(wordcom(arr->wordAr[arr->len - 1], empty).resOfCompare) &&
- *(str + arr->wordAr[arr->len - 1].index + arr->wordAr[arr->len - 1].len) != '\0');
- return arr;
- }
- void arrayFree(struct WordArray *array)
- {
- if (array->wordAr != NULL)
- {
- free(array->wordAr);
- }
- }
- void task3(void)
- {
- char *str = "993Simba\'s ___Tale \n is shook\nback-and-forth";
- struct Word word1 = {{'S', 'i', 'm', 'b', 'a', '\'', 's'}, 7, 1, 4, 3};
- struct Word word2 = {{'T', 'a', 'l', 'e'}, 4, 1, 15, 14};
- struct Word word3 = {{'i', 's'}, 2, 2, 2, 21};
- struct Word word4 = {{'s', 'h', 'o', 'o', 'k'}, 5, 2, 5, 24};
- struct Word word5 = {{'b', 'a', 'c', 'k', '-', 'a', 'n', 'd', '-', 'f', 'o', 'r', 't', 'h'}, 14, 3, 1, 30};
- struct WordArray arr1 = {NULL, 0};
- struct WordArray *array = &arr1;
- array = getAllWordsArrayNew(str, array);
- assert(wordcom(word1, array->wordAr[0]).resOfCompare == 1);
- assert(wordcom(word2, array->wordAr[1]).resOfCompare == 1);
- assert(wordcom(word3, array->wordAr[2]).resOfCompare == 1);
- assert(wordcom(word4, array->wordAr[3]).resOfCompare == 1);
- assert(wordcom(word5, array->wordAr[4]).resOfCompare == 1);
- arrayFree(array);
- }
- 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
- }
- void task4(char *file)
- {
- long bites = getFileSize(file);
- char *buffer = calloc(2 * bites, sizeof(char));
- bites = readFileToBuffer(file, buffer, bites) / sizeof(char);
- buffer[bites] = '\0';
- puts(buffer);
- struct WordArray arr1 = {NULL, 0};
- struct WordArray *array = &arr1;
- array = getAllWordsArrayNew(buffer, array);
- puts("Variant 7\n---------------------------------------------");
- puts("Illumination");
- char vow[] = {'a', 'e', 'y', 'u', 'i', 'o', 'A', 'E', 'Y', 'U', 'I', 'O'};
- int count = 0;
- for(int i = 0; i < bites; i++){
- if(isalpha(buffer[i])){
- if(array->wordAr[count].len < 5){
- Console_setCursorAttribute(BG_RED);
- Console_setCursorAttribute(FG_WHITE);
- }
- else if(strchr(vow, array->wordAr[count].word[0])){
- Console_setCursorAttribute(BG_BLUE);
- Console_setCursorAttribute(FG_WHITE);
- }
- printf("%s", array->wordAr[count].word);
- i = i + array->wordAr[count].len - 1;
- count++;
- Console_setCursorAttribute(BG_DEFAULT);
- Console_setCursorAttribute(FG_DEFAULT);
- }
- else{
- printf("%c", buffer[i]);
- }
- }
- puts("\nSpecial words");
- for(int i = 0; i < array->len; i++){
- if(strchr(vow, array->wordAr[i].word[0])){
- puts(array->wordAr[i].word);
- }
- }
- arrayFree(array);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement