diff --git a/BalanceBrackets/BalanceBrackets.sln b/BalanceBrackets/BalanceBrackets.sln new file mode 100644 index 0000000..da1353a --- /dev/null +++ b/BalanceBrackets/BalanceBrackets.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}") = "BalanceBrackets", "BalanceBrackets\BalanceBrackets.vcxproj", "{2095AD51-1509-4999-A54D-827860784952}" +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 + {2095AD51-1509-4999-A54D-827860784952}.Debug|x64.ActiveCfg = Debug|x64 + {2095AD51-1509-4999-A54D-827860784952}.Debug|x64.Build.0 = Debug|x64 + {2095AD51-1509-4999-A54D-827860784952}.Debug|x86.ActiveCfg = Debug|Win32 + {2095AD51-1509-4999-A54D-827860784952}.Debug|x86.Build.0 = Debug|Win32 + {2095AD51-1509-4999-A54D-827860784952}.Release|x64.ActiveCfg = Release|x64 + {2095AD51-1509-4999-A54D-827860784952}.Release|x64.Build.0 = Release|x64 + {2095AD51-1509-4999-A54D-827860784952}.Release|x86.ActiveCfg = Release|Win32 + {2095AD51-1509-4999-A54D-827860784952}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {241DB504-44F6-401D-9485-FE90894D17CA} + EndGlobalSection +EndGlobal diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c new file mode 100644 index 0000000..0ea5367 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -0,0 +1,65 @@ +#include "BalanceBrackets.h" +#include "../../Stack/Stack/Stack.h" + +bool isOpeningBracket(char parentheses) +{ + return parentheses == '(' || parentheses == '{' || parentheses == '['; +} + +bool isClosingBracket(char paretheses) +{ + return paretheses == ')' || paretheses == '}' || paretheses == ']'; +} + +bool openingAndClosingOfTheSameType(char openingParetheses, char closingParetheses) +{ + return openingParetheses != '(' && closingParetheses == ')' + || openingParetheses != '{' && closingParetheses == '}' + || openingParetheses != '[' && closingParetheses == ']'; +} + +bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* errorCode) +{ + Stack* head = createStack(); + int counter = 0; + while (expressionFromParentheses[counter] != '\0') + { + if (isOpeningBracket(expressionFromParentheses[counter])) + { + int error = 0; + push(&head, expressionFromParentheses[counter], &error); + if (error == 2) + { + *errorCode = 2; + deleteStack(&head); + return false; + } + } + else if (isClosingBracket(expressionFromParentheses[counter])) + { + int error = 0; + char topOfTheStack = (char)pop(&head, &error); + if (error == 1) + { + *errorCode = 1; + deleteStack(&head); + return false; + } + if (openingAndClosingOfTheSameType(topOfTheStack, expressionFromParentheses[counter])) + { + *errorCode = 1; + deleteStack(&head); + return false; + } + } + counter++; + } + if (isEmpty(head)) + { + deleteStack(&head); + return true; + } + *errorCode = 1; + deleteStack(&head); + return false; +} \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h new file mode 100644 index 0000000..bb6e3be --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function to check the correct position of the brackets +bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* errorCode); \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj new file mode 100644 index 0000000..0abc1f9 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {2095ad51-1509-4999-a54d-827860784952} + BalanceBrackets + 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/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters new file mode 100644 index 0000000..61a5f87 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters @@ -0,0 +1,42 @@ + + + + + {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/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c new file mode 100644 index 0000000..eb1df90 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -0,0 +1,35 @@ +#include "BalanceBracketsTest.h" +#include "BalanceBrackets.h" + +bool balanceBracketsTest() +{ + int errors[14] = {0}; + return checkCorrectOrderBrackets("((15 - x) - (13 + 45) * ( 1 0 - 1 6))", &errors[0]) + && checkCorrectOrderBrackets("(([(){()}][]{}{}{()}))", &errors[1]) + && checkCorrectOrderBrackets("([[]({})]({}(())))", &errors[2]) + && checkCorrectOrderBrackets("(x * x) * ( c - a)", &errors[3]) + && checkCorrectOrderBrackets("[[]]{{}}(())", &errors[4]) + && checkCorrectOrderBrackets("(({{{}[]}[]}[]))", &errors[5]) + && checkCorrectOrderBrackets("{}()[]", &errors[6]) + && !checkCorrectOrderBrackets("{{", &errors[7]) + && !checkCorrectOrderBrackets("{())[][]}", &errors[8]) + && !checkCorrectOrderBrackets("(x-a)*(c+a))", &errors[9]) + && !checkCorrectOrderBrackets("(]])()()[][]{}{{}}", &errors[10]) + && !checkCorrectOrderBrackets("{()[]{}{{))))))(}", &errors[11]) + && !checkCorrectOrderBrackets("{{}}(((", &errors[12]) + && !checkCorrectOrderBrackets("(", &errors[13]) + && errors[0] == 0 + && errors[1] == 0 + && errors[2] == 0 + && errors[3] == 0 + && errors[4] == 0 + && errors[5] == 0 + && errors[6] == 0 + && errors[7] == 1 + && errors[8] == 1 + && errors[9] == 1 + && errors[10] == 1 + && errors[11] == 1 + && errors[12] == 1 + && errors[13] == 1; +} \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.h b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.h new file mode 100644 index 0000000..e277708 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function for testing a function that checks for the correct position of the brackets +bool balanceBracketsTest(); diff --git a/BalanceBrackets/BalanceBrackets/Main.c b/BalanceBrackets/BalanceBrackets/Main.c new file mode 100644 index 0000000..dac54f3 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/Main.c @@ -0,0 +1,28 @@ +#include "BalanceBrackets.h" +#include "BalanceBracketsTest.h" +#include + +int main() +{ + if (!balanceBracketsTest()) + { + printf("Test failed"); + return -1; + } + char expressionFromParentheses[250] = { '\0' }; + printf("enter the expression that you want to check for the correct placement of brackets\n"); + scanf_s("%[^\n]s", expressionFromParentheses, (unsigned)sizeof(expressionFromParentheses)); + int errorCode = 0; + checkCorrectOrderBrackets(expressionFromParentheses, &errorCode); + if (errorCode == 1) + { + printf("The balance of the brackets is incorrect"); + return 0; + } + if (errorCode == 2) + { + printf("Insufficient memory"); + return -1; + } + printf("The balance of the brackets is correct"); +} \ No newline at end of file diff --git a/Stack/Stack.sln b/Stack/Stack.sln new file mode 100644 index 0000000..ce2c4f2 --- /dev/null +++ b/Stack/Stack.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}") = "Stack", "Stack\Stack.vcxproj", "{A4F19B27-54C9-4841-98B0-728EDCD283D2}" +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 + {A4F19B27-54C9-4841-98B0-728EDCD283D2}.Debug|x64.ActiveCfg = Debug|x64 + {A4F19B27-54C9-4841-98B0-728EDCD283D2}.Debug|x64.Build.0 = Debug|x64 + {A4F19B27-54C9-4841-98B0-728EDCD283D2}.Debug|x86.ActiveCfg = Debug|Win32 + {A4F19B27-54C9-4841-98B0-728EDCD283D2}.Debug|x86.Build.0 = Debug|Win32 + {A4F19B27-54C9-4841-98B0-728EDCD283D2}.Release|x64.ActiveCfg = Release|x64 + {A4F19B27-54C9-4841-98B0-728EDCD283D2}.Release|x64.Build.0 = Release|x64 + {A4F19B27-54C9-4841-98B0-728EDCD283D2}.Release|x86.ActiveCfg = Release|Win32 + {A4F19B27-54C9-4841-98B0-728EDCD283D2}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5B7E38AF-1E89-49D4-95EB-B55DB0F7ADDF} + EndGlobalSection +EndGlobal diff --git a/Stack/Stack/Main.c b/Stack/Stack/Main.c new file mode 100644 index 0000000..798f891 --- /dev/null +++ b/Stack/Stack/Main.c @@ -0,0 +1,11 @@ +#include "StackTest.h" +#include + +int main() +{ + if (!pushTest() || !popTest() || !deleteStackTest()) + { + printf("Test failed"); + return -1; + } +} \ No newline at end of file diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c new file mode 100644 index 0000000..692595d --- /dev/null +++ b/Stack/Stack/Stack.c @@ -0,0 +1,50 @@ +#include "Stack.h" +#include + +bool isEmpty(Stack* head) +{ + return head == NULL; +} + +Stack* createStack() +{ + return NULL; +} + +void push(Stack** head, int element, int* error) +{ + *error = 0; + Stack* newStack = (Stack*)calloc(1, sizeof(Stack)); + if (newStack == NULL) + { + *error = 2; + return; + } + newStack->value = element; + newStack->next = *head; + *head = newStack; +} + +int pop(Stack** head, int* error) +{ + *error = 0; + if (*head == NULL) + { + *error = 1; + return 0; + } + const int element = (*head)->value; + Stack* temporary = *head; + *head = (*head)->next; + free(temporary); + return element; +} + +void deleteStack(Stack** head) +{ + int error = 0; + while (!isEmpty(*head)) + { + pop(head, &error); + } +} diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h new file mode 100644 index 0000000..46f0491 --- /dev/null +++ b/Stack/Stack/Stack.h @@ -0,0 +1,24 @@ +#pragma once +#include + +// Structure for implementing a stack consisting of a value and a pointer to the next element +typedef struct Stack +{ + int value; + struct Stack* next; +} Stack; + +// Function for testing if stack is empty. +bool isEmpty(Stack* head); + +// Function for adding an element to the top of the stack +void push(Stack** head, int element, int* error); + +// Function to remove an element from the top of the stack that returns the value of that element +int pop(Stack** head, int* error); + +// Function for deleting all stack elements +void deleteStack(Stack** head); + +// Creating a stack +Stack* createStack(); \ No newline at end of file diff --git a/Stack/Stack/Stack.vcxproj b/Stack/Stack/Stack.vcxproj new file mode 100644 index 0000000..6ced32d --- /dev/null +++ b/Stack/Stack/Stack.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {a4f19b27-54c9-4841-98b0-728edcd283d2} + Stack + 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/Stack/Stack/Stack.vcxproj.filters b/Stack/Stack/Stack.vcxproj.filters new file mode 100644 index 0000000..ff73484 --- /dev/null +++ b/Stack/Stack/Stack.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/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c new file mode 100644 index 0000000..e6a1cc0 --- /dev/null +++ b/Stack/Stack/StackTest.c @@ -0,0 +1,81 @@ +#include "StackTest.h" +#include "Stack.h" +#include + +bool pushTest() +{ + Stack* head = NULL; + int error = 0; + push(&head, '[', &error); + if (error == 2) + { + return false; + } + const int firstResult = head->value; + push(&head, ')', &error); + if (error == 2) + { + return false; + } + const int secondResult = head->value; + deleteStack(&head); + return firstResult == '[' && secondResult == ')'; +} + +bool popTest() +{ + Stack* head = NULL; + int error = 0; + push(&head, 'a', &error); + if (error == 2) + { + return false; + } + push(&head, '^', &error); + if (error == 2) + { + return false; + } + push(&head, '1', &error); + if (error == 2) + { + return false; + } + const int firstPopResult = pop(&head, &error); + if (error == 1) + { + return false; + } + const int firstUpperElement = head->value; + const int secondPopResult = pop(&head, &error); + if (error == 1) + { + return false; + } + const int secondUpperElement = head->value; + deleteStack(&head); + return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; +} + +bool deleteStackTest() +{ + Stack* head = NULL; + int error = 0; + push(&head, 'a', &error); + if (error == 2) + { + return false; + } + push(&head, 'b', &error); + if (error == 2) + { + return false; + } + push(&head, 'c', &error); + if (error == 2) + { + return false; + } + deleteStack(&head); + return head == NULL; +} \ No newline at end of file diff --git a/Stack/Stack/StackTest.h b/Stack/Stack/StackTest.h new file mode 100644 index 0000000..edfce8b --- /dev/null +++ b/Stack/Stack/StackTest.h @@ -0,0 +1,11 @@ +#pragma once +#include + +// Function to check the function that adds an element to the top of the stack +bool pushTest(); + +// Function to check the function that removes from the top of the stack +bool popTest(); + +// Function for checking a function that removes all stack elements +bool deleteStackTest();