From 1a83386b8b8d3005f1011850f7794cb55cbdf78a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 15 Oct 2021 21:09:51 +0500 Subject: [PATCH 01/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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/39] 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 400b92a6edf9d55085c64bfabf5637139f9ddec2 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 00:51:48 +0500 Subject: [PATCH 13/39] start --- sortStation/sortStation.sln | 31 ++++ sortStation/sortStation/SortStation.c | 31 ++++ sortStation/sortStation/SortingStation.h | 1 + sortStation/sortStation/sortStation.vcxproj | 152 ++++++++++++++++++ .../sortStation/sortStation.vcxproj.filters | 33 ++++ 5 files changed, 248 insertions(+) create mode 100644 sortStation/sortStation.sln create mode 100644 sortStation/sortStation/SortStation.c create mode 100644 sortStation/sortStation/SortingStation.h create mode 100644 sortStation/sortStation/sortStation.vcxproj create mode 100644 sortStation/sortStation/sortStation.vcxproj.filters diff --git a/sortStation/sortStation.sln b/sortStation/sortStation.sln new file mode 100644 index 0000000..5082e95 --- /dev/null +++ b/sortStation/sortStation.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}") = "sortStation", "sortStation\sortStation.vcxproj", "{EA285019-A4A5-4E08-A502-6288146E2213}" +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 + {EA285019-A4A5-4E08-A502-6288146E2213}.Debug|x64.ActiveCfg = Debug|x64 + {EA285019-A4A5-4E08-A502-6288146E2213}.Debug|x64.Build.0 = Debug|x64 + {EA285019-A4A5-4E08-A502-6288146E2213}.Debug|x86.ActiveCfg = Debug|Win32 + {EA285019-A4A5-4E08-A502-6288146E2213}.Debug|x86.Build.0 = Debug|Win32 + {EA285019-A4A5-4E08-A502-6288146E2213}.Release|x64.ActiveCfg = Release|x64 + {EA285019-A4A5-4E08-A502-6288146E2213}.Release|x64.Build.0 = Release|x64 + {EA285019-A4A5-4E08-A502-6288146E2213}.Release|x86.ActiveCfg = Release|Win32 + {EA285019-A4A5-4E08-A502-6288146E2213}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0AEDE0AE-D1E1-4E26-B975-247E2B049FAD} + EndGlobalSection +EndGlobal diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c new file mode 100644 index 0000000..c17d632 --- /dev/null +++ b/sortStation/sortStation/SortStation.c @@ -0,0 +1,31 @@ +#include "../../Stack/Stack/Stack.h" +#include "../../Stack/Stack/StackTest.h" +#include +#include + +char* sortStation(char* array) +{ + Stack* head = NULL; + int counterForTheOutputArray = 0; + int counter = 0; + char* arrayToOutput = (char*)malloc(1000*sizeof(char)); + while (array[counter] != '\0') + { + if (array[counter] >= '0' || array[counter] <= '9') + { + arrayToOutput[counterForTheOutputArray] = array[counter]; + counterForTheOutputArray; + } + if(array[counter] == '-' || array[counter] == '+') + + } +} + +int main() +{ + char array[1000] = {'\0'}; + printf("aa\n"); + scanf("%s", array); + sortStation(array); + +} \ No newline at end of file diff --git a/sortStation/sortStation/SortingStation.h b/sortStation/sortStation/SortingStation.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/sortStation/sortStation/SortingStation.h @@ -0,0 +1 @@ +#pragma once diff --git a/sortStation/sortStation/sortStation.vcxproj b/sortStation/sortStation/sortStation.vcxproj new file mode 100644 index 0000000..8173bc5 --- /dev/null +++ b/sortStation/sortStation/sortStation.vcxproj @@ -0,0 +1,152 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {ea285019-a4a5-4e08-a502-6288146e2213} + sortStation + 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/sortStation/sortStation/sortStation.vcxproj.filters b/sortStation/sortStation/sortStation.vcxproj.filters new file mode 100644 index 0000000..217d4ff --- /dev/null +++ b/sortStation/sortStation/sortStation.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file From b720cdc45696703ca2a2942e2dc120a7672cec6d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 01:05:52 +0500 Subject: [PATCH 14/39] 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 3bfe82ab479f1e70ff665efc1f5fd7d6fbfe9b39 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 01:10:44 +0500 Subject: [PATCH 15/39] the beginning of writing the program --- sortStation/sortStation/SortingStation.h | 1 - sortStation/sortStation/sortStation.vcxproj | 4 ---- sortStation/sortStation/sortStation.vcxproj.filters | 8 -------- 3 files changed, 13 deletions(-) delete mode 100644 sortStation/sortStation/SortingStation.h diff --git a/sortStation/sortStation/SortingStation.h b/sortStation/sortStation/SortingStation.h deleted file mode 100644 index 6f70f09..0000000 --- a/sortStation/sortStation/SortingStation.h +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/sortStation/sortStation/sortStation.vcxproj b/sortStation/sortStation/sortStation.vcxproj index 8173bc5..c75652d 100644 --- a/sortStation/sortStation/sortStation.vcxproj +++ b/sortStation/sortStation/sortStation.vcxproj @@ -142,10 +142,6 @@ - - - - diff --git a/sortStation/sortStation/sortStation.vcxproj.filters b/sortStation/sortStation/sortStation.vcxproj.filters index 217d4ff..88e36bb 100644 --- a/sortStation/sortStation/sortStation.vcxproj.filters +++ b/sortStation/sortStation/sortStation.vcxproj.filters @@ -22,12 +22,4 @@ Исходные файлы - - - Файлы заголовков - - - Файлы заголовков - - \ No newline at end of file From 41ce91279ac40472d6b4beec784feec48a9a09fd Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 01:28:23 +0500 Subject: [PATCH 16/39] part of the function is written --- sortStation/sortStation/SortStation.c | 18 +++++++++++++++--- sortStation/sortStation/sortStation.vcxproj | 3 +++ .../sortStation/sortStation.vcxproj.filters | 5 +++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index c17d632..ff234b5 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -6,6 +6,7 @@ char* sortStation(char* array) { Stack* head = NULL; + bool check = true; int counterForTheOutputArray = 0; int counter = 0; char* arrayToOutput = (char*)malloc(1000*sizeof(char)); @@ -14,10 +15,21 @@ char* sortStation(char* array) if (array[counter] >= '0' || array[counter] <= '9') { arrayToOutput[counterForTheOutputArray] = array[counter]; - counterForTheOutputArray; + counterForTheOutputArray++; } - if(array[counter] == '-' || array[counter] == '+') - + if (array[counter] == '-' || array[counter] == '+') + { + while (!isEmpty(head) && (array[counter] != '*' || array[counter] != '/')) + { + arrayToOutput[counterForTheOutputArray] = pop(&head, &check); + counterForTheOutputArray++; + } + push(&head, array[counter]); + } + } + if (array[counter] == '(') + { + push(&head, array[counter]) } } diff --git a/sortStation/sortStation/sortStation.vcxproj b/sortStation/sortStation/sortStation.vcxproj index c75652d..acfd780 100644 --- a/sortStation/sortStation/sortStation.vcxproj +++ b/sortStation/sortStation/sortStation.vcxproj @@ -142,6 +142,9 @@ + + + diff --git a/sortStation/sortStation/sortStation.vcxproj.filters b/sortStation/sortStation/sortStation.vcxproj.filters index 88e36bb..aa5034c 100644 --- a/sortStation/sortStation/sortStation.vcxproj.filters +++ b/sortStation/sortStation/sortStation.vcxproj.filters @@ -22,4 +22,9 @@ Исходные файлы + + + Файлы ресурсов + + \ 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 17/39] 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 b49b752a93a214798d1e3162a6d4532073696725 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 01:58:27 +0500 Subject: [PATCH 18/39] writing a function --- sortStation/sortStation/SortStation.c | 30 ++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index ff234b5..f4e6235 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -19,17 +19,41 @@ char* sortStation(char* array) } if (array[counter] == '-' || array[counter] == '+') { - while (!isEmpty(head) && (array[counter] != '*' || array[counter] != '/')) + while (!isEmpty(head) && (top(&head, &check) != '*' || top(&head, &check) != '/')) { arrayToOutput[counterForTheOutputArray] = pop(&head, &check); counterForTheOutputArray++; } push(&head, array[counter]); } + if (array[counter] == '(') + { + push(&head, array[counter]); + } + if (array[counter] == ')') + { + while ((!isEmpty) && top(&head, check) != ')') + { + arrayToOutput[counterForTheOutputArray] = pop(&head, &check); + counterForTheOutputArray; + if (isEmpty(head)) + { + check = false; + return NULL; + } + } + pop(&head, &check); + } } - if (array[counter] == '(') + while (!isEmpty(head)) { - push(&head, array[counter]) + if(top(&head, &check) == ')') + { + check = false; + return NULL; + } + arrayToOutput[counterForTheOutputArray] = pop(&head, &check); + counterForTheOutputArray++; } } From 9a45a27126cceea193e4c5ca1b99eff8d37ea91f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 02:00:44 +0500 Subject: [PATCH 19/39] 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 20/39] 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 b04b39115650b5a80e816bc3ca60df76d7eb4fb4 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 18:14:01 +0500 Subject: [PATCH 21/39] correction of some errors --- sortStation/sortStation/SortStation.c | 89 ++++++++++++++++----- sortStation/sortStation/sortStation.vcxproj | 2 +- 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index f4e6235..e392707 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -3,65 +3,112 @@ #include #include -char* sortStation(char* array) +char* sortStation(char* array, bool* check) { + bool err = *check; Stack* head = NULL; - bool check = true; int counterForTheOutputArray = 0; int counter = 0; - char* arrayToOutput = (char*)malloc(1000*sizeof(char)); + char* arrayToOutput = (char*)malloc(1000 * sizeof(char)); + if (arrayToOutput == NULL) + { + return NULL; + } while (array[counter] != '\0') { - if (array[counter] >= '0' || array[counter] <= '9') + + if (array[counter] >= '0' && array[counter] <= '9') { arrayToOutput[counterForTheOutputArray] = array[counter]; counterForTheOutputArray++; } - if (array[counter] == '-' || array[counter] == '+') + else if (array[counter] == ' ') { - while (!isEmpty(head) && (top(&head, &check) != '*' || top(&head, &check) != '/')) + counter++; + continue; + } + else if (array[counter] == '-' || array[counter] == '+') + { + while (!isEmpty(head) && top(&head, &err) == '*' || top(&head, &err) == '/') { - arrayToOutput[counterForTheOutputArray] = pop(&head, &check); + arrayToOutput[counterForTheOutputArray] = pop(&head, &err); counterForTheOutputArray++; } push(&head, array[counter]); - } - if (array[counter] == '(') + } + else if (array[counter] == '/' || array[counter] == '*') { + while (!isEmpty(head) && top(&head, &err) == '*' || top(&head, &err) == '/') + { + arrayToOutput[counterForTheOutputArray] = pop(&head, &err); + counterForTheOutputArray++; + } push(&head, array[counter]); } - if (array[counter] == ')') + else if (array[counter] == '(') { - while ((!isEmpty) && top(&head, check) != ')') + push(&head, array[counter]); + } + else if (array[counter] == ')') + { + while ((!isEmpty(head)) && top(&head, &err) != '(') { - arrayToOutput[counterForTheOutputArray] = pop(&head, &check); - counterForTheOutputArray; - if (isEmpty(head)) + arrayToOutput[counterForTheOutputArray] = pop(&head, &err); + counterForTheOutputArray++; + if (!err) { - check = false; + *check = false; return NULL; } } - pop(&head, &check); + if ((!isEmpty(head)) && top(&head, &err) == '(') + { + pop(&head, &err); + } + else + { + *check = false; + return NULL; + } + } + else + { + *check = false; + return NULL; } + counter++; } while (!isEmpty(head)) { - if(top(&head, &check) == ')') + if (top(&head, &err) == '(') { - check = false; + *check = false; return NULL; } - arrayToOutput[counterForTheOutputArray] = pop(&head, &check); + arrayToOutput[counterForTheOutputArray] = pop(&head, &err); counterForTheOutputArray++; } + arrayToOutput[counterForTheOutputArray] = '\0'; + return arrayToOutput; } int main() { char array[1000] = {'\0'}; printf("aa\n"); + bool check = true; scanf("%s", array); - sortStation(array); - + char* arrayToOutput = sortStation(array, &check); + { + if (!check) + { + return -1; + } + } + int counter = 0; + while (arrayToOutput[counter] != '\0') + { + printf("%c ", arrayToOutput[counter]); + counter++; + } } \ No newline at end of file diff --git a/sortStation/sortStation/sortStation.vcxproj b/sortStation/sortStation/sortStation.vcxproj index acfd780..c18f05f 100644 --- a/sortStation/sortStation/sortStation.vcxproj +++ b/sortStation/sortStation/sortStation.vcxproj @@ -86,7 +86,7 @@ Level3 true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CRT_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions) true From e226c084e692ca2f0a0efe33a16eab0ed0289e26 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 23 Oct 2021 18:33:35 +0500 Subject: [PATCH 22/39] 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 23/39] 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 24/39] 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 25/39] 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 26/39] 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 65351568c7a462d5330e2be9e56e01402f7eb047 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 24 Oct 2021 04:22:11 +0500 Subject: [PATCH 27/39] adding test, function and comments --- sortStation/sortStation/Main.c | 40 ++++++++ sortStation/sortStation/SortStantion.h | 4 + sortStation/sortStation/SortStantionTest.c | 18 ++++ sortStation/sortStation/SortStantionTest.h | 4 + sortStation/sortStation/SortStation.c | 92 ++++++++----------- sortStation/sortStation/sortStation.vcxproj | 4 + .../sortStation/sortStation.vcxproj.filters | 12 +++ 7 files changed, 120 insertions(+), 54 deletions(-) create mode 100644 sortStation/sortStation/Main.c create mode 100644 sortStation/sortStation/SortStantion.h create mode 100644 sortStation/sortStation/SortStantionTest.c create mode 100644 sortStation/sortStation/SortStantionTest.h diff --git a/sortStation/sortStation/Main.c b/sortStation/sortStation/Main.c new file mode 100644 index 0000000..344a998 --- /dev/null +++ b/sortStation/sortStation/Main.c @@ -0,0 +1,40 @@ +#include "../../Stack/Stack/Stack.h" +#include "SortStantion.h" +#include "SortStantionTest.h" +#include +#include + +int main() +{ + if (!translationIntoPostfixFormAreTestPassing()) + { + printf("Test failed"); + return -1; + } + char array[1000] = { '\0' }; + printf("enter the expression in the infix form\n"); + scanf_s("%[^\n]s", array, (unsigned)sizeof(array)); + errno = 0; + char* arrayToOutput = translationIntoPostfixForm(array); + if (errno == 1) + { + printf("Stack is empty(Incorrect expression input)"); + return -1; + } + if (errno == 2) + { + printf("Memory not allocated"); + return -1; + } + if (errno == 3) + { + printf("The parenthesis is missing in the expression"); + return -1; + } + int counter = 0; + while (arrayToOutput[counter] != '\0') + { + printf("%c ", arrayToOutput[counter]); + counter++; + } +} \ No newline at end of file diff --git a/sortStation/sortStation/SortStantion.h b/sortStation/sortStation/SortStantion.h new file mode 100644 index 0000000..c4feb2e --- /dev/null +++ b/sortStation/sortStation/SortStantion.h @@ -0,0 +1,4 @@ +#pragma once + +// Function for translating an expression from infix to postfix form +char* translationIntoPostfixForm(char* array); diff --git a/sortStation/sortStation/SortStantionTest.c b/sortStation/sortStation/SortStantionTest.c new file mode 100644 index 0000000..5368fd7 --- /dev/null +++ b/sortStation/sortStation/SortStantionTest.c @@ -0,0 +1,18 @@ +#include "SortStantionTest.h" +#include "SortStantion.h" +#include +#include + +bool translationIntoPostfixFormAreTestPassing() +{ + char firstCorrectExpression[250] = "( 7 - 6 ) * ( 7 - ( 6 - 4 ) * 2 )"; + char secondCorrectExpression[250] = "6 * ( 8 - 4 ) / 2 + 4"; + char thirdCorrectExpression[250] = " 3 - 4 * ( 5 + 5 ) / 2 + ( 7 - ( 4 - 6 ))"; + char fourthCorrectExpression[250] = "( 5 + 7 ) / ( 4 - 2 ) - 6"; + char fifthCorrectExpression[250] = "6 - 4 * ( 5 + 4 )"; + return strcmp("76-764-2*-*", translationIntoPostfixForm(firstCorrectExpression)) == 0 + && strcmp("684-*2/4+", translationIntoPostfixForm(secondCorrectExpression)) == 0 + && strcmp("3455+*2/746--+-", translationIntoPostfixForm(thirdCorrectExpression)) == 0 + && strcmp("57+42-/6-", translationIntoPostfixForm(fourthCorrectExpression)) == 0 + && strcmp("6454+*-", translationIntoPostfixForm(fifthCorrectExpression)) == 0; +} \ No newline at end of file diff --git a/sortStation/sortStation/SortStantionTest.h b/sortStation/sortStation/SortStantionTest.h new file mode 100644 index 0000000..a119d5e --- /dev/null +++ b/sortStation/sortStation/SortStantionTest.h @@ -0,0 +1,4 @@ +#pragma once +#include + +bool translationIntoPostfixFormAreTestPassing(); \ No newline at end of file diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index e392707..89797a6 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -1,22 +1,21 @@ #include "../../Stack/Stack/Stack.h" -#include "../../Stack/Stack/StackTest.h" #include #include +#include -char* sortStation(char* array, bool* check) +char* translationIntoPostfixForm(char* array) { - bool err = *check; Stack* head = NULL; int counterForTheOutputArray = 0; int counter = 0; char* arrayToOutput = (char*)malloc(1000 * sizeof(char)); if (arrayToOutput == NULL) { + errno = 2; return NULL; } while (array[counter] != '\0') { - if (array[counter] >= '0' && array[counter] <= '9') { arrayToOutput[counterForTheOutputArray] = array[counter]; @@ -27,20 +26,16 @@ char* sortStation(char* array, bool* check) counter++; continue; } - else if (array[counter] == '-' || array[counter] == '+') - { - while (!isEmpty(head) && top(&head, &err) == '*' || top(&head, &err) == '/') - { - arrayToOutput[counterForTheOutputArray] = pop(&head, &err); - counterForTheOutputArray++; - } - push(&head, array[counter]); - } - else if (array[counter] == '/' || array[counter] == '*') + else if (array[counter] == '-' || array[counter] == '+' + || array[counter] == '/' || array[counter] == '*') { - while (!isEmpty(head) && top(&head, &err) == '*' || top(&head, &err) == '/') + while (!isEmpty(head) && top(&head) == '*' || top(&head) == '/') { - arrayToOutput[counterForTheOutputArray] = pop(&head, &err); + arrayToOutput[counterForTheOutputArray] = pop(&head); + if (errno == 1) + { + return NULL; + } counterForTheOutputArray++; } push(&head, array[counter]); @@ -51,64 +46,53 @@ char* sortStation(char* array, bool* check) } else if (array[counter] == ')') { - while ((!isEmpty(head)) && top(&head, &err) != '(') + while (top(&head) != '(') { - arrayToOutput[counterForTheOutputArray] = pop(&head, &err); - counterForTheOutputArray++; - if (!err) + if(!isEmpty(head)) { - *check = false; - return NULL; + arrayToOutput[counterForTheOutputArray] = pop(&head); + if (errno == 1) + { + return NULL; + } + counterForTheOutputArray++; + } + if (isEmpty(head)) + { + errno = 3; + return 0; } } - if ((!isEmpty(head)) && top(&head, &err) == '(') - { - pop(&head, &err); - } - else + if ((!isEmpty(head)) && top(&head) == '(') { - *check = false; - return NULL; + pop(&head); + if (errno == 1) + { + return NULL; + } } } else { - *check = false; + errno = 3; return NULL; } counter++; } while (!isEmpty(head)) { - if (top(&head, &err) == '(') + if (top(&head) == '(') + { + errno = 3; + return NULL; + } + arrayToOutput[counterForTheOutputArray] = pop(&head); + if (errno == 1) { - *check = false; return NULL; } - arrayToOutput[counterForTheOutputArray] = pop(&head, &err); counterForTheOutputArray++; } arrayToOutput[counterForTheOutputArray] = '\0'; return arrayToOutput; -} - -int main() -{ - char array[1000] = {'\0'}; - printf("aa\n"); - bool check = true; - scanf("%s", array); - char* arrayToOutput = sortStation(array, &check); - { - if (!check) - { - return -1; - } - } - int counter = 0; - while (arrayToOutput[counter] != '\0') - { - printf("%c ", arrayToOutput[counter]); - counter++; - } } \ No newline at end of file diff --git a/sortStation/sortStation/sortStation.vcxproj b/sortStation/sortStation/sortStation.vcxproj index c18f05f..459486e 100644 --- a/sortStation/sortStation/sortStation.vcxproj +++ b/sortStation/sortStation/sortStation.vcxproj @@ -140,10 +140,14 @@ + + + + diff --git a/sortStation/sortStation/sortStation.vcxproj.filters b/sortStation/sortStation/sortStation.vcxproj.filters index aa5034c..518c5b7 100644 --- a/sortStation/sortStation/sortStation.vcxproj.filters +++ b/sortStation/sortStation/sortStation.vcxproj.filters @@ -21,10 +21,22 @@ Исходные файлы + + Исходные файлы + + + Исходные файлы + Файлы ресурсов + + Файлы заголовков + + + Файлы заголовков + \ No newline at end of file From f8c6b6ca0cbf731875aeaa25f665b809027f8096 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 24 Oct 2021 19:19:37 +0500 Subject: [PATCH 28/39] fixed bugs --- sortStation/sortStation/Main.c | 9 +++- sortStation/sortStation/SortStantionTest.c | 34 ++++++++---- sortStation/sortStation/SortStation.c | 63 ++++++++++++++++++++-- 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/sortStation/sortStation/Main.c b/sortStation/sortStation/Main.c index 344a998..76e3e62 100644 --- a/sortStation/sortStation/Main.c +++ b/sortStation/sortStation/Main.c @@ -18,7 +18,7 @@ int main() char* arrayToOutput = translationIntoPostfixForm(array); if (errno == 1) { - printf("Stack is empty(Incorrect expression input)"); + printf("Stack is empty(Due to an incorrectly entered expression)"); return -1; } if (errno == 2) @@ -31,10 +31,15 @@ int main() printf("The parenthesis is missing in the expression"); return -1; } + if (errno == 4) + { + printf("Incorrect expression input)"); + return -1; + } int counter = 0; while (arrayToOutput[counter] != '\0') { - printf("%c ", arrayToOutput[counter]); + printf("%c", arrayToOutput[counter]); counter++; } } \ No newline at end of file diff --git a/sortStation/sortStation/SortStantionTest.c b/sortStation/sortStation/SortStantionTest.c index 5368fd7..dbf7204 100644 --- a/sortStation/sortStation/SortStantionTest.c +++ b/sortStation/sortStation/SortStantionTest.c @@ -5,14 +5,28 @@ bool translationIntoPostfixFormAreTestPassing() { - char firstCorrectExpression[250] = "( 7 - 6 ) * ( 7 - ( 6 - 4 ) * 2 )"; - char secondCorrectExpression[250] = "6 * ( 8 - 4 ) / 2 + 4"; - char thirdCorrectExpression[250] = " 3 - 4 * ( 5 + 5 ) / 2 + ( 7 - ( 4 - 6 ))"; - char fourthCorrectExpression[250] = "( 5 + 7 ) / ( 4 - 2 ) - 6"; - char fifthCorrectExpression[250] = "6 - 4 * ( 5 + 4 )"; - return strcmp("76-764-2*-*", translationIntoPostfixForm(firstCorrectExpression)) == 0 - && strcmp("684-*2/4+", translationIntoPostfixForm(secondCorrectExpression)) == 0 - && strcmp("3455+*2/746--+-", translationIntoPostfixForm(thirdCorrectExpression)) == 0 - && strcmp("57+42-/6-", translationIntoPostfixForm(fourthCorrectExpression)) == 0 - && strcmp("6454+*-", translationIntoPostfixForm(fifthCorrectExpression)) == 0; + char firstCorrectExpression[250] = "(7 - 6) * (7 - (6 - 4) * 2)"; + char secondCorrectExpression[250] = "6 * (8 - 4) / 2 + 4"; + char thirdCorrectExpression[250] = "3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))"; + char fourthCorrectExpression[250] = "(5 + 7) / (4 - 2) - 6"; + char fifthCorrectExpression[250] = "6 - 4 * (5 + 4)"; + + char firstIncorrectExpression[250] = "( 7 - 6 )"; + char secondIncorrectExpression[250] = " 7-6"; + char thirdIncorrectExpression[250] = "(7 - 3)*(4 - 5)"; + char fourthIncorrectExpression[250] = "4 * ( 3 + 4)"; + char fifthIncorrectExpression[250] = "12 - 8 *"; + + return strcmp("7 6 - 7 6 4 - 2 * - *", translationIntoPostfixForm(firstCorrectExpression)) == 0 + && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm(secondCorrectExpression)) == 0 + && strcmp("3 4 5 5 + * 2 / 7 4 6 - - + -", translationIntoPostfixForm(thirdCorrectExpression)) == 0 + && strcmp("5 7 + 4 2 - / 6 -", translationIntoPostfixForm(fourthCorrectExpression)) == 0 + && strcmp("6 4 5 4 + * -", translationIntoPostfixForm(fifthCorrectExpression)) == 0 + + && translationIntoPostfixForm(firstIncorrectExpression) == NULL + && translationIntoPostfixForm(secondIncorrectExpression) == NULL + && translationIntoPostfixForm(thirdIncorrectExpression) == NULL + && translationIntoPostfixForm(fourthIncorrectExpression) == NULL + && translationIntoPostfixForm(fifthIncorrectExpression) == NULL; + } \ No newline at end of file diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index 89797a6..b907f26 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -16,18 +16,55 @@ char* translationIntoPostfixForm(char* array) } while (array[counter] != '\0') { - if (array[counter] >= '0' && array[counter] <= '9') + if (array[counter] == ' ') { + if (array[counter - 1] == ' ') + { + deleteStack(&head); + if (errno == 1) + { + return NULL; + } + errno = 4; + return NULL; + } + if (arrayToOutput[counterForTheOutputArray - 1] == ' ') + { + counter++; + continue; + } arrayToOutput[counterForTheOutputArray] = array[counter]; counterForTheOutputArray++; + counter++; + continue; + } + else if (array[counter] == '*' || array[counter] == '/') + { + if (array[counter + 1] != ' ') + { + deleteStack(&head); + if (errno == 1) + { + return NULL; + } + errno = 4; + return NULL; + } } - else if (array[counter] == ' ') + if (array[counter] >= '0' && array[counter] <= '9') { + if (array[counter + 1] != ' ' && array[counter + 1] != ')' && array[counter + 1] != '\0') + { + deleteStack(&head); + errno = 1; + return NULL; + } + arrayToOutput[counterForTheOutputArray] = array[counter]; + counterForTheOutputArray++; counter++; continue; } - else if (array[counter] == '-' || array[counter] == '+' - || array[counter] == '/' || array[counter] == '*') + else if (array[counter] == '-' || array[counter] == '+' || array[counter] == '/' || array[counter] == '*') { while (!isEmpty(head) && top(&head) == '*' || top(&head) == '/') { @@ -42,14 +79,28 @@ char* translationIntoPostfixForm(char* array) } else if (array[counter] == '(') { + if (array[counter + 1] == ' ') + { + deleteStack(&head); + errno = 1; + return NULL; + } push(&head, array[counter]); } else if (array[counter] == ')') { + if (array[counter - 1] == ' ') + { + deleteStack(&head); + errno = 1; + return NULL; + } while (top(&head) != '(') { if(!isEmpty(head)) { + arrayToOutput[counterForTheOutputArray] = ' '; + counterForTheOutputArray++; arrayToOutput[counterForTheOutputArray] = pop(&head); if (errno == 1) { @@ -74,6 +125,7 @@ char* translationIntoPostfixForm(char* array) } else { + deleteStack(&head); errno = 3; return NULL; } @@ -83,9 +135,12 @@ char* translationIntoPostfixForm(char* array) { if (top(&head) == '(') { + deleteStack(&head); errno = 3; return NULL; } + arrayToOutput[counterForTheOutputArray] = ' '; + counterForTheOutputArray++; arrayToOutput[counterForTheOutputArray] = pop(&head); if (errno == 1) { From 1bb324ab674875e2e4cc69528bb4dea92620ba02 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 24 Oct 2021 20:01:04 +0500 Subject: [PATCH 29/39] bug fix (now the program works to translate any numbers, not just digits from 0 to 9) --- sortStation/sortStation/Main.c | 4 +-- sortStation/sortStation/SortStantionTest.c | 8 +++--- sortStation/sortStation/SortStantionTest.h | 1 + sortStation/sortStation/SortStation.c | 31 +++++++--------------- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/sortStation/sortStation/Main.c b/sortStation/sortStation/Main.c index 76e3e62..9beff15 100644 --- a/sortStation/sortStation/Main.c +++ b/sortStation/sortStation/Main.c @@ -28,12 +28,12 @@ int main() } if (errno == 3) { - printf("The parenthesis is missing in the expression"); + printf("A parenthesis is missing or an invalid character is entered"); return -1; } if (errno == 4) { - printf("Incorrect expression input)"); + printf("An extra space has been put (or skipped)"); return -1; } int counter = 0; diff --git a/sortStation/sortStation/SortStantionTest.c b/sortStation/sortStation/SortStantionTest.c index dbf7204..cce3128 100644 --- a/sortStation/sortStation/SortStantionTest.c +++ b/sortStation/sortStation/SortStantionTest.c @@ -8,8 +8,8 @@ bool translationIntoPostfixFormAreTestPassing() char firstCorrectExpression[250] = "(7 - 6) * (7 - (6 - 4) * 2)"; char secondCorrectExpression[250] = "6 * (8 - 4) / 2 + 4"; char thirdCorrectExpression[250] = "3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))"; - char fourthCorrectExpression[250] = "(5 + 7) / (4 - 2) - 6"; - char fifthCorrectExpression[250] = "6 - 4 * (5 + 4)"; + char fourthCorrectExpression[250] = "2834 - 123 * (12 + 45 - (12 * (34 - 12) + 7))"; + char fifthCorrectExpression[250] = "76 - 45 * (34 + 27)"; char firstIncorrectExpression[250] = "( 7 - 6 )"; char secondIncorrectExpression[250] = " 7-6"; @@ -20,8 +20,8 @@ bool translationIntoPostfixFormAreTestPassing() return strcmp("7 6 - 7 6 4 - 2 * - *", translationIntoPostfixForm(firstCorrectExpression)) == 0 && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm(secondCorrectExpression)) == 0 && strcmp("3 4 5 5 + * 2 / 7 4 6 - - + -", translationIntoPostfixForm(thirdCorrectExpression)) == 0 - && strcmp("5 7 + 4 2 - / 6 -", translationIntoPostfixForm(fourthCorrectExpression)) == 0 - && strcmp("6 4 5 4 + * -", translationIntoPostfixForm(fifthCorrectExpression)) == 0 + && strcmp("2834 123 12 45 12 34 12 - * 7 + - + * -", translationIntoPostfixForm(fourthCorrectExpression)) == 0 + && strcmp("76 45 34 27 + * -", translationIntoPostfixForm(fifthCorrectExpression)) == 0 && translationIntoPostfixForm(firstIncorrectExpression) == NULL && translationIntoPostfixForm(secondIncorrectExpression) == NULL diff --git a/sortStation/sortStation/SortStantionTest.h b/sortStation/sortStation/SortStantionTest.h index a119d5e..a053c55 100644 --- a/sortStation/sortStation/SortStantionTest.h +++ b/sortStation/sortStation/SortStantionTest.h @@ -1,4 +1,5 @@ #pragma once #include +// Function for testing a function that translates an expression from infix to postfix form bool translationIntoPostfixFormAreTestPassing(); \ No newline at end of file diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index b907f26..ce4ceea 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -21,10 +21,6 @@ char* translationIntoPostfixForm(char* array) if (array[counter - 1] == ' ') { deleteStack(&head); - if (errno == 1) - { - return NULL; - } errno = 4; return NULL; } @@ -38,25 +34,12 @@ char* translationIntoPostfixForm(char* array) counter++; continue; } - else if (array[counter] == '*' || array[counter] == '/') - { - if (array[counter + 1] != ' ') - { - deleteStack(&head); - if (errno == 1) - { - return NULL; - } - errno = 4; - return NULL; - } - } if (array[counter] >= '0' && array[counter] <= '9') { - if (array[counter + 1] != ' ' && array[counter + 1] != ')' && array[counter + 1] != '\0') + if (array[counter + 1] == '*' || array[counter + 1] == '/' || array[counter + 1] == '(' || array[counter + 1] == '+' || array[counter + 1] == '-') { deleteStack(&head); - errno = 1; + errno = 4; return NULL; } arrayToOutput[counterForTheOutputArray] = array[counter]; @@ -66,6 +49,12 @@ char* translationIntoPostfixForm(char* array) } else if (array[counter] == '-' || array[counter] == '+' || array[counter] == '/' || array[counter] == '*') { + if (array[counter + 1] != ' ') + { + deleteStack(&head); + errno = 4; + return NULL; + } while (!isEmpty(head) && top(&head) == '*' || top(&head) == '/') { arrayToOutput[counterForTheOutputArray] = pop(&head); @@ -82,7 +71,7 @@ char* translationIntoPostfixForm(char* array) if (array[counter + 1] == ' ') { deleteStack(&head); - errno = 1; + errno = 4; return NULL; } push(&head, array[counter]); @@ -92,7 +81,7 @@ char* translationIntoPostfixForm(char* array) if (array[counter - 1] == ' ') { deleteStack(&head); - errno = 1; + errno = 4; return NULL; } while (top(&head) != '(') From c3488e02dd68433b7a105e3d9a0ce9af0f1826d5 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 24 Oct 2021 20:20:15 +0500 Subject: [PATCH 30/39] fixed bugs --- BalanceBrackets/BalanceBrackets.sln | 31 ---- .../BalanceBrackets/BalanceBrackets.c | 47 ------ .../BalanceBrackets/BalanceBrackets.h | 5 - .../BalanceBrackets/BalanceBrackets.vcxproj | 157 ------------------ .../BalanceBrackets.vcxproj.filters | 48 ------ .../BalanceBrackets/BalanceBracketsTest.c | 35 ---- .../BalanceBrackets/BalanceBracketsTest.h | 5 - BalanceBrackets/BalanceBrackets/Main.c | 23 --- 8 files changed, 351 deletions(-) delete mode 100644 BalanceBrackets/BalanceBrackets.sln delete mode 100644 BalanceBrackets/BalanceBrackets/BalanceBrackets.c delete mode 100644 BalanceBrackets/BalanceBrackets/BalanceBrackets.h delete mode 100644 BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj delete mode 100644 BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters delete mode 100644 BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c delete mode 100644 BalanceBrackets/BalanceBrackets/BalanceBracketsTest.h delete mode 100644 BalanceBrackets/BalanceBrackets/Main.c diff --git a/BalanceBrackets/BalanceBrackets.sln b/BalanceBrackets/BalanceBrackets.sln deleted file mode 100644 index da1353a..0000000 --- a/BalanceBrackets/BalanceBrackets.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31410.357 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "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 deleted file mode 100644 index e061c29..0000000 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "BalanceBrackets.h" -#include "../../Stack/Stack/Stack.h" -#include - -bool checkCorrectOrderBrackets(char* expressionFromParentheses) -{ - bool check = true; - 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 topOfTheStack = pop(&head, &check); - { - if (!check) - { - return false; - } - } - if ((topOfTheStack == '(' && expressionFromParentheses[counter] != ')') - || (topOfTheStack == '{' && expressionFromParentheses[counter] != '}') - || (topOfTheStack == '[' && expressionFromParentheses[counter] != ']')) - { - return false; - } - } - counter++; - } - if (isEmpty(head)) - { - deleteStack(&head); - return true; - } - deleteStack(&head); - return false; -} \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h b/BalanceBrackets/BalanceBrackets/BalanceBrackets.h deleted file mode 100644 index 95b8c78..0000000 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.h +++ /dev/null @@ -1,5 +0,0 @@ -#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 deleted file mode 100644 index 7ed29e0..0000000 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj +++ /dev/null @@ -1,157 +0,0 @@ - - - - - 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 deleted file mode 100644 index ef4a7dc..0000000 --- a/BalanceBrackets/BalanceBrackets/BalanceBrackets.vcxproj.filters +++ /dev/null @@ -1,48 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Исходные файлы - - - Исходные файлы - - - Исходные файлы - - - Исходные файлы - - - Исходные файлы - - - - - Файлы заголовков - - - Файлы заголовков - - - Файлы заголовков - - - Файлы заголовков - - - \ No newline at end of file diff --git a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c b/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c deleted file mode 100644 index 648a9d5..0000000 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.c +++ /dev/null @@ -1,35 +0,0 @@ -#include "BalanceBracketsTest.h" -#include "BalanceBrackets.h" - -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] = "{}()[]"; - 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 deleted file mode 100644 index e277708..0000000 --- a/BalanceBrackets/BalanceBrackets/BalanceBracketsTest.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include - -// Function for testing a function that checks for the correct position of the brackets -bool balanceBracketsTest(); diff --git a/BalanceBrackets/BalanceBrackets/Main.c b/BalanceBrackets/BalanceBrackets/Main.c deleted file mode 100644 index 2da68c3..0000000 --- a/BalanceBrackets/BalanceBrackets/Main.c +++ /dev/null @@ -1,23 +0,0 @@ -#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 that you want to check for the correct placement of brackets\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 a6443f5dc2bd383a15155bc31f8753233b3caae9 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 18:39:40 +0500 Subject: [PATCH 31/39] fixed bugs related to error values --- Stack/Stack/Stack.c | 16 +++++++-------- Stack/Stack/Stack.h | 4 ++-- Stack/Stack/StackTest.c | 43 ++++++++++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index c809346..240eee2 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,26 @@ 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) +char top(Stack** head, int* error) { - errno = 0; + *error = 0; if (!isEmpty(*head)) { return (*head)->value; } - errno = 1; + *error = 1; return 0; } diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index e6db7b8..57ca12b 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -15,10 +15,10 @@ 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 +char top(Stack** head, int* error); \ No newline at end of file diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index 9d0ab00..969c046 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -6,9 +6,10 @@ 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 == ')'; } @@ -18,10 +19,20 @@ bool popTest() push(&head, 'a'); push(&head, '^'); push(&head, '1'); - char firstPopResult = pop(&head); - char firstUpperElement = head->value; - char secondPopResult = pop(&head); - char secondUpperElement = head->value; + int error = 0; + const char firstPopResult = pop(&head, &error); + if (error == 1) + { + return false; + } + const char firstUpperElement = head->value; + const char secondPopResult = pop(&head, &error); + if (error == 1) + { + return false; + } + const char secondUpperElement = head->value; + deleteStack(&head); return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; } @@ -39,10 +50,24 @@ bool topTest() { Stack* head = NULL; push(&head, 'a'); - char firstTopResult = head->value; + int error = 0; + const char firstTopResult = top(&head, &error); + if (error == 1) + { + return false; + } push(&head, '^'); - char secondTopResult = head->value; + const char secondTopResult = top(&head, &error); + if (error == 1) + { + return false; + } push(&head, '1'); - char thirdTopResult = head->value; + const char thirdTopResult = top(&head, &error); + if (error == 1) + { + return false; + } + deleteStack(&head); return firstTopResult == 'a' && secondTopResult == '^' && thirdTopResult == '1'; } \ No newline at end of file From d1fd65fc46a13754c0c6c74f606f1db5effa67f5 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 19:23:32 +0500 Subject: [PATCH 32/39] changing tests, function arguments --- sortStation/sortStation/Main.c | 15 ++++--- sortStation/sortStation/SortStantion.h | 2 +- sortStation/sortStation/SortStantionTest.c | 43 +++++++++---------- sortStation/sortStation/SortStation.c | 49 ++++++++++++---------- 4 files changed, 57 insertions(+), 52 deletions(-) diff --git a/sortStation/sortStation/Main.c b/sortStation/sortStation/Main.c index 9beff15..ea4cd67 100644 --- a/sortStation/sortStation/Main.c +++ b/sortStation/sortStation/Main.c @@ -2,7 +2,6 @@ #include "SortStantion.h" #include "SortStantionTest.h" #include -#include int main() { @@ -14,24 +13,24 @@ int main() char array[1000] = { '\0' }; printf("enter the expression in the infix form\n"); scanf_s("%[^\n]s", array, (unsigned)sizeof(array)); - errno = 0; - char* arrayToOutput = translationIntoPostfixForm(array); - if (errno == 1) + int errorCode = 0; + char* arrayToOutput = translationIntoPostfixForm(array, &errorCode); + if (errorCode == 1) { - printf("Stack is empty(Due to an incorrectly entered expression)"); + printf("Incorrectly entered expression"); return -1; } - if (errno == 2) + if (errorCode == 2) { printf("Memory not allocated"); return -1; } - if (errno == 3) + if (errorCode == 3) { printf("A parenthesis is missing or an invalid character is entered"); return -1; } - if (errno == 4) + if (errorCode == 4) { printf("An extra space has been put (or skipped)"); return -1; diff --git a/sortStation/sortStation/SortStantion.h b/sortStation/sortStation/SortStantion.h index c4feb2e..0526bbb 100644 --- a/sortStation/sortStation/SortStantion.h +++ b/sortStation/sortStation/SortStantion.h @@ -1,4 +1,4 @@ #pragma once // Function for translating an expression from infix to postfix form -char* translationIntoPostfixForm(char* array); +char* translationIntoPostfixForm(const char* array, int* errorCode); diff --git a/sortStation/sortStation/SortStantionTest.c b/sortStation/sortStation/SortStantionTest.c index cce3128..b3ebfd2 100644 --- a/sortStation/sortStation/SortStantionTest.c +++ b/sortStation/sortStation/SortStantionTest.c @@ -1,32 +1,33 @@ #include "SortStantionTest.h" #include "SortStantion.h" -#include #include bool translationIntoPostfixFormAreTestPassing() { - char firstCorrectExpression[250] = "(7 - 6) * (7 - (6 - 4) * 2)"; - char secondCorrectExpression[250] = "6 * (8 - 4) / 2 + 4"; - char thirdCorrectExpression[250] = "3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))"; - char fourthCorrectExpression[250] = "2834 - 123 * (12 + 45 - (12 * (34 - 12) + 7))"; - char fifthCorrectExpression[250] = "76 - 45 * (34 + 27)"; + const char firstCorrectExpression[250] = "(7 - 6) * (7 - (6 - 4) * 2)"; + const char secondCorrectExpression[250] = "6 * (8 - 4) / 2 + 4"; + const char thirdCorrectExpression[250] = "3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))"; + const char fourthCorrectExpression[250] = "2834 - 123 * (12 + 45 - (12 * (34 - 12) + 7))"; + const char fifthCorrectExpression[250] = "76 - 45 * (34 + 27)"; - char firstIncorrectExpression[250] = "( 7 - 6 )"; - char secondIncorrectExpression[250] = " 7-6"; - char thirdIncorrectExpression[250] = "(7 - 3)*(4 - 5)"; - char fourthIncorrectExpression[250] = "4 * ( 3 + 4)"; - char fifthIncorrectExpression[250] = "12 - 8 *"; + const char firstIncorrectExpression[250] = "( 7 - 6 )"; + const char secondIncorrectExpression[250] = " 7-6"; + const char thirdIncorrectExpression[250] = "(7 - 3) * 4 - 5)"; + const char fourthIncorrectExpression[250] = "4 * ( 3 + 4)"; + const char fifthIncorrectExpression[250] = "12 - 8 / a "; - return strcmp("7 6 - 7 6 4 - 2 * - *", translationIntoPostfixForm(firstCorrectExpression)) == 0 - && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm(secondCorrectExpression)) == 0 - && strcmp("3 4 5 5 + * 2 / 7 4 6 - - + -", translationIntoPostfixForm(thirdCorrectExpression)) == 0 - && strcmp("2834 123 12 45 12 34 12 - * 7 + - + * -", translationIntoPostfixForm(fourthCorrectExpression)) == 0 - && strcmp("76 45 34 27 + * -", translationIntoPostfixForm(fifthCorrectExpression)) == 0 + int errors[10] = {0}; - && translationIntoPostfixForm(firstIncorrectExpression) == NULL - && translationIntoPostfixForm(secondIncorrectExpression) == NULL - && translationIntoPostfixForm(thirdIncorrectExpression) == NULL - && translationIntoPostfixForm(fourthIncorrectExpression) == NULL - && translationIntoPostfixForm(fifthIncorrectExpression) == NULL; + return strcmp("7 6 - 7 6 4 - 2 * - *", translationIntoPostfixForm(firstCorrectExpression, &errors[0])) == 0 && errors[0] == 0 + && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm(secondCorrectExpression, &errors[1])) == 0 && errors[1] == 0 + && strcmp("3 4 5 5 + * 2 / 7 4 6 - - + -", translationIntoPostfixForm(thirdCorrectExpression, &errors[2])) == 0 && errors[2] == 0 + && strcmp("2834 123 12 45 12 34 12 - * 7 + - + * -", translationIntoPostfixForm(fourthCorrectExpression, &errors[3])) == 0 && errors[3] == 0 + && strcmp("76 45 34 27 + * -", translationIntoPostfixForm(fifthCorrectExpression, &errors[4])) == 0 && errors[4] == 0 + + && translationIntoPostfixForm(firstIncorrectExpression, &errors[5]) == NULL && errors[5] == 4 + && translationIntoPostfixForm(secondIncorrectExpression, &errors[6]) == NULL && errors[6] == 4 + && translationIntoPostfixForm(thirdIncorrectExpression, &errors[7]) == NULL && errors[7] == 3 + && translationIntoPostfixForm(fourthIncorrectExpression, &errors[8]) == NULL && errors[8] == 4 + && translationIntoPostfixForm(fifthIncorrectExpression, &errors[9]) == NULL && errors[9] == 3; } \ No newline at end of file diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index ce4ceea..c7e83b2 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -3,15 +3,16 @@ #include #include -char* translationIntoPostfixForm(char* array) +char* translationIntoPostfixForm(const char* array, int* errorCode) { Stack* head = NULL; int counterForTheOutputArray = 0; int counter = 0; + int error = 0 ; char* arrayToOutput = (char*)malloc(1000 * sizeof(char)); if (arrayToOutput == NULL) { - errno = 2; + *errorCode = 2; return NULL; } while (array[counter] != '\0') @@ -21,7 +22,7 @@ char* translationIntoPostfixForm(char* array) if (array[counter - 1] == ' ') { deleteStack(&head); - errno = 4; + *errorCode = 4; return NULL; } if (arrayToOutput[counterForTheOutputArray - 1] == ' ') @@ -39,7 +40,7 @@ char* translationIntoPostfixForm(char* array) if (array[counter + 1] == '*' || array[counter + 1] == '/' || array[counter + 1] == '(' || array[counter + 1] == '+' || array[counter + 1] == '-') { deleteStack(&head); - errno = 4; + *errorCode = 4; return NULL; } arrayToOutput[counterForTheOutputArray] = array[counter]; @@ -52,14 +53,15 @@ char* translationIntoPostfixForm(char* array) if (array[counter + 1] != ' ') { deleteStack(&head); - errno = 4; + *errorCode = 4; return NULL; } - while (!isEmpty(head) && top(&head) == '*' || top(&head) == '/') + while (!isEmpty(head) && top(&head, &error) == '*' || top(&head, &error) == '/') { - arrayToOutput[counterForTheOutputArray] = pop(&head); - if (errno == 1) + arrayToOutput[counterForTheOutputArray] = pop(&head, &error); + if (error == 1) { + *errorCode = 1; return NULL; } counterForTheOutputArray++; @@ -71,7 +73,7 @@ char* translationIntoPostfixForm(char* array) if (array[counter + 1] == ' ') { deleteStack(&head); - errno = 4; + *errorCode = 4; return NULL; } push(&head, array[counter]); @@ -81,33 +83,35 @@ char* translationIntoPostfixForm(char* array) if (array[counter - 1] == ' ') { deleteStack(&head); - errno = 4; + *errorCode = 4; return NULL; } - while (top(&head) != '(') + while (top(&head, &error) != '(') { if(!isEmpty(head)) { arrayToOutput[counterForTheOutputArray] = ' '; counterForTheOutputArray++; - arrayToOutput[counterForTheOutputArray] = pop(&head); - if (errno == 1) + arrayToOutput[counterForTheOutputArray] = pop(&head, &error); + if (error == 1) { + *errorCode = 1; return NULL; } counterForTheOutputArray++; } if (isEmpty(head)) { - errno = 3; + *errorCode = 3; return 0; } } - if ((!isEmpty(head)) && top(&head) == '(') + if ((!isEmpty(head)) && top(&head, &error) == '(') { - pop(&head); - if (errno == 1) + pop(&head, &error); + if (error == 1) { + *errorCode = 1; return NULL; } } @@ -115,24 +119,25 @@ char* translationIntoPostfixForm(char* array) else { deleteStack(&head); - errno = 3; + *errorCode = 3; return NULL; } counter++; } while (!isEmpty(head)) { - if (top(&head) == '(') + if (top(&head, &error) == '(') { deleteStack(&head); - errno = 3; + *errorCode = 3; return NULL; } arrayToOutput[counterForTheOutputArray] = ' '; counterForTheOutputArray++; - arrayToOutput[counterForTheOutputArray] = pop(&head); - if (errno == 1) + arrayToOutput[counterForTheOutputArray] = pop(&head, &error); + if (error == 1) { + *errorCode = 1; return NULL; } counterForTheOutputArray++; From 46e663efbf6178cc24626d177988aea95fb2f843 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 19:26:53 +0500 Subject: [PATCH 33/39] 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 240eee2..a63b467 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 128ce17368b30fcc39237473dbcb79586b6fe479 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 25 Oct 2021 19:37:27 +0500 Subject: [PATCH 34/39] removed the space bar --- sortStation/sortStation/SortStation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index c7e83b2..af28310 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -3,7 +3,7 @@ #include #include -char* translationIntoPostfixForm(const char* array, int* errorCode) +char* translationIntoPostfixForm(const char* array, int* errorCode) { Stack* head = NULL; int counterForTheOutputArray = 0; From 9c875d3448b1cd75d4ec3f25cf2dfe027677c147 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 27 Oct 2021 00:11:44 +0500 Subject: [PATCH 35/39] fixed a problem with spaces --- sortStation/sortStation/SortStantionTest.c | 29 +++--- sortStation/sortStation/SortStation.c | 95 ++++++++++--------- .../sortStation/sortStation.vcxproj.filters | 6 +- 3 files changed, 66 insertions(+), 64 deletions(-) diff --git a/sortStation/sortStation/SortStantionTest.c b/sortStation/sortStation/SortStantionTest.c index b3ebfd2..923bee5 100644 --- a/sortStation/sortStation/SortStantionTest.c +++ b/sortStation/sortStation/SortStantionTest.c @@ -4,13 +4,7 @@ bool translationIntoPostfixFormAreTestPassing() { - const char firstCorrectExpression[250] = "(7 - 6) * (7 - (6 - 4) * 2)"; - const char secondCorrectExpression[250] = "6 * (8 - 4) / 2 + 4"; - const char thirdCorrectExpression[250] = "3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))"; - const char fourthCorrectExpression[250] = "2834 - 123 * (12 + 45 - (12 * (34 - 12) + 7))"; - const char fifthCorrectExpression[250] = "76 - 45 * (34 + 27)"; - - const char firstIncorrectExpression[250] = "( 7 - 6 )"; + const char firstIncorrectExpression[250] = "( 7 - 6a )"; const char secondIncorrectExpression[250] = " 7-6"; const char thirdIncorrectExpression[250] = "(7 - 3) * 4 - 5)"; const char fourthIncorrectExpression[250] = "4 * ( 3 + 4)"; @@ -18,16 +12,15 @@ bool translationIntoPostfixFormAreTestPassing() int errors[10] = {0}; - return strcmp("7 6 - 7 6 4 - 2 * - *", translationIntoPostfixForm(firstCorrectExpression, &errors[0])) == 0 && errors[0] == 0 - && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm(secondCorrectExpression, &errors[1])) == 0 && errors[1] == 0 - && strcmp("3 4 5 5 + * 2 / 7 4 6 - - + -", translationIntoPostfixForm(thirdCorrectExpression, &errors[2])) == 0 && errors[2] == 0 - && strcmp("2834 123 12 45 12 34 12 - * 7 + - + * -", translationIntoPostfixForm(fourthCorrectExpression, &errors[3])) == 0 && errors[3] == 0 - && strcmp("76 45 34 27 + * -", translationIntoPostfixForm(fifthCorrectExpression, &errors[4])) == 0 && errors[4] == 0 - - && translationIntoPostfixForm(firstIncorrectExpression, &errors[5]) == NULL && errors[5] == 4 - && translationIntoPostfixForm(secondIncorrectExpression, &errors[6]) == NULL && errors[6] == 4 - && translationIntoPostfixForm(thirdIncorrectExpression, &errors[7]) == NULL && errors[7] == 3 - && translationIntoPostfixForm(fourthIncorrectExpression, &errors[8]) == NULL && errors[8] == 4 - && translationIntoPostfixForm(fifthIncorrectExpression, &errors[9]) == NULL && errors[9] == 3; + return strcmp("7 6 - 7 6 4 - 2 * - *", translationIntoPostfixForm("(7 - 6) * (7 - (6 - 4) * 2)", &errors[0])) == 0 + && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm("6 * (8 - 4) / 2 + 4", &errors[1])) == 0 + && strcmp("3 4 5 5 + * 2 / - 7 4 6 - - +", translationIntoPostfixForm("3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))", &errors[2])) == 0 + && strcmp("1 8 - 9 + 4 1 3 + * -", translationIntoPostfixForm("1 - 8 + 9 - 4 * (1 + 3)", &errors[3])) == 0 + && strcmp("1 2 - 3 +", translationIntoPostfixForm("1 - 2 + 3", &errors[4])) == 0 + && translationIntoPostfixForm("( 7 - 6a )", &errors[5]) == NULL + && translationIntoPostfixForm("7--", &errors[6]) == NULL + && translationIntoPostfixForm("(7 - 3) * 4 - 5)", &errors[7]) == NULL + && translationIntoPostfixForm("2 + 2)", &errors[8]) == NULL + && translationIntoPostfixForm("12 - 8 / a", &errors[9]) == NULL; } \ No newline at end of file diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index af28310..9fc1b9f 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -3,6 +3,21 @@ #include #include +bool isRepeatTheOperation(char operation) +{ + return operation == '+' || operation == '-' || operation == '*' || operation == '/'; +} + +bool isPriorityForDivisionandMultiplication(char operation) +{ + return operation == '*' || operation == '/'; +} + +bool isPriorityForMinusAndPlus(char operation) +{ + return isPriorityForDivisionandMultiplication(operation) || operation == '-' || operation == '+'; +} + char* translationIntoPostfixForm(const char* array, int* errorCode) { Stack* head = NULL; @@ -17,46 +32,45 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) } while (array[counter] != '\0') { - if (array[counter] == ' ') + if (array[counter] >= '0' && array[counter] <= '9') { - if (array[counter - 1] == ' ') - { - deleteStack(&head); - *errorCode = 4; - return NULL; - } - if (arrayToOutput[counterForTheOutputArray - 1] == ' ') - { - counter++; - continue; - } arrayToOutput[counterForTheOutputArray] = array[counter]; counterForTheOutputArray++; + arrayToOutput[counterForTheOutputArray] = ' '; + counterForTheOutputArray++; counter++; continue; } - if (array[counter] >= '0' && array[counter] <= '9') + else if (array[counter] == '-' || array[counter] == '+') { - if (array[counter + 1] == '*' || array[counter + 1] == '/' || array[counter + 1] == '(' || array[counter + 1] == '+' || array[counter + 1] == '-') + if (isRepeatTheOperation(array[counter + 1]) || isRepeatTheOperation(array[counter + 2])) { - deleteStack(&head); - *errorCode = 4; + *errorCode = 1; return NULL; } - arrayToOutput[counterForTheOutputArray] = array[counter]; - counterForTheOutputArray++; - counter++; - continue; + while (!isEmpty(head) && isPriorityForMinusAndPlus((char)top(&head, &error))) + { + arrayToOutput[counterForTheOutputArray] = pop(&head, &error); + if (error == 1) + { + *errorCode = 1; + return NULL; + } + counterForTheOutputArray++; + arrayToOutput[counterForTheOutputArray] = ' '; + counterForTheOutputArray++; + } + push(&head, array[counter]); } - else if (array[counter] == '-' || array[counter] == '+' || array[counter] == '/' || array[counter] == '*') + else if (array[counter] == '/' || array[counter] == '*') { - if (array[counter + 1] != ' ') + if (isRepeatTheOperation(array[counter + 1]) || isRepeatTheOperation(array[counter + 2])) { - deleteStack(&head); - *errorCode = 4; + *errorCode = 1; return NULL; } - while (!isEmpty(head) && top(&head, &error) == '*' || top(&head, &error) == '/') + + while (!isEmpty(head) && isPriorityForDivisionandMultiplication((char)top(&head, &error))) { arrayToOutput[counterForTheOutputArray] = pop(&head, &error); if (error == 1) @@ -65,33 +79,21 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) return NULL; } counterForTheOutputArray++; + arrayToOutput[counterForTheOutputArray] = ' '; + counterForTheOutputArray++; } push(&head, array[counter]); } else if (array[counter] == '(') { - if (array[counter + 1] == ' ') - { - deleteStack(&head); - *errorCode = 4; - return NULL; - } push(&head, array[counter]); } else if (array[counter] == ')') { - if (array[counter - 1] == ' ') - { - deleteStack(&head); - *errorCode = 4; - return NULL; - } while (top(&head, &error) != '(') { if(!isEmpty(head)) - { - arrayToOutput[counterForTheOutputArray] = ' '; - counterForTheOutputArray++; + { arrayToOutput[counterForTheOutputArray] = pop(&head, &error); if (error == 1) { @@ -99,6 +101,8 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) return NULL; } counterForTheOutputArray++; + arrayToOutput[counterForTheOutputArray] = ' '; + counterForTheOutputArray++; } if (isEmpty(head)) { @@ -116,6 +120,11 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) } } } + else if (array[counter] == ' ') + { + counter++; + continue; + } else { deleteStack(&head); @@ -132,8 +141,6 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) *errorCode = 3; return NULL; } - arrayToOutput[counterForTheOutputArray] = ' '; - counterForTheOutputArray++; arrayToOutput[counterForTheOutputArray] = pop(&head, &error); if (error == 1) { @@ -141,7 +148,9 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) return NULL; } counterForTheOutputArray++; + arrayToOutput[counterForTheOutputArray] = ' '; + counterForTheOutputArray++; } - arrayToOutput[counterForTheOutputArray] = '\0'; + arrayToOutput[counterForTheOutputArray - 1] = '\0'; return arrayToOutput; } \ No newline at end of file diff --git a/sortStation/sortStation/sortStation.vcxproj.filters b/sortStation/sortStation/sortStation.vcxproj.filters index 518c5b7..35e6026 100644 --- a/sortStation/sortStation/sortStation.vcxproj.filters +++ b/sortStation/sortStation/sortStation.vcxproj.filters @@ -29,14 +29,14 @@ - - Файлы ресурсов - Файлы заголовков Файлы заголовков + + Файлы заголовков + \ No newline at end of file From c118f0655b7d5f343bc5a098026512bb8d3ac5f6 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 27 Oct 2021 01:00:01 +0500 Subject: [PATCH 36/39] add several function, changed test --- sortStation/sortStation/Main.c | 14 +---- sortStation/sortStation/SortStantionTest.c | 36 +++++------ sortStation/sortStation/SortStation.c | 70 +++++++++++++++------- 3 files changed, 70 insertions(+), 50 deletions(-) diff --git a/sortStation/sortStation/Main.c b/sortStation/sortStation/Main.c index ea4cd67..46d9d11 100644 --- a/sortStation/sortStation/Main.c +++ b/sortStation/sortStation/Main.c @@ -14,7 +14,7 @@ int main() printf("enter the expression in the infix form\n"); scanf_s("%[^\n]s", array, (unsigned)sizeof(array)); int errorCode = 0; - char* arrayToOutput = translationIntoPostfixForm(array, &errorCode); + const char* const arrayToOutput = translationIntoPostfixForm(array, &errorCode); if (errorCode == 1) { printf("Incorrectly entered expression"); @@ -30,15 +30,5 @@ int main() printf("A parenthesis is missing or an invalid character is entered"); return -1; } - if (errorCode == 4) - { - printf("An extra space has been put (or skipped)"); - return -1; - } - int counter = 0; - while (arrayToOutput[counter] != '\0') - { - printf("%c", arrayToOutput[counter]); - counter++; - } + printf("%s\n", arrayToOutput); } \ No newline at end of file diff --git a/sortStation/sortStation/SortStantionTest.c b/sortStation/sortStation/SortStantionTest.c index 923bee5..6eb7999 100644 --- a/sortStation/sortStation/SortStantionTest.c +++ b/sortStation/sortStation/SortStantionTest.c @@ -4,23 +4,25 @@ bool translationIntoPostfixFormAreTestPassing() { - const char firstIncorrectExpression[250] = "( 7 - 6a )"; - const char secondIncorrectExpression[250] = " 7-6"; - const char thirdIncorrectExpression[250] = "(7 - 3) * 4 - 5)"; - const char fourthIncorrectExpression[250] = "4 * ( 3 + 4)"; - const char fifthIncorrectExpression[250] = "12 - 8 / a "; - int errors[10] = {0}; - return strcmp("7 6 - 7 6 4 - 2 * - *", translationIntoPostfixForm("(7 - 6) * (7 - (6 - 4) * 2)", &errors[0])) == 0 - && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm("6 * (8 - 4) / 2 + 4", &errors[1])) == 0 - && strcmp("3 4 5 5 + * 2 / - 7 4 6 - - +", translationIntoPostfixForm("3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))", &errors[2])) == 0 - && strcmp("1 8 - 9 + 4 1 3 + * -", translationIntoPostfixForm("1 - 8 + 9 - 4 * (1 + 3)", &errors[3])) == 0 - && strcmp("1 2 - 3 +", translationIntoPostfixForm("1 - 2 + 3", &errors[4])) == 0 - && translationIntoPostfixForm("( 7 - 6a )", &errors[5]) == NULL - && translationIntoPostfixForm("7--", &errors[6]) == NULL - && translationIntoPostfixForm("(7 - 3) * 4 - 5)", &errors[7]) == NULL - && translationIntoPostfixForm("2 + 2)", &errors[8]) == NULL - && translationIntoPostfixForm("12 - 8 / a", &errors[9]) == NULL; - + && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm("6 * (8 - 4) / 2 + 4", &errors[1])) == 0 + && strcmp("3 4 5 5 + * 2 / - 7 4 6 - - +", translationIntoPostfixForm("3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))", &errors[2])) == 0 + && strcmp("1 8 - 9 + 4 1 3 + * -", translationIntoPostfixForm("1 - 8 + 9 - 4 * (1 + 3)", &errors[3])) == 0 + && strcmp("1 2 - 3 +", translationIntoPostfixForm("1 - 2 + 3", &errors[4])) == 0 + && translationIntoPostfixForm("( 7 + * 2)", &errors[5]) == NULL + && translationIntoPostfixForm("7--", &errors[6]) == NULL + && translationIntoPostfixForm("(7 - 3) * 4 - 5)", &errors[7]) == NULL + && translationIntoPostfixForm("2 + 2)", &errors[8]) == NULL + && translationIntoPostfixForm("12 - 8 / a", &errors[9]) == NULL + && errors[0] == 0 + && errors[1] == 0 + && errors[2] == 0 + && errors[3] == 0 + && errors[4] == 0 + && errors[5] == 1 + && errors[6] == 1 + && errors[7] == 3 + && errors[8] == 3 + && errors[9] == 3; } \ No newline at end of file diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index 9fc1b9f..1a19c70 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -1,7 +1,7 @@ #include "../../Stack/Stack/Stack.h" #include #include -#include +#include bool isRepeatTheOperation(char operation) { @@ -18,16 +18,22 @@ bool isPriorityForMinusAndPlus(char operation) return isPriorityForDivisionandMultiplication(operation) || operation == '-' || operation == '+'; } +void addSpace(char* symbol) +{ + *symbol = ' '; +} + char* translationIntoPostfixForm(const char* array, int* errorCode) { - Stack* head = NULL; + Stack* head = createStack(); int counterForTheOutputArray = 0; int counter = 0; - int error = 0 ; - char* arrayToOutput = (char*)malloc(1000 * sizeof(char)); + int error = 0; + char* arrayToOutput = (char*)calloc((2 * strlen(array) + 1), sizeof(char)); if (arrayToOutput == NULL) { *errorCode = 2; + deleteStack(&head); return NULL; } while (array[counter] != '\0') @@ -36,8 +42,7 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) { arrayToOutput[counterForTheOutputArray] = array[counter]; counterForTheOutputArray++; - arrayToOutput[counterForTheOutputArray] = ' '; - counterForTheOutputArray++; + addSpace(&arrayToOutput[counterForTheOutputArray++]); counter++; continue; } @@ -46,47 +51,67 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) if (isRepeatTheOperation(array[counter + 1]) || isRepeatTheOperation(array[counter + 2])) { *errorCode = 1; + deleteStack(&head); return NULL; } while (!isEmpty(head) && isPriorityForMinusAndPlus((char)top(&head, &error))) { - arrayToOutput[counterForTheOutputArray] = pop(&head, &error); + arrayToOutput[counterForTheOutputArray] = (char)pop(&head, &error); if (error == 1) { + deleteStack(&head); *errorCode = 1; return NULL; } counterForTheOutputArray++; - arrayToOutput[counterForTheOutputArray] = ' '; - counterForTheOutputArray++; + addSpace(&arrayToOutput[counterForTheOutputArray++]); + } + push(&head, array[counter], &error); + if (error == 2) + { + *errorCode = 2; + deleteStack(&head); + return NULL; } - push(&head, array[counter]); } else if (array[counter] == '/' || array[counter] == '*') { if (isRepeatTheOperation(array[counter + 1]) || isRepeatTheOperation(array[counter + 2])) { + deleteStack(&head); *errorCode = 1; return NULL; } while (!isEmpty(head) && isPriorityForDivisionandMultiplication((char)top(&head, &error))) { - arrayToOutput[counterForTheOutputArray] = pop(&head, &error); + arrayToOutput[counterForTheOutputArray] = (char)pop(&head, &error); if (error == 1) { + deleteStack(&head); *errorCode = 1; return NULL; } counterForTheOutputArray++; - arrayToOutput[counterForTheOutputArray] = ' '; - counterForTheOutputArray++; + addSpace(&arrayToOutput[counterForTheOutputArray++]); + } + push(&head, array[counter], &error); + if (error == 2) + { + *errorCode = 2; + deleteStack(&head); + return NULL; } - push(&head, array[counter]); } else if (array[counter] == '(') { - push(&head, array[counter]); + push(&head, array[counter], &error); + if (error == 2) + { + *errorCode = 2; + deleteStack(&head); + return NULL; + } } else if (array[counter] == ')') { @@ -94,18 +119,19 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) { if(!isEmpty(head)) { - arrayToOutput[counterForTheOutputArray] = pop(&head, &error); + arrayToOutput[counterForTheOutputArray] = (char)pop(&head, &error); if (error == 1) { + deleteStack(&head); *errorCode = 1; return NULL; } counterForTheOutputArray++; - arrayToOutput[counterForTheOutputArray] = ' '; - counterForTheOutputArray++; + addSpace(&arrayToOutput[counterForTheOutputArray++]); } if (isEmpty(head)) { + deleteStack(&head); *errorCode = 3; return 0; } @@ -115,6 +141,7 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) pop(&head, &error); if (error == 1) { + deleteStack(&head); *errorCode = 1; return NULL; } @@ -141,16 +168,17 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) *errorCode = 3; return NULL; } - arrayToOutput[counterForTheOutputArray] = pop(&head, &error); + arrayToOutput[counterForTheOutputArray] = (char)pop(&head, &error); if (error == 1) { + deleteStack(&head); *errorCode = 1; return NULL; } counterForTheOutputArray++; - arrayToOutput[counterForTheOutputArray] = ' '; - counterForTheOutputArray++; + addSpace(&arrayToOutput[counterForTheOutputArray++]); } arrayToOutput[counterForTheOutputArray - 1] = '\0'; + deleteStack(&head); return arrayToOutput; } \ No newline at end of file From b098b7e516b3bacedcdc496c4f9bfc5c42fd7915 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 27 Oct 2021 01:00:20 +0500 Subject: [PATCH 37/39] fixed top() --- Stack/Stack/Stack.c | 29 ++++++++++++++++++----------- Stack/Stack/Stack.h | 13 ++++++++----- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index a63b467..2a81f17 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,19 +25,19 @@ 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) + 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) @@ -42,7 +49,7 @@ void deleteStack(Stack** head) } } -char top(Stack** head, int* error) +int top(Stack** head, int* error) { *error = 0; if (!isEmpty(*head)) diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index 57ca12b..db2d9d2 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -4,21 +4,24 @@ // 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); +// Creating a stack +Stack* createStack(); + // Function that returns the value of an element from the top of the stack -char top(Stack** head, int* error); \ No newline at end of file +int top(Stack** head, int* error); \ No newline at end of file From b58ccb8984c0d2cac3745d4ac9f3567f3e5592de Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Thu, 4 Nov 2021 19:58:18 +0500 Subject: [PATCH 38/39] fixed push() --- Stack/Stack/Stack.c | 45 ++++++++------ Stack/Stack/Stack.h | 13 ++-- Stack/Stack/StackTest.c | 135 +++++++++++++++++++++++++++++++++------- 3 files changed, 145 insertions(+), 48 deletions(-) diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c index c809346..d3a6b8f 100644 --- a/Stack/Stack/Stack.c +++ b/Stack/Stack/Stack.c @@ -1,17 +1,23 @@ #include "Stack.h" #include -#include 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; @@ -19,36 +25,37 @@ void push(Stack** head, char element) *head = newStack; } -char pop(Stack** head) +int pop(Stack** head, int* error) { - errno = 0; - if (*head != NULL) + *error = 0; + if (*head == NULL) { - const int element = (*head)->value; - Stack* temporary = *head; - *head = (*head)->next; - free(temporary); - return element; + *error = 1; + return 0; } - errno = 1; - return 0; + const int element = (*head)->value; + Stack* temporary = *head; + *head = (*head)->next; + free(temporary); + return element; } void deleteStack(Stack** head) { + int error = 0; while (!isEmpty(*head)) { - pop(head); + pop(head, &error); } } -char top(Stack** head) +int top(Stack** head, int* error) { - errno = 0; - if (!isEmpty(*head)) + *error = 0; + if (isEmpty(*head)) { - return (*head)->value; + *error = 1; + return 0; } - errno = 1; - return 0; + return (*head)->value; } diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h index e6db7b8..d1e73ad 100644 --- a/Stack/Stack/Stack.h +++ b/Stack/Stack/Stack.h @@ -4,21 +4,24 @@ // 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 pop(Stack** head, int* error); // Function for deleting all stack elements void deleteStack(Stack** head); +// Creating a stack +Stack* createStack(); + // Function that returns the value of an element from the top of the stack -char top(Stack** head); \ No newline at end of file +int top(Stack** head, int* error); diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c index 9d0ab00..31c9cfc 100644 --- a/Stack/Stack/StackTest.c +++ b/Stack/Stack/StackTest.c @@ -4,45 +4,132 @@ bool pushTest() { - Stack* head = NULL; - push(&head, '['); - char firstResult = head->value; - push(&head, ')'); - char secondResult = head->value; + Stack* head = createStack(); + int error = 0; + push(&head, '[', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + const int firstResult = head->value; + push(&head, ')', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + const int secondResult = head->value; + deleteStack(&head); return firstResult == '[' && secondResult == ')'; } bool popTest() { - Stack* head = NULL; - push(&head, 'a'); - push(&head, '^'); - push(&head, '1'); - char firstPopResult = pop(&head); - char firstUpperElement = head->value; - char secondPopResult = pop(&head); - char secondUpperElement = head->value; + Stack* head = createStack(); + int error = 0; + push(&head, 'a', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + push(&head, '^', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + push(&head, '1', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + const int firstPopResult = pop(&head, &error); + if (error == 1) + { + deleteStack(&head); + return false; + } + const int firstUpperElement = head->value; + const int secondPopResult = pop(&head, &error); + if (error == 1) + { + deleteStack(&head); + return false; + } + const int secondUpperElement = head->value; + deleteStack(&head); return firstPopResult == '1' && firstUpperElement == '^' && secondPopResult == '^' && secondUpperElement == 'a'; } bool deleteStackTest() { - Stack* head = NULL; - push(&head, 'a'); - push(&head, 'b'); - push(&head, 'c'); + Stack* head = createStack(); + int error = 0; + push(&head, 'a', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + push(&head, 'b', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + push(&head, 'c', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } 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; + Stack* head = createStack(); + int error = 0; + push(&head, 'a', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + const int firstTopResult = top(&head, &error); + if (error == 1) + { + deleteStack(&head); + return false; + } + push(&head, '^', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + const int secondTopResult = top(&head, &error); + if (error == 1) + { + deleteStack(&head); + return false; + } + push(&head, '1', &error); + if (error == 2) + { + deleteStack(&head); + return false; + } + const int thirdTopResult = top(&head, &error); + if (error == 1) + { + deleteStack(&head); + return false; + } + deleteStack(&head); return firstTopResult == 'a' && secondTopResult == '^' && thirdTopResult == '1'; } \ No newline at end of file From a5b71c969d6a3105edeaa173a54bebfec1118cd1 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Thu, 4 Nov 2021 20:45:22 +0500 Subject: [PATCH 39/39] added new features, fixed memory leaks --- sortStation/sortStation/SortStantionTest.c | 38 +++++++-------- sortStation/sortStation/SortStation.c | 57 ++++++++++++---------- 2 files changed, 50 insertions(+), 45 deletions(-) diff --git a/sortStation/sortStation/SortStantionTest.c b/sortStation/sortStation/SortStantionTest.c index 6eb7999..4e475ea 100644 --- a/sortStation/sortStation/SortStantionTest.c +++ b/sortStation/sortStation/SortStantionTest.c @@ -6,23 +6,23 @@ bool translationIntoPostfixFormAreTestPassing() { int errors[10] = {0}; return strcmp("7 6 - 7 6 4 - 2 * - *", translationIntoPostfixForm("(7 - 6) * (7 - (6 - 4) * 2)", &errors[0])) == 0 - && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm("6 * (8 - 4) / 2 + 4", &errors[1])) == 0 - && strcmp("3 4 5 5 + * 2 / - 7 4 6 - - +", translationIntoPostfixForm("3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))", &errors[2])) == 0 - && strcmp("1 8 - 9 + 4 1 3 + * -", translationIntoPostfixForm("1 - 8 + 9 - 4 * (1 + 3)", &errors[3])) == 0 - && strcmp("1 2 - 3 +", translationIntoPostfixForm("1 - 2 + 3", &errors[4])) == 0 - && translationIntoPostfixForm("( 7 + * 2)", &errors[5]) == NULL - && translationIntoPostfixForm("7--", &errors[6]) == NULL - && translationIntoPostfixForm("(7 - 3) * 4 - 5)", &errors[7]) == NULL - && translationIntoPostfixForm("2 + 2)", &errors[8]) == NULL - && translationIntoPostfixForm("12 - 8 / a", &errors[9]) == NULL - && errors[0] == 0 - && errors[1] == 0 - && errors[2] == 0 - && errors[3] == 0 - && errors[4] == 0 - && errors[5] == 1 - && errors[6] == 1 - && errors[7] == 3 - && errors[8] == 3 - && errors[9] == 3; + && strcmp("6 8 4 - * 2 / 4 +", translationIntoPostfixForm("6 * (8 - 4) / 2 + 4", &errors[1])) == 0 + && strcmp("3 4 5 5 + * 2 / - 7 4 6 - - +", translationIntoPostfixForm("3 - 4 * (5 + 5) / 2 + (7 - (4 - 6))", &errors[2])) == 0 + && strcmp("1 8 - 9 + 4 1 3 + * -", translationIntoPostfixForm("1 - 8 + 9 - 4 * (1 + 3)", &errors[3])) == 0 + && strcmp("1 2 - 3 +", translationIntoPostfixForm("1 - 2 + 3", &errors[4])) == 0 + && translationIntoPostfixForm("( 7 +* 2)", &errors[5]) == NULL + && translationIntoPostfixForm("7--", &errors[6]) == NULL + && translationIntoPostfixForm("(7 - 3) * 4 - 5)", &errors[7]) == NULL + && translationIntoPostfixForm("2 + 2)", &errors[8]) == NULL + && translationIntoPostfixForm("12 - 8 / a", &errors[9]) == NULL + && errors[0] == 0 + && errors[1] == 0 + && errors[2] == 0 + && errors[3] == 0 + && errors[4] == 0 + && errors[5] == 1 + && errors[6] == 1 + && errors[7] == 3 + && errors[8] == 3 + && errors[9] == 3; } \ No newline at end of file diff --git a/sortStation/sortStation/SortStation.c b/sortStation/sortStation/SortStation.c index 1a19c70..3ab54b2 100644 --- a/sortStation/sortStation/SortStation.c +++ b/sortStation/sortStation/SortStation.c @@ -3,33 +3,36 @@ #include #include -bool isRepeatTheOperation(char operation) +bool isSecondOperationInRow(char operation) { return operation == '+' || operation == '-' || operation == '*' || operation == '/'; } -bool isPriorityForDivisionandMultiplication(char operation) +bool isDivisionAndMultiplication(char operation) { return operation == '*' || operation == '/'; } bool isPriorityForMinusAndPlus(char operation) { - return isPriorityForDivisionandMultiplication(operation) || operation == '-' || operation == '+'; + return isDivisionAndMultiplication(operation) || operation == '-' || operation == '+'; } -void addSpace(char* symbol) +void writeCharacterToArrayToOutput(char* arrayToOutput, char character, int* counterForTheOutputArray) { - *symbol = ' '; + arrayToOutput[*counterForTheOutputArray] = character; + (*counterForTheOutputArray)++; + arrayToOutput[*counterForTheOutputArray] = ' '; + (*counterForTheOutputArray)++; } char* translationIntoPostfixForm(const char* array, int* errorCode) { Stack* head = createStack(); + char* arrayToOutput = (char*)calloc((2 * strlen(array) + 1), sizeof(char)); int counterForTheOutputArray = 0; int counter = 0; int error = 0; - char* arrayToOutput = (char*)calloc((2 * strlen(array) + 1), sizeof(char)); if (arrayToOutput == NULL) { *errorCode = 2; @@ -40,65 +43,64 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) { if (array[counter] >= '0' && array[counter] <= '9') { - arrayToOutput[counterForTheOutputArray] = array[counter]; - counterForTheOutputArray++; - addSpace(&arrayToOutput[counterForTheOutputArray++]); + writeCharacterToArrayToOutput(arrayToOutput, array[counter], &counterForTheOutputArray); counter++; continue; } else if (array[counter] == '-' || array[counter] == '+') { - if (isRepeatTheOperation(array[counter + 1]) || isRepeatTheOperation(array[counter + 2])) + if (isSecondOperationInRow(array[counter + 1])) { *errorCode = 1; + free(arrayToOutput); deleteStack(&head); return NULL; } while (!isEmpty(head) && isPriorityForMinusAndPlus((char)top(&head, &error))) { - arrayToOutput[counterForTheOutputArray] = (char)pop(&head, &error); + writeCharacterToArrayToOutput(arrayToOutput, (char)pop(&head, &error), &counterForTheOutputArray); if (error == 1) { + free(arrayToOutput); deleteStack(&head); *errorCode = 1; return NULL; } - counterForTheOutputArray++; - addSpace(&arrayToOutput[counterForTheOutputArray++]); } push(&head, array[counter], &error); if (error == 2) { *errorCode = 2; + free(arrayToOutput); deleteStack(&head); return NULL; } } else if (array[counter] == '/' || array[counter] == '*') { - if (isRepeatTheOperation(array[counter + 1]) || isRepeatTheOperation(array[counter + 2])) + if (isSecondOperationInRow(array[counter + 1])) { deleteStack(&head); + free(arrayToOutput); *errorCode = 1; return NULL; } - - while (!isEmpty(head) && isPriorityForDivisionandMultiplication((char)top(&head, &error))) + while (!isEmpty(head) && isDivisionAndMultiplication((char)top(&head, &error))) { - arrayToOutput[counterForTheOutputArray] = (char)pop(&head, &error); + writeCharacterToArrayToOutput(arrayToOutput, (char)pop(&head, &error), &counterForTheOutputArray); if (error == 1) { deleteStack(&head); + free(arrayToOutput); *errorCode = 1; return NULL; } - counterForTheOutputArray++; - addSpace(&arrayToOutput[counterForTheOutputArray++]); } push(&head, array[counter], &error); if (error == 2) { *errorCode = 2; + free(arrayToOutput); deleteStack(&head); return NULL; } @@ -109,6 +111,7 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) if (error == 2) { *errorCode = 2; + free(arrayToOutput); deleteStack(&head); return NULL; } @@ -117,21 +120,21 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) { while (top(&head, &error) != '(') { - if(!isEmpty(head)) + if (!isEmpty(head)) { - arrayToOutput[counterForTheOutputArray] = (char)pop(&head, &error); + writeCharacterToArrayToOutput(arrayToOutput, (char)pop(&head, &error), &counterForTheOutputArray); if (error == 1) { deleteStack(&head); + free(arrayToOutput); *errorCode = 1; return NULL; } - counterForTheOutputArray++; - addSpace(&arrayToOutput[counterForTheOutputArray++]); } if (isEmpty(head)) { deleteStack(&head); + free(arrayToOutput); *errorCode = 3; return 0; } @@ -142,6 +145,7 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) if (error == 1) { deleteStack(&head); + free(arrayToOutput); *errorCode = 1; return NULL; } @@ -155,6 +159,7 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) else { deleteStack(&head); + free(arrayToOutput); *errorCode = 3; return NULL; } @@ -165,18 +170,18 @@ char* translationIntoPostfixForm(const char* array, int* errorCode) if (top(&head, &error) == '(') { deleteStack(&head); + free(arrayToOutput); *errorCode = 3; return NULL; } - arrayToOutput[counterForTheOutputArray] = (char)pop(&head, &error); + writeCharacterToArrayToOutput(arrayToOutput, (char)pop(&head, &error), &counterForTheOutputArray); if (error == 1) { deleteStack(&head); + free(arrayToOutput); *errorCode = 1; return NULL; } - counterForTheOutputArray++; - addSpace(&arrayToOutput[counterForTheOutputArray++]); } arrayToOutput[counterForTheOutputArray - 1] = '\0'; deleteStack(&head);