diff --git a/Control2/List/List.sln b/Control2/List/List.sln new file mode 100644 index 0000000..c70d9c5 --- /dev/null +++ b/Control2/List/List.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31410.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "List", "List\List.vcxproj", "{721388A3-2CFA-44FE-915A-0A846DB709F4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {721388A3-2CFA-44FE-915A-0A846DB709F4}.Debug|x64.ActiveCfg = Debug|x64 + {721388A3-2CFA-44FE-915A-0A846DB709F4}.Debug|x64.Build.0 = Debug|x64 + {721388A3-2CFA-44FE-915A-0A846DB709F4}.Debug|x86.ActiveCfg = Debug|Win32 + {721388A3-2CFA-44FE-915A-0A846DB709F4}.Debug|x86.Build.0 = Debug|Win32 + {721388A3-2CFA-44FE-915A-0A846DB709F4}.Release|x64.ActiveCfg = Release|x64 + {721388A3-2CFA-44FE-915A-0A846DB709F4}.Release|x64.Build.0 = Release|x64 + {721388A3-2CFA-44FE-915A-0A846DB709F4}.Release|x86.ActiveCfg = Release|Win32 + {721388A3-2CFA-44FE-915A-0A846DB709F4}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E12AB49D-6742-4F31-87F2-2C61C86207CE} + EndGlobalSection +EndGlobal diff --git a/Control2/List/List/List.c b/Control2/List/List/List.c new file mode 100644 index 0000000..8864364 --- /dev/null +++ b/Control2/List/List/List.c @@ -0,0 +1,225 @@ +#define _CRT_SECURE_NO_WARNINGS +#include "List.h" +#include +#include +#include +#include + +// Structure containing pointers to the beginning and end of the list +typedef struct List +{ + int size; + struct ListElement* head; + struct ListElement* tail; +} List; + +// Structure containing a pointer to the next list item and a value variable for the list items +typedef struct ListElement +{ + char* value; + struct ListElement* next; +} ListElement; + +// Structure containing a pointer to a ListElement +typedef struct Position +{ + ListElement* position; +} Position; + +List* createList(Error* error) +{ + List* list = calloc(1, sizeof(List)); + if (list == NULL) + { + *error = INSUFFICIENT_MEMORY; + } + return list; +} + +void deleteList(List* list) +{ + ListElement* position = list->head; + while (position != NULL) + { + list->head = list->head->next; + free(position->value); + free(position); + position = list->head; + } + free(list); +} + +void deletePosition(Position* position) +{ + free(position); +} + +void removeFirstElement(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + if (list->head == NULL) + { + *error = EMPTY_LIST; + return; + } + if (list->head == list->tail) + { + list->size = 0; + free(list->head->value); + free(list->head); + list->head = NULL; + list->tail = NULL; + return; + } + ListElement* element = list->head; + list->head = list->head->next; + list->size--; + free(element->value); + free(element); + +} + +Position* first(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return NULL; + } + Position* positionFirst = malloc(sizeof(Position)); + if (positionFirst == NULL) + { + *error = EMPTY_LIST; + return NULL; + } + positionFirst->position = list->head; + return positionFirst; +} + +Position* last(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return NULL; + } + Position* positionLast = malloc(sizeof(Position)); + if (positionLast == NULL) + { + *error = EMPTY_LIST; + return NULL; + } + positionLast->position = list->tail; + return positionLast; +} + +Position* next(Position* position) +{ + position->position = position->position->next; + return position; +} + +bool isLastElement(Position* position) +{ + return position->position->next == NULL; +} + +int numberOfElements(List* list) +{ + return list->size; +} + +char* getHeadValue(List* list) +{ + if (list->head == NULL) + { + return NULL; + } + return list->head->value; +} + +void add(List* list, const char* value, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + char* valueCopy = calloc(strlen(value) + 1, sizeof(char)); + if (valueCopy == NULL) + { + *error = INSUFFICIENT_MEMORY; + return; + } + strcpy(valueCopy, value); + ListElement* newElement = calloc(1, sizeof(ListElement)); + if (newElement == NULL) + { + free(valueCopy); + *error = INSUFFICIENT_MEMORY; + return; + } + newElement->value = valueCopy; + if (list->head == NULL) + { + list->size = 1; + list->head = newElement; + list->tail = newElement; + return; + } + list->size++; + list->tail->next = newElement; + list->tail = list->tail->next; +} + +char* getValue(Position* position) +{ + return position->position->value; +} + +bool isEmpty(List* list) +{ + return list->head == NULL; +} + +bool isOneElement(List* list) +{ + return list->head->next == NULL; +} + +void print(List* list) +{ + ListElement* element = list->head; + while (element != NULL) + { + printf("%s\n", element->value); + element = element->next; + } +} + +const char* decodingError(Error error) +{ + if (error == EMPTY_LIST) + { + return "Error in the program"; + } + if (error == INSUFFICIENT_MEMORY) + { + return "Memory not allocated"; + } + return NULL; +} + +bool inList(List* list, const char* value) +{ + ListElement* element = list->head; + while (element != NULL) + { + if (strcmp(value, element->value) == 0) + { + return true; + } + element = element->next; + } + return false; +} \ No newline at end of file diff --git a/Control2/List/List/List.h b/Control2/List/List/List.h new file mode 100644 index 0000000..896feea --- /dev/null +++ b/Control2/List/List/List.h @@ -0,0 +1,62 @@ +#pragma once +#include + +// Structure that represents list +typedef struct List List; + +// This is a structure describing the position of an item in the list. +typedef struct Position Position; + +// Enum type for working with errors +typedef enum Error +{ + NOT_ERROR, + EMPTY_LIST, + INSUFFICIENT_MEMORY, + ELEMENT_REPEATS +} Error; + +// Function for creating a list +List* createList(Error* error); + +// Function for deleting a list +void deleteList(List* list); + +// Function for freeing up memory +void deletePosition(Position* position); + +// Function for adding an item to a list +void add(List* list, const char* value, Error* error); + +// function to find a position to the first element +Position* first(List* list, Error* error); + +// Function for finding a position to the next element +Position* next(Position* position); + +// Function for checking an item for being at the end of the list +bool isLastElement(Position* position); + +// Function for printing a list +void print(List* list); + +// Function for deleting the first element +void removeFirstElement(List* list, Error* error); + +// Function for finding the number of items in the list +int numberOfElements(List* list); + +// Function for checking the emptiness of the list +bool isEmpty(List* list); + +// Function that returns a value for the first element +char* getHeadValue(List* list); + +// Function that returns a value of element +char* getValue(Position* position); + +// Function for error decoding +const char* decodingError(Error error); + +// Function for determining the location of an item in the list +bool inList(List* list, const char* value); diff --git a/Control2/List/List/List.vcxproj b/Control2/List/List/List.vcxproj new file mode 100644 index 0000000..3db9d2a --- /dev/null +++ b/Control2/List/List/List.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {721388a3-2cfa-44fe-915a-0a846db709f4} + List + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Control2/List/List/List.vcxproj.filters b/Control2/List/List/List.vcxproj.filters new file mode 100644 index 0000000..9c7580a --- /dev/null +++ b/Control2/List/List/List.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/Control2/List/List/Main.c b/Control2/List/List/Main.c new file mode 100644 index 0000000..6ffb51a --- /dev/null +++ b/Control2/List/List/Main.c @@ -0,0 +1,9 @@ +#include "TestList.h" + +int main() +{ + if (!allTest()) + { + return -1; + } +} \ No newline at end of file diff --git a/Control2/List/List/TestList.c b/Control2/List/List/TestList.c new file mode 100644 index 0000000..efbaa79 --- /dev/null +++ b/Control2/List/List/TestList.c @@ -0,0 +1,98 @@ +#include "TestList.h" +#include "List.h" +#include + +// Function to check the function that adds an item to the list +bool testAdd() +{ + Error error = NOT_ERROR; + List* newList = createList(&error); + add(newList, "12", &error); + add(newList, "wddw", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* firstValue = getHeadValue(newList); + Position* position = first(newList, &error); + if (error == EMPTY_LIST) + { + deleteList(newList); + return false; + } + next(position); + const char* secondValue = getValue(position); + bool result = strcmp(firstValue, "12") == 0 && strcmp(secondValue, "wddw") == 0; + deletePosition(position); + deleteList(newList); + return result; +} + +// Function to check the function that deletes the first item in the list +bool testRemoveHead() +{ + Error error = NOT_ERROR; + List* newList = createList(&error); + add(newList, "qwerty", &error); + add(newList, "uiop", &error); + removeFirstElement(newList, &error); + if (error == EMPTY_LIST || error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* headOfListValue = getHeadValue(newList); + bool result = strcmp(headOfListValue, "uiop") == 0; + deleteList(newList); + return result; +} + +// Function for checking the function counting the number of elements +bool testNumberOfElements() +{ + Error error = NOT_ERROR; + List* newList = createList(&error); + add(newList, "asd", &error); + add(newList, "xcxz", &error); + add(newList, "sfsf", &error); + add(newList, "123", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const int number = numberOfElements(newList); + deleteList(newList); + return number == 4; +} + +// Function that checks the function that returns the value of the first element +bool testGetValue() +{ + Error error = NOT_ERROR; + List* newList = createList(&error); + add(newList, "dg", &error); + add(newList, "hello", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* firstValue = getHeadValue(newList); + Position* position = first(newList, &error); + if (error != NOT_ERROR) + { + return false; + } + const char* secondValue = getValue(next(position)); + bool result = strcmp(firstValue, "dg") == 0 && strcmp(secondValue, "hello") == 0; + deletePosition(position); + deleteList(newList); + return result; +} + +bool allTest() +{ + return testAdd() && testGetValue() && testNumberOfElements() && testRemoveHead(); +} \ No newline at end of file diff --git a/Control2/List/List/TestList.h b/Control2/List/List/TestList.h new file mode 100644 index 0000000..13ded01 --- /dev/null +++ b/Control2/List/List/TestList.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// A function that combines the results of all tests +bool allTest(); diff --git a/Control2/Task2/Task2.sln b/Control2/Task2/Task2.sln new file mode 100644 index 0000000..d7ae6ab --- /dev/null +++ b/Control2/Task2/Task2.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31410.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Task2", "Task2\Task2.vcxproj", "{E9448556-3156-4F51-9F5C-A697FF45DB45}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E9448556-3156-4F51-9F5C-A697FF45DB45}.Debug|x64.ActiveCfg = Debug|x64 + {E9448556-3156-4F51-9F5C-A697FF45DB45}.Debug|x64.Build.0 = Debug|x64 + {E9448556-3156-4F51-9F5C-A697FF45DB45}.Debug|x86.ActiveCfg = Debug|Win32 + {E9448556-3156-4F51-9F5C-A697FF45DB45}.Debug|x86.Build.0 = Debug|Win32 + {E9448556-3156-4F51-9F5C-A697FF45DB45}.Release|x64.ActiveCfg = Release|x64 + {E9448556-3156-4F51-9F5C-A697FF45DB45}.Release|x64.Build.0 = Release|x64 + {E9448556-3156-4F51-9F5C-A697FF45DB45}.Release|x86.ActiveCfg = Release|Win32 + {E9448556-3156-4F51-9F5C-A697FF45DB45}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F2903201-C7BD-4261-A81D-06F9E6E652CC} + EndGlobalSection +EndGlobal diff --git a/Control2/Task2/Task2/ReadFile.c b/Control2/Task2/Task2/ReadFile.c new file mode 100644 index 0000000..ab6c02c --- /dev/null +++ b/Control2/Task2/Task2/ReadFile.c @@ -0,0 +1,26 @@ +#define _CRT_SECURE_NO_WARNINGS +#include "ReadFile.h" +#include +#include + +int readFile(const char* filename, List* list) +{ + FILE* file = fopen(filename, "r"); + if (file == NULL) + { + return -2; + } + Error error = NOT_ERROR; + char string[100] = { '\0' }; + while (!feof(file)) + { + const int readBytes = fscanf(file, "%s", string); + if (readBytes < 0) + { + break; + } + add(list, string, &error); + } + fclose(file); + return 0; +} \ No newline at end of file diff --git a/Control2/Task2/Task2/ReadFile.h b/Control2/Task2/Task2/ReadFile.h new file mode 100644 index 0000000..4d4cdba --- /dev/null +++ b/Control2/Task2/Task2/ReadFile.h @@ -0,0 +1,4 @@ +#pragma once +#include "../../List/List/List.h" + +int readFile(const char* filename, List* list); diff --git a/Control2/Task2/Task2/Task2.c b/Control2/Task2/Task2/Task2.c new file mode 100644 index 0000000..0181ac4 --- /dev/null +++ b/Control2/Task2/Task2/Task2.c @@ -0,0 +1,44 @@ +#include "../../List/List/List.h" +#include "ReadFile.h" +#include +#include + +List* removeRepeatElement(List* list, Error* error) +{ + List* newList = createList(error); + Position* position = first(list, error); + Position* newPosition = first(list, error); + while(!isLastElement(position)) + { + newPosition = position; + while (!isLastElement(newPosition)) + { + if (strcmp(getValue(position), getValue(newPosition)) == 0 && newPosition != position) + { + next(position); + break; + } + newPosition = next(newPosition); + } + if (strcmp(getValue(position), getValue(newPosition)) == 0 && newPosition != position) + { + next(position); + continue; + } + add(newList, getValue(position), error); + position = next(position); + print(newList); + } + free(position); + free(newPosition); + return newList; +} + +int main() +{ + Error error = 0; + List* list = createList(&error); + readFile("Text.txt", list); + List* newList = removeRepeatElement(list, &error); + print(newList); +} \ No newline at end of file diff --git a/Control2/Task2/Task2/Task2.vcxproj b/Control2/Task2/Task2/Task2.vcxproj new file mode 100644 index 0000000..034dd5d --- /dev/null +++ b/Control2/Task2/Task2/Task2.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {e9448556-3156-4f51-9f5c-a697ff45db45} + Task2 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Control2/Task2/Task2/Task2.vcxproj.filters b/Control2/Task2/Task2/Task2.vcxproj.filters new file mode 100644 index 0000000..e2a46a0 --- /dev/null +++ b/Control2/Task2/Task2/Task2.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/Control2/Task2/Task2/Text.txt b/Control2/Task2/Task2/Text.txt new file mode 100644 index 0000000..d0cf8fc --- /dev/null +++ b/Control2/Task2/Task2/Text.txt @@ -0,0 +1 @@ +asdafgwe zxcsa qweWD WQDWQ \ No newline at end of file