From 678c07b70d1eb47cc59e86133fde0d142748322a Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 4 Dec 2022 23:13:04 +0300 Subject: [PATCH 1/6] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=B2=20=D0=BC=D0=B0=D1=82?= =?UTF-8?q?=D1=80=D0=B8=D1=86=D1=83,=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=BE?= =?UTF-8?q?=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=BB=D0=B8=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphCountry/graphCountry.sln | 31 ++++ graphCountry/graphCountry/graph.c | 123 ++++++++++++++ graphCountry/graphCountry/graph.h | 15 ++ .../graphCountry/graphCountry.vcxproj | 151 ++++++++++++++++++ .../graphCountry/graphCountry.vcxproj.filters | 30 ++++ graphCountry/graphCountry/main.c | 82 ++++++++++ graphCountry/graphCountry/test.txt | 11 ++ 7 files changed, 443 insertions(+) create mode 100644 graphCountry/graphCountry.sln create mode 100644 graphCountry/graphCountry/graph.c create mode 100644 graphCountry/graphCountry/graph.h create mode 100644 graphCountry/graphCountry/graphCountry.vcxproj create mode 100644 graphCountry/graphCountry/graphCountry.vcxproj.filters create mode 100644 graphCountry/graphCountry/main.c create mode 100644 graphCountry/graphCountry/test.txt diff --git a/graphCountry/graphCountry.sln b/graphCountry/graphCountry.sln new file mode 100644 index 0000000..041a321 --- /dev/null +++ b/graphCountry/graphCountry.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "graphCountry", "graphCountry\graphCountry.vcxproj", "{37B8E2ED-8212-47F8-A168-44A98D5D7568}" +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 + {37B8E2ED-8212-47F8-A168-44A98D5D7568}.Debug|x64.ActiveCfg = Debug|x64 + {37B8E2ED-8212-47F8-A168-44A98D5D7568}.Debug|x64.Build.0 = Debug|x64 + {37B8E2ED-8212-47F8-A168-44A98D5D7568}.Debug|x86.ActiveCfg = Debug|Win32 + {37B8E2ED-8212-47F8-A168-44A98D5D7568}.Debug|x86.Build.0 = Debug|Win32 + {37B8E2ED-8212-47F8-A168-44A98D5D7568}.Release|x64.ActiveCfg = Release|x64 + {37B8E2ED-8212-47F8-A168-44A98D5D7568}.Release|x64.Build.0 = Release|x64 + {37B8E2ED-8212-47F8-A168-44A98D5D7568}.Release|x86.ActiveCfg = Release|Win32 + {37B8E2ED-8212-47F8-A168-44A98D5D7568}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7DD259E5-DE48-41D7-844B-628ABC8055CD} + EndGlobalSection +EndGlobal diff --git a/graphCountry/graphCountry/graph.c b/graphCountry/graphCountry/graph.c new file mode 100644 index 0000000..654890b --- /dev/null +++ b/graphCountry/graphCountry/graph.c @@ -0,0 +1,123 @@ +#include "graph.h" +#include +#include +#include + +typedef enum Error { + ok, + error +}; + +typedef enum Item { + road, + notCapital, + capital +}; + +typedef struct ValueRoadAndInfoAboutCity { + bool isExists; + int sizeRoad; + enum Item item; + int controledTown; +} ValueRoadAndInfoAboutCity; + +typedef struct AdjacencyMatrix { + int sizeMatrix; + struct ValueRoadAndInfoAboutCity** arrayMatrix; +}; + +AdjacencyMatrix* createMatrix(int sizeMatrix, Error errorCheck) { + if (sizeMatrix == INT_MAX) { + errorCheck = error; + return NULL; + } + AdjacencyMatrix* matrix = calloc(sizeMatrix, sizeof(AdjacencyMatrix)); + if (matrix == NULL) { + errorCheck = error; + return NULL; + } + matrix->arrayMatrix = (ValueRoadAndInfoAboutCity**)malloc(sizeMatrix * sizeof(ValueRoadAndInfoAboutCity*)); + if (matrix->arrayMatrix == NULL) { + errorCheck = error; + free(matrix); + return NULL; + } + for (int i = 0; i < sizeMatrix; ++i) { + matrix->arrayMatrix[i] = (ValueRoadAndInfoAboutCity*)malloc((sizeMatrix + 1) * sizeof(ValueRoadAndInfoAboutCity)); + if (matrix->arrayMatrix[i] == NULL) { + for (int j = 0; j < i; ++j) { + free(matrix->arrayMatrix[j]); + } + free(matrix); + errorCheck = error; + return NULL; + } + for (int j = 0; j < sizeMatrix; ++j) { + matrix->arrayMatrix[i][j].isExists = false; + matrix->arrayMatrix[i][j].sizeRoad = 0; + matrix->arrayMatrix[i][j].item = road; + } + } + for (int i = 0; i < sizeMatrix; ++i) { + matrix->arrayMatrix[i][sizeMatrix].item = notCapital; + matrix->arrayMatrix[i][sizeMatrix].isExists = false; + matrix->arrayMatrix[i][sizeMatrix].sizeRoad = 0; + } + matrix->sizeMatrix = sizeMatrix; + return matrix; +} + +Error addToCapitalControlledTown(int numberCapital, AdjacencyMatrix* matrix) { + if (matrix == NULL) { + return error; + } + return ok; +} + +Error addRoad(int from, int to, int sizeRoad, AdjacencyMatrix* matrix) { + if (matrix == NULL || from >= matrix->sizeMatrix || to >= matrix->sizeMatrix) { + return error; + } + matrix->arrayMatrix[from][to].isExists = true; + matrix->arrayMatrix[from][to].sizeRoad = sizeRoad; + matrix->arrayMatrix[to][from].isExists = true; + matrix->arrayMatrix[to][from].sizeRoad = sizeRoad; + return ok; +} + +Error addCapital(int numberCapital, AdjacencyMatrix* matrix) { + if (matrix == NULL) { + return error; + } + if (numberCapital >= matrix->sizeMatrix) { + return error; + } + matrix->arrayMatrix[numberCapital][matrix->sizeMatrix].item = capital; + return ok; +} + +Error printMatrix(AdjacencyMatrix* matrix) { + if (matrix == NULL) { + return error; + } + for (int i = 0; i < matrix->sizeMatrix; ++i) { + printf("matrix[%2d][] - ", i); + for (int j = 0; j < matrix->sizeMatrix; ++j) { + printf("%4d", matrix->arrayMatrix[i][j].sizeRoad); + } + printf("\n"); + } + return ok; +} + +AdjacencyMatrix* clearMatrix(AdjacencyMatrix* matrix) { + if (matrix == NULL) { + return NULL; + } + for (int i = 0; i < matrix->sizeMatrix; ++i) { + free(matrix->arrayMatrix[i]); + } + free(matrix->arrayMatrix); + free(matrix); + return NULL; +} \ No newline at end of file diff --git a/graphCountry/graphCountry/graph.h b/graphCountry/graphCountry/graph.h new file mode 100644 index 0000000..da3c7e3 --- /dev/null +++ b/graphCountry/graphCountry/graph.h @@ -0,0 +1,15 @@ +#pragma once + +typedef enum Error Error; +typedef struct AdjacencyMatrix AdjacencyMatrix; +typedef struct Graph Graph; + +AdjacencyMatrix* createMatrix(int sizeMatrix, Error error); + +Error addRoad(int numberCityFrom, int numberCityTo, int sizeRoad, AdjacencyMatrix* matrix); + +Error printMatrix(AdjacencyMatrix* matrix); + +Error addCapital(int numberCapital, AdjacencyMatrix* matrix); + +AdjacencyMatrix* clearMatrix(AdjacencyMatrix* matrix); \ No newline at end of file diff --git a/graphCountry/graphCountry/graphCountry.vcxproj b/graphCountry/graphCountry/graphCountry.vcxproj new file mode 100644 index 0000000..4208352 --- /dev/null +++ b/graphCountry/graphCountry/graphCountry.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {37b8e2ed-8212-47f8-a168-44a98d5d7568} + graphCountry + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + 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 + false + _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/graphCountry/graphCountry/graphCountry.vcxproj.filters b/graphCountry/graphCountry/graphCountry.vcxproj.filters new file mode 100644 index 0000000..c32ea24 --- /dev/null +++ b/graphCountry/graphCountry/graphCountry.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {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/graphCountry/graphCountry/main.c b/graphCountry/graphCountry/main.c new file mode 100644 index 0000000..60026d4 --- /dev/null +++ b/graphCountry/graphCountry/main.c @@ -0,0 +1,82 @@ +#include "graph.h" +#include +#include +#include + +typedef enum Error { + ok, + error +}; + +Error workWithGraph(const char* fileName) { + Error checkError = 0; + FILE* file = fopen(fileName, "r"); + if (file == NULL) { + return error; + } + int numberOfRoads = 0; + if (fscanf(file, "%d", &numberOfRoads) != 1) { + return error; + } + AdjacencyMatrix* matrix = createMatrix(numberOfRoads, checkError); + if (checkError != ok) { + return error; + } + int numberOfPaths = 0; + if (fscanf(file, "%d", &numberOfPaths) != 1) { + matrix = clearMatrix(matrix); + return error; + } + for (int i = 0; i < numberOfPaths; ++i) { + int to = 0; + int from = 0; + int sizeRoad = 0; + if (fscanf(file, "%d %d %d", &from, &to, &sizeRoad) != 3) { + matrix = clearMatrix(matrix); + return error; + } + checkError = addRoad(from, to, sizeRoad, matrix); + if (checkError != ok) { + matrix = clearMatrix(matrix); + return error; + } + } + int sizeCapitals = 0; + if (fscanf(file, "%d", &sizeCapitals) != 1) { + matrix = clearMatrix(matrix); + return error; + } + int numberCapital = 0; + for (int i = 0; i < sizeCapitals; ++i) { + if (fscanf(file, "%d", &numberCapital) != 1) { + matrix = clearMatrix(matrix); + return error; + } + if (addCapital(numberCapital, matrix) != ok) { + matrix = clearMatrix(matrix); + return error; + } + } + fclose(file); + if (printMatrix(matrix) != ok) { + matrix = clearMatrix(matrix); + return error; + } + return ok; +} + +int main() { + printf("Input fileName, no more than 100 characters\n"); + char fileName[100] = { '\0' }; + char checkScanf = scanf("%s", &fileName); + while (checkScanf != 1) { + while (getchar() != '\n') { + } + printf("ERROR\n"); + checkScanf = scanf("%s", &fileName); + } + if (workWithGraph(fileName) != ok) { + printf("Error!\n"); + return -1; + } +} \ No newline at end of file diff --git a/graphCountry/graphCountry/test.txt b/graphCountry/graphCountry/test.txt new file mode 100644 index 0000000..dc5ff1f --- /dev/null +++ b/graphCountry/graphCountry/test.txt @@ -0,0 +1,11 @@ +5 7 +1 2 30 +0 3 33 +3 1 44 +1 0 55 +2 3 75 +4 0 23 +3 4 17 +2 +0 +1 \ No newline at end of file From ef661c942167ee8d94f751d5702cd71ac6111ea6 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 7 Dec 2022 16:58:19 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=81=D1=82=D0=B5=D0=BA=20=D0=B8=20=D0=BB=D0=B8=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphCountry/graphCountry/graph.c | 28 +++++++- graphCountry/graphCountry/list.c | 113 ++++++++++++++++++++++++++++++ graphCountry/graphCountry/list.h | 12 ++++ graphCountry/graphCountry/main.c | 1 + graphCountry/graphCountry/stack.c | 65 +++++++++++++++++ graphCountry/graphCountry/stack.h | 11 +++ 6 files changed, 227 insertions(+), 3 deletions(-) create mode 100644 graphCountry/graphCountry/list.c create mode 100644 graphCountry/graphCountry/list.h create mode 100644 graphCountry/graphCountry/stack.c create mode 100644 graphCountry/graphCountry/stack.h diff --git a/graphCountry/graphCountry/graph.c b/graphCountry/graphCountry/graph.c index 654890b..8b20082 100644 --- a/graphCountry/graphCountry/graph.c +++ b/graphCountry/graphCountry/graph.c @@ -1,4 +1,6 @@ #include "graph.h" +#include "list.h" +#include "stack.h" #include #include #include @@ -56,6 +58,7 @@ AdjacencyMatrix* createMatrix(int sizeMatrix, Error errorCheck) { matrix->arrayMatrix[i][j].isExists = false; matrix->arrayMatrix[i][j].sizeRoad = 0; matrix->arrayMatrix[i][j].item = road; + matrix->arrayMatrix[i][j].controledTown = -1; } } for (int i = 0; i < sizeMatrix; ++i) { @@ -67,10 +70,26 @@ AdjacencyMatrix* createMatrix(int sizeMatrix, Error errorCheck) { return matrix; } -Error addToCapitalControlledTown(int numberCapital, AdjacencyMatrix* matrix) { +Error addToCapitalControlledTown(int numberCapital, AdjacencyMatrix* matrix, Stack* stack, Error errorCheck) { if (matrix == NULL) { return error; } + for (int i = 0; i < matrix->sizeMatrix; ++i) { + if (matrix->arrayMatrix[numberCapital][i].isExists && i != numberCapital) { + if (matrix->arrayMatrix[numberCapital][i].controledTown == numberCapital) { + pushElements(stack, matrix->arrayMatrix[numberCapital][i].controledTown); + addToCapitalControlledTown(i, matrix, stack, errorCheck); + if (errorCheck == error) { + return error; + } + } else if (matrix->arrayMatrix[numberCapital][i].controledTown == -1) { + int item = top(stack); + if (item == -1 || ) { + + } + } + } + } return ok; } @@ -85,13 +104,16 @@ Error addRoad(int from, int to, int sizeRoad, AdjacencyMatrix* matrix) { return ok; } -Error addCapital(int numberCapital, AdjacencyMatrix* matrix) { - if (matrix == NULL) { +Error addCapital(int numberCapital, AdjacencyMatrix* matrix, List* list) { + if (matrix == NULL || list == NULL) { return error; } if (numberCapital >= matrix->sizeMatrix) { return error; } + if (addCapitalToList(list, numberCapital) == error) { + return error; + } matrix->arrayMatrix[numberCapital][matrix->sizeMatrix].item = capital; return ok; } diff --git a/graphCountry/graphCountry/list.c b/graphCountry/graphCountry/list.c new file mode 100644 index 0000000..ae0be5d --- /dev/null +++ b/graphCountry/graphCountry/list.c @@ -0,0 +1,113 @@ +#include "list.h" +#include "graph.h" +#include +#include + +typedef enum Error { + ok, + error +}; + +typedef struct Town { + int numberTown; + struct Town* nextTown; + int sizeWay; +}Town; + +typedef struct Capital { + struct Capital* nextCapital; + struct Town* firstTown; + int numberCapital; +} Capital; + +typedef struct List { + struct Capital* firstCapital; +} List; + +List* createList(Error errorCheck) { + List* list = calloc(1, sizeof(List)); + if (list == NULL) { + errorCheck = error; + return NULL; + } + return list; +} + +Error addCapitalToList(List* list, int numberCapital) { + if (list == NULL) { + return error; + } + Capital* temp = calloc(1, sizeof(Capital)); + if (temp == NULL) { + return error; + } + temp->numberCapital = numberCapital; + temp->firstTown = NULL; + temp->nextCapital = NULL; + if (list->firstCapital == NULL) { + list->firstCapital = temp; + return ok; + } + Capital* walker = list->firstCapital; + while (walker->nextCapital != NULL) { + walker = walker->nextCapital; + } + walker->nextCapital = temp; + return ok; +} + +void clearList(List* list) { + if (list == NULL) { + return; + } + + if (list->firstCapital == NULL) { + free(list); + list = NULL; + return; + } + + while (list->firstCapital != NULL) { + Capital* walker = list->firstCapital; + if (walker->nextCapital == NULL) { + free(walker->firstTown); + free(walker); + free(list); + } + while (walker->nextCapital->nextCapital != NULL) { + walker = walker->nextCapital; + } + free(walker->nextCapital->firstTown); + free(walker->nextCapital); + } +} + +Error addTownToCapital(int numberCapital, int sizeWay, int numberTown, List* list) { + if (list == NULL) { + return error; + } + Capital* walker = list->firstCapital; + while (walker != NULL) { + if (walker->numberCapital == numberCapital) { + Town* newTown = calloc(1, sizeof(Town)); + if (newTown == NULL) { + return error; + } + newTown->sizeWay = sizeWay; + newTown->numberTown = numberTown; + newTown->nextTown = NULL; + if (walker->firstTown != NULL) { + Town* temp = walker->firstTown; + while (temp->nextTown != NULL) { + temp = temp->nextTown; + } + temp->nextTown = newTown; + return ok; + } + walker->firstTown = newTown; + return ok; + } + walker = walker->nextCapital; + } + return error; +} \ No newline at end of file diff --git a/graphCountry/graphCountry/list.h b/graphCountry/graphCountry/list.h new file mode 100644 index 0000000..ee5052e --- /dev/null +++ b/graphCountry/graphCountry/list.h @@ -0,0 +1,12 @@ +#pragma once + +typedef struct List List; +typedef enum Error Error; + +List* createList(Error errorCheck); + +Error addCapitalToList(List* list, int numberCapital); + +void clearList(List* list); + +Error addTownToCapital(int numberCapital, int sizeWay, int numberTown, List* list); \ No newline at end of file diff --git a/graphCountry/graphCountry/main.c b/graphCountry/graphCountry/main.c index 60026d4..3321be6 100644 --- a/graphCountry/graphCountry/main.c +++ b/graphCountry/graphCountry/main.c @@ -62,6 +62,7 @@ Error workWithGraph(const char* fileName) { matrix = clearMatrix(matrix); return error; } + matrix = clearMatrix(matrix); return ok; } diff --git a/graphCountry/graphCountry/stack.c b/graphCountry/graphCountry/stack.c new file mode 100644 index 0000000..2cb6881 --- /dev/null +++ b/graphCountry/graphCountry/stack.c @@ -0,0 +1,65 @@ +#include "stack.h" +#include +#include +#include + +typedef enum Error { + ok, + error +}Error; + +typedef struct ElementStack { + struct ElementStack* next; + int value; +} ElementStack; + +typedef struct Stack { + struct ElementStack* head; +}Stack; + +int pushElements(Stack* stack, int value) { + ElementStack* temp = malloc(sizeof(ElementStack)); + if (temp == NULL) { + return -1; + } + temp->value = value; + temp->next = stack; + + stack = temp; + return 0; +} + +bool isEmpty(Stack* stack) { + return stack == NULL || stack->head == NULL; +} + +void clear(Stack* stack) { + if (stack == NULL) { + return; + } + while (!isEmpty(stack)) { + popElements(stack, NULL); + } +} + +int popElements(Stack* stack, Error checkError) { + if (stack == NULL || stack->head) { + checkError = error; + return -1; + } + + int value = stack->head->value; + + ElementStack* temp = stack->head->next; + free(stack->head); + stack->head = temp; + + return value; +} + +int top(Stack* stack) { + if (stack == NULL || stack->head == NULL) { + return -1; + } + return stack->head->value; +} \ No newline at end of file diff --git a/graphCountry/graphCountry/stack.h b/graphCountry/graphCountry/stack.h new file mode 100644 index 0000000..f8287ee --- /dev/null +++ b/graphCountry/graphCountry/stack.h @@ -0,0 +1,11 @@ +#pragma once +#include + +typedef struct Stack Stack; +typedef enum Error Error; + +int pushElements(Stack* stack, int value); +bool isEmpty(Stack* stack); +void clear(Stack* stack); +int popElements(Stack* stack, Error checkError); +int top(Stack* stack); \ No newline at end of file From db611e66ea47afa045fb19405f44fd4ccd35524b Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 7 Dec 2022 17:20:23 +0300 Subject: [PATCH 3/6] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D0=B1=D0=BE=D0=BB=D0=BE=D1=87=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphCountry/graphCountry/graphCountry.vcxproj | 4 ++++ .../graphCountry/graphCountry.vcxproj.filters | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/graphCountry/graphCountry/graphCountry.vcxproj b/graphCountry/graphCountry/graphCountry.vcxproj index 4208352..8f687ef 100644 --- a/graphCountry/graphCountry/graphCountry.vcxproj +++ b/graphCountry/graphCountry/graphCountry.vcxproj @@ -140,10 +140,14 @@ + + + + diff --git a/graphCountry/graphCountry/graphCountry.vcxproj.filters b/graphCountry/graphCountry/graphCountry.vcxproj.filters index c32ea24..0fff785 100644 --- a/graphCountry/graphCountry/graphCountry.vcxproj.filters +++ b/graphCountry/graphCountry/graphCountry.vcxproj.filters @@ -21,10 +21,22 @@ Исходные файлы + + Исходные файлы + + + Исходные файлы + Исходные файлы + + Исходные файлы + + + Исходные файлы + \ No newline at end of file From d303a9caadc2f854fe212bf56ffc4d7fd89d6c24 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 9 Dec 2022 15:13:54 +0300 Subject: [PATCH 4/6] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80=D1=83=20gra?= =?UTF-8?q?pha=20=D0=BD=D0=B0=20c=D0=BF=D0=B8=D1=81=D0=BE=D0=BA=20=D1=81?= =?UTF-8?q?=D0=BC=D0=B5=D0=B6=D0=BD=D0=BE=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphCountry/graphCountry/graph.c | 216 ++++++++++-------- graphCountry/graphCountry/graph.h | 13 +- .../graphCountry/graphCountry.vcxproj | 7 +- .../graphCountry/graphCountry.vcxproj.filters | 15 +- graphCountry/graphCountry/main.c | 72 ++---- graphCountry/graphCountry/test.txt | 18 +- 6 files changed, 158 insertions(+), 183 deletions(-) diff --git a/graphCountry/graphCountry/graph.c b/graphCountry/graphCountry/graph.c index 8b20082..ee5b5d1 100644 --- a/graphCountry/graphCountry/graph.c +++ b/graphCountry/graphCountry/graph.c @@ -6,140 +6,154 @@ #include typedef enum Error { + memmoryError, + anotherError, + nullGraph, + outOfGraph, + fileError, ok, - error -}; - -typedef enum Item { - road, - notCapital, - capital }; -typedef struct ValueRoadAndInfoAboutCity { - bool isExists; - int sizeRoad; - enum Item item; - int controledTown; -} ValueRoadAndInfoAboutCity; +typedef enum TypeTown { + capital, + notBusy, + Busy +}TypeTown; -typedef struct AdjacencyMatrix { - int sizeMatrix; - struct ValueRoadAndInfoAboutCity** arrayMatrix; -}; +typedef struct Node { + int vertex; + TypeTown typeTown; + int onWhichControl; + struct Node* next; + int sizeRoadWithMainVertex; +}Node; -AdjacencyMatrix* createMatrix(int sizeMatrix, Error errorCheck) { - if (sizeMatrix == INT_MAX) { - errorCheck = error; - return NULL; - } - AdjacencyMatrix* matrix = calloc(sizeMatrix, sizeof(AdjacencyMatrix)); - if (matrix == NULL) { - errorCheck = error; +typedef struct Graph { + int numNode; + struct Node** nodeList; +}Graph; + +Graph* createGraph(Error errorCheck, int sizeGraph) { + Graph* graph = calloc(1, sizeof(Graph)); + if (graph == NULL) { + errorCheck = memmoryError; return NULL; } - matrix->arrayMatrix = (ValueRoadAndInfoAboutCity**)malloc(sizeMatrix * sizeof(ValueRoadAndInfoAboutCity*)); - if (matrix->arrayMatrix == NULL) { - errorCheck = error; - free(matrix); + graph->numNode = sizeGraph; + graph->nodeList = (Node**)malloc(sizeGraph * sizeof(Node)); + if (graph->nodeList == NULL) { + errorCheck = memmoryError; + free(graph); return NULL; } - for (int i = 0; i < sizeMatrix; ++i) { - matrix->arrayMatrix[i] = (ValueRoadAndInfoAboutCity*)malloc((sizeMatrix + 1) * sizeof(ValueRoadAndInfoAboutCity)); - if (matrix->arrayMatrix[i] == NULL) { + for (int i = 0; i < sizeGraph; ++i) { + graph->nodeList[i] = calloc(1, sizeof(Node*)); + if (graph->nodeList[i] == NULL) { + errorCheck = memmoryError; for (int j = 0; j < i; ++j) { - free(matrix->arrayMatrix[j]); + free(graph->nodeList[j]); } - free(matrix); - errorCheck = error; + free(graph); return NULL; } - for (int j = 0; j < sizeMatrix; ++j) { - matrix->arrayMatrix[i][j].isExists = false; - matrix->arrayMatrix[i][j].sizeRoad = 0; - matrix->arrayMatrix[i][j].item = road; - matrix->arrayMatrix[i][j].controledTown = -1; - } + graph->nodeList[i][0].vertex = i; + graph->nodeList[i][0].next = NULL; + graph->nodeList[i][0].onWhichControl = -1; + graph->nodeList[i][0].typeTown = notBusy; } - for (int i = 0; i < sizeMatrix; ++i) { - matrix->arrayMatrix[i][sizeMatrix].item = notCapital; - matrix->arrayMatrix[i][sizeMatrix].isExists = false; - matrix->arrayMatrix[i][sizeMatrix].sizeRoad = 0; - } - matrix->sizeMatrix = sizeMatrix; - return matrix; + + return graph; } -Error addToCapitalControlledTown(int numberCapital, AdjacencyMatrix* matrix, Stack* stack, Error errorCheck) { - if (matrix == NULL) { - return error; +void helpToAddRoad(Graph* graph, int firstNode, int secondNode, int sizeRoad, Error errorCheck) { + Node* walkerInGraph = graph->nodeList[firstNode]; + while (walkerInGraph->next != NULL) { + walkerInGraph = walkerInGraph->next; } - for (int i = 0; i < matrix->sizeMatrix; ++i) { - if (matrix->arrayMatrix[numberCapital][i].isExists && i != numberCapital) { - if (matrix->arrayMatrix[numberCapital][i].controledTown == numberCapital) { - pushElements(stack, matrix->arrayMatrix[numberCapital][i].controledTown); - addToCapitalControlledTown(i, matrix, stack, errorCheck); - if (errorCheck == error) { - return error; - } - } else if (matrix->arrayMatrix[numberCapital][i].controledTown == -1) { - int item = top(stack); - if (item == -1 || ) { - - } - } + Node* temp = calloc(1, sizeof(Node)); + if (temp == NULL) { + errorCheck = memmoryError; + for (int i = 0; i < graph->numNode; ++i) { + free(graph->nodeList[i]); } + free(graph); + return; } - return ok; + walkerInGraph->sizeRoadWithMainVertex = sizeRoad; + temp->vertex = secondNode; + temp->next = NULL; + temp->onWhichControl = -1; + temp->typeTown = notBusy; + walkerInGraph->next = temp; } -Error addRoad(int from, int to, int sizeRoad, AdjacencyMatrix* matrix) { - if (matrix == NULL || from >= matrix->sizeMatrix || to >= matrix->sizeMatrix) { - return error; +void addRoad(Graph* graph, Error errorCheck, int firstNode, int secondNode, int sizeRoad) { + if (graph == NULL) { + errorCheck = nullGraph; + return; } - matrix->arrayMatrix[from][to].isExists = true; - matrix->arrayMatrix[from][to].sizeRoad = sizeRoad; - matrix->arrayMatrix[to][from].isExists = true; - matrix->arrayMatrix[to][from].sizeRoad = sizeRoad; - return ok; -} - -Error addCapital(int numberCapital, AdjacencyMatrix* matrix, List* list) { - if (matrix == NULL || list == NULL) { - return error; + if (firstNode >= graph->numNode || secondNode >= graph->numNode) { + errorCheck = outOfGraph; + for (int i = 0; i < graph->numNode; ++i) { + free(graph->nodeList[i]); + } + free(graph); + return; } - if (numberCapital >= matrix->sizeMatrix) { - return error; + helpToAddRoad(graph, firstNode, secondNode, sizeRoad, errorCheck); + if (errorCheck != ok) { + return; } - if (addCapitalToList(list, numberCapital) == error) { - return error; + + if (firstNode != secondNode) { + helpToAddRoad(graph, secondNode, firstNode, sizeRoad, errorCheck); + if (errorCheck != ok) { + return; + } } - matrix->arrayMatrix[numberCapital][matrix->sizeMatrix].item = capital; - return ok; } -Error printMatrix(AdjacencyMatrix* matrix) { - if (matrix == NULL) { - return error; +void printGraph(Graph* graph) { + if (graph == NULL) { + return; } - for (int i = 0; i < matrix->sizeMatrix; ++i) { - printf("matrix[%2d][] - ", i); - for (int j = 0; j < matrix->sizeMatrix; ++j) { - printf("%4d", matrix->arrayMatrix[i][j].sizeRoad); + for (int i = 0; i < graph->numNode; ++i) { + Node* walker = graph->nodeList[i]; + printf("%d: ", walker->vertex); + if (walker->next != NULL) { + printf("-%d- ", walker->sizeRoadWithMainVertex); + } + walker = walker->next; + while (walker != NULL) { + printf("%d ", walker->vertex); if (walker->next != NULL) { + printf("-%d- ", walker->sizeRoadWithMainVertex); + } + walker = walker->next; } printf("\n"); } - return ok; } -AdjacencyMatrix* clearMatrix(AdjacencyMatrix* matrix) { - if (matrix == NULL) { - return NULL; +void addCapital(Graph* graph, int numberCapital, Error checkError) { + if (numberCapital >= graph->numNode) { + checkError = anotherError; + return; } - for (int i = 0; i < matrix->sizeMatrix; ++i) { - free(matrix->arrayMatrix[i]); + if (graph == NULL) { + checkError = anotherError; + return; } - free(matrix->arrayMatrix); - free(matrix); - return NULL; + graph->nodeList[numberCapital]->typeTown = capital; +} + +void recursionAddTown(Graph* graph, int* min, int* numberTown) { + { + + } +} + +void addTown(Graph* graph, int numberTown) { + int min = -1; + int numberAddedTown = -1; + recursionAddTown(graph, &min, &numberTown); } \ No newline at end of file diff --git a/graphCountry/graphCountry/graph.h b/graphCountry/graphCountry/graph.h index da3c7e3..dfe1520 100644 --- a/graphCountry/graphCountry/graph.h +++ b/graphCountry/graphCountry/graph.h @@ -1,15 +1,10 @@ #pragma once -typedef enum Error Error; -typedef struct AdjacencyMatrix AdjacencyMatrix; typedef struct Graph Graph; +typedef enum Error Error; -AdjacencyMatrix* createMatrix(int sizeMatrix, Error error); - -Error addRoad(int numberCityFrom, int numberCityTo, int sizeRoad, AdjacencyMatrix* matrix); - -Error printMatrix(AdjacencyMatrix* matrix); +Graph* createGraph(Error errorCheck, int sizeGraph); -Error addCapital(int numberCapital, AdjacencyMatrix* matrix); +void addRoad(Graph* graph, Error errorCheck, int firstNode, int secondNode, int sizeRoad); -AdjacencyMatrix* clearMatrix(AdjacencyMatrix* matrix); \ No newline at end of file +void printGraph(Graph* graph); \ No newline at end of file diff --git a/graphCountry/graphCountry/graphCountry.vcxproj b/graphCountry/graphCountry/graphCountry.vcxproj index 8f687ef..f13fcd0 100644 --- a/graphCountry/graphCountry/graphCountry.vcxproj +++ b/graphCountry/graphCountry/graphCountry.vcxproj @@ -140,14 +140,13 @@ - - - - + + + diff --git a/graphCountry/graphCountry/graphCountry.vcxproj.filters b/graphCountry/graphCountry/graphCountry.vcxproj.filters index 0fff785..3269ea4 100644 --- a/graphCountry/graphCountry/graphCountry.vcxproj.filters +++ b/graphCountry/graphCountry/graphCountry.vcxproj.filters @@ -21,22 +21,15 @@ Исходные файлы - - Исходные файлы - - - Исходные файлы - Исходные файлы - - Исходные файлы - - + + + Исходные файлы - + \ No newline at end of file diff --git a/graphCountry/graphCountry/main.c b/graphCountry/graphCountry/main.c index 3321be6..34f17b4 100644 --- a/graphCountry/graphCountry/main.c +++ b/graphCountry/graphCountry/main.c @@ -4,65 +4,43 @@ #include typedef enum Error { + memmoryError, + anotherError, + nullGraph, + outOfGraph, + fileError, ok, - error }; Error workWithGraph(const char* fileName) { - Error checkError = 0; + Error errorCheck = ok; FILE* file = fopen(fileName, "r"); if (file == NULL) { - return error; + return fileError; } + int numberOfTowns = 0; + if (fscanf(file, "%d", &numberOfTowns) != 1) { + return fileError; + } + Graph* graph = createGraph(errorCheck, numberOfTowns); + int numberOfRoads = 0; if (fscanf(file, "%d", &numberOfRoads) != 1) { - return error; - } - AdjacencyMatrix* matrix = createMatrix(numberOfRoads, checkError); - if (checkError != ok) { - return error; - } - int numberOfPaths = 0; - if (fscanf(file, "%d", &numberOfPaths) != 1) { - matrix = clearMatrix(matrix); - return error; - } - for (int i = 0; i < numberOfPaths; ++i) { - int to = 0; - int from = 0; - int sizeRoad = 0; - if (fscanf(file, "%d %d %d", &from, &to, &sizeRoad) != 3) { - matrix = clearMatrix(matrix); - return error; + return fileError; + } + int firstTown = 0; + int secondTown = 0; + int sizeRoad = 0; + for (int i = 0; i < numberOfRoads; ++i) { + if (fscanf(file, "%d %d %d", &firstTown, &secondTown, &sizeRoad) != 3) { + return fileError; } - checkError = addRoad(from, to, sizeRoad, matrix); - if (checkError != ok) { - matrix = clearMatrix(matrix); - return error; + addRoad(graph, errorCheck, firstTown, secondTown, sizeRoad); + if (errorCheck != ok) { + return errorCheck; } } - int sizeCapitals = 0; - if (fscanf(file, "%d", &sizeCapitals) != 1) { - matrix = clearMatrix(matrix); - return error; - } - int numberCapital = 0; - for (int i = 0; i < sizeCapitals; ++i) { - if (fscanf(file, "%d", &numberCapital) != 1) { - matrix = clearMatrix(matrix); - return error; - } - if (addCapital(numberCapital, matrix) != ok) { - matrix = clearMatrix(matrix); - return error; - } - } - fclose(file); - if (printMatrix(matrix) != ok) { - matrix = clearMatrix(matrix); - return error; - } - matrix = clearMatrix(matrix); + printGraph(graph); return ok; } diff --git a/graphCountry/graphCountry/test.txt b/graphCountry/graphCountry/test.txt index dc5ff1f..3e18cd7 100644 --- a/graphCountry/graphCountry/test.txt +++ b/graphCountry/graphCountry/test.txt @@ -1,11 +1,7 @@ -5 7 -1 2 30 -0 3 33 -3 1 44 -1 0 55 -2 3 75 -4 0 23 -3 4 17 -2 -0 -1 \ No newline at end of file +6 6 +0 1 10 +0 2 12 +1 3 14 +1 4 15 +2 5 16 +3 5 21 \ No newline at end of file From 0ad481a4448e085f04aeee5e4320182c736b2228 Mon Sep 17 00:00:00 2001 From: Artem Date: Sat, 10 Dec 2022 15:57:28 +0300 Subject: [PATCH 5/6] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B8=20=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphCountry/graphCountry/graph.c | 166 +++++++++++++++--- graphCountry/graphCountry/graph.h | 20 ++- .../graphCountry/graphCountry.vcxproj | 2 + .../graphCountry/graphCountry.vcxproj.filters | 6 + graphCountry/graphCountry/main.c | 120 +++++++++++-- graphCountry/graphCountry/test.c | 73 ++++++++ graphCountry/graphCountry/test.h | 5 + graphCountry/graphCountry/test.txt | 5 +- 8 files changed, 349 insertions(+), 48 deletions(-) create mode 100644 graphCountry/graphCountry/test.c create mode 100644 graphCountry/graphCountry/test.h diff --git a/graphCountry/graphCountry/graph.c b/graphCountry/graphCountry/graph.c index ee5b5d1..2795514 100644 --- a/graphCountry/graphCountry/graph.c +++ b/graphCountry/graphCountry/graph.c @@ -5,19 +5,10 @@ #include #include -typedef enum Error { - memmoryError, - anotherError, - nullGraph, - outOfGraph, - fileError, - ok, -}; - typedef enum TypeTown { capital, notBusy, - Busy + busy }TypeTown; typedef struct Node { @@ -31,25 +22,26 @@ typedef struct Node { typedef struct Graph { int numNode; struct Node** nodeList; + int numberOfCapitals; }Graph; Graph* createGraph(Error errorCheck, int sizeGraph) { Graph* graph = calloc(1, sizeof(Graph)); if (graph == NULL) { - errorCheck = memmoryError; + errorCheck = memoryError; return NULL; } graph->numNode = sizeGraph; graph->nodeList = (Node**)malloc(sizeGraph * sizeof(Node)); if (graph->nodeList == NULL) { - errorCheck = memmoryError; + errorCheck = memoryError; free(graph); return NULL; } for (int i = 0; i < sizeGraph; ++i) { graph->nodeList[i] = calloc(1, sizeof(Node*)); if (graph->nodeList[i] == NULL) { - errorCheck = memmoryError; + errorCheck = memoryError; for (int j = 0; j < i; ++j) { free(graph->nodeList[j]); } @@ -60,6 +52,7 @@ Graph* createGraph(Error errorCheck, int sizeGraph) { graph->nodeList[i][0].next = NULL; graph->nodeList[i][0].onWhichControl = -1; graph->nodeList[i][0].typeTown = notBusy; + graph->nodeList[i][0].sizeRoadWithMainVertex = 0; } return graph; @@ -72,14 +65,14 @@ void helpToAddRoad(Graph* graph, int firstNode, int secondNode, int sizeRoad, Er } Node* temp = calloc(1, sizeof(Node)); if (temp == NULL) { - errorCheck = memmoryError; + errorCheck = memoryError; for (int i = 0; i < graph->numNode; ++i) { free(graph->nodeList[i]); } free(graph); return; } - walkerInGraph->sizeRoadWithMainVertex = sizeRoad; + temp->sizeRoadWithMainVertex = sizeRoad; temp->vertex = secondNode; temp->next = NULL; temp->onWhichControl = -1; @@ -92,6 +85,7 @@ void addRoad(Graph* graph, Error errorCheck, int firstNode, int secondNode, int errorCheck = nullGraph; return; } + if (firstNode >= graph->numNode || secondNode >= graph->numNode) { errorCheck = outOfGraph; for (int i = 0; i < graph->numNode; ++i) { @@ -100,14 +94,22 @@ void addRoad(Graph* graph, Error errorCheck, int firstNode, int secondNode, int free(graph); return; } + + if (firstNode == secondNode) { + graph->nodeList[firstNode]->sizeRoadWithMainVertex = sizeRoad; + return; + } + helpToAddRoad(graph, firstNode, secondNode, sizeRoad, errorCheck); if (errorCheck != ok) { + clearGraph(graph); return; } if (firstNode != secondNode) { helpToAddRoad(graph, secondNode, firstNode, sizeRoad, errorCheck); if (errorCheck != ok) { + clearGraph(graph); return; } } @@ -119,15 +121,14 @@ void printGraph(Graph* graph) { } for (int i = 0; i < graph->numNode; ++i) { Node* walker = graph->nodeList[i]; - printf("%d: ", walker->vertex); - if (walker->next != NULL) { + printf("%d : ", walker->vertex); + if (walker->sizeRoadWithMainVertex != 0) { printf("-%d- ", walker->sizeRoadWithMainVertex); } walker = walker->next; while (walker != NULL) { - printf("%d ", walker->vertex); if (walker->next != NULL) { - printf("-%d- ", walker->sizeRoadWithMainVertex); - } + printf("%d ", walker->vertex); + printf("(%d) ", walker->sizeRoadWithMainVertex); walker = walker->next; } printf("\n"); @@ -135,25 +136,134 @@ void printGraph(Graph* graph) { } void addCapital(Graph* graph, int numberCapital, Error checkError) { - if (numberCapital >= graph->numNode) { + if (graph == NULL) { checkError = anotherError; return; } - if (graph == NULL) { + + if (numberCapital >= graph->numNode) { checkError = anotherError; + clearGraph(graph); return; } + graph->nodeList[numberCapital]->typeTown = capital; + Node* walker = graph->nodeList[numberCapital]->next; + while (walker != NULL) { + Node* newWalker = graph->nodeList[walker->vertex]; + while (newWalker->vertex != numberCapital) { + newWalker = newWalker->next; + } + newWalker->typeTown = capital; + walker = walker->next; + } + ++graph->numberOfCapitals; } -void recursionAddTown(Graph* graph, int* min, int* numberTown) { - { - +void recursionAddTown(Graph* graph, int* min, int* numberTown, int index, const int numberCapital, int localMin, Error errorCheck) { + Node* walker = graph->nodeList[index]->next; + + while (walker != NULL) { + if (walker->typeTown == busy && walker->onWhichControl == numberCapital) { + localMin += walker->sizeRoadWithMainVertex; + recursionAddTown(graph, min, numberTown, walker->vertex, numberCapital, localMin, errorCheck); + } else if (walker->typeTown == notBusy) { + localMin += walker->sizeRoadWithMainVertex; + if (localMin < *min || *min == -1) { + *min = localMin; + *numberTown = walker->vertex; + } + localMin -= walker->sizeRoadWithMainVertex; + } + walker = walker->next; } + } -void addTown(Graph* graph, int numberTown) { +void addTown(Graph* graph, int numberCapital, Error errorCheck, bool* isAdded) { + if (graph == NULL) { + errorCheck = nullGraph; + return; + } int min = -1; - int numberAddedTown = -1; - recursionAddTown(graph, &min, &numberTown); + int numberAddedTown = numberCapital; + int index = numberCapital; + recursionAddTown(graph, &min, &numberAddedTown, index, numberCapital, 0, errorCheck); + if (min != -1) { + graph->nodeList[numberAddedTown]->typeTown = busy; + graph->nodeList[numberAddedTown]->onWhichControl = numberCapital; + Node* walker = graph->nodeList[numberAddedTown]->next; + while (walker != NULL) { + Node* anotherWalker = graph->nodeList[walker->vertex]; + while (anotherWalker->vertex != numberAddedTown) { + anotherWalker = anotherWalker->next; + } + anotherWalker->typeTown = busy; + anotherWalker->onWhichControl = numberCapital; + walker = walker->next; + } + *isAdded = true; + } +} + +void toControllTowns(Graph* graph, Error errorCheck) { + if (graph == NULL) { + errorCheck = nullGraph; + return; + } + int i = 0; + int indexCapital = 0; + bool isAdded = false; + while (i < graph->numNode - graph->numberOfCapitals) { + while (graph->nodeList[indexCapital]->typeTown != capital) { + ++indexCapital; + if (indexCapital == graph->numNode) { + indexCapital = 0; + } + } + isAdded = false; + addTown(graph, indexCapital, errorCheck, &isAdded); + ++indexCapital; + if (indexCapital == graph->numNode) { + indexCapital = 0; + } + if (isAdded) { + ++i; + } + } +} + +void returnArrayWithControlledTowns(Graph* graph, Error errorCheck, bool** controlledTownsArray) { + int indexSizeTowns = 0; + Node* walker = graph->nodeList[0]; + while (indexSizeTowns < graph->numNode) { + while (walker != NULL) { + if (walker->typeTown == capital) { + controlledTownsArray[walker->vertex][0] = true; + } + else { + controlledTownsArray[walker->onWhichControl][walker->vertex + 1] = true; + } + walker = walker->next; + } + walker = graph->nodeList[indexSizeTowns]; + ++indexSizeTowns; + } + return controlledTownsArray; +} + +void clearGraph(Graph* graph) { + int i = 0; + while (i < graph->numNode) { + Node* walker = graph->nodeList[i]->next; + while (walker != NULL) { + Node* temp = walker->next; + free(walker); + walker = temp; + } +// free(graph->nodeList[i]); + ++i; + } + free(graph->nodeList); + free(graph); } \ No newline at end of file diff --git a/graphCountry/graphCountry/graph.h b/graphCountry/graphCountry/graph.h index dfe1520..63e2719 100644 --- a/graphCountry/graphCountry/graph.h +++ b/graphCountry/graphCountry/graph.h @@ -1,10 +1,26 @@ #pragma once +#include typedef struct Graph Graph; -typedef enum Error Error; +typedef enum Error { + memoryError, + anotherError, + nullGraph, + outOfGraph, + fileError, + ok, +}Error; Graph* createGraph(Error errorCheck, int sizeGraph); void addRoad(Graph* graph, Error errorCheck, int firstNode, int secondNode, int sizeRoad); -void printGraph(Graph* graph); \ No newline at end of file +void printGraph(Graph* graph); + +void toControllTowns(Graph* graph, Error errorCheck); + +void addCapital(Graph* graph, int numberCapital, Error checkError); + +void returnArrayWithControlledTowns(Graph* graph, Error errorCheck, bool** controlledTownsArray); + +void clearGraph(Graph* graph); \ No newline at end of file diff --git a/graphCountry/graphCountry/graphCountry.vcxproj b/graphCountry/graphCountry/graphCountry.vcxproj index f13fcd0..b3ef69a 100644 --- a/graphCountry/graphCountry/graphCountry.vcxproj +++ b/graphCountry/graphCountry/graphCountry.vcxproj @@ -141,9 +141,11 @@ + + diff --git a/graphCountry/graphCountry/graphCountry.vcxproj.filters b/graphCountry/graphCountry/graphCountry.vcxproj.filters index 3269ea4..472db61 100644 --- a/graphCountry/graphCountry/graphCountry.vcxproj.filters +++ b/graphCountry/graphCountry/graphCountry.vcxproj.filters @@ -21,11 +21,17 @@ Исходные файлы + + Исходные файлы + Исходные файлы + + Исходные файлы + diff --git a/graphCountry/graphCountry/main.c b/graphCountry/graphCountry/main.c index 34f17b4..eabbcfa 100644 --- a/graphCountry/graphCountry/main.c +++ b/graphCountry/graphCountry/main.c @@ -1,17 +1,9 @@ #include "graph.h" +#include "test.h" #include #include #include -typedef enum Error { - memmoryError, - anotherError, - nullGraph, - outOfGraph, - fileError, - ok, -}; - Error workWithGraph(const char* fileName) { Error errorCheck = ok; FILE* file = fopen(fileName, "r"); @@ -19,43 +11,137 @@ Error workWithGraph(const char* fileName) { return fileError; } int numberOfTowns = 0; - if (fscanf(file, "%d", &numberOfTowns) != 1) { + if (fscanf_s(file, "%d", &numberOfTowns) != 1) { + fclose(file); return fileError; } + if (numberOfTowns == INT_MAX) { + return anotherError; + } Graph* graph = createGraph(errorCheck, numberOfTowns); int numberOfRoads = 0; - if (fscanf(file, "%d", &numberOfRoads) != 1) { + if (fscanf_s(file, "%d", &numberOfRoads) != 1) { + fclose(file); return fileError; } int firstTown = 0; int secondTown = 0; int sizeRoad = 0; for (int i = 0; i < numberOfRoads; ++i) { - if (fscanf(file, "%d %d %d", &firstTown, &secondTown, &sizeRoad) != 3) { + if (fscanf_s(file, "%d %d %d", &firstTown, &secondTown, &sizeRoad) != 3) { + fclose(file); return fileError; } addRoad(graph, errorCheck, firstTown, secondTown, sizeRoad); if (errorCheck != ok) { + clearGraph(graph); + fclose(file); return errorCheck; } } - printGraph(graph); + int numberOfCapitals = 0; + if (fscanf_s(file, "%d", &numberOfCapitals) != 1) { + clearGraph(graph); + fclose(file); + return fileError; + } + int numberCapital; + for (int i = 0; i < numberOfCapitals; ++i) { + if (fscanf_s(file, "%d", &numberCapital) != 1){ + clearGraph(graph); + fclose(file); + return fileError; + } + addCapital(graph, numberCapital, errorCheck); + } + fclose(file); + toControllTowns(graph, errorCheck); + if (errorCheck != ok) { + clearGraph(graph); + return errorCheck; + } + int sizeArray = 0; + bool** arrayControlledTowns = (bool**)malloc((numberOfTowns + 1) * sizeof(bool*)); + if (arrayControlledTowns == NULL) { + clearGraph(graph); + return memoryError; + } + for (int i = 0; i < numberOfTowns + 1; ++i) { + arrayControlledTowns[i] = calloc((numberOfTowns + 1),sizeof(bool*)); + if (arrayControlledTowns[i] == NULL) { + for (int j = 0; j < i; ++j) { + free(arrayControlledTowns[j]); + } + free(arrayControlledTowns); + clearGraph(graph); + return memoryError; + } + } + returnArrayWithControlledTowns(graph, errorCheck, arrayControlledTowns); + if (errorCheck != ok) { + for (int i = 0; i < numberOfTowns + 1; ++i) { + free(arrayControlledTowns[i]); + } + free(arrayControlledTowns); + clearGraph(graph); + return errorCheck; + } + + for (int i = 0; i < numberOfTowns + 1; ++i) { + if (arrayControlledTowns[i][0]) { + printf("%d: ", i); + for (int j = 1; j < numberOfTowns + 1; ++j) { + if (arrayControlledTowns[i][j]) { + printf("%d ", j - 1); + } + } + printf("\n"); + } + } + for (int i = 0; i < numberOfTowns + 1; ++i) { + free(arrayControlledTowns[i]); + } + free(arrayControlledTowns); + clearGraph(graph); return ok; } int main() { + if (test()) { + printf("Tests correct!\n"); + } else { + printf("Error!\n"); + return -1; + } printf("Input fileName, no more than 100 characters\n"); char fileName[100] = { '\0' }; - char checkScanf = scanf("%s", &fileName); + char checkScanf = scanf_s("%s", &fileName, 99); while (checkScanf != 1) { while (getchar() != '\n') { } printf("ERROR\n"); - checkScanf = scanf("%s", &fileName); + checkScanf = scanf_s("%s", &fileName, 99); } - if (workWithGraph(fileName) != ok) { - printf("Error!\n"); + Error errorCheck = workWithGraph(fileName); + switch (errorCheck) + { + case(ok): + return 0; + case(memoryError): + printf("Error with memory\n"); + return -1; + case(anotherError): + printf("Error\n"); + return -1; + case(nullGraph): + printf("Error with null graph\n"); + return -1; + case(outOfGraph): + printf("Error going beyond the graph\n"); + return -1; + case(fileError): + printf("Error with file\n"); return -1; } } \ No newline at end of file diff --git a/graphCountry/graphCountry/test.c b/graphCountry/graphCountry/test.c new file mode 100644 index 0000000..842df04 --- /dev/null +++ b/graphCountry/graphCountry/test.c @@ -0,0 +1,73 @@ +#include "graph.h" +#include "test.h" +#include + +bool test() { + Error errorCheck = ok; + Graph* graph = createGraph(errorCheck, 6); + if (errorCheck != ok) { + return false; + } + addRoad(graph, errorCheck, 0, 1, 10); + if (errorCheck != ok) { + return false; + } + addRoad(graph, errorCheck, 0, 2, 12); + if (errorCheck != ok) { + return false; + } + addRoad(graph, errorCheck, 1, 3, 10); + if (errorCheck != ok) { + return false; + } + addRoad(graph, errorCheck, 1, 4, 14); + if (errorCheck != ok) { + return false; + } + addRoad(graph, errorCheck, 2, 5, 16); + if (errorCheck != ok) { + return false; + } + addRoad(graph, errorCheck, 3, 5, 21); + if (errorCheck != ok) { + return false; + } + addCapital(graph, 1, errorCheck); + if (errorCheck != ok) { + return false; + } + addCapital(graph, 5, errorCheck); + if (errorCheck != ok) { + return false; + } + toControllTowns(graph, errorCheck); + if (errorCheck != ok) { + return false; + } + bool** arrayControlledTowns = (bool**)malloc(7 * sizeof(bool*)); + if (arrayControlledTowns == NULL) { + clearGraph(graph); + return false; + } + for (int i = 0; i < 7; ++i) { + arrayControlledTowns[i] = calloc(7, sizeof(bool*)); + if (arrayControlledTowns[i] == NULL) { + for (int j = 0; j < i; ++j) { + free(arrayControlledTowns[j]); + } + free(arrayControlledTowns); + return false; + } + } + returnArrayWithControlledTowns(graph, errorCheck, arrayControlledTowns); + if (errorCheck != ok) { + for (int i = 0; i < 7; ++i) { + free(arrayControlledTowns[i]); + } + free(arrayControlledTowns); + clearGraph(graph); + return false; + } + return arrayControlledTowns[1][0] && arrayControlledTowns[1][1] && arrayControlledTowns[1][4] + && arrayControlledTowns[1][5] && arrayControlledTowns[5][0] && arrayControlledTowns[5][3]; +} \ No newline at end of file diff --git a/graphCountry/graphCountry/test.h b/graphCountry/graphCountry/test.h new file mode 100644 index 0000000..755c9bd --- /dev/null +++ b/graphCountry/graphCountry/test.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// +bool test(void); \ No newline at end of file diff --git a/graphCountry/graphCountry/test.txt b/graphCountry/graphCountry/test.txt index 3e18cd7..2dfd5e1 100644 --- a/graphCountry/graphCountry/test.txt +++ b/graphCountry/graphCountry/test.txt @@ -4,4 +4,7 @@ 1 3 14 1 4 15 2 5 16 -3 5 21 \ No newline at end of file +3 5 21 +2 +1 +5 \ No newline at end of file From 1f056b21f27a9a8c094498238695a84c5928b8b3 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 23 Dec 2022 18:10:08 +0300 Subject: [PATCH 6/6] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphCountry/graphCountry/graphCountry.vcxproj | 1 + graphCountry/graphCountry/graphCountry.vcxproj.filters | 3 +++ graphCountry/graphCountry/newTest.txt | 10 ++++++++++ 3 files changed, 14 insertions(+) create mode 100644 graphCountry/graphCountry/newTest.txt diff --git a/graphCountry/graphCountry/graphCountry.vcxproj b/graphCountry/graphCountry/graphCountry.vcxproj index b3ef69a..67e79fc 100644 --- a/graphCountry/graphCountry/graphCountry.vcxproj +++ b/graphCountry/graphCountry/graphCountry.vcxproj @@ -148,6 +148,7 @@ + diff --git a/graphCountry/graphCountry/graphCountry.vcxproj.filters b/graphCountry/graphCountry/graphCountry.vcxproj.filters index 472db61..3c9d096 100644 --- a/graphCountry/graphCountry/graphCountry.vcxproj.filters +++ b/graphCountry/graphCountry/graphCountry.vcxproj.filters @@ -37,5 +37,8 @@ Исходные файлы + + Исходные файлы + \ No newline at end of file diff --git a/graphCountry/graphCountry/newTest.txt b/graphCountry/graphCountry/newTest.txt new file mode 100644 index 0000000..08e507e --- /dev/null +++ b/graphCountry/graphCountry/newTest.txt @@ -0,0 +1,10 @@ +6 5 +0 2 1 +1 2 1 +2 3 1 +2 4 1 +4 5 1 +2 +0 +5 +// \ No newline at end of file