From 1a83386b8b8d3005f1011850f7794cb55cbdf78a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 15 Oct 2021 21:09:51 +0500 Subject: [PATCH 01/34] creating functions for working with the stack and testing them --- Stack/Stack.sln | 31 ++++++ Stack/Stack/Main.c | 10 ++ Stack/Stack/Stack.c | 44 +++++++++ Stack/Stack/Stack.h | 15 +++ Stack/Stack/Stack.vcxproj | 153 ++++++++++++++++++++++++++++++ Stack/Stack/Stack.vcxproj.filters | 36 +++++++ Stack/Stack/StackTest.c | 36 +++++++ Stack/Stack/StackTest.h | 8 ++ 8 files changed, 333 insertions(+) create mode 100644 Stack/Stack.sln create mode 100644 Stack/Stack/Main.c create mode 100644 Stack/Stack/Stack.c create mode 100644 Stack/Stack/Stack.h create mode 100644 Stack/Stack/Stack.vcxproj create mode 100644 Stack/Stack/Stack.vcxproj.filters create mode 100644 Stack/Stack/StackTest.c create mode 100644 Stack/Stack/StackTest.h 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..9c67d17 --- /dev/null +++ b/Stack/Stack/Main.c @@ -0,0 +1,10 @@ +#include +#include "StackTest.h" + +int main() +{ + if (!pushTest() || !popTest() || !deleteStackTest()) + { + 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..83a54ae --- /dev/null +++ b/Stack/Stack/Stack.c @@ -0,0 +1,44 @@ +#include +#include +#include + +typedef struct Stack +{ + int value; + struct Stack* next; +}Stack; + +bool isEmpty(Stack* head) +{ + return head == NULL; +} + +void push(Stack** head, int element) +{ + Stack* newStack = (Stack*)calloc(1, sizeof(Stack)); + if (newStack == NULL) + { + return; + } + newStack->value = element; + newStack->next = *head; + *head = newStack; +} + +int pop(Stack** head) +{ + int element = ((*head)->value); + Stack* temporary = *head; + Stack* help = (*head) -> next; + free(temporary); + *head = help; + return element; +} + +void deleteStack(Stack** head) +{ + while (!isEmpty(*head)) + { + pop(head); + } +} diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h new file mode 100644 index 0000000..662f63a --- /dev/null +++ b/Stack/Stack/Stack.h @@ -0,0 +1,15 @@ +#pragma once + +typedef struct Stack +{ + int value; + struct Stack* next; +}Stack; + +bool isEmpty(Stack* head); + +void push(Stack** head, int element); + +int pop(Stack** head); + +void deleteStack(Stack** head); \ 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..4428432 --- /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..1d2af0d --- /dev/null +++ b/Stack/Stack/StackTest.c @@ -0,0 +1,36 @@ +#include "StackTest.h" +#include "Stack.h" +#include + +bool pushTest() +{ + Stack* p = NULL; + push(&p, 12); + int firstResult = p->value; + push(&p, 128); + int secondResult = p->value; + return firstResult == 12 && secondResult == 128; +} + +bool popTest() +{ + Stack* p = NULL; + push(&p, 12); + push(&p, 128); + push(&p, 147); + int firstPopResult = pop(&p); + int firstUpperElement = p -> value; + int secondPopResult = pop(&p); + int secondUpperElement = p->value; + return firstPopResult == 147 && firstUpperElement == 128 && secondPopResult == 128 && secondUpperElement == 12; +} + +bool deleteStackTest() +{ + Stack* p = NULL; + push(&p, 12); + push(&p, 128); + push(&p, 147); + deleteStack(&p); + return p == 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..3715ed1 --- /dev/null +++ b/Stack/Stack/StackTest.h @@ -0,0 +1,8 @@ +#pragma once +#include + +bool pushTest(); + +bool popTest(); + +bool deleteStackTest(); \ No newline at end of file From 2549f2f4c184e87538f017792b85ca21f565a09b Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 16 Oct 2021 00:30:59 +0500 Subject: [PATCH 02/34] fixed bugs --- Stack/Stack/Stack.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index 83a54ae..9147894 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -1,12 +1,7 @@ #include #include #include - -typedef struct Stack -{ - int value; - struct Stack* next; -}Stack; +#include "Stack.h" bool isEmpty(Stack* head) { From 8d6727ea2be14c0768b8a988360e5e199d56d0ea Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 16 Oct 2021 02:26:23 +0500 Subject: [PATCH 03/34] fixed bugs --- Stack/Stack/Stack.h | 5 ++++ Stack/Stack/Stack.vcxproj.filters | 6 ++--- Stack/Stack/StackTest.c | 38 +++++++++++++++---------------- Stack/Stack/StackTest.h | 3 +++ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 662f63a..0cdaa86 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -1,15 +1,20 @@ #pragma once +// 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 that checks the stack for emptiness bool isEmpty(Stack* head); +// Function for adding an element to the top of the stack void push(Stack** head, int element); +// Function to remove an element from the top of the stack that returns the value of that element int pop(Stack** head); +// Function for deleting all stack elements void deleteStack(Stack** head); \ No newline at end of file diff --git a/Stack/Stack/Stack.vcxproj.filters b/Stack/Stack/Stack.vcxproj.filters index 4428432..ff73484 100644 --- a/Stack/Stack/Stack.vcxproj.filters +++ b/Stack/Stack/Stack.vcxproj.filters @@ -15,15 +15,15 @@ - - Исходные файлы - Исходные файлы Исходные файлы + + Исходные файлы + diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index 1d2af0d..25e812d 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -4,33 +4,33 @@ bool pushTest() { - Stack* p = NULL; - push(&p, 12); - int firstResult = p->value; - push(&p, 128); - int secondResult = p->value; + Stack* head = NULL; + push(&head, 12); + int firstResult = head->value; + push(&head, 128); + int secondResult = head->value; return firstResult == 12 && secondResult == 128; } bool popTest() { - Stack* p = NULL; - push(&p, 12); - push(&p, 128); - push(&p, 147); - int firstPopResult = pop(&p); - int firstUpperElement = p -> value; - int secondPopResult = pop(&p); - int secondUpperElement = p->value; + Stack* head = NULL; + push(&head, 12); + push(&head, 128); + push(&head, 147); + int firstPopResult = pop(&head); + int firstUpperElement = head -> value; + int secondPopResult = pop(&head); + int secondUpperElement = head->value; return firstPopResult == 147 && firstUpperElement == 128 && secondPopResult == 128 && secondUpperElement == 12; } bool deleteStackTest() { - Stack* p = NULL; - push(&p, 12); - push(&p, 128); - push(&p, 147); - deleteStack(&p); - return p == NULL; + Stack* head = NULL; + push(&head, 12); + push(&head, 128); + push(&head, 147); + deleteStack(&head); + return head == NULL; } \ No newline at end of file diff --git a/Stack/Stack/StackTest.h b/Stack/Stack/StackTest.h index 3715ed1..ca939fc 100644 --- a/Stack/Stack/StackTest.h +++ b/Stack/Stack/StackTest.h @@ -1,8 +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(); \ No newline at end of file From e7e22d69f8093efe9dd9c3a1143fc7d5413c616e Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 16 Oct 2021 16:46:47 +0500 Subject: [PATCH 04/34] add function --- Stack/Stack/Stack.c | 9 ++++----- Stack/Stack/Stack.h | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index 9147894..32eb4d1 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -1,7 +1,6 @@ +#include "Stack.h" #include -#include #include -#include "Stack.h" bool isEmpty(Stack* head) { @@ -15,14 +14,14 @@ void push(Stack** head, int element) { return; } - newStack->value = element; - newStack->next = *head; + newStack -> value = element; + newStack -> next = *head; *head = newStack; } int pop(Stack** head) { - int element = ((*head)->value); + int element = (*head) -> value; Stack* temporary = *head; Stack* help = (*head) -> next; free(temporary); diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 0cdaa86..b58f850 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -1,5 +1,5 @@ #pragma once - +#include // Structure for implementing a stack consisting of a value and a pointer to the next element typedef struct Stack { @@ -7,7 +7,7 @@ typedef struct Stack struct Stack* next; }Stack; -// Function that checks the stack for emptiness +// Function for test empty stack bool isEmpty(Stack* head); // Function for adding an element to the top of the stack From 2f9e2a0327ca32d6427f571b8a993e85b81c0ed0 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 16 Oct 2021 17:02:46 +0500 Subject: [PATCH 05/34] fixed bugs --- Stack/Stack/Main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Stack/Stack/Main.c b/Stack/Stack/Main.c index 9c67d17..6eff2a4 100644 --- a/Stack/Stack/Main.c +++ b/Stack/Stack/Main.c @@ -1,5 +1,5 @@ -#include #include "StackTest.h" +#include int main() { From a04a9a8cda8b9d27f28b30153cf6ee0ea2add991 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 17 Oct 2021 00:25:34 +0500 Subject: [PATCH 06/34] writing all functions, text and comments --- BalanceBrackets/BalanceBrackets.sln | 31 ++++ .../BalanceBrackets/BalanceBrackets.c | 60 +++++++ .../BalanceBrackets/BalanceBrackets.h | 5 + .../BalanceBrackets/BalanceBrackets.vcxproj | 156 ++++++++++++++++++ .../BalanceBrackets.vcxproj.filters | 45 +++++ .../BalanceBrackets/BalanceBracketsTest.c | 35 ++++ .../BalanceBrackets/BalanceBracketsTest.h | 5 + BalanceBrackets/BalanceBrackets/Stack.c | 38 +++++ BalanceBrackets/BalanceBrackets/Stack.h | 21 +++ BalanceBrackets/BalanceBrackets/StackTest.c | 36 ++++ BalanceBrackets/BalanceBrackets/StackTest.h | 11 ++ 11 files changed, 443 insertions(+) create mode 100644 BalanceBrackets/BalanceBrackets.sln create mode 100644 BalanceBrackets/BalanceBrackets/BalanceBrackets.c create mode 100644 BalanceBrackets/BalanceBrackets/BalanceBrackets.h create mode 100644 BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj create mode 100644 BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters create mode 100644 BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c create mode 100644 BalanceBrackets/BalanceBrackets/BalanceBracketsTest.h create mode 100644 BalanceBrackets/BalanceBrackets/Stack.c create mode 100644 BalanceBrackets/BalanceBrackets/Stack.h create mode 100644 BalanceBrackets/BalanceBrackets/StackTest.c create mode 100644 BalanceBrackets/BalanceBrackets/StackTest.h 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..b1ededf --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -0,0 +1,60 @@ +#include "BalanceBrackets.h" +#include "Stack.h" +#include "StackTest.h" +#include "BalanceBracketsTest.h" +#include +#include + +bool checkCorrectOrderBrackets(char* expressionFromParentheses) +{ + Stack* head = NULL; + int counter = 0; + while (expressionFromParentheses[counter] != '\0') + { + if (expressionFromParentheses[counter] == '(' || expressionFromParentheses[counter] == '{' + || expressionFromParentheses[counter] == '[') + { + push(&head, expressionFromParentheses[counter]); + } + else if (expressionFromParentheses[counter] == ')' || expressionFromParentheses[counter] == '}' + || expressionFromParentheses[counter] == ']') + { + if(isEmpty(head)) + { + return false; + } + char k = pop(&head); + if ((k == '(' && expressionFromParentheses[counter] != ')') + || (k == '{' && expressionFromParentheses[counter] != '}') + || (k == '[' && expressionFromParentheses[counter] != ']')) + { + return false; + } + } + counter++; + } + if (isEmpty(head)) + { + deleteStack(&head); + return true; + } + deleteStack(&head); + return false; +} +int main() +{ + if (!balanceBracketsTest() || !popTest() || !pushTest() || !deleteStackTest()) + { + printf("Test failed"); + return -1; + } + char expressionFromParentheses[250] = { '\0' }; + printf("enter the expression in postfix form\n"); + scanf_s("%s", expressionFromParentheses, (unsigned)_countof(expressionFromParentheses)); + if (!checkCorrectOrderBrackets(expressionFromParentheses)) + { + printf("incorrect"); + return 0; + } + printf("correct"); +} \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h new file mode 100644 index 0000000..95b8c78 --- /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(char* expressionFromParentheses); \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj new file mode 100644 index 0000000..81ddfc4 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj @@ -0,0 +1,156 @@ + + + + + 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..a60ce2e --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters @@ -0,0 +1,45 @@ + + + + + {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..130e4d5 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -0,0 +1,35 @@ +#include "BalanceBracketsTest.h" +#include "BalanceBrackets.h" + +bool balanceBracketsTest() +{ + char firstCorrectExpressionFromParentheses[250] = "((15 - x) - (13 + 45) * ( 10 - 16))"; + char secondCorrectExpressionFromParentheses[250] = "(([(){()}][]{}{}{()}))"; + char thirdCorrectExpressionFromParentheses[250] = "([[]({})]({}(())))"; + char fourthCorrectExpressionFromParentheses[250] = "(x * x) * ( c - a)"; + char fifthCorrectExpressionFromParentheses[250] = "[[]]{{}}(())"; + char sixthCorrectExpressionFromParentheses[250] = "(({{{}[]}[]}[]))"; + char seventhCorrectExpressionFromParentheses[250] = "{}()[]"; + char firstIncorrectExpressionFromParentheses[250] = "{{"; + char secondIncorrectExpressionFromParentheses[250] = "{())[][]}"; + char thirdIncorrectExpressionFromParentheses[250] = "(x-a)*(c+a))"; + char fourthIncorrectExpressionFromParentheses[250] = "(]])()()[][]{}{{}}"; + char fifthIncorrectExpressionFromParentheses[250] = "{()[]{}{{))))))(}"; + char sixthIncorrectExpressionFromParentheses[250] = "{{}}((("; + char seventhIncorrectExpressionFromParentheses[250] = "("; + return checkCorrectOrderBrackets(firstCorrectExpressionFromParentheses) + && checkCorrectOrderBrackets(secondCorrectExpressionFromParentheses) + && checkCorrectOrderBrackets(thirdCorrectExpressionFromParentheses) + && checkCorrectOrderBrackets(fourthCorrectExpressionFromParentheses) + && checkCorrectOrderBrackets(fifthCorrectExpressionFromParentheses) + && checkCorrectOrderBrackets(sixthCorrectExpressionFromParentheses) + && checkCorrectOrderBrackets(seventhCorrectExpressionFromParentheses) + && !checkCorrectOrderBrackets(firstIncorrectExpressionFromParentheses) + && !checkCorrectOrderBrackets(secondIncorrectExpressionFromParentheses) + && !checkCorrectOrderBrackets(thirdIncorrectExpressionFromParentheses) + && !checkCorrectOrderBrackets(fourthIncorrectExpressionFromParentheses) + && !checkCorrectOrderBrackets(fifthIncorrectExpressionFromParentheses) + && !checkCorrectOrderBrackets(sixthIncorrectExpressionFromParentheses) + && !checkCorrectOrderBrackets(seventhIncorrectExpressionFromParentheses); + +} \ 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/Stack.c b/BalanceBrackets/BalanceBrackets/Stack.c new file mode 100644 index 0000000..32eb4d1 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/Stack.c @@ -0,0 +1,38 @@ +#include "Stack.h" +#include +#include + +bool isEmpty(Stack* head) +{ + return head == NULL; +} + +void push(Stack** head, int element) +{ + Stack* newStack = (Stack*)calloc(1, sizeof(Stack)); + if (newStack == NULL) + { + return; + } + newStack -> value = element; + newStack -> next = *head; + *head = newStack; +} + +int pop(Stack** head) +{ + int element = (*head) -> value; + Stack* temporary = *head; + Stack* help = (*head) -> next; + free(temporary); + *head = help; + return element; +} + +void deleteStack(Stack** head) +{ + while (!isEmpty(*head)) + { + pop(head); + } +} diff --git a/BalanceBrackets/BalanceBrackets/Stack.h b/BalanceBrackets/BalanceBrackets/Stack.h new file mode 100644 index 0000000..fb3b361 --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/Stack.h @@ -0,0 +1,21 @@ +#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 test empty stack +bool isEmpty(Stack* head); + +// Function for adding an element to the top of the stack +void push(Stack** head, int element); + +// Function to remove an element from the top of the stack that returns the value of that element +int pop(Stack** head); + +// Function for deleting all stack elements +void deleteStack(Stack** head); \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/StackTest.c b/BalanceBrackets/BalanceBrackets/StackTest.c new file mode 100644 index 0000000..25e812d --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/StackTest.c @@ -0,0 +1,36 @@ +#include "StackTest.h" +#include "Stack.h" +#include + +bool pushTest() +{ + Stack* head = NULL; + push(&head, 12); + int firstResult = head->value; + push(&head, 128); + int secondResult = head->value; + return firstResult == 12 && secondResult == 128; +} + +bool popTest() +{ + Stack* head = NULL; + push(&head, 12); + push(&head, 128); + push(&head, 147); + int firstPopResult = pop(&head); + int firstUpperElement = head -> value; + int secondPopResult = pop(&head); + int secondUpperElement = head->value; + return firstPopResult == 147 && firstUpperElement == 128 && secondPopResult == 128 && secondUpperElement == 12; +} + +bool deleteStackTest() +{ + Stack* head = NULL; + push(&head, 12); + push(&head, 128); + push(&head, 147); + deleteStack(&head); + return head == NULL; +} \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/StackTest.h b/BalanceBrackets/BalanceBrackets/StackTest.h new file mode 100644 index 0000000..ca939fc --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/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(); \ No newline at end of file From ac6b3905b0bd7ffe4987de16cc37b2504dcf808c Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 17 Oct 2021 00:57:35 +0500 Subject: [PATCH 07/34] fixed bugs --- .../BalanceBrackets/BalanceBrackets.c | 4 +- .../BalanceBrackets/BalanceBrackets.vcxproj | 8 ++-- .../BalanceBrackets.vcxproj.filters | 14 +++---- BalanceBrackets/BalanceBrackets/Stack.c | 38 ------------------- BalanceBrackets/BalanceBrackets/Stack.h | 21 ---------- BalanceBrackets/BalanceBrackets/StackTest.c | 36 ------------------ BalanceBrackets/BalanceBrackets/StackTest.h | 11 ------ 7 files changed, 13 insertions(+), 119 deletions(-) delete mode 100644 BalanceBrackets/BalanceBrackets/Stack.c delete mode 100644 BalanceBrackets/BalanceBrackets/Stack.h delete mode 100644 BalanceBrackets/BalanceBrackets/StackTest.c delete mode 100644 BalanceBrackets/BalanceBrackets/StackTest.h diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index b1ededf..807d426 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -1,6 +1,6 @@ #include "BalanceBrackets.h" -#include "Stack.h" -#include "StackTest.h" +#include "../../Stack/Stack/Stack.h" +#include "../../Stack/Stack/StackTest.h" #include "BalanceBracketsTest.h" #include #include diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj index 81ddfc4..1ef1971 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj @@ -139,16 +139,16 @@ + + - - + + - - diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters index a60ce2e..ebcc7f5 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters @@ -18,27 +18,27 @@ Исходные файлы - + Исходные файлы - + Исходные файлы - + Исходные файлы - + Файлы заголовков - + Файлы заголовков - + Файлы заголовков - + Файлы заголовков diff --git a/BalanceBrackets/BalanceBrackets/Stack.c b/BalanceBrackets/BalanceBrackets/Stack.c deleted file mode 100644 index 32eb4d1..0000000 --- a/BalanceBrackets/BalanceBrackets/Stack.c +++ /dev/null @@ -1,38 +0,0 @@ -#include "Stack.h" -#include -#include - -bool isEmpty(Stack* head) -{ - return head == NULL; -} - -void push(Stack** head, int element) -{ - Stack* newStack = (Stack*)calloc(1, sizeof(Stack)); - if (newStack == NULL) - { - return; - } - newStack -> value = element; - newStack -> next = *head; - *head = newStack; -} - -int pop(Stack** head) -{ - int element = (*head) -> value; - Stack* temporary = *head; - Stack* help = (*head) -> next; - free(temporary); - *head = help; - return element; -} - -void deleteStack(Stack** head) -{ - while (!isEmpty(*head)) - { - pop(head); - } -} diff --git a/BalanceBrackets/BalanceBrackets/Stack.h b/BalanceBrackets/BalanceBrackets/Stack.h deleted file mode 100644 index fb3b361..0000000 --- a/BalanceBrackets/BalanceBrackets/Stack.h +++ /dev/null @@ -1,21 +0,0 @@ -#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 test empty stack -bool isEmpty(Stack* head); - -// Function for adding an element to the top of the stack -void push(Stack** head, int element); - -// Function to remove an element from the top of the stack that returns the value of that element -int pop(Stack** head); - -// Function for deleting all stack elements -void deleteStack(Stack** head); \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/StackTest.c b/BalanceBrackets/BalanceBrackets/StackTest.c deleted file mode 100644 index 25e812d..0000000 --- a/BalanceBrackets/BalanceBrackets/StackTest.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "StackTest.h" -#include "Stack.h" -#include - -bool pushTest() -{ - Stack* head = NULL; - push(&head, 12); - int firstResult = head->value; - push(&head, 128); - int secondResult = head->value; - return firstResult == 12 && secondResult == 128; -} - -bool popTest() -{ - Stack* head = NULL; - push(&head, 12); - push(&head, 128); - push(&head, 147); - int firstPopResult = pop(&head); - int firstUpperElement = head -> value; - int secondPopResult = pop(&head); - int secondUpperElement = head->value; - return firstPopResult == 147 && firstUpperElement == 128 && secondPopResult == 128 && secondUpperElement == 12; -} - -bool deleteStackTest() -{ - Stack* head = NULL; - push(&head, 12); - push(&head, 128); - push(&head, 147); - deleteStack(&head); - return head == NULL; -} \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/StackTest.h b/BalanceBrackets/BalanceBrackets/StackTest.h deleted file mode 100644 index ca939fc..0000000 --- a/BalanceBrackets/BalanceBrackets/StackTest.h +++ /dev/null @@ -1,11 +0,0 @@ -#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(); \ No newline at end of file From 015e87ef14d7237253535cb277fd258c301fb5ba Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 17 Oct 2021 01:30:28 +0500 Subject: [PATCH 08/34] fixed bugs --- Stack/Stack/Stack.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index b58f850..fb3b361 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -1,5 +1,6 @@ #pragma once #include + // Structure for implementing a stack consisting of a value and a pointer to the next element typedef struct Stack { From d85663e4bf9d8cc0dc3d5a494f81a3453b09191f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 22 Oct 2021 17:22:48 +0500 Subject: [PATCH 09/34] fixed bugs --- Stack/Stack/Stack.c | 23 ++++++++++++++--------- Stack/Stack/Stack.h | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index 32eb4d1..d904d0e 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -14,19 +14,24 @@ void push(Stack** head, int element) { return; } - newStack -> value = element; - newStack -> next = *head; + newStack->value = element; + newStack->next = *head; *head = newStack; } -int pop(Stack** head) +int pop(Stack** head, errno_t err) { - int element = (*head) -> value; - Stack* temporary = *head; - Stack* help = (*head) -> next; - free(temporary); - *head = help; - return element; + if (*head != NULL) + { + int element = (*head)->value; + Stack* temporary = *head; + *head = (*head)->next; + free(temporary); + return element; + } + errno_t err = 1; + error(err); + return 0; } void deleteStack(Stack** head) diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index fb3b361..1906590 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -15,7 +15,7 @@ bool isEmpty(Stack* head); void push(Stack** head, int element); // Function to remove an element from the top of the stack that returns the value of that element -int pop(Stack** head); +int pop(Stack** head, errno_t err); // Function for deleting all stack elements void deleteStack(Stack** head); \ No newline at end of file From 6a502c15fc6b0141b88e3a1b976c5d2be5af67d5 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 22 Oct 2021 17:42:27 +0500 Subject: [PATCH 10/34] fixed bugs --- Stack/Stack/Stack.c | 8 ++++---- Stack/Stack/Stack.h | 2 +- Stack/Stack/StackTest.c | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index d904d0e..595797c 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -19,7 +19,7 @@ void push(Stack** head, int element) *head = newStack; } -int pop(Stack** head, errno_t err) +int pop(Stack** head, bool* err) { if (*head != NULL) { @@ -29,15 +29,15 @@ int pop(Stack** head, errno_t err) free(temporary); return element; } - errno_t err = 1; - error(err); + *err = false; return 0; } void deleteStack(Stack** head) { + bool err = true; while (!isEmpty(*head)) { - pop(head); + pop(head, &err); } } diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 1906590..3c551c7 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -15,7 +15,7 @@ bool isEmpty(Stack* head); void push(Stack** head, int element); // Function to remove an element from the top of the stack that returns the value of that element -int pop(Stack** head, errno_t err); +int pop(Stack** head, bool* err); // Function for deleting all stack elements void deleteStack(Stack** head); \ No newline at end of file diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index 25e812d..bef4be9 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -14,13 +14,14 @@ bool pushTest() bool popTest() { + bool err = true; Stack* head = NULL; push(&head, 12); push(&head, 128); push(&head, 147); - int firstPopResult = pop(&head); + int firstPopResult = pop(&head, &err); int firstUpperElement = head -> value; - int secondPopResult = pop(&head); + int secondPopResult = pop(&head, &err); int secondUpperElement = head->value; return firstPopResult == 147 && firstUpperElement == 128 && secondPopResult == 128 && secondUpperElement == 12; } From 77c9d1528cc5e75ed90879d65c41dc0df9e2db4b Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 22 Oct 2021 19:04:59 +0500 Subject: [PATCH 11/34] fixed bugs --- Stack/Stack/Stack.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 3c551c7..26a1a0a 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -15,7 +15,7 @@ bool isEmpty(Stack* head); void push(Stack** head, int element); // Function to remove an element from the top of the stack that returns the value of that element -int pop(Stack** head, bool* err); +int pop(Stack** head, bool* check); // Function for deleting all stack elements void deleteStack(Stack** head); \ No newline at end of file From 0dfb7c24c8a6eef4c1c7cac251eb020aa046f035 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 22 Oct 2021 19:46:18 +0500 Subject: [PATCH 12/34] fixed bugs --- BalanceBrackets/BalanceBrackets/BalanceBrackets.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index 807d426..381030e 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -7,6 +7,7 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) { + bool check = true; Stack* head = NULL; int counter = 0; while (expressionFromParentheses[counter] != '\0') @@ -23,7 +24,13 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) { return false; } - char k = pop(&head); + char k = pop(&head, &check); + { + if (!check) + { + return false; + } + } if ((k == '(' && expressionFromParentheses[counter] != ')') || (k == '{' && expressionFromParentheses[counter] != '}') || (k == '[' && expressionFromParentheses[counter] != ']')) From b720cdc45696703ca2a2942e2dc120a7672cec6d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 01:05:52 +0500 Subject: [PATCH 13/34] adding a function that returns the value of an element from the top of the stack --- Stack/Stack/Stack.c | 13 +++++++++++-- Stack/Stack/Stack.h | 9 ++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index 595797c..e927cc6 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -19,11 +19,11 @@ void push(Stack** head, int element) *head = newStack; } -int pop(Stack** head, bool* err) +char pop(Stack** head, bool* err) { if (*head != NULL) { - int element = (*head)->value; + char element = (*head)->value; Stack* temporary = *head; *head = (*head)->next; free(temporary); @@ -41,3 +41,12 @@ void deleteStack(Stack** head) pop(head, &err); } } + +char top(Stack* head, bool* check) +{ + if (!isEmpty(head)) + { + return head->value; + } + return 0; +} diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 26a1a0a..1326ac0 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -4,7 +4,7 @@ // Structure for implementing a stack consisting of a value and a pointer to the next element typedef struct Stack { - int value; + char value; struct Stack* next; }Stack; @@ -15,7 +15,10 @@ bool isEmpty(Stack* head); void push(Stack** head, int element); // Function to remove an element from the top of the stack that returns the value of that element -int pop(Stack** head, bool* check); +char pop(Stack** head, bool* check); // Function for deleting all stack elements -void deleteStack(Stack** head); \ No newline at end of file +void deleteStack(Stack** head); + +// Function that returns the value of an element from the top of the stack +char top(Stack*head, bool* check); \ No newline at end of file From c713a82e972e870301b49401280ab674435806ad Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 01:38:09 +0500 Subject: [PATCH 14/34] fixed bugs --- Stack/Stack/Main.c | 1 + Stack/Stack/Stack.c | 2 +- Stack/Stack/Stack.h | 4 ++-- Stack/Stack/StackTest.c | 32 ++++++++++++++++---------------- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Stack/Stack/Main.c b/Stack/Stack/Main.c index 6eff2a4..798f891 100644 --- a/Stack/Stack/Main.c +++ b/Stack/Stack/Main.c @@ -5,6 +5,7 @@ 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 index e927cc6..28a4ea1 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -7,7 +7,7 @@ bool isEmpty(Stack* head) return head == NULL; } -void push(Stack** head, int element) +void push(Stack** head, char element) { Stack* newStack = (Stack*)calloc(1, sizeof(Stack)); if (newStack == NULL) diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 1326ac0..c889ca6 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -12,7 +12,7 @@ typedef struct Stack bool isEmpty(Stack* head); // Function for adding an element to the top of the stack -void push(Stack** head, int element); +void push(Stack** head, char element); // Function to remove an element from the top of the stack that returns the value of that element char pop(Stack** head, bool* check); @@ -21,4 +21,4 @@ char pop(Stack** head, bool* check); void deleteStack(Stack** head); // Function that returns the value of an element from the top of the stack -char top(Stack*head, bool* check); \ No newline at end of file +char top(Stack* head, bool* check); \ No newline at end of file diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index bef4be9..ad78993 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -5,33 +5,33 @@ bool pushTest() { Stack* head = NULL; - push(&head, 12); - int firstResult = head->value; - push(&head, 128); - int secondResult = head->value; - return firstResult == 12 && secondResult == 128; + push(&head, '['); + char firstResult = head->value; + push(&head, ')'); + char secondResult = head->value; + return firstResult == '[' && secondResult == ')'; } bool popTest() { bool err = true; Stack* head = NULL; - push(&head, 12); - push(&head, 128); - push(&head, 147); - int firstPopResult = pop(&head, &err); - int firstUpperElement = head -> value; - int secondPopResult = pop(&head, &err); - int secondUpperElement = head->value; - return firstPopResult == 147 && firstUpperElement == 128 && secondPopResult == 128 && secondUpperElement == 12; + push(&head, 'a'); + push(&head, '^'); + push(&head, '1'); + char firstPopResult = pop(&head, &err); + char firstUpperElement = head->value; + char secondPopResult = pop(&head, &err); + char secondUpperElement = head->value; + return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; } bool deleteStackTest() { Stack* head = NULL; - push(&head, 12); - push(&head, 128); - push(&head, 147); + push(&head, 'a'); + push(&head, 'b'); + push(&head, 'c'); deleteStack(&head); return head == NULL; } \ No newline at end of file From 9a45a27126cceea193e4c5ca1b99eff8d37ea91f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 02:00:44 +0500 Subject: [PATCH 15/34] fixed bugs --- Stack/Stack/Stack.c | 6 +++--- Stack/Stack/Stack.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index 28a4ea1..8ef00cc 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -42,11 +42,11 @@ void deleteStack(Stack** head) } } -char top(Stack* head, bool* check) +char top(Stack** head, bool* check) { - if (!isEmpty(head)) + if (!isEmpty(*head)) { - return head->value; + return (*head)->value; } return 0; } diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index c889ca6..78de56a 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -21,4 +21,4 @@ char pop(Stack** head, bool* check); void deleteStack(Stack** head); // Function that returns the value of an element from the top of the stack -char top(Stack* head, bool* check); \ No newline at end of file +char top(Stack** head, bool* check); \ No newline at end of file From ae4b6a9c963aa07571828890a5a809a78afbe04f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 02:05:36 +0500 Subject: [PATCH 16/34] fixed bugs --- Stack/Stack/Stack.c | 9 --------- Stack/Stack/Stack.h | 5 +---- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index e927cc6..b5c8af5 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -41,12 +41,3 @@ void deleteStack(Stack** head) pop(head, &err); } } - -char top(Stack* head, bool* check) -{ - if (!isEmpty(head)) - { - return head->value; - } - return 0; -} diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 1326ac0..db68b9a 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -18,7 +18,4 @@ void push(Stack** head, int element); char pop(Stack** head, bool* check); // Function for deleting all stack elements -void deleteStack(Stack** head); - -// Function that returns the value of an element from the top of the stack -char top(Stack*head, bool* check); \ No newline at end of file +void deleteStack(Stack** head); \ No newline at end of file From e226c084e692ca2f0a0efe33a16eab0ed0289e26 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 18:33:35 +0500 Subject: [PATCH 17/34] fixed test --- Stack/Stack/Stack.c | 2 +- Stack/Stack/Stack.h | 2 +- Stack/Stack/StackTest.c | 32 ++++++++++++++++---------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index b5c8af5..d7b344b 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -7,7 +7,7 @@ bool isEmpty(Stack* head) return head == NULL; } -void push(Stack** head, int element) +void push(Stack** head, char element) { Stack* newStack = (Stack*)calloc(1, sizeof(Stack)); if (newStack == NULL) diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index db68b9a..326a209 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -12,7 +12,7 @@ typedef struct Stack bool isEmpty(Stack* head); // Function for adding an element to the top of the stack -void push(Stack** head, int element); +void push(Stack** head, char element); // Function to remove an element from the top of the stack that returns the value of that element char pop(Stack** head, bool* check); diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index bef4be9..ad78993 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -5,33 +5,33 @@ bool pushTest() { Stack* head = NULL; - push(&head, 12); - int firstResult = head->value; - push(&head, 128); - int secondResult = head->value; - return firstResult == 12 && secondResult == 128; + push(&head, '['); + char firstResult = head->value; + push(&head, ')'); + char secondResult = head->value; + return firstResult == '[' && secondResult == ')'; } bool popTest() { bool err = true; Stack* head = NULL; - push(&head, 12); - push(&head, 128); - push(&head, 147); - int firstPopResult = pop(&head, &err); - int firstUpperElement = head -> value; - int secondPopResult = pop(&head, &err); - int secondUpperElement = head->value; - return firstPopResult == 147 && firstUpperElement == 128 && secondPopResult == 128 && secondUpperElement == 12; + push(&head, 'a'); + push(&head, '^'); + push(&head, '1'); + char firstPopResult = pop(&head, &err); + char firstUpperElement = head->value; + char secondPopResult = pop(&head, &err); + char secondUpperElement = head->value; + return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; } bool deleteStackTest() { Stack* head = NULL; - push(&head, 12); - push(&head, 128); - push(&head, 147); + push(&head, 'a'); + push(&head, 'b'); + push(&head, 'c'); deleteStack(&head); return head == NULL; } \ No newline at end of file From 13aa80488d09660478d8bdd63f8db559fee39d1f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 18:36:23 +0500 Subject: [PATCH 18/34] correction of an error related to the absence of main.c --- .../BalanceBrackets/BalanceBrackets.c | 19 --------------- .../BalanceBrackets/BalanceBrackets.vcxproj | 1 + .../BalanceBrackets.vcxproj.filters | 3 +++ .../BalanceBrackets/BalanceBracketsTest.c | 2 +- BalanceBrackets/BalanceBrackets/Main.c | 23 +++++++++++++++++++ 5 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 BalanceBrackets/BalanceBrackets/Main.c diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index 381030e..8f75463 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -1,7 +1,5 @@ #include "BalanceBrackets.h" #include "../../Stack/Stack/Stack.h" -#include "../../Stack/Stack/StackTest.h" -#include "BalanceBracketsTest.h" #include #include @@ -47,21 +45,4 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) } deleteStack(&head); return false; -} -int main() -{ - if (!balanceBracketsTest() || !popTest() || !pushTest() || !deleteStackTest()) - { - printf("Test failed"); - return -1; - } - char expressionFromParentheses[250] = { '\0' }; - printf("enter the expression in postfix form\n"); - scanf_s("%s", expressionFromParentheses, (unsigned)_countof(expressionFromParentheses)); - if (!checkCorrectOrderBrackets(expressionFromParentheses)) - { - printf("incorrect"); - return 0; - } - printf("correct"); } \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj index 1ef1971..7ed29e0 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj @@ -143,6 +143,7 @@ + diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters index ebcc7f5..ef4a7dc 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters @@ -27,6 +27,9 @@ Исходные файлы + + Исходные файлы + diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c index 130e4d5..648a9d5 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -3,7 +3,7 @@ bool balanceBracketsTest() { - char firstCorrectExpressionFromParentheses[250] = "((15 - x) - (13 + 45) * ( 10 - 16))"; + char firstCorrectExpressionFromParentheses[250] = "((15 - x) - (13 + 45) * ( 1 0 - 1 6))"; char secondCorrectExpressionFromParentheses[250] = "(([(){()}][]{}{}{()}))"; char thirdCorrectExpressionFromParentheses[250] = "([[]({})]({}(())))"; char fourthCorrectExpressionFromParentheses[250] = "(x * x) * ( c - a)"; diff --git a/BalanceBrackets/BalanceBrackets/Main.c b/BalanceBrackets/BalanceBrackets/Main.c new file mode 100644 index 0000000..196cc5e --- /dev/null +++ b/BalanceBrackets/BalanceBrackets/Main.c @@ -0,0 +1,23 @@ +#include "BalanceBrackets.h" +#include "../../Stack/Stack/Stack.h" +#include "../../Stack/Stack/StackTest.h" +#include "BalanceBracketsTest.h" +#include + +int main() +{ + if (!balanceBracketsTest() || !popTest() || !pushTest() || !deleteStackTest()) + { + printf("Test failed"); + return -1; + } + char expressionFromParentheses[250] = { '\0' }; + printf("enter the expression in postfix form\n"); + scanf_s("%[^\n]s", expressionFromParentheses, (unsigned)sizeof(expressionFromParentheses)); + if (!checkCorrectOrderBrackets(expressionFromParentheses)) + { + printf("incorrect"); + return 0; + } + printf("correct"); +} \ No newline at end of file From 954da9f4dcfcdc7aa4a85d7fd33f0e6fd7f496d7 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 18:39:08 +0500 Subject: [PATCH 19/34] correction of defects related to the styleguide --- BalanceBrackets/BalanceBrackets/BalanceBrackets.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index 8f75463..2664747 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -18,20 +18,20 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) else if (expressionFromParentheses[counter] == ')' || expressionFromParentheses[counter] == '}' || expressionFromParentheses[counter] == ']') { - if(isEmpty(head)) + if (isEmpty(head)) { return false; } - char k = pop(&head, &check); + char topOfTheStack = pop(&head, &check); { if (!check) { return false; } } - if ((k == '(' && expressionFromParentheses[counter] != ')') - || (k == '{' && expressionFromParentheses[counter] != '}') - || (k == '[' && expressionFromParentheses[counter] != ']')) + if ((topOfTheStack == '(' && expressionFromParentheses[counter] != ')') + || (topOfTheStack == '{' && expressionFromParentheses[counter] != '}') + || (topOfTheStack == '[' && expressionFromParentheses[counter] != ']')) { return false; } From a121942784d06e3acb154b2ec4400fa9fa893175 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 18:42:01 +0500 Subject: [PATCH 20/34] fixed bugs --- BalanceBrackets/BalanceBrackets/BalanceBrackets.c | 1 - BalanceBrackets/BalanceBrackets/Main.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index 2664747..e061c29 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -1,6 +1,5 @@ #include "BalanceBrackets.h" #include "../../Stack/Stack/Stack.h" -#include #include bool checkCorrectOrderBrackets(char* expressionFromParentheses) diff --git a/BalanceBrackets/BalanceBrackets/Main.c b/BalanceBrackets/BalanceBrackets/Main.c index 196cc5e..2da68c3 100644 --- a/BalanceBrackets/BalanceBrackets/Main.c +++ b/BalanceBrackets/BalanceBrackets/Main.c @@ -12,7 +12,7 @@ int main() return -1; } char expressionFromParentheses[250] = { '\0' }; - printf("enter the expression in postfix form\n"); + printf("enter the expression that you want to check for the correct placement of brackets\n"); scanf_s("%[^\n]s", expressionFromParentheses, (unsigned)sizeof(expressionFromParentheses)); if (!checkCorrectOrderBrackets(expressionFromParentheses)) { From fe152de630b05d2a66970e99baf5e14c7c894f6d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 24 Oct 2021 00:19:19 +0500 Subject: [PATCH 21/34] adding a test for a function --- Stack/Stack/Main.c | 2 +- Stack/Stack/Stack.c | 18 ++++++++++-------- Stack/Stack/Stack.h | 8 ++++---- Stack/Stack/StackTest.c | 17 ++++++++++++++--- Stack/Stack/StackTest.h | 5 ++++- 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/Stack/Stack/Main.c b/Stack/Stack/Main.c index 798f891..970d023 100644 --- a/Stack/Stack/Main.c +++ b/Stack/Stack/Main.c @@ -3,7 +3,7 @@ int main() { - if (!pushTest() || !popTest() || !deleteStackTest()) + if (!pushTest() || !popTest() || !deleteStackTest() || !topTest()) { printf("Test failed"); return -1; diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index 8ef00cc..e0044b0 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -1,6 +1,6 @@ #include "Stack.h" -#include #include +#include bool isEmpty(Stack* head) { @@ -19,34 +19,36 @@ void push(Stack** head, char element) *head = newStack; } -char pop(Stack** head, bool* err) +char pop(Stack** head) { + errno = 0; if (*head != NULL) { - char element = (*head)->value; + const int element = (*head)->value; Stack* temporary = *head; *head = (*head)->next; free(temporary); return element; } - *err = false; + errno = 1; return 0; } void deleteStack(Stack** head) { - bool err = true; while (!isEmpty(*head)) { - pop(head, &err); + pop(head); } } -char top(Stack** head, bool* check) +char top(Stack** head) { + errno = 0; if (!isEmpty(*head)) { return (*head)->value; } + errno = 1; return 0; -} +} \ No newline at end of file diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 78de56a..e6db7b8 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -6,19 +6,19 @@ typedef struct Stack { char value; struct Stack* next; -}Stack; +} Stack; -// Function for test empty 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, char element); // Function to remove an element from the top of the stack that returns the value of that element -char pop(Stack** head, bool* check); +char pop(Stack** head); // Function for deleting all stack elements void deleteStack(Stack** head); // Function that returns the value of an element from the top of the stack -char top(Stack** head, bool* check); \ No newline at end of file +char top(Stack** head); \ No newline at end of file diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index ad78993..9d0ab00 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -14,14 +14,13 @@ bool pushTest() bool popTest() { - bool err = true; Stack* head = NULL; push(&head, 'a'); push(&head, '^'); push(&head, '1'); - char firstPopResult = pop(&head, &err); + char firstPopResult = pop(&head); char firstUpperElement = head->value; - char secondPopResult = pop(&head, &err); + char secondPopResult = pop(&head); char secondUpperElement = head->value; return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; } @@ -34,4 +33,16 @@ bool deleteStackTest() push(&head, 'c'); deleteStack(&head); return head == NULL; +} + +bool topTest() +{ + Stack* head = NULL; + push(&head, 'a'); + char firstTopResult = head->value; + push(&head, '^'); + char secondTopResult = head->value; + push(&head, '1'); + char thirdTopResult = head->value; + return firstTopResult == 'a' && secondTopResult == '^' && thirdTopResult == '1'; } \ No newline at end of file diff --git a/Stack/Stack/StackTest.h b/Stack/Stack/StackTest.h index ca939fc..280c3ce 100644 --- a/Stack/Stack/StackTest.h +++ b/Stack/Stack/StackTest.h @@ -8,4 +8,7 @@ bool pushTest(); bool popTest(); // Function for checking a function that removes all stack elements -bool deleteStackTest(); \ No newline at end of file +bool deleteStackTest(); + +// Function for testing a function that returns the value of an element from the top of the stack +bool topTest(); From 022b69259ae2dbef7011e58e1a6fd80c40ae82fe Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 24 Oct 2021 00:43:47 +0500 Subject: [PATCH 22/34] fixed bugs --- BalanceBrackets/BalanceBrackets/BalanceBrackets.c | 11 +++-------- .../BalanceBrackets/BalanceBrackets.vcxproj | 2 -- .../BalanceBrackets/BalanceBrackets.vcxproj.filters | 6 ------ BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c | 3 +-- BalanceBrackets/BalanceBrackets/Main.c | 7 +++++-- 5 files changed, 9 insertions(+), 20 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index e061c29..e1e0a77 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -1,10 +1,10 @@ #include "BalanceBrackets.h" #include "../../Stack/Stack/Stack.h" #include +#include bool checkCorrectOrderBrackets(char* expressionFromParentheses) { - bool check = true; Stack* head = NULL; int counter = 0; while (expressionFromParentheses[counter] != '\0') @@ -17,13 +17,9 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) else if (expressionFromParentheses[counter] == ')' || expressionFromParentheses[counter] == '}' || expressionFromParentheses[counter] == ']') { - if (isEmpty(head)) + char topOfTheStack = pop(&head); { - return false; - } - char topOfTheStack = pop(&head, &check); - { - if (!check) + if (errno == 1) { return false; } @@ -39,7 +35,6 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) } if (isEmpty(head)) { - deleteStack(&head); return true; } deleteStack(&head); diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj index 7ed29e0..0abc1f9 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj @@ -140,14 +140,12 @@ - - diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters index ef4a7dc..0bff73d 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters @@ -24,9 +24,6 @@ Исходные файлы - - Исходные файлы - Исходные файлы @@ -41,8 +38,5 @@ Файлы заголовков - - Файлы заголовков - \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c index 648a9d5..10e1cf6 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -30,6 +30,5 @@ bool balanceBracketsTest() && !checkCorrectOrderBrackets(fourthIncorrectExpressionFromParentheses) && !checkCorrectOrderBrackets(fifthIncorrectExpressionFromParentheses) && !checkCorrectOrderBrackets(sixthIncorrectExpressionFromParentheses) - && !checkCorrectOrderBrackets(seventhIncorrectExpressionFromParentheses); - + && !checkCorrectOrderBrackets(seventhIncorrectExpressionFromParentheses); } \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/Main.c b/BalanceBrackets/BalanceBrackets/Main.c index 2da68c3..b0abb36 100644 --- a/BalanceBrackets/BalanceBrackets/Main.c +++ b/BalanceBrackets/BalanceBrackets/Main.c @@ -3,10 +3,11 @@ #include "../../Stack/Stack/StackTest.h" #include "BalanceBracketsTest.h" #include +#include int main() { - if (!balanceBracketsTest() || !popTest() || !pushTest() || !deleteStackTest()) + if (!balanceBracketsTest()) { printf("Test failed"); return -1; @@ -14,7 +15,9 @@ int main() 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)); - if (!checkCorrectOrderBrackets(expressionFromParentheses)) + errno = 0; + bool result = checkCorrectOrderBrackets(expressionFromParentheses); + if (!result || errno == 1) { printf("incorrect"); return 0; From 7f6d2c302c21eb40dfc42c5e92727eb72185ca83 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 24 Oct 2021 00:45:26 +0500 Subject: [PATCH 23/34] removed unnecessary .h --- BalanceBrackets/BalanceBrackets/Main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/BalanceBrackets/BalanceBrackets/Main.c b/BalanceBrackets/BalanceBrackets/Main.c index b0abb36..543457a 100644 --- a/BalanceBrackets/BalanceBrackets/Main.c +++ b/BalanceBrackets/BalanceBrackets/Main.c @@ -1,6 +1,5 @@ #include "BalanceBrackets.h" #include "../../Stack/Stack/Stack.h" -#include "../../Stack/Stack/StackTest.h" #include "BalanceBracketsTest.h" #include #include From 7fe9a12be7d9bbcae774b6ce9ca46cb03a6eb8bb Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 17:47:05 +0500 Subject: [PATCH 24/34] the error with the error code has been changed, the tests have been changed --- Stack/Stack/Main.c | 2 +- Stack/Stack/Stack.c | 21 +++++---------------- Stack/Stack/Stack.h | 7 ++----- Stack/Stack/StackTest.c | 27 +++++++++++++-------------- 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/Stack/Stack/Main.c b/Stack/Stack/Main.c index 970d023..798f891 100644 --- a/Stack/Stack/Main.c +++ b/Stack/Stack/Main.c @@ -3,7 +3,7 @@ int main() { - if (!pushTest() || !popTest() || !deleteStackTest() || !topTest()) + if (!pushTest() || !popTest() || !deleteStackTest()) { printf("Test failed"); return -1; diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index c809346..4a0eb4e 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -1,6 +1,5 @@ #include "Stack.h" #include -#include bool isEmpty(Stack* head) { @@ -19,9 +18,9 @@ void push(Stack** head, char element) *head = newStack; } -char pop(Stack** head) +char pop(Stack** head, int* error) { - errno = 0; + *error = 0; if (*head != NULL) { const int element = (*head)->value; @@ -30,25 +29,15 @@ char pop(Stack** head) free(temporary); return element; } - errno = 1; + *error = 1; return 0; } void deleteStack(Stack** head) { + int error = 0; while (!isEmpty(*head)) { - pop(head); + pop(head, &error); } } - -char top(Stack** head) -{ - errno = 0; - if (!isEmpty(*head)) - { - return (*head)->value; - } - errno = 1; - return 0; -} diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index e6db7b8..a06c762 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -15,10 +15,7 @@ bool isEmpty(Stack* head); void push(Stack** head, char element); // Function to remove an element from the top of the stack that returns the value of that element -char pop(Stack** head); +char pop(Stack** head, int* error); // Function for deleting all stack elements -void deleteStack(Stack** head); - -// Function that returns the value of an element from the top of the stack -char top(Stack** head); \ No newline at end of file +void deleteStack(Stack** head); \ No newline at end of file diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index 9d0ab00..0dce2ed 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -9,6 +9,7 @@ bool pushTest() char firstResult = head->value; push(&head, ')'); char secondResult = head->value; + deleteStack(&head); return firstResult == '[' && secondResult == ')'; } @@ -18,10 +19,20 @@ bool popTest() push(&head, 'a'); push(&head, '^'); push(&head, '1'); - char firstPopResult = pop(&head); + int error = 0; + char firstPopResult = pop(&head, &error); + if (error == 1) + { + return false; + } char firstUpperElement = head->value; - char secondPopResult = pop(&head); + char secondPopResult = pop(&head, &error); + if (error == 1) + { + return false; + } char secondUpperElement = head->value; + deleteStack(&head); return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; } @@ -33,16 +44,4 @@ bool deleteStackTest() push(&head, 'c'); deleteStack(&head); return head == NULL; -} - -bool topTest() -{ - Stack* head = NULL; - push(&head, 'a'); - char firstTopResult = head->value; - push(&head, '^'); - char secondTopResult = head->value; - push(&head, '1'); - char thirdTopResult = head->value; - return firstTopResult == 'a' && secondTopResult == '^' && thirdTopResult == '1'; } \ No newline at end of file From ad099c8736aab1b196e0812a5c8e573b0bff8f8a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 18:21:50 +0500 Subject: [PATCH 25/34] fixed bugs related to the error value --- BalanceBrackets/BalanceBrackets/BalanceBrackets.c | 12 ++++++------ BalanceBrackets/BalanceBrackets/BalanceBrackets.h | 2 +- .../BalanceBrackets/BalanceBrackets.vcxproj.filters | 6 +++--- .../BalanceBrackets/BalanceBracketsTest.c | 3 +++ BalanceBrackets/BalanceBrackets/Main.c | 9 +++------ 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index e1e0a77..90465af 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -1,7 +1,6 @@ #include "BalanceBrackets.h" #include "../../Stack/Stack/Stack.h" #include -#include bool checkCorrectOrderBrackets(char* expressionFromParentheses) { @@ -17,16 +16,17 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) else if (expressionFromParentheses[counter] == ')' || expressionFromParentheses[counter] == '}' || expressionFromParentheses[counter] == ']') { - char topOfTheStack = pop(&head); + int error = 0; + char topOfTheStack = pop(&head, &error); { - if (errno == 1) + if (error == 1) { return false; } } - if ((topOfTheStack == '(' && expressionFromParentheses[counter] != ')') - || (topOfTheStack == '{' && expressionFromParentheses[counter] != '}') - || (topOfTheStack == '[' && expressionFromParentheses[counter] != ']')) + if ((topOfTheStack != '(' && expressionFromParentheses[counter] == ')') + || (topOfTheStack != '{' && expressionFromParentheses[counter] == '}') + || (topOfTheStack != '[' && expressionFromParentheses[counter] == ']')) { return false; } diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h index 95b8c78..808976e 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h @@ -2,4 +2,4 @@ #include // Function to check the correct position of the brackets -bool checkCorrectOrderBrackets(char* expressionFromParentheses); \ No newline at end of file +bool checkCorrectOrderBrackets(char* expressionFromParentheses); \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters index 0bff73d..61a5f87 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters @@ -29,14 +29,14 @@ - - Файлы заголовков - Файлы заголовков Файлы заголовков + + Файлы заголовков + \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c index 10e1cf6..c14e35f 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -10,6 +10,7 @@ bool balanceBracketsTest() char fifthCorrectExpressionFromParentheses[250] = "[[]]{{}}(())"; char sixthCorrectExpressionFromParentheses[250] = "(({{{}[]}[]}[]))"; char seventhCorrectExpressionFromParentheses[250] = "{}()[]"; + char firstIncorrectExpressionFromParentheses[250] = "{{"; char secondIncorrectExpressionFromParentheses[250] = "{())[][]}"; char thirdIncorrectExpressionFromParentheses[250] = "(x-a)*(c+a))"; @@ -17,6 +18,7 @@ bool balanceBracketsTest() char fifthIncorrectExpressionFromParentheses[250] = "{()[]{}{{))))))(}"; char sixthIncorrectExpressionFromParentheses[250] = "{{}}((("; char seventhIncorrectExpressionFromParentheses[250] = "("; + return checkCorrectOrderBrackets(firstCorrectExpressionFromParentheses) && checkCorrectOrderBrackets(secondCorrectExpressionFromParentheses) && checkCorrectOrderBrackets(thirdCorrectExpressionFromParentheses) @@ -24,6 +26,7 @@ bool balanceBracketsTest() && checkCorrectOrderBrackets(fifthCorrectExpressionFromParentheses) && checkCorrectOrderBrackets(sixthCorrectExpressionFromParentheses) && checkCorrectOrderBrackets(seventhCorrectExpressionFromParentheses) + && !checkCorrectOrderBrackets(firstIncorrectExpressionFromParentheses) && !checkCorrectOrderBrackets(secondIncorrectExpressionFromParentheses) && !checkCorrectOrderBrackets(thirdIncorrectExpressionFromParentheses) diff --git a/BalanceBrackets/BalanceBrackets/Main.c b/BalanceBrackets/BalanceBrackets/Main.c index 543457a..d63e9f2 100644 --- a/BalanceBrackets/BalanceBrackets/Main.c +++ b/BalanceBrackets/BalanceBrackets/Main.c @@ -2,7 +2,6 @@ #include "../../Stack/Stack/Stack.h" #include "BalanceBracketsTest.h" #include -#include int main() { @@ -14,12 +13,10 @@ int main() 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)); - errno = 0; - bool result = checkCorrectOrderBrackets(expressionFromParentheses); - if (!result || errno == 1) + if (!checkCorrectOrderBrackets(expressionFromParentheses)) { - printf("incorrect"); + printf("The balance of the brackets is incorrect"); return 0; } - printf("correct"); + printf("The balance of the brackets is correct"); } \ No newline at end of file From f6c2352e106c93af043e351a265297500783485a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 18:26:48 +0500 Subject: [PATCH 26/34] changing the code according to the style guide --- BalanceBrackets/BalanceBrackets/BalanceBrackets.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index 90465af..59b8fa2 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -17,13 +17,11 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) || expressionFromParentheses[counter] == ']') { int error = 0; - char topOfTheStack = pop(&head, &error); - { - if (error == 1) - { - return false; - } - } + char topOfTheStack = pop(&head, &error) + if (error == 1) + { + return false; + } if ((topOfTheStack != '(' && expressionFromParentheses[counter] == ')') || (topOfTheStack != '{' && expressionFromParentheses[counter] == '}') || (topOfTheStack != '[' && expressionFromParentheses[counter] == ']')) From 6c04a727a58b76e35a547b8fc120e6c6e25d93d5 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 18:51:24 +0500 Subject: [PATCH 27/34] adding const --- Stack/Stack/StackTest.c | 12 ++++++------ Stack/Stack/StackTest.h | 3 --- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index 0dce2ed..c2f2536 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -6,9 +6,9 @@ bool pushTest() { Stack* head = NULL; push(&head, '['); - char firstResult = head->value; + const char firstResult = head->value; push(&head, ')'); - char secondResult = head->value; + const char secondResult = head->value; deleteStack(&head); return firstResult == '[' && secondResult == ')'; } @@ -20,18 +20,18 @@ bool popTest() push(&head, '^'); push(&head, '1'); int error = 0; - char firstPopResult = pop(&head, &error); + const char firstPopResult = pop(&head, &error); if (error == 1) { return false; } - char firstUpperElement = head->value; - char secondPopResult = pop(&head, &error); + const char firstUpperElement = head->value; + const char secondPopResult = pop(&head, &error); if (error == 1) { return false; } - char secondUpperElement = head->value; + const char secondUpperElement = head->value; deleteStack(&head); return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; } diff --git a/Stack/Stack/StackTest.h b/Stack/Stack/StackTest.h index 280c3ce..edfce8b 100644 --- a/Stack/Stack/StackTest.h +++ b/Stack/Stack/StackTest.h @@ -9,6 +9,3 @@ bool popTest(); // Function for checking a function that removes all stack elements bool deleteStackTest(); - -// Function for testing a function that returns the value of an element from the top of the stack -bool topTest(); From 5f1e13dc94217927d6e4ca723a45e8fd29577425 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 18:54:27 +0500 Subject: [PATCH 28/34] adding const --- .../BalanceBrackets/BalanceBrackets.c | 10 +++---- .../BalanceBrackets/BalanceBrackets.h | 2 +- .../BalanceBrackets/BalanceBracketsTest.c | 28 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index 59b8fa2..4f1e991 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -2,7 +2,7 @@ #include "../../Stack/Stack/Stack.h" #include -bool checkCorrectOrderBrackets(char* expressionFromParentheses) +bool checkCorrectOrderBrackets(const char* expressionFromParentheses) { Stack* head = NULL; int counter = 0; @@ -17,11 +17,11 @@ bool checkCorrectOrderBrackets(char* expressionFromParentheses) || expressionFromParentheses[counter] == ']') { int error = 0; - char topOfTheStack = pop(&head, &error) - if (error == 1) - { + char topOfTheStack = pop(&head, &error); + if (error == 1) + { return false; - } + } if ((topOfTheStack != '(' && expressionFromParentheses[counter] == ')') || (topOfTheStack != '{' && expressionFromParentheses[counter] == '}') || (topOfTheStack != '[' && expressionFromParentheses[counter] == ']')) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h index 808976e..0acf07c 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h @@ -2,4 +2,4 @@ #include // Function to check the correct position of the brackets -bool checkCorrectOrderBrackets(char* expressionFromParentheses); \ No newline at end of file +bool checkCorrectOrderBrackets(const char* expressionFromParentheses); \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c index c14e35f..4f1cfe1 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -3,21 +3,21 @@ bool balanceBracketsTest() { - char firstCorrectExpressionFromParentheses[250] = "((15 - x) - (13 + 45) * ( 1 0 - 1 6))"; - char secondCorrectExpressionFromParentheses[250] = "(([(){()}][]{}{}{()}))"; - char thirdCorrectExpressionFromParentheses[250] = "([[]({})]({}(())))"; - char fourthCorrectExpressionFromParentheses[250] = "(x * x) * ( c - a)"; - char fifthCorrectExpressionFromParentheses[250] = "[[]]{{}}(())"; - char sixthCorrectExpressionFromParentheses[250] = "(({{{}[]}[]}[]))"; - char seventhCorrectExpressionFromParentheses[250] = "{}()[]"; + const char firstCorrectExpressionFromParentheses[250] = "((15 - x) - (13 + 45) * ( 1 0 - 1 6))"; + const char secondCorrectExpressionFromParentheses[250] = "(([(){()}][]{}{}{()}))"; + const char thirdCorrectExpressionFromParentheses[250] = "([[]({})]({}(())))"; + const char fourthCorrectExpressionFromParentheses[250] = "(x * x) * ( c - a)"; + const char fifthCorrectExpressionFromParentheses[250] = "[[]]{{}}(())"; + const char sixthCorrectExpressionFromParentheses[250] = "(({{{}[]}[]}[]))"; + const char seventhCorrectExpressionFromParentheses[250] = "{}()[]"; - char firstIncorrectExpressionFromParentheses[250] = "{{"; - char secondIncorrectExpressionFromParentheses[250] = "{())[][]}"; - char thirdIncorrectExpressionFromParentheses[250] = "(x-a)*(c+a))"; - char fourthIncorrectExpressionFromParentheses[250] = "(]])()()[][]{}{{}}"; - char fifthIncorrectExpressionFromParentheses[250] = "{()[]{}{{))))))(}"; - char sixthIncorrectExpressionFromParentheses[250] = "{{}}((("; - char seventhIncorrectExpressionFromParentheses[250] = "("; + const char firstIncorrectExpressionFromParentheses[250] = "{{"; + const char secondIncorrectExpressionFromParentheses[250] = "{())[][]}"; + const char thirdIncorrectExpressionFromParentheses[250] = "(x-a)*(c+a))"; + const char fourthIncorrectExpressionFromParentheses[250] = "(]])()()[][]{}{{}}"; + const char fifthIncorrectExpressionFromParentheses[250] = "{()[]{}{{))))))(}"; + const char sixthIncorrectExpressionFromParentheses[250] = "{{}}((("; + const char seventhIncorrectExpressionFromParentheses[250] = "("; return checkCorrectOrderBrackets(firstCorrectExpressionFromParentheses) && checkCorrectOrderBrackets(secondCorrectExpressionFromParentheses) From 4fee3128ff33bc7fd3bf178e213189a3d97ad2da Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 19:29:42 +0500 Subject: [PATCH 29/34] changing tests, function arguments --- Stack/Stack/Stack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index 4a0eb4e..9e0fafc 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -23,7 +23,7 @@ char pop(Stack** head, int* error) *error = 0; if (*head != NULL) { - const int element = (*head)->value; + const char element = (*head)->value; Stack* temporary = *head; *head = (*head)->next; free(temporary); From cb50cd233406fc40f791c473a1dd295156ef7e2d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 26 Oct 2021 04:42:00 +0500 Subject: [PATCH 30/34] Added a check for push() --- Stack/Stack/Stack.c | 11 ++++++-- Stack/Stack/Stack.h | 13 +++++---- Stack/Stack/StackTest.c | 62 +++++++++++++++++++++++++++++++---------- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index 9e0fafc..f21363d 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -6,11 +6,18 @@ bool isEmpty(Stack* head) return head == NULL; } -void push(Stack** head, char element) +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; @@ -18,7 +25,7 @@ void push(Stack** head, char element) *head = newStack; } -char pop(Stack** head, int* error) +int pop(Stack** head, int* error) { *error = 0; if (*head != NULL) diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index a06c762..46f0491 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -4,18 +4,21 @@ // Structure for implementing a stack consisting of a value and a pointer to the next element typedef struct Stack { - char value; + int value; struct Stack* next; } Stack; -// Function for testing if stack is empty +// 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, char element); +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 -char pop(Stack** head, int* error); +int pop(Stack** head, int* error); // Function for deleting all stack elements -void deleteStack(Stack** head); \ No newline at end of file +void deleteStack(Stack** head); + +// Creating a stack +Stack* createStack(); \ No newline at end of file diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index c2f2536..e6a1cc0 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -5,10 +5,19 @@ bool pushTest() { Stack* head = NULL; - push(&head, '['); - const char firstResult = head->value; - push(&head, ')'); - const char secondResult = head->value; + 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 == ')'; } @@ -16,22 +25,34 @@ bool pushTest() bool popTest() { Stack* head = NULL; - push(&head, 'a'); - push(&head, '^'); - push(&head, '1'); int error = 0; - const char firstPopResult = pop(&head, &error); + 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 char firstUpperElement = head->value; - const char secondPopResult = pop(&head, &error); + const int firstUpperElement = head->value; + const int secondPopResult = pop(&head, &error); if (error == 1) { return false; } - const char secondUpperElement = head->value; + const int secondUpperElement = head->value; deleteStack(&head); return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; } @@ -39,9 +60,22 @@ bool popTest() bool deleteStackTest() { Stack* head = NULL; - push(&head, 'a'); - push(&head, 'b'); - push(&head, 'c'); + 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 From 8e7b5a15d3e1ce3b91248db94580cbf9802c2125 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 26 Oct 2021 04:42:27 +0500 Subject: [PATCH 31/34] changing functions, tests --- .../BalanceBrackets/BalanceBrackets.c | 45 +++++++++++++----- .../BalanceBrackets/BalanceBrackets.h | 2 +- .../BalanceBrackets/BalanceBracketsTest.c | 46 ++++++------------- BalanceBrackets/BalanceBrackets/Main.c | 15 +++++- 4 files changed, 62 insertions(+), 46 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index 4f1e991..c326fd6 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -1,31 +1,51 @@ #include "BalanceBrackets.h" #include "../../Stack/Stack/Stack.h" -#include -bool checkCorrectOrderBrackets(const char* expressionFromParentheses) +bool isOpeningBracket(char parentheses) { - Stack* head = NULL; + 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 (expressionFromParentheses[counter] == '(' || expressionFromParentheses[counter] == '{' - || expressionFromParentheses[counter] == '[') + if (isOpeningBracket(expressionFromParentheses[counter])) { - push(&head, expressionFromParentheses[counter]); + int error = 0; + push(&head, expressionFromParentheses[counter], &error); + if (error == 2) + { + deleteStack(&head); + return false; + } } - else if (expressionFromParentheses[counter] == ')' || expressionFromParentheses[counter] == '}' - || expressionFromParentheses[counter] == ']') + else if (isClosingBracket(expressionFromParentheses[counter])) { int error = 0; - char topOfTheStack = pop(&head, &error); + char topOfTheStack = (char)pop(&head, &error); if (error == 1) { + deleteStack(&head); return false; } - if ((topOfTheStack != '(' && expressionFromParentheses[counter] == ')') - || (topOfTheStack != '{' && expressionFromParentheses[counter] == '}') - || (topOfTheStack != '[' && expressionFromParentheses[counter] == ']')) + if (openingAndClosingOfTheSameType(topOfTheStack, expressionFromParentheses[counter])) { + deleteStack(&head); return false; } } @@ -33,6 +53,7 @@ bool checkCorrectOrderBrackets(const char* expressionFromParentheses) } if (isEmpty(head)) { + deleteStack(&head); return true; } deleteStack(&head); diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h index 0acf07c..bb6e3be 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h @@ -2,4 +2,4 @@ #include // Function to check the correct position of the brackets -bool checkCorrectOrderBrackets(const char* expressionFromParentheses); \ No newline at end of file +bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* errorCode); \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c index 4f1cfe1..07ad7b8 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -3,35 +3,19 @@ bool balanceBracketsTest() { - const char firstCorrectExpressionFromParentheses[250] = "((15 - x) - (13 + 45) * ( 1 0 - 1 6))"; - const char secondCorrectExpressionFromParentheses[250] = "(([(){()}][]{}{}{()}))"; - const char thirdCorrectExpressionFromParentheses[250] = "([[]({})]({}(())))"; - const char fourthCorrectExpressionFromParentheses[250] = "(x * x) * ( c - a)"; - const char fifthCorrectExpressionFromParentheses[250] = "[[]]{{}}(())"; - const char sixthCorrectExpressionFromParentheses[250] = "(({{{}[]}[]}[]))"; - const char seventhCorrectExpressionFromParentheses[250] = "{}()[]"; - - const char firstIncorrectExpressionFromParentheses[250] = "{{"; - const char secondIncorrectExpressionFromParentheses[250] = "{())[][]}"; - const char thirdIncorrectExpressionFromParentheses[250] = "(x-a)*(c+a))"; - const char fourthIncorrectExpressionFromParentheses[250] = "(]])()()[][]{}{{}}"; - const char fifthIncorrectExpressionFromParentheses[250] = "{()[]{}{{))))))(}"; - const char sixthIncorrectExpressionFromParentheses[250] = "{{}}((("; - const char seventhIncorrectExpressionFromParentheses[250] = "("; - - return checkCorrectOrderBrackets(firstCorrectExpressionFromParentheses) - && checkCorrectOrderBrackets(secondCorrectExpressionFromParentheses) - && checkCorrectOrderBrackets(thirdCorrectExpressionFromParentheses) - && checkCorrectOrderBrackets(fourthCorrectExpressionFromParentheses) - && checkCorrectOrderBrackets(fifthCorrectExpressionFromParentheses) - && checkCorrectOrderBrackets(sixthCorrectExpressionFromParentheses) - && checkCorrectOrderBrackets(seventhCorrectExpressionFromParentheses) - - && !checkCorrectOrderBrackets(firstIncorrectExpressionFromParentheses) - && !checkCorrectOrderBrackets(secondIncorrectExpressionFromParentheses) - && !checkCorrectOrderBrackets(thirdIncorrectExpressionFromParentheses) - && !checkCorrectOrderBrackets(fourthIncorrectExpressionFromParentheses) - && !checkCorrectOrderBrackets(fifthIncorrectExpressionFromParentheses) - && !checkCorrectOrderBrackets(sixthIncorrectExpressionFromParentheses) - && !checkCorrectOrderBrackets(seventhIncorrectExpressionFromParentheses); + 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]); } \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/Main.c b/BalanceBrackets/BalanceBrackets/Main.c index d63e9f2..9e31e63 100644 --- a/BalanceBrackets/BalanceBrackets/Main.c +++ b/BalanceBrackets/BalanceBrackets/Main.c @@ -1,5 +1,4 @@ #include "BalanceBrackets.h" -#include "../../Stack/Stack/Stack.h" #include "BalanceBracketsTest.h" #include @@ -13,7 +12,19 @@ int main() 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)); - if (!checkCorrectOrderBrackets(expressionFromParentheses)) + int errorCode = 0; + bool result = checkCorrectOrderBrackets(expressionFromParentheses, &errorCode); + if (errorCode == 1) + { + printf("The balance of the brackets is incorrect"); + return 0; + } + if (errorCode == 2) + { + printf("Insufficient memory"); + return -1; + } + if(!result) { printf("The balance of the brackets is incorrect"); return 0; From f50a25c7e656d309ee0ca37b2cc53f0fee80135f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 26 Oct 2021 04:59:09 +0500 Subject: [PATCH 32/34] fixed errors in the main function --- .../BalanceBrackets/BalanceBrackets.c | 5 +++++ .../BalanceBrackets/BalanceBracketsTest.c | 17 ++++++++++++++++- BalanceBrackets/BalanceBrackets/Main.c | 7 +------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index c326fd6..3d4cb27 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -30,6 +30,7 @@ bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* error push(&head, expressionFromParentheses[counter], &error); if (error == 2) { + *errorCode = 2; deleteStack(&head); return false; } @@ -40,11 +41,13 @@ bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* error 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; } @@ -53,9 +56,11 @@ bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* error } if (isEmpty(head)) { + *errorCode = 0; deleteStack(&head); return true; } + *errorCode = 1; deleteStack(&head); return false; } \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c index 07ad7b8..a04a712 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -17,5 +17,20 @@ bool balanceBracketsTest() && !checkCorrectOrderBrackets("(]])()()[][]{}{{}}", &errors[10]) && !checkCorrectOrderBrackets("{()[]{}{{))))))(}", &errors[11]) && !checkCorrectOrderBrackets("{{}}(((", &errors[12]) - && !checkCorrectOrderBrackets("(", &errors[13]); + && !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/Main.c b/BalanceBrackets/BalanceBrackets/Main.c index 9e31e63..dac54f3 100644 --- a/BalanceBrackets/BalanceBrackets/Main.c +++ b/BalanceBrackets/BalanceBrackets/Main.c @@ -13,7 +13,7 @@ int main() 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; - bool result = checkCorrectOrderBrackets(expressionFromParentheses, &errorCode); + checkCorrectOrderBrackets(expressionFromParentheses, &errorCode); if (errorCode == 1) { printf("The balance of the brackets is incorrect"); @@ -24,10 +24,5 @@ int main() printf("Insufficient memory"); return -1; } - if(!result) - { - printf("The balance of the brackets is incorrect"); - return 0; - } printf("The balance of the brackets is correct"); } \ No newline at end of file From bcf969b47d21e998dfb1efc50871592e2086b647 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 26 Oct 2021 05:02:57 +0500 Subject: [PATCH 33/34] delete space --- BalanceBrackets/BalanceBrackets/BalanceBrackets.c | 1 - BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c | 1 - 2 files changed, 2 deletions(-) diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c index 3d4cb27..0ea5367 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBrackets.c @@ -56,7 +56,6 @@ bool checkCorrectOrderBrackets(const char* expressionFromParentheses, int* error } if (isEmpty(head)) { - *errorCode = 0; deleteStack(&head); return true; } diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c index a04a712..eb1df90 100644 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c +++ b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c @@ -32,5 +32,4 @@ bool balanceBracketsTest() && errors[11] == 1 && errors[12] == 1 && errors[13] == 1; - } \ No newline at end of file From 12dbf94c5b439185c3a6c9b109cbbae7ce922c5f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 27 Oct 2021 01:05:00 +0500 Subject: [PATCH 34/34] fixed pop() --- Stack/Stack/Stack.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index f21363d..692595d 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -28,16 +28,16 @@ void push(Stack** head, int element, int* error) int pop(Stack** head, int* error) { *error = 0; - if (*head != NULL) + if (*head == NULL) { - const char element = (*head)->value; - Stack* temporary = *head; - *head = (*head)->next; - free(temporary); - return element; + *error = 1; + return 0; } - *error = 1; - return 0; + const int element = (*head)->value; + Stack* temporary = *head; + *head = (*head)->next; + free(temporary); + return element; } void deleteStack(Stack** head)