From 531d1f90c741dc71a3ef52e38b58afebd5aacc6d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 12 Nov 2021 20:20:45 +0500 Subject: [PATCH 01/19] Writing functions for building a tree, counting expressions, and writing tests --- ParsingTree/ParsingTree.sln | 31 +++ ParsingTree/ParsingTree/Main.c | 9 + ParsingTree/ParsingTree/ParsingTree.c | 258 ++++++++++++++++++ ParsingTree/ParsingTree/ParsingTree.h | 37 +++ ParsingTree/ParsingTree/ParsingTree.vcxproj | 153 +++++++++++ .../ParsingTree/ParsingTree.vcxproj.filters | 36 +++ ParsingTree/ParsingTree/TestParsingTree.c | 41 +++ ParsingTree/ParsingTree/TestParsingTree.h | 8 + 8 files changed, 573 insertions(+) create mode 100644 ParsingTree/ParsingTree.sln create mode 100644 ParsingTree/ParsingTree/Main.c create mode 100644 ParsingTree/ParsingTree/ParsingTree.c create mode 100644 ParsingTree/ParsingTree/ParsingTree.h create mode 100644 ParsingTree/ParsingTree/ParsingTree.vcxproj create mode 100644 ParsingTree/ParsingTree/ParsingTree.vcxproj.filters create mode 100644 ParsingTree/ParsingTree/TestParsingTree.c create mode 100644 ParsingTree/ParsingTree/TestParsingTree.h diff --git a/ParsingTree/ParsingTree.sln b/ParsingTree/ParsingTree.sln new file mode 100644 index 0000000..3f0d84b --- /dev/null +++ b/ParsingTree/ParsingTree.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}") = "ParsingTree", "ParsingTree\ParsingTree.vcxproj", "{AEA90FA6-F26C-4B74-9508-AD999204C4FA}" +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 + {AEA90FA6-F26C-4B74-9508-AD999204C4FA}.Debug|x64.ActiveCfg = Debug|x64 + {AEA90FA6-F26C-4B74-9508-AD999204C4FA}.Debug|x64.Build.0 = Debug|x64 + {AEA90FA6-F26C-4B74-9508-AD999204C4FA}.Debug|x86.ActiveCfg = Debug|Win32 + {AEA90FA6-F26C-4B74-9508-AD999204C4FA}.Debug|x86.Build.0 = Debug|Win32 + {AEA90FA6-F26C-4B74-9508-AD999204C4FA}.Release|x64.ActiveCfg = Release|x64 + {AEA90FA6-F26C-4B74-9508-AD999204C4FA}.Release|x64.Build.0 = Release|x64 + {AEA90FA6-F26C-4B74-9508-AD999204C4FA}.Release|x86.ActiveCfg = Release|Win32 + {AEA90FA6-F26C-4B74-9508-AD999204C4FA}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {65329AED-E765-4723-93C5-B2671094F619} + EndGlobalSection +EndGlobal diff --git a/ParsingTree/ParsingTree/Main.c b/ParsingTree/ParsingTree/Main.c new file mode 100644 index 0000000..792f14a --- /dev/null +++ b/ParsingTree/ParsingTree/Main.c @@ -0,0 +1,9 @@ +#include "TestParsingTree.h" + +int main() +{ + if (!testFindAnswer() || !testBuildTree()) + { + return -1; + } +} \ No newline at end of file diff --git a/ParsingTree/ParsingTree/ParsingTree.c b/ParsingTree/ParsingTree/ParsingTree.c new file mode 100644 index 0000000..5b2b05b --- /dev/null +++ b/ParsingTree/ParsingTree/ParsingTree.c @@ -0,0 +1,258 @@ +#include "ParsingTree.h" +#include +#include +#include + +typedef struct Node +{ + struct Node* leftSon; + struct Node* rightSon; + struct Node* parent; + char value; + int number; +} Node; + +Node* createTree() +{ + return NULL; +} + +void deleteTreeRecursive(Node* root) +{ + if (root == NULL) + { + return; + } + deleteTreeRecursive(root->leftSon); + deleteTreeRecursive(root->rightSon); + free(root); +} + +void deleteTree(Node** root) +{ + deleteTreeRecursive(*root); + *root = NULL; +} + +Node* returnHead(Node* root) +{ + if (root == NULL) + { + return root; + } + while (root->parent != NULL) + { + root = root->parent; + } + return root; +} + +int returnAnswer(Node* root) +{ + return root->number; +} + +bool compare(char symbol) +{ + return symbol == '+' || symbol == '-' + || symbol == '*' || symbol == '/'; +} + +Node* buildTree(char* array) +{ + int counter = 0; + Node* tree = createTree(); + while (array[counter] != '\0') + { + if (array[counter] == '(' || array[counter] == ')' || array[counter] == ' ') + { + counter++; + continue; + } + Node* newRoot = (Node*)calloc(1, sizeof(Node)); + if (newRoot == NULL) + { + return NULL; + } + if (tree == NULL) + { + newRoot->value = array[counter]; + tree = newRoot; + if (array[counter] <= '9' && array[counter] >= '0') + { + newRoot->number = array[counter] - '0'; + if (array[counter + 1] != '\0') + { + + } + continue; + } + newRoot->number = 0; + counter++; + continue; + } + if (compare(array[counter])) + { + if (tree->leftSon == NULL && compare(tree->value)) + { + tree->leftSon = newRoot; + newRoot->parent = tree; + newRoot->value = array[counter]; + newRoot->number = 0; + tree = newRoot; + counter++; + continue; + } + else if (tree->rightSon == NULL && compare(tree->value)) + { + tree->rightSon = newRoot; + newRoot->parent = tree; + newRoot->value = array[counter]; + newRoot->number = 0; + tree = newRoot; + counter++; + continue; + } + while (tree->parent != NULL) + { + tree = tree->parent; + if (tree->leftSon == NULL && compare(tree->value)) + { + tree->leftSon = newRoot; + newRoot->parent = tree; + newRoot->value = array[counter]; + newRoot->number = 0; + counter++; + tree = newRoot; + break; + } + else if (tree->rightSon == NULL && compare(tree->value)) + { + tree->rightSon = newRoot; + newRoot->parent = tree; + newRoot->value = array[counter]; + newRoot->number = 0; + counter++; + tree = newRoot; + break; + } + } + continue; + } + if (array[counter] <= '9' && array[counter] >= '0') + { + if (compare(tree->value) && tree->leftSon == NULL) + { + tree->leftSon = newRoot; + newRoot->parent = tree; + newRoot->value = array[counter]; + newRoot->number = array[counter] - '0'; + counter++; + continue; + } + else if (tree->rightSon == NULL && compare(tree->value)) + { + tree->rightSon = newRoot; + newRoot->parent = tree; + newRoot->value = array[counter]; + newRoot->number = array[counter] - '0'; + counter++; + continue; + } + while (tree->parent != NULL) + { + tree = tree->parent; + if (tree->leftSon == NULL && compare(tree->value)) + { + tree->leftSon = newRoot; + newRoot->parent = tree; + newRoot->value = array[counter]; + newRoot->number = array[counter] - '0'; + counter++; + break; + } + else if (tree->rightSon == NULL && compare(tree->value)) + { + tree->rightSon = newRoot; + newRoot->parent = tree; + newRoot->value = array[counter]; + newRoot->number = array[counter] - '0'; + counter++; + break; + } + } + } + } + return tree; +} + +void findAnswer(Node* root) +{ + if (root == NULL) + { + return; + } + if (root->parent == NULL && root->number != 0) + { + return; + } + if (root->leftSon != NULL && root->rightSon != NULL && root->leftSon->number != 0 && root->rightSon->number != 0 && compare(root->value)) + { + if (root->value == '+') + { + root->number = root->leftSon->number + root->rightSon->number; + } + if (root->value == '-') + { + root->number = root->leftSon->number - root->rightSon->number; + } + if (root->value == '*') + { + root->number = root->leftSon->number * root->rightSon->number; + } + if (root->value == '/') + { + root->number = root->leftSon->number / root->rightSon->number; + } + findAnswer(root->parent); + } + if (root->leftSon != NULL && root->leftSon->number == 0) + { + findAnswer(root->leftSon); + } + if (root->rightSon != NULL && root->rightSon->number == 0) + { + findAnswer(root->rightSon); + } +} + +void printTree(Node* root) +{ + if (root == NULL) + { + return; + } + printTree(root->leftSon); + printTree(root->rightSon); + printf("%c", (root->value)); +} + +Node* rightSon(Node* root) +{ + return root->rightSon; +} + +Node* leftSon(Node* root) +{ + return root->leftSon; +} + +Node* parent(Node* root) +{ + return root->parent; +} + +char getValue(Node* root) +{ + return root->value; +} \ No newline at end of file diff --git a/ParsingTree/ParsingTree/ParsingTree.h b/ParsingTree/ParsingTree/ParsingTree.h new file mode 100644 index 0000000..d47f86c --- /dev/null +++ b/ParsingTree/ParsingTree/ParsingTree.h @@ -0,0 +1,37 @@ +#pragma once + +// Structure representing a tree +typedef struct Node Node; + +// Function for creating a tree +Node* createTree(); + +// Function for deleting a tree +void deleteTree(Node** root); + +// Function that returns the root of the tree +Node* returnHead(Node* root); + +// Function for building a tree +Node* buildTree(char* array); + +// Function for calculating the value of an expression +void findAnswer(Node* root); + +// Function for tree output +void printTree(Node* root); + +// Function for returning the result of counting +int returnAnswer(Node* root); + +// Function for accessing the right son of the current root +Node* rightSon(Node* root); + +// Function for accessing the left son of the current root +Node* leftSon(Node* root); + +// Function for accessing the parent of the current root +Node* parent(Node* root); + +// Function to get the value +char getValue(Node* root); diff --git a/ParsingTree/ParsingTree/ParsingTree.vcxproj b/ParsingTree/ParsingTree/ParsingTree.vcxproj new file mode 100644 index 0000000..6574a5c --- /dev/null +++ b/ParsingTree/ParsingTree/ParsingTree.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {aea90fa6-f26c-4b74-9508-ad999204c4fa} + ParsingTree + 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/ParsingTree/ParsingTree/ParsingTree.vcxproj.filters b/ParsingTree/ParsingTree/ParsingTree.vcxproj.filters new file mode 100644 index 0000000..3c2eee1 --- /dev/null +++ b/ParsingTree/ParsingTree/ParsingTree.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/ParsingTree/ParsingTree/TestParsingTree.c b/ParsingTree/ParsingTree/TestParsingTree.c new file mode 100644 index 0000000..2b24558 --- /dev/null +++ b/ParsingTree/ParsingTree/TestParsingTree.c @@ -0,0 +1,41 @@ +#include "ParsingTree.h" +#include "TestParsingTree.h" + +bool testFindAnswer() +{ + Node* firstTree = createTree(); + firstTree = buildTree("- * + 4 3 5 * 2 7"); + firstTree = returnHead(firstTree); + findAnswer(firstTree); + const int firstAnswer = returnAnswer(firstTree); + deleteTree(&firstTree); + + Node* secondTree = createTree(); + secondTree = buildTree("/ (+ * + 5 6 7 8) (+ - + 5 6 9 3)"); + secondTree = returnHead(secondTree); + findAnswer(secondTree); + const int secondAnswer = returnAnswer(secondTree); + deleteTree(&secondTree); + + Node* thirdTree = createTree(); + thirdTree = buildTree("(* (+ 1 1) 2)"); + thirdTree = returnHead(thirdTree); + findAnswer(thirdTree); + const int thirdAnswer = returnAnswer(thirdTree); + deleteTree(&thirdTree); + + return firstAnswer == 21 && secondAnswer == 17 && thirdAnswer == 4 ; +} + +bool testBuildTree() +{ + Node* firstTree = createTree(); + firstTree = buildTree("(* (+ 1 4) 2)"); + const char firstSymbol = getValue(firstTree); + const char secondSymbol = getValue(rightSon(firstTree)); + const char thirdSymbol = getValue(leftSon(firstTree)); + const char fourthSymbol = getValue(leftSon(leftSon(firstTree))); + const char fifthSymbol = getValue(rightSon(leftSon(firstTree))); + deleteTree(&firstTree); + return firstSymbol == '*' && secondSymbol == '2' && thirdSymbol == '+' && fourthSymbol == '1' && fifthSymbol == '4'; +} \ No newline at end of file diff --git a/ParsingTree/ParsingTree/TestParsingTree.h b/ParsingTree/ParsingTree/TestParsingTree.h new file mode 100644 index 0000000..2d093fa --- /dev/null +++ b/ParsingTree/ParsingTree/TestParsingTree.h @@ -0,0 +1,8 @@ +#pragma once +#include + +// Function for testing a function that finds the value of an expression +bool testFindAnswer(); + +// Function for testing a function that builds a tree +bool testBuildTree(); \ No newline at end of file From ee27bffc67fdab694c34750b44f3649eee2d451e Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 12 Nov 2021 21:02:36 +0500 Subject: [PATCH 02/19] Solving the problem --- TreeSolution/TreeSolution.sln | 31 ++++ TreeSolution/TreeSolution/Expression.txt | 1 + TreeSolution/TreeSolution/TreeSolution.c | 39 +++++ .../TreeSolution/TreeSolution.vcxproj | 151 ++++++++++++++++++ .../TreeSolution/TreeSolution.vcxproj.filters | 30 ++++ 5 files changed, 252 insertions(+) create mode 100644 TreeSolution/TreeSolution.sln create mode 100644 TreeSolution/TreeSolution/Expression.txt create mode 100644 TreeSolution/TreeSolution/TreeSolution.c create mode 100644 TreeSolution/TreeSolution/TreeSolution.vcxproj create mode 100644 TreeSolution/TreeSolution/TreeSolution.vcxproj.filters diff --git a/TreeSolution/TreeSolution.sln b/TreeSolution/TreeSolution.sln new file mode 100644 index 0000000..78f7792 --- /dev/null +++ b/TreeSolution/TreeSolution.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}") = "TreeSolution", "TreeSolution\TreeSolution.vcxproj", "{927A7B23-607A-4F48-908C-EEA30FF861D3}" +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 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x64.ActiveCfg = Debug|x64 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x64.Build.0 = Debug|x64 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x86.ActiveCfg = Debug|Win32 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x86.Build.0 = Debug|Win32 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x64.ActiveCfg = Release|x64 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x64.Build.0 = Release|x64 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x86.ActiveCfg = Release|Win32 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C939FC48-9E8F-43EC-906A-34C40C84EEFA} + EndGlobalSection +EndGlobal diff --git a/TreeSolution/TreeSolution/Expression.txt b/TreeSolution/TreeSolution/Expression.txt new file mode 100644 index 0000000..ff85f09 --- /dev/null +++ b/TreeSolution/TreeSolution/Expression.txt @@ -0,0 +1 @@ +/ (+ * + 5 6 7 8) (+ - + 5 6 9 3) \ No newline at end of file diff --git a/TreeSolution/TreeSolution/TreeSolution.c b/TreeSolution/TreeSolution/TreeSolution.c new file mode 100644 index 0000000..527875f --- /dev/null +++ b/TreeSolution/TreeSolution/TreeSolution.c @@ -0,0 +1,39 @@ +#include "../../ParsingTree/ParsingTree/ParsingTree.h" +#include + +Node* readExpression(Node* tree, const char* fileName, int* error) +{ + FILE* file = fopen(fileName, "r"); + if (file == NULL) + { + *error = 1; + return NULL; + } + char expression[100] = { '\0' }; + return buildTree(fgets(expression, 100, file)); + fclose(file); +} + +int main() +{ + Node* tree = createTree(); + int error = 0; + tree = readExpression(tree, "Expression.txt", &error); + if (error == 1) + { + deleteTree(&tree); + printf("Could not open the file"); + return -1; + } + if (tree == NULL) + { + printf("Could not read expression from file"); + return -1; + } + tree = returnHead(tree); + findAnswer(tree); + const int answer = returnAnswer(tree); + printf("Meaning of the expression : %d\n", answer); + printTree(tree); + deleteTree(&tree); +} \ No newline at end of file diff --git a/TreeSolution/TreeSolution/TreeSolution.vcxproj b/TreeSolution/TreeSolution/TreeSolution.vcxproj new file mode 100644 index 0000000..57d4943 --- /dev/null +++ b/TreeSolution/TreeSolution/TreeSolution.vcxproj @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {927a7b23-607a-4f48-908c-eea30ff861d3} + TreeSolution + 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;_CRT_SECURE_NO_WARNINGS;%(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/TreeSolution/TreeSolution/TreeSolution.vcxproj.filters b/TreeSolution/TreeSolution/TreeSolution.vcxproj.filters new file mode 100644 index 0000000..1a51f0f --- /dev/null +++ b/TreeSolution/TreeSolution/TreeSolution.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 From 0d36fbd378bcecae72f753edd62e46709204c020 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 12 Nov 2021 21:04:23 +0500 Subject: [PATCH 03/19] Changing the function for tree output --- ParsingTree/ParsingTree/ParsingTree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ParsingTree/ParsingTree/ParsingTree.c b/ParsingTree/ParsingTree/ParsingTree.c index 5b2b05b..0e51b19 100644 --- a/ParsingTree/ParsingTree/ParsingTree.c +++ b/ParsingTree/ParsingTree/ParsingTree.c @@ -234,7 +234,7 @@ void printTree(Node* root) } printTree(root->leftSon); printTree(root->rightSon); - printf("%c", (root->value)); + printf("%c ", (root->value)); } Node* rightSon(Node* root) From 918278c5876d4ef8b4d296c4db7e18ef2a00ee53 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 13 Nov 2021 20:11:09 +0500 Subject: [PATCH 04/19] a flaw has been changed (0 is a valid number) --- ParsingTree/ParsingTree/ParsingTree.c | 54 ++++++++++++++++------- ParsingTree/ParsingTree/TestParsingTree.c | 9 +++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/ParsingTree/ParsingTree/ParsingTree.c b/ParsingTree/ParsingTree/ParsingTree.c index 0e51b19..029d135 100644 --- a/ParsingTree/ParsingTree/ParsingTree.c +++ b/ParsingTree/ParsingTree/ParsingTree.c @@ -10,6 +10,7 @@ typedef struct Node struct Node* parent; char value; int number; + char help; } Node; Node* createTree() @@ -78,15 +79,7 @@ Node* buildTree(char* array) { newRoot->value = array[counter]; tree = newRoot; - if (array[counter] <= '9' && array[counter] >= '0') - { - newRoot->number = array[counter] - '0'; - if (array[counter + 1] != '\0') - { - - } - continue; - } + newRoot->help = '!'; newRoot->number = 0; counter++; continue; @@ -97,6 +90,7 @@ Node* buildTree(char* array) { tree->leftSon = newRoot; newRoot->parent = tree; + newRoot->help = '!'; newRoot->value = array[counter]; newRoot->number = 0; tree = newRoot; @@ -107,6 +101,7 @@ Node* buildTree(char* array) { tree->rightSon = newRoot; newRoot->parent = tree; + newRoot->help = '!'; newRoot->value = array[counter]; newRoot->number = 0; tree = newRoot; @@ -120,6 +115,7 @@ Node* buildTree(char* array) { tree->leftSon = newRoot; newRoot->parent = tree; + newRoot->help = '!'; newRoot->value = array[counter]; newRoot->number = 0; counter++; @@ -130,6 +126,7 @@ Node* buildTree(char* array) { tree->rightSon = newRoot; newRoot->parent = tree; + newRoot->help = '!'; newRoot->value = array[counter]; newRoot->number = 0; counter++; @@ -146,6 +143,7 @@ Node* buildTree(char* array) tree->leftSon = newRoot; newRoot->parent = tree; newRoot->value = array[counter]; + newRoot->help = '$'; newRoot->number = array[counter] - '0'; counter++; continue; @@ -155,6 +153,7 @@ Node* buildTree(char* array) tree->rightSon = newRoot; newRoot->parent = tree; newRoot->value = array[counter]; + newRoot->help = '$'; newRoot->number = array[counter] - '0'; counter++; continue; @@ -167,6 +166,7 @@ Node* buildTree(char* array) tree->leftSon = newRoot; newRoot->parent = tree; newRoot->value = array[counter]; + newRoot->help = '$'; newRoot->number = array[counter] - '0'; counter++; break; @@ -176,6 +176,7 @@ Node* buildTree(char* array) tree->rightSon = newRoot; newRoot->parent = tree; newRoot->value = array[counter]; + newRoot->help = '$'; newRoot->number = array[counter] - '0'; counter++; break; @@ -186,18 +187,33 @@ Node* buildTree(char* array) return tree; } -void findAnswer(Node* root) +void restoreField(Node* root) { if (root == NULL) { return; } - if (root->parent == NULL && root->number != 0) + restoreField(root->leftSon); + restoreField(root->rightSon); + if (compare(root->value)) + { + root->help = '!'; + } + else + { + root->help = '$'; + } +} + +void findAnswer(Node* root) +{ + if (root == NULL) { return; } - if (root->leftSon != NULL && root->rightSon != NULL && root->leftSon->number != 0 && root->rightSon->number != 0 && compare(root->value)) + if (root->leftSon != NULL && root->rightSon != NULL && root->rightSon->help == '$' && root->leftSon->help == '$' && root->help != '$') { + root->help = '$'; if (root->value == '+') { root->number = root->leftSon->number + root->rightSon->number; @@ -214,13 +230,21 @@ void findAnswer(Node* root) { root->number = root->leftSon->number / root->rightSon->number; } - findAnswer(root->parent); + if (root->parent != NULL) + { + findAnswer(root->parent); + } + else + { + restoreField(root); + return; + } } - if (root->leftSon != NULL && root->leftSon->number == 0) + else if (root->leftSon != NULL && compare(root->leftSon->value) && root->leftSon->help != '$') { findAnswer(root->leftSon); } - if (root->rightSon != NULL && root->rightSon->number == 0) + else if (root->rightSon != NULL && compare(root->rightSon->value) && root->rightSon->help != '$') { findAnswer(root->rightSon); } diff --git a/ParsingTree/ParsingTree/TestParsingTree.c b/ParsingTree/ParsingTree/TestParsingTree.c index 2b24558..aa769b0 100644 --- a/ParsingTree/ParsingTree/TestParsingTree.c +++ b/ParsingTree/ParsingTree/TestParsingTree.c @@ -24,7 +24,14 @@ bool testFindAnswer() const int thirdAnswer = returnAnswer(thirdTree); deleteTree(&thirdTree); - return firstAnswer == 21 && secondAnswer == 17 && thirdAnswer == 4 ; + Node* fourthTree = createTree(); + fourthTree = buildTree("+-556"); + fourthTree = returnHead(fourthTree); + findAnswer(fourthTree); + const int fourthAnswer = returnAnswer(fourthTree); + deleteTree(&fourthTree); + + return firstAnswer == 21 && secondAnswer == 17 && thirdAnswer == 4 && fourthAnswer == 6 ; } bool testBuildTree() From 8780d7e7b05331473bd5358b4a6cca921fbde57e Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 19 Nov 2021 20:11:52 +0300 Subject: [PATCH 05/19] changed the function for adding a node --- ParsingTree/ParsingTree/ParsingTree.c | 230 +++++++++------------- ParsingTree/ParsingTree/ParsingTree.h | 2 +- ParsingTree/ParsingTree/TestParsingTree.c | 16 +- 3 files changed, 105 insertions(+), 143 deletions(-) diff --git a/ParsingTree/ParsingTree/ParsingTree.c b/ParsingTree/ParsingTree/ParsingTree.c index 029d135..c259812 100644 --- a/ParsingTree/ParsingTree/ParsingTree.c +++ b/ParsingTree/ParsingTree/ParsingTree.c @@ -3,6 +3,12 @@ #include #include +typedef enum VisitedNode +{ + nodeVisited, + nodeNotVisited +} VisitedNode; + typedef struct Node { struct Node* leftSon; @@ -10,9 +16,15 @@ typedef struct Node struct Node* parent; char value; int number; - char help; + VisitedNode isVisitedNode; } Node; +typedef enum Direction +{ + left, + right +} Direction; + Node* createTree() { return NULL; @@ -53,138 +65,86 @@ int returnAnswer(Node* root) return root->number; } -bool compare(char symbol) +bool isOperator(char symbol) { return symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'; } -Node* buildTree(char* array) +void attach(Node* parent, Node* child, Direction direction) { - int counter = 0; - Node* tree = createTree(); - while (array[counter] != '\0') + if (direction == left) { - if (array[counter] == '(' || array[counter] == ')' || array[counter] == ' ') - { - counter++; - continue; - } - Node* newRoot = (Node*)calloc(1, sizeof(Node)); - if (newRoot == NULL) + parent->leftSon = child; + } + else + { + parent->rightSon = child; + } + if (child != NULL) + { + child->parent = parent; + } +} + +Node* createNode(char array) +{ + Node* newRoot = (Node*)calloc(1, sizeof(Node)); + if (newRoot == NULL) + { + return NULL; + } + newRoot->value = array; + newRoot->isVisitedNode = isOperator(array) ? nodeNotVisited : nodeVisited; + newRoot->number = isOperator(array) ? 0 : array - '0'; + return newRoot; +} + +Node* kek(char* array, int* counter, Node* node) +{ + if (*counter > 0 && node == NULL) + { + return NULL; + } + if (array[*counter] == '(' || array[*counter] == ')' + || array[*counter] == ' ') + { + (*counter)++; + } + if (isOperator(array[*counter])) + { + Node* temporary = createNode(array[*counter]); + if (temporary == NULL) { + node = returnHead(node); + deleteTree(&node); return NULL; } - if (tree == NULL) - { - newRoot->value = array[counter]; - tree = newRoot; - newRoot->help = '!'; - newRoot->number = 0; - counter++; - continue; - } - if (compare(array[counter])) - { - if (tree->leftSon == NULL && compare(tree->value)) - { - tree->leftSon = newRoot; - newRoot->parent = tree; - newRoot->help = '!'; - newRoot->value = array[counter]; - newRoot->number = 0; - tree = newRoot; - counter++; - continue; - } - else if (tree->rightSon == NULL && compare(tree->value)) - { - tree->rightSon = newRoot; - newRoot->parent = tree; - newRoot->help = '!'; - newRoot->value = array[counter]; - newRoot->number = 0; - tree = newRoot; - counter++; - continue; - } - while (tree->parent != NULL) - { - tree = tree->parent; - if (tree->leftSon == NULL && compare(tree->value)) - { - tree->leftSon = newRoot; - newRoot->parent = tree; - newRoot->help = '!'; - newRoot->value = array[counter]; - newRoot->number = 0; - counter++; - tree = newRoot; - break; - } - else if (tree->rightSon == NULL && compare(tree->value)) - { - tree->rightSon = newRoot; - newRoot->parent = tree; - newRoot->help = '!'; - newRoot->value = array[counter]; - newRoot->number = 0; - counter++; - tree = newRoot; - break; - } - } - continue; - } - if (array[counter] <= '9' && array[counter] >= '0') + Node* operator = temporary; + (*counter)++; + attach(operator, kek(array, counter, operator), left); + attach(operator, kek(array, counter, operator), right); + return operator; + } + else + { + Node* operand = createNode(array[*counter]); + if (operand == NULL) { - if (compare(tree->value) && tree->leftSon == NULL) - { - tree->leftSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->help = '$'; - newRoot->number = array[counter] - '0'; - counter++; - continue; - } - else if (tree->rightSon == NULL && compare(tree->value)) - { - tree->rightSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->help = '$'; - newRoot->number = array[counter] - '0'; - counter++; - continue; - } - while (tree->parent != NULL) - { - tree = tree->parent; - if (tree->leftSon == NULL && compare(tree->value)) - { - tree->leftSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->help = '$'; - newRoot->number = array[counter] - '0'; - counter++; - break; - } - else if (tree->rightSon == NULL && compare(tree->value)) - { - tree->rightSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->help = '$'; - newRoot->number = array[counter] - '0'; - counter++; - break; - } - } + node = returnHead(node); + deleteTree(&node); + return NULL; } + (*counter)++; + return operand; } - return tree; +} + +Node* buildTree(char* array) +{ + int counter = 0; + Node* node = NULL; + return kek(array, &counter, node); } void restoreField(Node* root) @@ -195,25 +155,26 @@ void restoreField(Node* root) } restoreField(root->leftSon); restoreField(root->rightSon); - if (compare(root->value)) + if (isOperator(root->value)) { - root->help = '!'; + root->isVisitedNode = nodeNotVisited; } else { - root->help = '$'; + root->isVisitedNode = nodeVisited; } } -void findAnswer(Node* root) +int findAnswer(Node* root, int* error) { if (root == NULL) { - return; + *error = 1; + return 0; } - if (root->leftSon != NULL && root->rightSon != NULL && root->rightSon->help == '$' && root->leftSon->help == '$' && root->help != '$') + if (root->leftSon != NULL && root->rightSon != NULL && root->rightSon->isVisitedNode == nodeVisited && root->leftSon->isVisitedNode == nodeVisited && root->isVisitedNode == nodeNotVisited) { - root->help = '$'; + root->isVisitedNode = nodeVisited; if (root->value == '+') { root->number = root->leftSon->number + root->rightSon->number; @@ -232,22 +193,23 @@ void findAnswer(Node* root) } if (root->parent != NULL) { - findAnswer(root->parent); + return findAnswer(root->parent, error); } else { restoreField(root); - return; + return root->number; } } - else if (root->leftSon != NULL && compare(root->leftSon->value) && root->leftSon->help != '$') + else if (root->leftSon != NULL && isOperator(root->leftSon->value) && root->leftSon->isVisitedNode == nodeNotVisited) { - findAnswer(root->leftSon); + return findAnswer(root->leftSon, error); } - else if (root->rightSon != NULL && compare(root->rightSon->value) && root->rightSon->help != '$') + else if (root->rightSon != NULL && isOperator(root->rightSon->value) && root->rightSon->isVisitedNode == nodeNotVisited) { - findAnswer(root->rightSon); + return findAnswer(root->rightSon, error); } + return findAnswer(root, error); } void printTree(Node* root) @@ -258,7 +220,7 @@ void printTree(Node* root) } printTree(root->leftSon); printTree(root->rightSon); - printf("%c ", (root->value)); + printf("%c ", root->value); } Node* rightSon(Node* root) diff --git a/ParsingTree/ParsingTree/ParsingTree.h b/ParsingTree/ParsingTree/ParsingTree.h index d47f86c..9b26963 100644 --- a/ParsingTree/ParsingTree/ParsingTree.h +++ b/ParsingTree/ParsingTree/ParsingTree.h @@ -16,7 +16,7 @@ Node* returnHead(Node* root); Node* buildTree(char* array); // Function for calculating the value of an expression -void findAnswer(Node* root); +int findAnswer(Node* root, int* error); // Function for tree output void printTree(Node* root); diff --git a/ParsingTree/ParsingTree/TestParsingTree.c b/ParsingTree/ParsingTree/TestParsingTree.c index aa769b0..a7e5db7 100644 --- a/ParsingTree/ParsingTree/TestParsingTree.c +++ b/ParsingTree/ParsingTree/TestParsingTree.c @@ -6,28 +6,28 @@ bool testFindAnswer() Node* firstTree = createTree(); firstTree = buildTree("- * + 4 3 5 * 2 7"); firstTree = returnHead(firstTree); - findAnswer(firstTree); - const int firstAnswer = returnAnswer(firstTree); + int error = 0; + const int firstAnswer = findAnswer(firstTree, &error); deleteTree(&firstTree); Node* secondTree = createTree(); - secondTree = buildTree("/ (+ * + 5 6 7 8) (+ - + 5 6 9 3)"); + secondTree = buildTree("/+*+5678+-+5693"); secondTree = returnHead(secondTree); - findAnswer(secondTree); + findAnswer(secondTree, &error); const int secondAnswer = returnAnswer(secondTree); deleteTree(&secondTree); Node* thirdTree = createTree(); - thirdTree = buildTree("(* (+ 1 1) 2)"); + thirdTree = buildTree("*+112"); thirdTree = returnHead(thirdTree); - findAnswer(thirdTree); + findAnswer(thirdTree, &error); const int thirdAnswer = returnAnswer(thirdTree); deleteTree(&thirdTree); Node* fourthTree = createTree(); fourthTree = buildTree("+-556"); fourthTree = returnHead(fourthTree); - findAnswer(fourthTree); + findAnswer(fourthTree, &error); const int fourthAnswer = returnAnswer(fourthTree); deleteTree(&fourthTree); @@ -37,7 +37,7 @@ bool testFindAnswer() bool testBuildTree() { Node* firstTree = createTree(); - firstTree = buildTree("(* (+ 1 4) 2)"); + firstTree = buildTree("*+142"); const char firstSymbol = getValue(firstTree); const char secondSymbol = getValue(rightSon(firstTree)); const char thirdSymbol = getValue(leftSon(firstTree)); From e1c6faae0c67587d6dcadc709a34361065854a46 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 19 Nov 2021 22:10:12 +0300 Subject: [PATCH 06/19] changing functions to add an element, changing tests --- ParsingTree/ParsingTree/ParsingTree.c | 81 +++++++++-------------- ParsingTree/ParsingTree/ParsingTree.h | 23 +------ ParsingTree/ParsingTree/TestParsingTree.c | 40 +++-------- ParsingTree/ParsingTree/TestParsingTree.h | 5 +- 4 files changed, 42 insertions(+), 107 deletions(-) diff --git a/ParsingTree/ParsingTree/ParsingTree.c b/ParsingTree/ParsingTree/ParsingTree.c index c259812..878c79c 100644 --- a/ParsingTree/ParsingTree/ParsingTree.c +++ b/ParsingTree/ParsingTree/ParsingTree.c @@ -14,8 +14,8 @@ typedef struct Node struct Node* leftSon; struct Node* rightSon; struct Node* parent; - char value; - int number; + char symbol; + int expressionValues; VisitedNode isVisitedNode; } Node; @@ -60,11 +60,6 @@ Node* returnHead(Node* root) return root; } -int returnAnswer(Node* root) -{ - return root->number; -} - bool isOperator(char symbol) { return symbol == '+' || symbol == '-' @@ -73,6 +68,10 @@ bool isOperator(char symbol) void attach(Node* parent, Node* child, Direction direction) { + if (parent == NULL) + { + return; + } if (direction == left) { parent->leftSon = child; @@ -87,20 +86,20 @@ void attach(Node* parent, Node* child, Direction direction) } } -Node* createNode(char array) +Node* createNode(char symbol) { Node* newRoot = (Node*)calloc(1, sizeof(Node)); if (newRoot == NULL) { return NULL; } - newRoot->value = array; - newRoot->isVisitedNode = isOperator(array) ? nodeNotVisited : nodeVisited; - newRoot->number = isOperator(array) ? 0 : array - '0'; + newRoot->symbol = symbol; + newRoot->isVisitedNode = isOperator(symbol) ? nodeNotVisited : nodeVisited; + newRoot->expressionValues = isOperator(symbol) ? 0 : symbol - '0'; return newRoot; } -Node* kek(char* array, int* counter, Node* node) +Node* addNode(const char* array, int* counter, Node* node) { if (*counter > 0 && node == NULL) { @@ -122,8 +121,8 @@ Node* kek(char* array, int* counter, Node* node) } Node* operator = temporary; (*counter)++; - attach(operator, kek(array, counter, operator), left); - attach(operator, kek(array, counter, operator), right); + attach(operator, addNode(array, counter, operator), left); + attach(operator, addNode(array, counter, operator), right); return operator; } else @@ -140,11 +139,11 @@ Node* kek(char* array, int* counter, Node* node) } } -Node* buildTree(char* array) +Node* buildTree(const char* array) { int counter = 0; Node* node = NULL; - return kek(array, &counter, node); + return addNode(array, &counter, node); } void restoreField(Node* root) @@ -155,7 +154,7 @@ void restoreField(Node* root) } restoreField(root->leftSon); restoreField(root->rightSon); - if (isOperator(root->value)) + if (isOperator(root->symbol)) { root->isVisitedNode = nodeNotVisited; } @@ -167,6 +166,10 @@ void restoreField(Node* root) int findAnswer(Node* root, int* error) { + if (*error != 0) + { + return 0; + } if (root == NULL) { *error = 1; @@ -175,21 +178,21 @@ int findAnswer(Node* root, int* error) if (root->leftSon != NULL && root->rightSon != NULL && root->rightSon->isVisitedNode == nodeVisited && root->leftSon->isVisitedNode == nodeVisited && root->isVisitedNode == nodeNotVisited) { root->isVisitedNode = nodeVisited; - if (root->value == '+') + if (root->symbol == '+') { - root->number = root->leftSon->number + root->rightSon->number; + root->expressionValues = root->leftSon->expressionValues + root->rightSon->expressionValues; } - if (root->value == '-') + if (root->symbol == '-') { - root->number = root->leftSon->number - root->rightSon->number; + root->expressionValues = root->leftSon->expressionValues - root->rightSon->expressionValues; } - if (root->value == '*') + if (root->symbol == '*') { - root->number = root->leftSon->number * root->rightSon->number; + root->expressionValues = root->leftSon->expressionValues * root->rightSon->expressionValues; } - if (root->value == '/') + if (root->symbol == '/') { - root->number = root->leftSon->number / root->rightSon->number; + root->expressionValues = root->leftSon->expressionValues / root->rightSon->expressionValues; } if (root->parent != NULL) { @@ -198,14 +201,14 @@ int findAnswer(Node* root, int* error) else { restoreField(root); - return root->number; + return root->expressionValues; } } - else if (root->leftSon != NULL && isOperator(root->leftSon->value) && root->leftSon->isVisitedNode == nodeNotVisited) + else if (root->leftSon != NULL && isOperator(root->leftSon->symbol) && root->leftSon->isVisitedNode == nodeNotVisited) { return findAnswer(root->leftSon, error); } - else if (root->rightSon != NULL && isOperator(root->rightSon->value) && root->rightSon->isVisitedNode == nodeNotVisited) + else if (root->rightSon != NULL && isOperator(root->rightSon->symbol) && root->rightSon->isVisitedNode == nodeNotVisited) { return findAnswer(root->rightSon, error); } @@ -220,25 +223,5 @@ void printTree(Node* root) } printTree(root->leftSon); printTree(root->rightSon); - printf("%c ", root->value); -} - -Node* rightSon(Node* root) -{ - return root->rightSon; -} - -Node* leftSon(Node* root) -{ - return root->leftSon; -} - -Node* parent(Node* root) -{ - return root->parent; -} - -char getValue(Node* root) -{ - return root->value; + printf("%c ", root->symbol); } \ No newline at end of file diff --git a/ParsingTree/ParsingTree/ParsingTree.h b/ParsingTree/ParsingTree/ParsingTree.h index 9b26963..4c0e0b5 100644 --- a/ParsingTree/ParsingTree/ParsingTree.h +++ b/ParsingTree/ParsingTree/ParsingTree.h @@ -3,15 +3,9 @@ // Structure representing a tree typedef struct Node Node; -// Function for creating a tree -Node* createTree(); - // Function for deleting a tree void deleteTree(Node** root); -// Function that returns the root of the tree -Node* returnHead(Node* root); - // Function for building a tree Node* buildTree(char* array); @@ -19,19 +13,4 @@ Node* buildTree(char* array); int findAnswer(Node* root, int* error); // Function for tree output -void printTree(Node* root); - -// Function for returning the result of counting -int returnAnswer(Node* root); - -// Function for accessing the right son of the current root -Node* rightSon(Node* root); - -// Function for accessing the left son of the current root -Node* leftSon(Node* root); - -// Function for accessing the parent of the current root -Node* parent(Node* root); - -// Function to get the value -char getValue(Node* root); +void printTree(Node* root); \ No newline at end of file diff --git a/ParsingTree/ParsingTree/TestParsingTree.c b/ParsingTree/ParsingTree/TestParsingTree.c index a7e5db7..ec95be9 100644 --- a/ParsingTree/ParsingTree/TestParsingTree.c +++ b/ParsingTree/ParsingTree/TestParsingTree.c @@ -3,46 +3,22 @@ bool testFindAnswer() { - Node* firstTree = createTree(); - firstTree = buildTree("- * + 4 3 5 * 2 7"); - firstTree = returnHead(firstTree); + Node* firstTree = buildTree("- * + 4 3 5 * 2 7"); int error = 0; const int firstAnswer = findAnswer(firstTree, &error); deleteTree(&firstTree); - Node* secondTree = createTree(); - secondTree = buildTree("/+*+5678+-+5693"); - secondTree = returnHead(secondTree); - findAnswer(secondTree, &error); - const int secondAnswer = returnAnswer(secondTree); + Node* secondTree = buildTree("/ + * + 5 6 7 8 + - + 5 6 9 3"); + const int secondAnswer = findAnswer(secondTree, &error); deleteTree(&secondTree); - Node* thirdTree = createTree(); - thirdTree = buildTree("*+112"); - thirdTree = returnHead(thirdTree); - findAnswer(thirdTree, &error); - const int thirdAnswer = returnAnswer(thirdTree); + Node* thirdTree = buildTree("* + 1 1 2"); + const int thirdAnswer = findAnswer(thirdTree, &error); deleteTree(&thirdTree); - Node* fourthTree = createTree(); - fourthTree = buildTree("+-556"); - fourthTree = returnHead(fourthTree); - findAnswer(fourthTree, &error); - const int fourthAnswer = returnAnswer(fourthTree); + Node* fourthTree = buildTree("+ - 5 5 6"); + const int fourthAnswer = findAnswer(fourthTree, &error); deleteTree(&fourthTree); - return firstAnswer == 21 && secondAnswer == 17 && thirdAnswer == 4 && fourthAnswer == 6 ; -} - -bool testBuildTree() -{ - Node* firstTree = createTree(); - firstTree = buildTree("*+142"); - const char firstSymbol = getValue(firstTree); - const char secondSymbol = getValue(rightSon(firstTree)); - const char thirdSymbol = getValue(leftSon(firstTree)); - const char fourthSymbol = getValue(leftSon(leftSon(firstTree))); - const char fifthSymbol = getValue(rightSon(leftSon(firstTree))); - deleteTree(&firstTree); - return firstSymbol == '*' && secondSymbol == '2' && thirdSymbol == '+' && fourthSymbol == '1' && fifthSymbol == '4'; + return firstAnswer == 21 && secondAnswer == 17 && thirdAnswer == 4 && fourthAnswer == 6 && error == 0; } \ No newline at end of file diff --git a/ParsingTree/ParsingTree/TestParsingTree.h b/ParsingTree/ParsingTree/TestParsingTree.h index 2d093fa..ac1973d 100644 --- a/ParsingTree/ParsingTree/TestParsingTree.h +++ b/ParsingTree/ParsingTree/TestParsingTree.h @@ -2,7 +2,4 @@ #include // Function for testing a function that finds the value of an expression -bool testFindAnswer(); - -// Function for testing a function that builds a tree -bool testBuildTree(); \ No newline at end of file +bool testFindAnswer(); \ No newline at end of file From 2986452efd5d6c4787ad27ef1c9e1501da5b8b01 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 19 Nov 2021 22:29:23 +0300 Subject: [PATCH 07/19] changing bugs in the function that reads a string from a file --- .../TreeSolution/TreeSolution.sln" | 31 ++++ .../TreeSolution/TreeSolution/TreeSolution.c" | 40 +++++ .../TreeSolution/TreeSolution.vcxproj" | 151 ++++++++++++++++++ .../TreeSolution.vcxproj.filters" | 30 ++++ 4 files changed, 252 insertions(+) create mode 100644 "Homework \342\204\2267/TreeSolution/TreeSolution.sln" create mode 100644 "Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" create mode 100644 "Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj" create mode 100644 "Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj.filters" diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution.sln" "b/Homework \342\204\2267/TreeSolution/TreeSolution.sln" new file mode 100644 index 0000000..78f7792 --- /dev/null +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution.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}") = "TreeSolution", "TreeSolution\TreeSolution.vcxproj", "{927A7B23-607A-4F48-908C-EEA30FF861D3}" +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 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x64.ActiveCfg = Debug|x64 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x64.Build.0 = Debug|x64 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x86.ActiveCfg = Debug|Win32 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x86.Build.0 = Debug|Win32 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x64.ActiveCfg = Release|x64 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x64.Build.0 = Release|x64 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x86.ActiveCfg = Release|Win32 + {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C939FC48-9E8F-43EC-906A-34C40C84EEFA} + EndGlobalSection +EndGlobal diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" new file mode 100644 index 0000000..b18d3e7 --- /dev/null +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" @@ -0,0 +1,40 @@ +#include "../../ParsingTree/ParsingTree/ParsingTree.h" +#include + +Node* readExpression(const char* fileName, int* error) +{ + FILE* file = fopen(fileName, "r"); + if (file == NULL) + { + *error = 1; + return NULL; + } + char expression[100] = { '\0' }; + return buildTree(fgets(expression, 100, file)); + fclose(file); +} + +int main() +{ + int error = 0; + Node* tree = readExpression("Expression.txt", &error); + if (error == 1) + { + deleteTree(&tree); + printf("Could not open the file"); + return -1; + } + if (tree == NULL) + { + printf("Could not read expression from file"); + return -1; + } + const int answer = findAnswer(tree, &error); + if (error == 1) + { + return -1; + } + printf("value of the expression : %d\n", answer); + printTree(tree); + deleteTree(&tree); +} \ No newline at end of file diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj" "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj" new file mode 100644 index 0000000..57d4943 --- /dev/null +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj" @@ -0,0 +1,151 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {927a7b23-607a-4f48-908c-eea30ff861d3} + TreeSolution + 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;_CRT_SECURE_NO_WARNINGS;%(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/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj.filters" "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj.filters" new file mode 100644 index 0000000..1a51f0f --- /dev/null +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.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 From 42479cdd12cc3be714bafcc19bbc0ec3b4668f67 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 19 Nov 2021 23:15:00 +0300 Subject: [PATCH 08/19] changing the function to read a line from a file --- .../TreeSolution/TreeSolution/TreeSolution.c" | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" index b18d3e7..6d39589 100644 --- "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" @@ -1,7 +1,8 @@ #include "../../ParsingTree/ParsingTree/ParsingTree.h" #include +#include -Node* readExpression(const char* fileName, int* error) +char* readExpression(const char* fileName, int* error) { FILE* file = fopen(fileName, "r"); if (file == NULL) @@ -9,26 +10,34 @@ Node* readExpression(const char* fileName, int* error) *error = 1; return NULL; } - char expression[100] = { '\0' }; - return buildTree(fgets(expression, 100, file)); + char* expression = calloc(100, sizeof(char)); + while (!feof(file)) + { + fgets(expression, 99, file); + } + if (expression == NULL) + { + *error = 2; + } fclose(file); + return expression; } int main() { int error = 0; - Node* tree = readExpression("Expression.txt", &error); + const char* expression = readExpression("Expression.txt", &error); if (error == 1) { - deleteTree(&tree); printf("Could not open the file"); return -1; } - if (tree == NULL) + if (error == 2) { printf("Could not read expression from file"); return -1; } + Node* tree = buildTree(expression); const int answer = findAnswer(tree, &error); if (error == 1) { From 761ccf59a8d67f3598bc3d72295e4524ba994bc0 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 19 Nov 2021 23:17:24 +0300 Subject: [PATCH 09/19] changing the file location --- .../ParsingTree/ParsingTree.sln" | 0 .../ParsingTree/ParsingTree/Main.c" | 0 .../ParsingTree/ParsingTree/ParsingTree.c" | 2 +- .../ParsingTree/ParsingTree/ParsingTree.h" | 2 +- .../ParsingTree/ParsingTree.vcxproj" | 0 .../ParsingTree/ParsingTree.vcxproj.filters" | 0 .../ParsingTree/TestParsingTree.c" | 0 .../ParsingTree/TestParsingTree.h" | 0 .../TreeSolution/TreeSolution/Expression.txt" | 0 TreeSolution/TreeSolution.sln | 31 ---- TreeSolution/TreeSolution/TreeSolution.c | 39 ----- .../TreeSolution/TreeSolution.vcxproj | 151 ------------------ .../TreeSolution/TreeSolution.vcxproj.filters | 30 ---- 13 files changed, 2 insertions(+), 253 deletions(-) rename ParsingTree/ParsingTree.sln => "Homework \342\204\2267/ParsingTree/ParsingTree.sln" (100%) rename ParsingTree/ParsingTree/Main.c => "Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" (100%) rename ParsingTree/ParsingTree/ParsingTree.c => "Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" (98%) rename ParsingTree/ParsingTree/ParsingTree.h => "Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.h" (89%) rename ParsingTree/ParsingTree/ParsingTree.vcxproj => "Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj" (100%) rename ParsingTree/ParsingTree/ParsingTree.vcxproj.filters => "Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj.filters" (100%) rename ParsingTree/ParsingTree/TestParsingTree.c => "Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" (100%) rename ParsingTree/ParsingTree/TestParsingTree.h => "Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.h" (100%) rename TreeSolution/TreeSolution/Expression.txt => "Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" (100%) delete mode 100644 TreeSolution/TreeSolution.sln delete mode 100644 TreeSolution/TreeSolution/TreeSolution.c delete mode 100644 TreeSolution/TreeSolution/TreeSolution.vcxproj delete mode 100644 TreeSolution/TreeSolution/TreeSolution.vcxproj.filters diff --git a/ParsingTree/ParsingTree.sln "b/Homework \342\204\2267/ParsingTree/ParsingTree.sln" similarity index 100% rename from ParsingTree/ParsingTree.sln rename to "Homework \342\204\2267/ParsingTree/ParsingTree.sln" diff --git a/ParsingTree/ParsingTree/Main.c "b/Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" similarity index 100% rename from ParsingTree/ParsingTree/Main.c rename to "Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" diff --git a/ParsingTree/ParsingTree/ParsingTree.c "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" similarity index 98% rename from ParsingTree/ParsingTree/ParsingTree.c rename to "Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" index 878c79c..d25bdc4 100644 --- a/ParsingTree/ParsingTree/ParsingTree.c +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" @@ -105,7 +105,7 @@ Node* addNode(const char* array, int* counter, Node* node) { return NULL; } - if (array[*counter] == '(' || array[*counter] == ')' + while (array[*counter] == '(' || array[*counter] == ')' || array[*counter] == ' ') { (*counter)++; diff --git a/ParsingTree/ParsingTree/ParsingTree.h "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.h" similarity index 89% rename from ParsingTree/ParsingTree/ParsingTree.h rename to "Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.h" index 4c0e0b5..f796295 100644 --- a/ParsingTree/ParsingTree/ParsingTree.h +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.h" @@ -7,7 +7,7 @@ typedef struct Node Node; void deleteTree(Node** root); // Function for building a tree -Node* buildTree(char* array); +Node* buildTree(const char* array); // Function for calculating the value of an expression int findAnswer(Node* root, int* error); diff --git a/ParsingTree/ParsingTree/ParsingTree.vcxproj "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj" similarity index 100% rename from ParsingTree/ParsingTree/ParsingTree.vcxproj rename to "Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj" diff --git a/ParsingTree/ParsingTree/ParsingTree.vcxproj.filters "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj.filters" similarity index 100% rename from ParsingTree/ParsingTree/ParsingTree.vcxproj.filters rename to "Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj.filters" diff --git a/ParsingTree/ParsingTree/TestParsingTree.c "b/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" similarity index 100% rename from ParsingTree/ParsingTree/TestParsingTree.c rename to "Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" diff --git a/ParsingTree/ParsingTree/TestParsingTree.h "b/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.h" similarity index 100% rename from ParsingTree/ParsingTree/TestParsingTree.h rename to "Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.h" diff --git a/TreeSolution/TreeSolution/Expression.txt "b/Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" similarity index 100% rename from TreeSolution/TreeSolution/Expression.txt rename to "Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" diff --git a/TreeSolution/TreeSolution.sln b/TreeSolution/TreeSolution.sln deleted file mode 100644 index 78f7792..0000000 --- a/TreeSolution/TreeSolution.sln +++ /dev/null @@ -1,31 +0,0 @@ - -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}") = "TreeSolution", "TreeSolution\TreeSolution.vcxproj", "{927A7B23-607A-4F48-908C-EEA30FF861D3}" -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 - {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x64.ActiveCfg = Debug|x64 - {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x64.Build.0 = Debug|x64 - {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x86.ActiveCfg = Debug|Win32 - {927A7B23-607A-4F48-908C-EEA30FF861D3}.Debug|x86.Build.0 = Debug|Win32 - {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x64.ActiveCfg = Release|x64 - {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x64.Build.0 = Release|x64 - {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x86.ActiveCfg = Release|Win32 - {927A7B23-607A-4F48-908C-EEA30FF861D3}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {C939FC48-9E8F-43EC-906A-34C40C84EEFA} - EndGlobalSection -EndGlobal diff --git a/TreeSolution/TreeSolution/TreeSolution.c b/TreeSolution/TreeSolution/TreeSolution.c deleted file mode 100644 index 527875f..0000000 --- a/TreeSolution/TreeSolution/TreeSolution.c +++ /dev/null @@ -1,39 +0,0 @@ -#include "../../ParsingTree/ParsingTree/ParsingTree.h" -#include - -Node* readExpression(Node* tree, const char* fileName, int* error) -{ - FILE* file = fopen(fileName, "r"); - if (file == NULL) - { - *error = 1; - return NULL; - } - char expression[100] = { '\0' }; - return buildTree(fgets(expression, 100, file)); - fclose(file); -} - -int main() -{ - Node* tree = createTree(); - int error = 0; - tree = readExpression(tree, "Expression.txt", &error); - if (error == 1) - { - deleteTree(&tree); - printf("Could not open the file"); - return -1; - } - if (tree == NULL) - { - printf("Could not read expression from file"); - return -1; - } - tree = returnHead(tree); - findAnswer(tree); - const int answer = returnAnswer(tree); - printf("Meaning of the expression : %d\n", answer); - printTree(tree); - deleteTree(&tree); -} \ No newline at end of file diff --git a/TreeSolution/TreeSolution/TreeSolution.vcxproj b/TreeSolution/TreeSolution/TreeSolution.vcxproj deleted file mode 100644 index 57d4943..0000000 --- a/TreeSolution/TreeSolution/TreeSolution.vcxproj +++ /dev/null @@ -1,151 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - Win32Proj - {927a7b23-607a-4f48-908c-eea30ff861d3} - TreeSolution - 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;_CRT_SECURE_NO_WARNINGS;%(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/TreeSolution/TreeSolution/TreeSolution.vcxproj.filters b/TreeSolution/TreeSolution/TreeSolution.vcxproj.filters deleted file mode 100644 index 1a51f0f..0000000 --- a/TreeSolution/TreeSolution/TreeSolution.vcxproj.filters +++ /dev/null @@ -1,30 +0,0 @@ - - - - - {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 From 0215130c03624254a30e58559288726fe79aa6b3 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 19 Nov 2021 23:34:27 +0300 Subject: [PATCH 10/19] nodeVisited --- .../ParsingTree/ParsingTree/ParsingTree.c" | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" index d25bdc4..66d14b5 100644 --- "a/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" @@ -154,14 +154,7 @@ void restoreField(Node* root) } restoreField(root->leftSon); restoreField(root->rightSon); - if (isOperator(root->symbol)) - { - root->isVisitedNode = nodeNotVisited; - } - else - { - root->isVisitedNode = nodeVisited; - } + root->isVisitedNode = isOperator(root->symbol) ? nodeNotVisited : nodeVisited; } int findAnswer(Node* root, int* error) From 9e88d89dee75111aec4182a4d2cc99de8dc32994 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 19 Nov 2021 23:51:26 +0300 Subject: [PATCH 11/19] fixed a bug with a one-character expression --- .../ParsingTree/ParsingTree/ParsingTree.c" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" index 66d14b5..ff9c3cc 100644 --- "a/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" @@ -168,6 +168,10 @@ int findAnswer(Node* root, int* error) *error = 1; return 0; } + if (root->parent == NULL && root->rightSon == NULL && root->leftSon == NULL) + { + return root->expressionValues; + } if (root->leftSon != NULL && root->rightSon != NULL && root->rightSon->isVisitedNode == nodeVisited && root->leftSon->isVisitedNode == nodeVisited && root->isVisitedNode == nodeNotVisited) { root->isVisitedNode = nodeVisited; From 014c90940f18e756d85f20c10e6f3675fed84293 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 26 Nov 2021 14:41:30 +0300 Subject: [PATCH 12/19] the necessary files have been restored --- ParsingTree/ParsingTree/Main.c | 2 +- ParsingTree/ParsingTree/ParsingTree.c | 293 ++++++++++------------ ParsingTree/ParsingTree/ParsingTree.h | 27 +- ParsingTree/ParsingTree/TestParsingTree.c | 39 +-- ParsingTree/ParsingTree/TestParsingTree.h | 5 +- 5 files changed, 146 insertions(+), 220 deletions(-) diff --git a/ParsingTree/ParsingTree/Main.c b/ParsingTree/ParsingTree/Main.c index 792f14a..1a8c6b8 100644 --- a/ParsingTree/ParsingTree/Main.c +++ b/ParsingTree/ParsingTree/Main.c @@ -2,7 +2,7 @@ int main() { - if (!testFindAnswer() || !testBuildTree()) + if (!testFindAnswer()) { return -1; } diff --git a/ParsingTree/ParsingTree/ParsingTree.c b/ParsingTree/ParsingTree/ParsingTree.c index 5b2b05b..d1fc301 100644 --- a/ParsingTree/ParsingTree/ParsingTree.c +++ b/ParsingTree/ParsingTree/ParsingTree.c @@ -3,15 +3,28 @@ #include #include +typedef enum VisitedNode +{ + nodeVisited, + nodeNotVisited +} VisitedNode; + typedef struct Node { struct Node* leftSon; struct Node* rightSon; struct Node* parent; - char value; - int number; + char symbol; + int expressionValues; + VisitedNode isVisitedNode; } Node; +typedef enum Direction +{ + left, + right +} Direction; + Node* createTree() { return NULL; @@ -47,183 +60,157 @@ Node* returnHead(Node* root) return root; } -int returnAnswer(Node* root) +bool isOperator(char symbol) { - return root->number; + return symbol == '+' || symbol == '-' + || symbol == '*' || symbol == '/'; } -bool compare(char symbol) +void attach(Node* parent, Node* child, Direction direction) { - return symbol == '+' || symbol == '-' - || symbol == '*' || symbol == '/'; + if (parent == NULL) + { + return; + } + if (direction == left) + { + parent->leftSon = child; + } + else + { + parent->rightSon = child; + } + if (child != NULL) + { + child->parent = parent; + } } -Node* buildTree(char* array) +Node* createNode(char symbol) { - int counter = 0; - Node* tree = createTree(); - while (array[counter] != '\0') + Node* newRoot = (Node*)calloc(1, sizeof(Node)); + if (newRoot == NULL) { - if (array[counter] == '(' || array[counter] == ')' || array[counter] == ' ') - { - counter++; - continue; - } - Node* newRoot = (Node*)calloc(1, sizeof(Node)); - if (newRoot == NULL) - { - return NULL; - } - if (tree == NULL) - { - newRoot->value = array[counter]; - tree = newRoot; - if (array[counter] <= '9' && array[counter] >= '0') - { - newRoot->number = array[counter] - '0'; - if (array[counter + 1] != '\0') - { + return NULL; + } + newRoot->symbol = symbol; + newRoot->isVisitedNode = isOperator(symbol) ? nodeNotVisited : nodeVisited; + newRoot->expressionValues = isOperator(symbol) ? 0 : symbol - '0'; + return newRoot; +} - } - continue; - } - newRoot->number = 0; - counter++; - continue; - } - if (compare(array[counter])) +Node* addNode(const char* array, int* counter, Node* node) +{ + if (*counter > 0 && node == NULL) + { + return NULL; + } + while (array[*counter] == '(' || array[*counter] == ')' + || array[*counter] == ' ') + { + (*counter)++; + } + if (isOperator(array[*counter])) + { + Node* temporary = createNode(array[*counter]); + if (temporary == NULL) { - if (tree->leftSon == NULL && compare(tree->value)) - { - tree->leftSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->number = 0; - tree = newRoot; - counter++; - continue; - } - else if (tree->rightSon == NULL && compare(tree->value)) - { - tree->rightSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->number = 0; - tree = newRoot; - counter++; - continue; - } - while (tree->parent != NULL) - { - tree = tree->parent; - if (tree->leftSon == NULL && compare(tree->value)) - { - tree->leftSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->number = 0; - counter++; - tree = newRoot; - break; - } - else if (tree->rightSon == NULL && compare(tree->value)) - { - tree->rightSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->number = 0; - counter++; - tree = newRoot; - break; - } - } - continue; + node = returnHead(node); + deleteTree(&node); + return NULL; } - if (array[counter] <= '9' && array[counter] >= '0') + Node* operator = temporary; + (*counter)++; + attach(operator, addNode(array, counter, operator), left); + attach(operator, addNode(array, counter, operator), right); + return operator; + } + else + { + Node* operand = createNode(array[*counter]); + if (operand == NULL) { - if (compare(tree->value) && tree->leftSon == NULL) - { - tree->leftSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->number = array[counter] - '0'; - counter++; - continue; - } - else if (tree->rightSon == NULL && compare(tree->value)) - { - tree->rightSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->number = array[counter] - '0'; - counter++; - continue; - } - while (tree->parent != NULL) - { - tree = tree->parent; - if (tree->leftSon == NULL && compare(tree->value)) - { - tree->leftSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->number = array[counter] - '0'; - counter++; - break; - } - else if (tree->rightSon == NULL && compare(tree->value)) - { - tree->rightSon = newRoot; - newRoot->parent = tree; - newRoot->value = array[counter]; - newRoot->number = array[counter] - '0'; - counter++; - break; - } - } + node = returnHead(node); + deleteTree(&node); + return NULL; } + (*counter)++; + return operand; } - return tree; } -void findAnswer(Node* root) +Node* buildTree(const char* array) +{ + int counter = 0; + Node* node = NULL; + return addNode(array, &counter, node); +} + +void restoreField(Node* root) { if (root == NULL) { return; } - if (root->parent == NULL && root->number != 0) + restoreField(root->leftSon); + restoreField(root->rightSon); + root->isVisitedNode = isOperator(root->symbol) ? nodeNotVisited : nodeVisited; +} + +int findAnswer(Node* root, int* error) +{ + if (*error != 0) { - return; + return 0; + } + if (root == NULL) + { + *error = 1; + return 0; + } + if (root->parent == NULL && root->rightSon == NULL && root->leftSon == NULL) + { + return root->expressionValues; } - if (root->leftSon != NULL && root->rightSon != NULL && root->leftSon->number != 0 && root->rightSon->number != 0 && compare(root->value)) + if (root->leftSon != NULL && root->rightSon != NULL && root->rightSon->isVisitedNode == nodeVisited + && root->leftSon->isVisitedNode == nodeVisited && root->isVisitedNode == nodeNotVisited) { - if (root->value == '+') + root->isVisitedNode = nodeVisited; + if (root->symbol == '+') { - root->number = root->leftSon->number + root->rightSon->number; + root->expressionValues = root->leftSon->expressionValues + root->rightSon->expressionValues; } - if (root->value == '-') + if (root->symbol == '-') { - root->number = root->leftSon->number - root->rightSon->number; + root->expressionValues = root->leftSon->expressionValues - root->rightSon->expressionValues; } - if (root->value == '*') + if (root->symbol == '*') { - root->number = root->leftSon->number * root->rightSon->number; + root->expressionValues = root->leftSon->expressionValues * root->rightSon->expressionValues; } - if (root->value == '/') + if (root->symbol == '/') { - root->number = root->leftSon->number / root->rightSon->number; + root->expressionValues = root->leftSon->expressionValues / root->rightSon->expressionValues; + } + if (root->parent != NULL) + { + return findAnswer(root->parent, error); + } + else + { + restoreField(root); + return root->expressionValues; } - findAnswer(root->parent); } - if (root->leftSon != NULL && root->leftSon->number == 0) + else if (root->leftSon != NULL && isOperator(root->leftSon->symbol) && root->leftSon->isVisitedNode == nodeNotVisited) { - findAnswer(root->leftSon); + return findAnswer(root->leftSon, error); } - if (root->rightSon != NULL && root->rightSon->number == 0) + else if (root->rightSon != NULL && isOperator(root->rightSon->symbol) && root->rightSon->isVisitedNode == nodeNotVisited) { - findAnswer(root->rightSon); + return findAnswer(root->rightSon, error); } + return findAnswer(root, error); } void printTree(Node* root) @@ -234,25 +221,5 @@ void printTree(Node* root) } printTree(root->leftSon); printTree(root->rightSon); - printf("%c", (root->value)); -} - -Node* rightSon(Node* root) -{ - return root->rightSon; -} - -Node* leftSon(Node* root) -{ - return root->leftSon; -} - -Node* parent(Node* root) -{ - return root->parent; -} - -char getValue(Node* root) -{ - return root->value; + printf("%c ", root->symbol); } \ No newline at end of file diff --git a/ParsingTree/ParsingTree/ParsingTree.h b/ParsingTree/ParsingTree/ParsingTree.h index d47f86c..f796295 100644 --- a/ParsingTree/ParsingTree/ParsingTree.h +++ b/ParsingTree/ParsingTree/ParsingTree.h @@ -3,35 +3,14 @@ // Structure representing a tree typedef struct Node Node; -// Function for creating a tree -Node* createTree(); - // Function for deleting a tree void deleteTree(Node** root); -// Function that returns the root of the tree -Node* returnHead(Node* root); - // Function for building a tree -Node* buildTree(char* array); +Node* buildTree(const char* array); // Function for calculating the value of an expression -void findAnswer(Node* root); +int findAnswer(Node* root, int* error); // Function for tree output -void printTree(Node* root); - -// Function for returning the result of counting -int returnAnswer(Node* root); - -// Function for accessing the right son of the current root -Node* rightSon(Node* root); - -// Function for accessing the left son of the current root -Node* leftSon(Node* root); - -// Function for accessing the parent of the current root -Node* parent(Node* root); - -// Function to get the value -char getValue(Node* root); +void printTree(Node* root); \ No newline at end of file diff --git a/ParsingTree/ParsingTree/TestParsingTree.c b/ParsingTree/ParsingTree/TestParsingTree.c index 2b24558..f666dcd 100644 --- a/ParsingTree/ParsingTree/TestParsingTree.c +++ b/ParsingTree/ParsingTree/TestParsingTree.c @@ -3,39 +3,22 @@ bool testFindAnswer() { - Node* firstTree = createTree(); - firstTree = buildTree("- * + 4 3 5 * 2 7"); - firstTree = returnHead(firstTree); - findAnswer(firstTree); - const int firstAnswer = returnAnswer(firstTree); + Node* firstTree = buildTree("1"); + int error = 0; + const int firstAnswer = findAnswer(firstTree, &error); deleteTree(&firstTree); - Node* secondTree = createTree(); - secondTree = buildTree("/ (+ * + 5 6 7 8) (+ - + 5 6 9 3)"); - secondTree = returnHead(secondTree); - findAnswer(secondTree); - const int secondAnswer = returnAnswer(secondTree); + Node* secondTree = buildTree("/ + * + 5 6 7 8 + - + 5 6 9 3"); + const int secondAnswer = findAnswer(secondTree, &error); deleteTree(&secondTree); - Node* thirdTree = createTree(); - thirdTree = buildTree("(* (+ 1 1) 2)"); - thirdTree = returnHead(thirdTree); - findAnswer(thirdTree); - const int thirdAnswer = returnAnswer(thirdTree); + Node* thirdTree = buildTree("* + 1 1 2"); + const int thirdAnswer = findAnswer(thirdTree, &error); deleteTree(&thirdTree); - return firstAnswer == 21 && secondAnswer == 17 && thirdAnswer == 4 ; -} + Node* fourthTree = buildTree("+ - 5 5 6"); + const int fourthAnswer = findAnswer(fourthTree, &error); + deleteTree(&fourthTree); -bool testBuildTree() -{ - Node* firstTree = createTree(); - firstTree = buildTree("(* (+ 1 4) 2)"); - const char firstSymbol = getValue(firstTree); - const char secondSymbol = getValue(rightSon(firstTree)); - const char thirdSymbol = getValue(leftSon(firstTree)); - const char fourthSymbol = getValue(leftSon(leftSon(firstTree))); - const char fifthSymbol = getValue(rightSon(leftSon(firstTree))); - deleteTree(&firstTree); - return firstSymbol == '*' && secondSymbol == '2' && thirdSymbol == '+' && fourthSymbol == '1' && fifthSymbol == '4'; + return firstAnswer == 1 && secondAnswer == 17 && thirdAnswer == 4 && fourthAnswer == 6 && error == 0; } \ No newline at end of file diff --git a/ParsingTree/ParsingTree/TestParsingTree.h b/ParsingTree/ParsingTree/TestParsingTree.h index 2d093fa..ac1973d 100644 --- a/ParsingTree/ParsingTree/TestParsingTree.h +++ b/ParsingTree/ParsingTree/TestParsingTree.h @@ -2,7 +2,4 @@ #include // Function for testing a function that finds the value of an expression -bool testFindAnswer(); - -// Function for testing a function that builds a tree -bool testBuildTree(); \ No newline at end of file +bool testFindAnswer(); \ No newline at end of file From 1b46e0f5a475ae981ececf86a6afbeec8248fad0 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 26 Nov 2021 15:07:51 +0300 Subject: [PATCH 13/19] the necessary files have been restored --- .../TreeSolution/TreeSolution/TreeSolution.c" | 11 +++++++++++ .../TreeSolution/TreeSolution/TreeSolution.vcxproj" | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" index 6d39589..977d33b 100644 --- "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" @@ -11,6 +11,12 @@ char* readExpression(const char* fileName, int* error) return NULL; } char* expression = calloc(100, sizeof(char)); + if (expression == NULL) + { + *error = 3; + fclose(file); + return NULL; + } while (!feof(file)) { fgets(expression, 99, file); @@ -37,6 +43,11 @@ int main() printf("Could not read expression from file"); return -1; } + if (error == 3) + { + printf("Memory not allocated"); + return -1; + } Node* tree = buildTree(expression); const int answer = findAnswer(tree, &error); if (error == 1) diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj" "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj" index 57d4943..399c7d0 100644 --- "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj" +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.vcxproj" @@ -114,7 +114,7 @@ Level3 true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true From fe132ffc805990f0b5c63b655776db897c4cdc92 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 26 Nov 2021 15:26:47 +0300 Subject: [PATCH 14/19] the necessary files have been restored --- "Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" "b/Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" index 792f14a..1a8c6b8 100644 --- "a/Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" @@ -2,7 +2,7 @@ int main() { - if (!testFindAnswer() || !testBuildTree()) + if (!testFindAnswer()) { return -1; } From 51a7b22e79010e6ca1b6d0407ed5e033fa167901 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 3 Dec 2021 12:09:30 +0300 Subject: [PATCH 15/19] changing the function to traverse the tree and changed the format of the expression entry --- ParsingTree/ParsingTree/ParsingTree.c | 118 ++++++++++------------ ParsingTree/ParsingTree/TestParsingTree.c | 7 +- 2 files changed, 56 insertions(+), 69 deletions(-) diff --git a/ParsingTree/ParsingTree/ParsingTree.c b/ParsingTree/ParsingTree/ParsingTree.c index d1fc301..878920f 100644 --- a/ParsingTree/ParsingTree/ParsingTree.c +++ b/ParsingTree/ParsingTree/ParsingTree.c @@ -3,12 +3,6 @@ #include #include -typedef enum VisitedNode -{ - nodeVisited, - nodeNotVisited -} VisitedNode; - typedef struct Node { struct Node* leftSon; @@ -16,7 +10,6 @@ typedef struct Node struct Node* parent; char symbol; int expressionValues; - VisitedNode isVisitedNode; } Node; typedef enum Direction @@ -47,17 +40,17 @@ void deleteTree(Node** root) *root = NULL; } -Node* returnHead(Node* root) +Node* returnRoot(Node* node) { - if (root == NULL) + if (node == NULL) { - return root; + return node; } - while (root->parent != NULL) + while (node->parent != NULL) { - root = root->parent; + node = node->parent; } - return root; + return node; } bool isOperator(char symbol) @@ -88,15 +81,14 @@ void attach(Node* parent, Node* child, Direction direction) Node* createNode(char symbol) { - Node* newRoot = (Node*)calloc(1, sizeof(Node)); - if (newRoot == NULL) + Node* newNode = (Node*)calloc(1, sizeof(Node)); + if (newNode == NULL) { return NULL; } - newRoot->symbol = symbol; - newRoot->isVisitedNode = isOperator(symbol) ? nodeNotVisited : nodeVisited; - newRoot->expressionValues = isOperator(symbol) ? 0 : symbol - '0'; - return newRoot; + newNode->symbol = symbol; + newNode->expressionValues = isOperator(symbol) ? 0 : symbol - '0'; + return newNode; } Node* addNode(const char* array, int* counter, Node* node) @@ -115,7 +107,7 @@ Node* addNode(const char* array, int* counter, Node* node) Node* temporary = createNode(array[*counter]); if (temporary == NULL) { - node = returnHead(node); + node = returnRoot(node); deleteTree(&node); return NULL; } @@ -125,18 +117,15 @@ Node* addNode(const char* array, int* counter, Node* node) attach(operator, addNode(array, counter, operator), right); return operator; } - else + Node* operand = createNode(array[*counter]); + if (operand == NULL) { - Node* operand = createNode(array[*counter]); - if (operand == NULL) - { - node = returnHead(node); - deleteTree(&node); - return NULL; - } - (*counter)++; - return operand; + node = returnRoot(node); + deleteTree(&node); + return NULL; } + (*counter)++; + return operand; } Node* buildTree(const char* array) @@ -146,17 +135,6 @@ Node* buildTree(const char* array) return addNode(array, &counter, node); } -void restoreField(Node* root) -{ - if (root == NULL) - { - return; - } - restoreField(root->leftSon); - restoreField(root->rightSon); - root->isVisitedNode = isOperator(root->symbol) ? nodeNotVisited : nodeVisited; -} - int findAnswer(Node* root, int* error) { if (*error != 0) @@ -168,14 +146,20 @@ int findAnswer(Node* root, int* error) *error = 1; return 0; } - if (root->parent == NULL && root->rightSon == NULL && root->leftSon == NULL) + if (root->rightSon == NULL && root->leftSon == NULL) { return root->expressionValues; + } + if (root->leftSon != NULL) + { + root->leftSon->expressionValues = findAnswer(root->leftSon, error); + } + if (root->rightSon != NULL) + { + root->rightSon->expressionValues = findAnswer(root->rightSon, error); } - if (root->leftSon != NULL && root->rightSon != NULL && root->rightSon->isVisitedNode == nodeVisited - && root->leftSon->isVisitedNode == nodeVisited && root->isVisitedNode == nodeNotVisited) + if (root->rightSon != NULL && root->leftSon != NULL) { - root->isVisitedNode = nodeVisited; if (root->symbol == '+') { root->expressionValues = root->leftSon->expressionValues + root->rightSon->expressionValues; @@ -192,34 +176,36 @@ int findAnswer(Node* root, int* error) { root->expressionValues = root->leftSon->expressionValues / root->rightSon->expressionValues; } - if (root->parent != NULL) - { - return findAnswer(root->parent, error); - } - else - { - restoreField(root); - return root->expressionValues; - } } - else if (root->leftSon != NULL && isOperator(root->leftSon->symbol) && root->leftSon->isVisitedNode == nodeNotVisited) + return root->expressionValues; +} + +void printTreeRecursive(Node* root) +{ + if (root == NULL) { - return findAnswer(root->leftSon, error); + return; } - else if (root->rightSon != NULL && isOperator(root->rightSon->symbol) && root->rightSon->isVisitedNode == nodeNotVisited) + if (isOperator(root->symbol)) { - return findAnswer(root->rightSon, error); + printf("%c ", '('); + printf("%c ", root->symbol); } - return findAnswer(root, error); + else if (root->parent != NULL && root->parent->rightSon == root) + { + printf("%c ", root->symbol); + printf("%c ", ')'); + } + else + { + printf("%c ", root->symbol); + } + printTreeRecursive(root->leftSon); + printTreeRecursive(root->rightSon); } void printTree(Node* root) { - if (root == NULL) - { - return; - } - printTree(root->leftSon); - printTree(root->rightSon); - printf("%c ", root->symbol); + printTreeRecursive(root); + printf("%c", ')'); } \ No newline at end of file diff --git a/ParsingTree/ParsingTree/TestParsingTree.c b/ParsingTree/ParsingTree/TestParsingTree.c index f666dcd..e5d8edb 100644 --- a/ParsingTree/ParsingTree/TestParsingTree.c +++ b/ParsingTree/ParsingTree/TestParsingTree.c @@ -8,15 +8,16 @@ bool testFindAnswer() const int firstAnswer = findAnswer(firstTree, &error); deleteTree(&firstTree); - Node* secondTree = buildTree("/ + * + 5 6 7 8 + - + 5 6 9 3"); + Node* secondTree = buildTree("(/ (+ (* (+ 5 6) 7) 8) (+ (- (+ 5 6) 9) 3))"); + printTree(secondTree); const int secondAnswer = findAnswer(secondTree, &error); deleteTree(&secondTree); - Node* thirdTree = buildTree("* + 1 1 2"); + Node* thirdTree = buildTree("(* (+ 1 1) 2)"); const int thirdAnswer = findAnswer(thirdTree, &error); deleteTree(&thirdTree); - Node* fourthTree = buildTree("+ - 5 5 6"); + Node* fourthTree = buildTree("(+ (- 5 5) 6)"); const int fourthAnswer = findAnswer(fourthTree, &error); deleteTree(&fourthTree); From 30f11f037a8d80aba3fe721be1e5c2ac77ef44d9 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 3 Dec 2021 12:50:26 +0300 Subject: [PATCH 16/19] fixing memory leaks --- .../TreeSolution/TreeSolution/TreeSolution.c" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" index 977d33b..3c1c3ff 100644 --- "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" @@ -10,6 +10,7 @@ char* readExpression(const char* fileName, int* error) *error = 1; return NULL; } + // The size of the expression in the file is limited to 99 characters char* expression = calloc(100, sizeof(char)); if (expression == NULL) { @@ -17,16 +18,14 @@ char* readExpression(const char* fileName, int* error) fclose(file); return NULL; } - while (!feof(file)) - { - fgets(expression, 99, file); - } - if (expression == NULL) + const char* result = fgets(expression, 100, file); + if (result == NULL) { + free(expression); *error = 2; } fclose(file); - return expression; + return result; } int main() @@ -49,6 +48,7 @@ int main() return -1; } Node* tree = buildTree(expression); + free(expression); const int answer = findAnswer(tree, &error); if (error == 1) { From 48984d17a550f6654c09b1855967cdc78e2e3a79 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 3 Dec 2021 12:58:45 +0300 Subject: [PATCH 17/19] removed the extra code --- .../ParsingTree/ParsingTree/TestParsingTree.c" | 1 - 1 file changed, 1 deletion(-) diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" "b/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" index e5d8edb..3c35511 100644 --- "a/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" @@ -9,7 +9,6 @@ bool testFindAnswer() deleteTree(&firstTree); Node* secondTree = buildTree("(/ (+ (* (+ 5 6) 7) 8) (+ (- (+ 5 6) 9) 3))"); - printTree(secondTree); const int secondAnswer = findAnswer(secondTree, &error); deleteTree(&secondTree); From 2216d55f65631f7f6b5ba5ba14f7bd0dca540b1b Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 3 Dec 2021 13:21:38 +0300 Subject: [PATCH 18/19] more const --- .../TreeSolution/TreeSolution/TreeSolution.c" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" index 3c1c3ff..f1f9b55 100644 --- "a/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" @@ -2,7 +2,7 @@ #include #include -char* readExpression(const char* fileName, int* error) +const char* readExpression(const char* fileName, int* error) { FILE* file = fopen(fileName, "r"); if (file == NULL) From 4c9585b7389e83c8dccaa512d3484516eb95b994 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 3 Dec 2021 13:23:52 +0300 Subject: [PATCH 19/19] changing the format of an expression in a file --- .../TreeSolution/TreeSolution/Expression.txt" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" "b/Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" index ff85f09..97d33c2 100644 --- "a/Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" @@ -1 +1 @@ -/ (+ * + 5 6 7 8) (+ - + 5 6 9 3) \ No newline at end of file +(/ (+ (* (+ 5 6) 7) 8) (+ (- (+ 5 6) 9) 3)) \ No newline at end of file