diff --git a/Postfixform/Postfixform.sln b/Postfixform/Postfixform.sln new file mode 100644 index 0000000..96a9e8b --- /dev/null +++ b/Postfixform/Postfixform.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}") = "Postfixform", "Postfixform\Postfixform.vcxproj", "{15009E36-9D86-441E-A9EA-947AAC734F19}" +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 + {15009E36-9D86-441E-A9EA-947AAC734F19}.Debug|x64.ActiveCfg = Debug|x64 + {15009E36-9D86-441E-A9EA-947AAC734F19}.Debug|x64.Build.0 = Debug|x64 + {15009E36-9D86-441E-A9EA-947AAC734F19}.Debug|x86.ActiveCfg = Debug|Win32 + {15009E36-9D86-441E-A9EA-947AAC734F19}.Debug|x86.Build.0 = Debug|Win32 + {15009E36-9D86-441E-A9EA-947AAC734F19}.Release|x64.ActiveCfg = Release|x64 + {15009E36-9D86-441E-A9EA-947AAC734F19}.Release|x64.Build.0 = Release|x64 + {15009E36-9D86-441E-A9EA-947AAC734F19}.Release|x86.ActiveCfg = Release|Win32 + {15009E36-9D86-441E-A9EA-947AAC734F19}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C330BC54-81A6-4E6B-9775-51E396ACC586} + EndGlobalSection +EndGlobal diff --git a/Postfixform/Postfixform/Main.c b/Postfixform/Postfixform/Main.c new file mode 100644 index 0000000..d80a502 --- /dev/null +++ b/Postfixform/Postfixform/Main.c @@ -0,0 +1,34 @@ +#include "../../Stack/Stack/Stack.h" +#include "Postfix.h" +#include "PostfixFormTest.h" +#include + +int main() +{ + if (!areTestPassingPostfixForm()) + { + printf("Test failed"); + return -1; + } + char postfixEntry[250] = { '\0' }; + printf("enter the expression in postfix form\n"); + scanf_s("%[^\n]s", postfixEntry, (unsigned)sizeof(postfixEntry)); + int errorCode = 0; + const float answer = countTheExpression(postfixEntry, &errorCode); + if (errorCode == 1) + { + printf("Incorrect input of an expression in postfix form"); + return -1; + } + if (errorCode == 2) + { + printf("invalid character in the expression entry"); + return -1; + } + if (errorCode == 3) + { + printf("it is impossible to divide by 0"); + return -1; + } + printf("%f", answer); +} \ No newline at end of file diff --git a/Postfixform/Postfixform/Postfix.c b/Postfixform/Postfixform/Postfix.c new file mode 100644 index 0000000..3d2b162 --- /dev/null +++ b/Postfixform/Postfixform/Postfix.c @@ -0,0 +1,77 @@ +#include "../../Stack/Stack/Stack.h" +#include "Postfix.h" +#include + +float countTheExpression(const char* postfixEntry, int* errorCode) +{ + Stack* head = NULL; + int counter = 0; + int error = 0; + while (postfixEntry[counter] != '\0') + { + if (postfixEntry[counter] >= '0' && postfixEntry[counter] <= '9') + { + push(&head, (float)postfixEntry[counter] - '0'); + counter++; + continue; + } + else if (postfixEntry[counter] == ' ') + { + counter++; + continue; + } + float secondNumber = pop(&head, &error); + if (error == 1) + { + *errorCode = error; + return 0; + } + float firstNumber = pop(&head, &error); + if (error == 1) + { + *errorCode = error; + return 0; + } + if (postfixEntry[counter] == '-') + { + push(&head, (firstNumber - secondNumber)); + } + else if (postfixEntry[counter] == '+') + { + push(&head, firstNumber + secondNumber); + } + else if (postfixEntry[counter] == '*') + { + push(&head, firstNumber * secondNumber); + } + else if (postfixEntry[counter] == '/') + { + if (secondNumber == 0) + { + *errorCode = 3; + return 0; + } + push(&head, firstNumber / secondNumber); + } + else + { + deleteStack(&head); + *errorCode = 2; + return 0; + } + counter++; + } + const float answer = pop(&head, &error); + if (error == 1) + { + *errorCode = error; + return 0; + } + if (!isEmpty(head)) + { + deleteStack(&head); + *errorCode = 1; + return 0; + } + return answer; +} \ No newline at end of file diff --git a/Postfixform/Postfixform/Postfix.h b/Postfixform/Postfixform/Postfix.h new file mode 100644 index 0000000..2523b3c --- /dev/null +++ b/Postfixform/Postfixform/Postfix.h @@ -0,0 +1,4 @@ +#pragma once + +// Function for calculating the value of an expression in postfix form +float countTheExpression(const char* postfixEntry, int* errorCode); \ No newline at end of file diff --git a/Postfixform/Postfixform/PostfixFormTest.c b/Postfixform/Postfixform/PostfixFormTest.c new file mode 100644 index 0000000..343078b --- /dev/null +++ b/Postfixform/Postfixform/PostfixFormTest.c @@ -0,0 +1,31 @@ +#include "PostfixFormTest.h" +#include "Postfix.h" + +bool areTestPassingPostfixForm() +{ + const char firstCorrectPostfixEntry[250] = "3 2 - 4 5 * + 2 9 - *"; + const char secondCorrectPostfixEntry[250] = "2 3 - 4 5 * +"; + const char thirdCorrectPostfixEntry[250] = "9 6 - 1 2 + *"; + const char fourthCorrectPostfixEntry[250] = "9 6 - 5 2 / +"; + const char fifthCorrectPostfixEntry[250] = "9 6 - 3 -"; + + const char firstIncorrectPostfixEntry[250] = "3 5 a"; + const char secondIncorrectPostfixEntry[250] = "4 0 /"; + const char thirdIncorrectPostfixEntry[250] = "34 - 345"; + const char fourthIncorrectPostfixEntry[250] = "12 - 34 + 4"; + const char fifthIncorrectPostfixEntry[250] = "1234 - 12"; + + int errorCode[10] = {0}; + + return countTheExpression(firstCorrectPostfixEntry, &errorCode[0]) == -147 && errorCode[0] == 0 + && countTheExpression(secondCorrectPostfixEntry, &errorCode[1]) == 19 && errorCode[1] == 0 + && countTheExpression(thirdCorrectPostfixEntry, &errorCode[2]) == 9 && errorCode[2] == 0 + && countTheExpression(fourthCorrectPostfixEntry, &errorCode[3]) == 5.5 && errorCode[3] == 0 + && countTheExpression(fifthCorrectPostfixEntry, &errorCode[4]) == 0 && errorCode[4] == 0 + + && countTheExpression(firstIncorrectPostfixEntry, &errorCode[5]) == 0 && errorCode[5] == 2 + && countTheExpression(secondIncorrectPostfixEntry, &errorCode[6]) == 0 && errorCode[6] == 3 + && countTheExpression(thirdIncorrectPostfixEntry, &errorCode[7]) == 0 && errorCode[7] == 1 + && countTheExpression(fourthIncorrectPostfixEntry, &errorCode[8]) == 0 && errorCode[8] == 1 + && countTheExpression(fifthIncorrectPostfixEntry, &errorCode[9]) == 0 && errorCode[9] == 1; +} \ No newline at end of file diff --git a/Postfixform/Postfixform/PostfixFormTest.h b/Postfixform/Postfixform/PostfixFormTest.h new file mode 100644 index 0000000..2a977d7 --- /dev/null +++ b/Postfixform/Postfixform/PostfixFormTest.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function for testing a function that translates an expression from a postfix form +bool areTestPassingPostfixForm(); \ No newline at end of file diff --git a/Postfixform/Postfixform/Postfixform.vcxproj b/Postfixform/Postfixform/Postfixform.vcxproj new file mode 100644 index 0000000..dffc7d6 --- /dev/null +++ b/Postfixform/Postfixform/Postfixform.vcxproj @@ -0,0 +1,170 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + + + 16.0 + Win32Proj + {15009e36-9d86-441e-a9ea-947aac734f19} + Postfixform + 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 + + + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Postfixform/Postfixform/Postfixform.vcxproj.filters b/Postfixform/Postfixform/Postfixform.vcxproj.filters new file mode 100644 index 0000000..a524e54 --- /dev/null +++ b/Postfixform/Postfixform/Postfixform.vcxproj.filters @@ -0,0 +1,42 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/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..6eff2a4 --- /dev/null +++ b/Stack/Stack/Main.c @@ -0,0 +1,10 @@ +#include "StackTest.h" +#include + +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..d838ed5 --- /dev/null +++ b/Stack/Stack/Stack.c @@ -0,0 +1,43 @@ +#include "Stack.h" +#include + +bool isEmpty(Stack* head) +{ + return head == NULL; +} + +void push(Stack** head, float element) +{ + Stack* newStack = (Stack*)calloc(1, sizeof(Stack)); + if (newStack == NULL) + { + return; + } + newStack->value = element; + newStack->next = *head; + *head = newStack; +} + +float pop(Stack** head, int* error) +{ + *error = 0; + if (*head != NULL) + { + const float element = (*head)->value; + Stack* temporary = *head; + *head = (*head)->next; + free(temporary); + return element; + } + *error = 1; + return 0; +} + +void deleteStack(Stack** head) +{ + int error = 0; + while (!isEmpty(*head)) + { + pop(head, &error); + } +} diff --git a/Stack/Stack/Stack.h b/Stack/Stack/Stack.h new file mode 100644 index 0000000..9555b4e --- /dev/null +++ b/Stack/Stack/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 +{ + float 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, float element); + +// Function to remove an element from the top of the stack that returns the value of that element +float pop(Stack** head, int* error); + +// Function for deleting all stack elements +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..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..96d6c80 --- /dev/null +++ b/Stack/Stack/StackTest.c @@ -0,0 +1,47 @@ +#include "StackTest.h" +#include "Stack.h" +#include + +bool pushTest() +{ + Stack* head = NULL; + push(&head, 12); + const float firstResult = head->value; + push(&head, 128); + const float secondResult = head->value; + deleteStack(&head); + return firstResult == 12 && secondResult == 128; +} + +bool popTest() +{ + Stack* head = NULL; + push(&head, 12); + push(&head, 128); + push(&head, 147); + int error = 0; + const float firstPopResult = pop(&head, &error); + if (error == 1) + { + return false; + } + const float firstUpperElement = head->value; + const float secondPopResult = pop(&head, &error); + if (error == 1) + { + return false; + } + const float secondUpperElement = head->value; + deleteStack(&head); + 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/Stack/Stack/StackTest.h b/Stack/Stack/StackTest.h new file mode 100644 index 0000000..ca939fc --- /dev/null +++ b/Stack/Stack/StackTest.h @@ -0,0 +1,11 @@ +#pragma once +#include + +// Function to check the function that adds an element to the top of the stack +bool pushTest(); + +// Function to check the function that removes from the top of the stack +bool popTest(); + +// Function for checking a function that removes all stack elements +bool deleteStackTest(); \ No newline at end of file