diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree.sln" "b/Homework \342\204\2267/ParsingTree/ParsingTree.sln" new file mode 100644 index 0000000..3f0d84b --- /dev/null +++ "b/Homework \342\204\2267/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/Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" "b/Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" new file mode 100644 index 0000000..1a8c6b8 --- /dev/null +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/Main.c" @@ -0,0 +1,9 @@ +#include "TestParsingTree.h" + +int main() +{ + if (!testFindAnswer()) + { + return -1; + } +} \ No newline at end of file diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" new file mode 100644 index 0000000..162f430 --- /dev/null +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.c" @@ -0,0 +1,211 @@ +#include "ParsingTree.h" +#include +#include +#include + +typedef struct Node +{ + struct Node* leftSon; + struct Node* rightSon; + struct Node* parent; + char symbol; + int expressionValues; +} Node; + +typedef enum Direction +{ + left, + right +} Direction; + +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* returnRoot(Node* node) +{ + if (node == NULL) + { + return node; + } + while (node->parent != NULL) + { + node = node->parent; + } + return node; +} + +bool isOperator(char symbol) +{ + return symbol == '+' || symbol == '-' + || symbol == '*' || symbol == '/'; +} + +void attach(Node* parent, Node* child, Direction direction) +{ + if (parent == NULL) + { + return; + } + if (direction == left) + { + parent->leftSon = child; + } + else + { + parent->rightSon = child; + } + if (child != NULL) + { + child->parent = parent; + } +} + +Node* createNode(char symbol) +{ + Node* newNode = (Node*)calloc(1, sizeof(Node)); + if (newNode == NULL) + { + return NULL; + } + newNode->symbol = symbol; + newNode->expressionValues = isOperator(symbol) ? 0 : symbol - '0'; + return newNode; +} + +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) + { + node = returnRoot(node); + deleteTree(&node); + return NULL; + } + Node* operator = temporary; + (*counter)++; + attach(operator, addNode(array, counter, operator), left); + attach(operator, addNode(array, counter, operator), right); + return operator; + } + Node* operand = createNode(array[*counter]); + if (operand == NULL) + { + node = returnRoot(node); + deleteTree(&node); + return NULL; + } + (*counter)++; + return operand; +} + +Node* buildTree(const char* array) +{ + int counter = 0; + Node* node = NULL; + return addNode(array, &counter, node); +} + +int findAnswer(Node* root, int* error) +{ + if (*error != 0) + { + return 0; + } + if (root == NULL) + { + *error = 1; + return 0; + } + 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->rightSon != NULL && root->leftSon != NULL) + { + if (root->symbol == '+') + { + root->expressionValues = root->leftSon->expressionValues + root->rightSon->expressionValues; + } + if (root->symbol == '-') + { + root->expressionValues = root->leftSon->expressionValues - root->rightSon->expressionValues; + } + if (root->symbol == '*') + { + root->expressionValues = root->leftSon->expressionValues * root->rightSon->expressionValues; + } + if (root->symbol == '/') + { + root->expressionValues = root->leftSon->expressionValues / root->rightSon->expressionValues; + } + } + return root->expressionValues; +} + +void printTreeRecursive(Node* root) +{ + if (root == NULL) + { + return; + } + if (isOperator(root->symbol)) + { + printf("%c ", '('); + printf("%c ", root->symbol); + } + 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) +{ + printTreeRecursive(root); + printf("%c", ')'); +} \ No newline at end of file diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.h" "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.h" new file mode 100644 index 0000000..f796295 --- /dev/null +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.h" @@ -0,0 +1,16 @@ +#pragma once + +// Structure representing a tree +typedef struct Node Node; + +// Function for deleting a tree +void deleteTree(Node** root); + +// Function for building a tree +Node* buildTree(const char* array); + +// Function for calculating the value of an expression +int findAnswer(Node* root, int* error); + +// Function for tree output +void printTree(Node* root); \ No newline at end of file diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj" "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj" new file mode 100644 index 0000000..6574a5c --- /dev/null +++ "b/Homework \342\204\2267/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/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj.filters" "b/Homework \342\204\2267/ParsingTree/ParsingTree/ParsingTree.vcxproj.filters" new file mode 100644 index 0000000..3c2eee1 --- /dev/null +++ "b/Homework \342\204\2267/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/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" "b/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" new file mode 100644 index 0000000..3c35511 --- /dev/null +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.c" @@ -0,0 +1,24 @@ +#include "ParsingTree.h" +#include "TestParsingTree.h" + +bool testFindAnswer() +{ + Node* firstTree = buildTree("1"); + int error = 0; + const int firstAnswer = findAnswer(firstTree, &error); + deleteTree(&firstTree); + + Node* secondTree = buildTree("(/ (+ (* (+ 5 6) 7) 8) (+ (- (+ 5 6) 9) 3))"); + const int secondAnswer = findAnswer(secondTree, &error); + deleteTree(&secondTree); + + Node* thirdTree = buildTree("(* (+ 1 1) 2)"); + const int thirdAnswer = findAnswer(thirdTree, &error); + deleteTree(&thirdTree); + + Node* fourthTree = buildTree("(+ (- 5 5) 6)"); + const int fourthAnswer = findAnswer(fourthTree, &error); + deleteTree(&fourthTree); + + return firstAnswer == 1 && secondAnswer == 17 && thirdAnswer == 4 && fourthAnswer == 6 && error == 0; +} \ No newline at end of file diff --git "a/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.h" "b/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.h" new file mode 100644 index 0000000..ac1973d --- /dev/null +++ "b/Homework \342\204\2267/ParsingTree/ParsingTree/TestParsingTree.h" @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function for testing a function that finds the value of an expression +bool testFindAnswer(); \ No newline at end of file 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/Expression.txt" "b/Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" new file mode 100644 index 0000000..97d33c2 --- /dev/null +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/Expression.txt" @@ -0,0 +1 @@ +(/ (+ (* (+ 5 6) 7) 8) (+ (- (+ 5 6) 9) 3)) \ No newline at end of file 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..f1f9b55 --- /dev/null +++ "b/Homework \342\204\2267/TreeSolution/TreeSolution/TreeSolution.c" @@ -0,0 +1,60 @@ +#include "../../ParsingTree/ParsingTree/ParsingTree.h" +#include +#include + +const char* readExpression(const char* fileName, int* error) +{ + FILE* file = fopen(fileName, "r"); + if (file == NULL) + { + *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) + { + *error = 3; + fclose(file); + return NULL; + } + const char* result = fgets(expression, 100, file); + if (result == NULL) + { + free(expression); + *error = 2; + } + fclose(file); + return result; +} + +int main() +{ + int error = 0; + const char* expression = readExpression("Expression.txt", &error); + if (error == 1) + { + printf("Could not open the file"); + return -1; + } + if (error == 2) + { + printf("Could not read expression from file"); + return -1; + } + if (error == 3) + { + printf("Memory not allocated"); + return -1; + } + Node* tree = buildTree(expression); + free(expression); + 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..399c7d0 --- /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;_CRT_SECURE_NO_WARNINGS;%(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