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..970d023 --- /dev/null +++ b/Stack/Stack/Main.c @@ -0,0 +1,11 @@ +#include "StackTest.h" +#include + +int main() +{ + if (!pushTest() || !popTest() || !deleteStackTest() || !topTest()) + { + printf("Test failed"); + return -1; + } +} \ No newline at end of file diff --git a/Stack/Stack/Stack.c b/Stack/Stack/Stack.c new file mode 100644 index 0000000..d3a6b8f --- /dev/null +++ b/Stack/Stack/Stack.c @@ -0,0 +1,61 @@ +#include "Stack.h" +#include + +bool isEmpty(Stack* head) +{ + return head == NULL; +} + +Stack* createStack() +{ + return NULL; +} + +void push(Stack** head, int element, int* error) +{ + *error = 0; + Stack* newStack = (Stack*)calloc(1, sizeof(Stack)); + if (newStack == NULL) + { + *error = 2; + return; + } + newStack->value = element; + newStack->next = *head; + *head = newStack; +} + +int pop(Stack** head, int* error) +{ + *error = 0; + if (*head == NULL) + { + *error = 1; + return 0; + } + const int element = (*head)->value; + Stack* temporary = *head; + *head = (*head)->next; + free(temporary); + return element; +} + +void deleteStack(Stack** head) +{ + int error = 0; + while (!isEmpty(*head)) + { + pop(head, &error); + } +} + +int top(Stack** head, int* error) +{ + *error = 0; + if (isEmpty(*head)) + { + *error = 1; + return 0; + } + return (*head)->value; +} diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h new file mode 100644 index 0000000..d1e73ad --- /dev/null +++ b/Stack/Stack/Stack.h @@ -0,0 +1,27 @@ +#pragma once +#include + +// Structure for implementing a stack consisting of a value and a pointer to the next element +typedef struct Stack +{ + int value; + struct Stack* next; +} Stack; + +// Function for testing if stack is empty. +bool isEmpty(Stack* head); + +// Function for adding an element to the top of the stack +void push(Stack** head, int element, int* error); + +// Function to remove an element from the top of the stack that returns the value of that element +int pop(Stack** head, int* error); + +// Function for deleting all stack elements +void deleteStack(Stack** head); + +// Creating a stack +Stack* createStack(); + +// Function that returns the value of an element from the top of the stack +int top(Stack** head, int* error); diff --git a/Stack/Stack/Stack.vcxproj b/Stack/Stack/Stack.vcxproj new file mode 100644 index 0000000..6ced32d --- /dev/null +++ b/Stack/Stack/Stack.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {a4f19b27-54c9-4841-98b0-728edcd283d2} + Stack + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Stack/Stack/Stack.vcxproj.filters b/Stack/Stack/Stack.vcxproj.filters new file mode 100644 index 0000000..ff73484 --- /dev/null +++ b/Stack/Stack/Stack.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/Stack/Stack/StackTest.c b/Stack/Stack/StackTest.c new file mode 100644 index 0000000..ed4da0d --- /dev/null +++ b/Stack/Stack/StackTest.c @@ -0,0 +1,136 @@ +#include "StackTest.h" +#include "Stack.h" +#include + +bool pushTest() +{ + 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 = 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 = 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 = 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 diff --git a/Stack/Stack/StackTest.h b/Stack/Stack/StackTest.h new file mode 100644 index 0000000..280c3ce --- /dev/null +++ b/Stack/Stack/StackTest.h @@ -0,0 +1,14 @@ +#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(); + +// Function for testing a function that returns the value of an element from the top of the stack +bool topTest(); 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/Main.c b/sortStation/sortStation/Main.c new file mode 100644 index 0000000..46d9d11 --- /dev/null +++ b/sortStation/sortStation/Main.c @@ -0,0 +1,34 @@ +#include "../../Stack/Stack/Stack.h" +#include "SortStantion.h" +#include "SortStantionTest.h" +#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)); + int errorCode = 0; + const char* const arrayToOutput = translationIntoPostfixForm(array, &errorCode); + if (errorCode == 1) + { + printf("Incorrectly entered expression"); + return -1; + } + if (errorCode == 2) + { + printf("Memory not allocated"); + return -1; + } + if (errorCode == 3) + { + printf("A parenthesis is missing or an invalid character is entered"); + return -1; + } + printf("%s\n", arrayToOutput); +} \ No newline at end of file diff --git a/sortStation/sortStation/SortStantion.h b/sortStation/sortStation/SortStantion.h new file mode 100644 index 0000000..0526bbb --- /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(const char* array, int* errorCode); diff --git a/sortStation/sortStation/SortStantionTest.c b/sortStation/sortStation/SortStantionTest.c new file mode 100644 index 0000000..4e475ea --- /dev/null +++ b/sortStation/sortStation/SortStantionTest.c @@ -0,0 +1,28 @@ +#include "SortStantionTest.h" +#include "SortStantion.h" +#include + +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; +} \ No newline at end of file diff --git a/sortStation/sortStation/SortStantionTest.h b/sortStation/sortStation/SortStantionTest.h new file mode 100644 index 0000000..a053c55 --- /dev/null +++ b/sortStation/sortStation/SortStantionTest.h @@ -0,0 +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 new file mode 100644 index 0000000..3ab54b2 --- /dev/null +++ b/sortStation/sortStation/SortStation.c @@ -0,0 +1,189 @@ +#include "../../Stack/Stack/Stack.h" +#include +#include +#include + +bool isSecondOperationInRow(char operation) +{ + return operation == '+' || operation == '-' || operation == '*' || operation == '/'; +} + +bool isDivisionAndMultiplication(char operation) +{ + return operation == '*' || operation == '/'; +} + +bool isPriorityForMinusAndPlus(char operation) +{ + return isDivisionAndMultiplication(operation) || operation == '-' || operation == '+'; +} + +void writeCharacterToArrayToOutput(char* arrayToOutput, char character, int* counterForTheOutputArray) +{ + 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; + if (arrayToOutput == NULL) + { + *errorCode = 2; + deleteStack(&head); + return NULL; + } + while (array[counter] != '\0') + { + if (array[counter] >= '0' && array[counter] <= '9') + { + writeCharacterToArrayToOutput(arrayToOutput, array[counter], &counterForTheOutputArray); + counter++; + continue; + } + else if (array[counter] == '-' || array[counter] == '+') + { + if (isSecondOperationInRow(array[counter + 1])) + { + *errorCode = 1; + free(arrayToOutput); + deleteStack(&head); + return NULL; + } + while (!isEmpty(head) && isPriorityForMinusAndPlus((char)top(&head, &error))) + { + writeCharacterToArrayToOutput(arrayToOutput, (char)pop(&head, &error), &counterForTheOutputArray); + if (error == 1) + { + free(arrayToOutput); + deleteStack(&head); + *errorCode = 1; + return NULL; + } + } + push(&head, array[counter], &error); + if (error == 2) + { + *errorCode = 2; + free(arrayToOutput); + deleteStack(&head); + return NULL; + } + } + else if (array[counter] == '/' || array[counter] == '*') + { + if (isSecondOperationInRow(array[counter + 1])) + { + deleteStack(&head); + free(arrayToOutput); + *errorCode = 1; + return NULL; + } + while (!isEmpty(head) && isDivisionAndMultiplication((char)top(&head, &error))) + { + writeCharacterToArrayToOutput(arrayToOutput, (char)pop(&head, &error), &counterForTheOutputArray); + if (error == 1) + { + deleteStack(&head); + free(arrayToOutput); + *errorCode = 1; + return NULL; + } + } + push(&head, array[counter], &error); + if (error == 2) + { + *errorCode = 2; + free(arrayToOutput); + deleteStack(&head); + return NULL; + } + } + else if (array[counter] == '(') + { + push(&head, array[counter], &error); + if (error == 2) + { + *errorCode = 2; + free(arrayToOutput); + deleteStack(&head); + return NULL; + } + } + else if (array[counter] == ')') + { + while (top(&head, &error) != '(') + { + if (!isEmpty(head)) + { + writeCharacterToArrayToOutput(arrayToOutput, (char)pop(&head, &error), &counterForTheOutputArray); + if (error == 1) + { + deleteStack(&head); + free(arrayToOutput); + *errorCode = 1; + return NULL; + } + } + if (isEmpty(head)) + { + deleteStack(&head); + free(arrayToOutput); + *errorCode = 3; + return 0; + } + } + if ((!isEmpty(head)) && top(&head, &error) == '(') + { + pop(&head, &error); + if (error == 1) + { + deleteStack(&head); + free(arrayToOutput); + *errorCode = 1; + return NULL; + } + } + } + else if (array[counter] == ' ') + { + counter++; + continue; + } + else + { + deleteStack(&head); + free(arrayToOutput); + *errorCode = 3; + return NULL; + } + counter++; + } + while (!isEmpty(head)) + { + if (top(&head, &error) == '(') + { + deleteStack(&head); + free(arrayToOutput); + *errorCode = 3; + return NULL; + } + writeCharacterToArrayToOutput(arrayToOutput, (char)pop(&head, &error), &counterForTheOutputArray); + if (error == 1) + { + deleteStack(&head); + free(arrayToOutput); + *errorCode = 1; + return NULL; + } + } + arrayToOutput[counterForTheOutputArray - 1] = '\0'; + deleteStack(&head); + return arrayToOutput; +} \ No newline at end of file diff --git a/sortStation/sortStation/sortStation.vcxproj b/sortStation/sortStation/sortStation.vcxproj new file mode 100644 index 0000000..459486e --- /dev/null +++ b/sortStation/sortStation/sortStation.vcxproj @@ -0,0 +1,155 @@ + + + + + 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;_CRT_SECURE_NO_WARNINGS;_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..35e6026 --- /dev/null +++ b/sortStation/sortStation/sortStation.vcxproj.filters @@ -0,0 +1,42 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file