diff --git a/CyclicList/CyclicList.sln b/CyclicList/CyclicList.sln new file mode 100644 index 0000000..b7d7dd6 --- /dev/null +++ b/CyclicList/CyclicList.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}") = "CyclicList", "CyclicList\CyclicList.vcxproj", "{3E224E7A-155B-41F9-8988-80A502835B6E}" +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 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x64.ActiveCfg = Debug|x64 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x64.Build.0 = Debug|x64 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x86.ActiveCfg = Debug|Win32 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x86.Build.0 = Debug|Win32 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x64.ActiveCfg = Release|x64 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x64.Build.0 = Release|x64 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x86.ActiveCfg = Release|Win32 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {11CCDD59-DDF5-48B1-85A1-8BE50AF8E546} + EndGlobalSection +EndGlobal diff --git a/CyclicList/CyclicList/CircleOfMurders.c b/CyclicList/CyclicList/CircleOfMurders.c new file mode 100644 index 0000000..080a257 --- /dev/null +++ b/CyclicList/CyclicList/CircleOfMurders.c @@ -0,0 +1,55 @@ +#include "CircleOfMurders.h" +#include "CyclicList.h" + +int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error) +{ + if (numberOfWarriors == 0) + { + *error = 2; + return 0; + } + if (frequencyOfMurders == 0) + { + *error = 2; + return 0; + } + List* newList = createList(); + for (int i = 1; i <= numberOfWarriors; i++) + { + add(newList, i, error); + if (*error == 3) + { + deleteList(newList); + return 0; + } + } + Position* firstPosition = first(newList, error); + if (*error == 3) + { + deleteList(newList); + return 0; + } + int counter = 1; + while (numberOfElements(newList) != 1) + { + if (counter % frequencyOfMurders == 0) + { + removeElement(newList, firstPosition, error); + if (*error == 1) + { + freePosition(firstPosition); + deleteList(newList); + return 0; + } + } + else + { + next(firstPosition); + } + counter++; + } + const int answer = returnFirstElementValue(newList); + freePosition(firstPosition); + deleteList(newList); + return answer; +} \ No newline at end of file diff --git a/CyclicList/CyclicList/CircleOfMurders.h b/CyclicList/CyclicList/CircleOfMurders.h new file mode 100644 index 0000000..2877238 --- /dev/null +++ b/CyclicList/CyclicList/CircleOfMurders.h @@ -0,0 +1,4 @@ +#pragma once + +// Function for finding the position of the surviving warrior +int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error); \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c new file mode 100644 index 0000000..2b7658e --- /dev/null +++ b/CyclicList/CyclicList/CyclicList.c @@ -0,0 +1,167 @@ +#include "CyclicList.h" +#include +#include + +// Structure containing pointers to the "first" and "last" list item +typedef struct List +{ + int size; + struct ListElement* head; + struct ListElement* tail; +} List; + +// Structure containing pointers to the next and previous list item and a value variable for list items +typedef struct ListElement +{ + int value; + struct ListElement* next; + struct ListElement* previous; +} ListElement; + +// A structure that stores a pointer to a list item +typedef struct Position +{ + ListElement* position; +} Position; + +List* createList() +{ + return calloc(1, sizeof(List)); +} + +void deleteList(List* list) +{ + ListElement* position = list->head; + while (list->head != list->tail) + { + list->head = list->head->next; + free(position); + position = list->head; + } + list->size = 0; + free(position); + free(list); +} + +Position* first(List* list, int* error) +{ + *error = 0; + Position* positionFirst = malloc(sizeof(Position)); + if (positionFirst == NULL) + { + *error = 3; + return NULL; + } + positionFirst->position = list->head; + return positionFirst; +} + +Position* next(Position* position) +{ + position->position = position->position->next; + return position; +} + +Position* previous(Position* position) +{ + position->position = position->position->previous; + return position; +} + +void attach(ListElement* element) +{ + element->next->previous = element; + element->previous->next = element; +} + +void add(List* list, int value, int* error) +{ + ListElement* newElement = calloc(1, sizeof(ListElement)); + if (newElement == NULL) + { + *error = 3; + return; + } + newElement->value = value; + if (list->head == NULL) + { + list->size = 1; + list->head = newElement; + list->tail = newElement; + list->tail->next = list->head; + list->head->previous = list->tail; + return; + } + list->size++; + newElement->next = list->head; + newElement->previous = list->tail; + attach(newElement); + list->tail = list->tail->next; +} + +int returnFirstElementValue(List* list) +{ + return list->head->value; +} + +void removeElement(List* list, Position* position, int* error) +{ + if (list->head == NULL || position == NULL) + { + *error = 1; + return; + } + Position* newPosition = position; + if (position->position == list->head) + { + if (list->tail == list->head) + { + free(list->head); + free(position); + list->tail = NULL; + list->head = NULL; + list->size = 0; + return; + } + ListElement* element = list->head; + list->head = list->head->next; + list->head->previous = list->tail; + attach(list->head); + position->position = list->head; + free(element); + list->size--; + return; + } + if (position->position == list->tail) + { + ListElement* element = list->tail; + list->tail = list->tail->previous; + list->tail->next = list->head; + attach(list->tail); + position->position = list->head; + free(element); + list->size--; + return; + } + ListElement* element = position->position; + position->position = position->position->next; + position->position->previous = position->position->previous->previous; + free(element); + list->size--; + attach(position->position); +} + +int get(Position* position) +{ + return position->position->value; +} + +int numberOfElements(List* list) +{ + return list->size; +} + +void freePosition(Position* position) +{ + free(position); +} \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.h b/CyclicList/CyclicList/CyclicList.h new file mode 100644 index 0000000..792c771 --- /dev/null +++ b/CyclicList/CyclicList/CyclicList.h @@ -0,0 +1,41 @@ +#pragma once + +// Structure that represents cyclic list +typedef struct List List; + +// Structure for implementing a cyclic list +typedef struct Position Position; + +// Function for creating a list +List* createList(); + +// Function for deleting a list +void deleteList(List* list); + +// Function for adding an item to a list +void add(List* list, int value, int* error); + +// Function for finding a pointer to the first element +Position* first(List* list, int* error); + +// Function to remove an item from the list +void removeElement(List* list, Position* position, int* error); + +// Function for finding a pointer to the next element +Position* next(Position* position); + +// Function for finding a pointer to the previous element +Position* previous(Position* position); + +// Function that returns the value of the first element +int returnFirstElementValue(List* list); + +// Function that returns the value of an element +int get(Position* position); + +// Function for finding the number of items in the list +int numberOfElements(List* list); + +// Function for freeing up memory +void freePosition(Position* position); + diff --git a/CyclicList/CyclicList/CyclicList.vcxproj b/CyclicList/CyclicList/CyclicList.vcxproj new file mode 100644 index 0000000..284596f --- /dev/null +++ b/CyclicList/CyclicList/CyclicList.vcxproj @@ -0,0 +1,157 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {3e224e7a-155b-41f9-8988-80a502835b6e} + CyclicList + 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/CyclicList/CyclicList/CyclicList.vcxproj.filters b/CyclicList/CyclicList/CyclicList.vcxproj.filters new file mode 100644 index 0000000..639547e --- /dev/null +++ b/CyclicList/CyclicList/CyclicList.vcxproj.filters @@ -0,0 +1,48 @@ + + + + + {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/CyclicList/CyclicList/Main.c b/CyclicList/CyclicList/Main.c new file mode 100644 index 0000000..1acc1a2 --- /dev/null +++ b/CyclicList/CyclicList/Main.c @@ -0,0 +1,40 @@ +#include "CyclicList.h" +#include "TestCyclicList.h" +#include "CircleOfMurders.h" +#include "TestFindSurvivorPosition.h" +#include +#include + +int main() +{ + setlocale(LC_ALL, "rus"); + if (!allTest() || !testFindSurviviorPosition()) + { + printf(" "); + return -1; + } + printf(" \n"); + int numberOfWarriors = 0; + scanf_s("%d", &numberOfWarriors); + printf(" m- . m\n"); + int frequencyOfMurders = 0; + scanf_s("%d", &frequencyOfMurders); + int error = 0; + const int answer = findSurvivorPosition(numberOfWarriors, frequencyOfMurders, &error); + if (error == 1) + { + printf(" "); + return -1; + } + if (error == 2) + { + printf(" :)"); + return 0; + } + if (error == 3) + { + printf(" "); + return -1; + } + printf(" - %d", answer); +} \ No newline at end of file diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c new file mode 100644 index 0000000..7069df4 --- /dev/null +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -0,0 +1,63 @@ +#include "CyclicList.h" +#include "TestCyclicList.h" + +// Function to check the function that adds an item to the list +bool testAdd() +{ + int error = 0; + List* newList = createList(); + add(newList, 1, &error); + add(newList, 123, &error); + add(newList, 34, &error); + if (error == 3) + { + deleteList(newList); + return false; + } + Position* position = first(newList, &error); + const int firstNumber = get(position); + const int secondNumber = get(next(position)); + const int thirdNumber = get(next(position)); + freePosition(position); + deleteList(newList); + return firstNumber == 1 && secondNumber == 123 && thirdNumber == 34; +} + +// Function for checking a function that deletes a list item +bool testRemoveElement() +{ + int error = 0; + List* newList = createList(); + add(newList, 10, &error); + add(newList, 20, &error); + add(newList, 30, &error); + if (error == 3) + { + deleteList(newList); + return false; + } + Position* position = first(newList, &error); + if (error == 3) + { + deleteList(newList); + return false; + } + position = next(position); + removeElement(newList, position, &error); + if (error == 1) + { + freePosition(position); + deleteList(newList); + return false; + } + const int firstNumber = get(next(position)); + const int secondNumber = get(next(position)); + freePosition(position); + deleteList(newList); + return firstNumber == 10 && secondNumber == 30; +} + +bool allTest() +{ + return testAdd() && testRemoveElement(); +} \ No newline at end of file diff --git a/CyclicList/CyclicList/TestCyclicList.h b/CyclicList/CyclicList/TestCyclicList.h new file mode 100644 index 0000000..ca937d7 --- /dev/null +++ b/CyclicList/CyclicList/TestCyclicList.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function that combines the results of all tests +bool allTest(); \ No newline at end of file diff --git a/CyclicList/CyclicList/TestFindSurvivorPosition.c b/CyclicList/CyclicList/TestFindSurvivorPosition.c new file mode 100644 index 0000000..0f05c23 --- /dev/null +++ b/CyclicList/CyclicList/TestFindSurvivorPosition.c @@ -0,0 +1,9 @@ +#include "TestFindSurvivorPosition.h" +#include "CircleOfMurders.h" + +bool testFindSurviviorPosition() +{ + int error = 0; + return findSurvivorPosition(10, 5, &error) == 3 && findSurvivorPosition(7, 4, &error) == 2 + && findSurvivorPosition(20, 3, &error) == 20 && findSurvivorPosition(15, 7, &error) == 5 && findSurvivorPosition(12, 3, &error) == 10; +} \ No newline at end of file diff --git a/CyclicList/CyclicList/TestFindSurvivorPosition.h b/CyclicList/CyclicList/TestFindSurvivorPosition.h new file mode 100644 index 0000000..f608340 --- /dev/null +++ b/CyclicList/CyclicList/TestFindSurvivorPosition.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function for testing a function that finds the position of a surviving warrior +bool testFindSurviviorPosition(); \ No newline at end of file