diff --git a/HomeworksAccept/homework21.09/read.me b/HomeworksAccept/homework21.09/read.me deleted file mode 100644 index 5e4d090..0000000 --- a/HomeworksAccept/homework21.09/read.me +++ /dev/null @@ -1 +0,0 @@ -3 домашка diff --git a/HomeworksAccept/homework28.09/read.me b/HomeworksAccept/homework28.09/read.me deleted file mode 100644 index 5061ad0..0000000 --- a/HomeworksAccept/homework28.09/read.me +++ /dev/null @@ -1 +0,0 @@ -4 домашка diff --git a/HomeworksAccept/read.me b/HomeworksAccept/read.me deleted file mode 100644 index 1773bf8..0000000 --- a/HomeworksAccept/read.me +++ /dev/null @@ -1 +0,0 @@ -Здесь представлены домашнии работы, прошедшии проверку diff --git a/sortStation/sortStation.sln b/sortStation/sortStation.sln new file mode 100644 index 0000000..3b3d18f --- /dev/null +++ b/sortStation/sortStation.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sortStation", "sortStation\sortStation.vcxproj", "{6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}" +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 + {6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}.Debug|x64.ActiveCfg = Debug|x64 + {6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}.Debug|x64.Build.0 = Debug|x64 + {6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}.Debug|x86.ActiveCfg = Debug|Win32 + {6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}.Debug|x86.Build.0 = Debug|Win32 + {6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}.Release|x64.ActiveCfg = Release|x64 + {6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}.Release|x64.Build.0 = Release|x64 + {6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}.Release|x86.ActiveCfg = Release|Win32 + {6E239FD9-AD88-4EA0-B69A-CC5CF6417AD7}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3014B97C-51B5-43EE-9278-48A66DA4321D} + EndGlobalSection +EndGlobal diff --git a/sortStation/sortStation/main.c b/sortStation/sortStation/main.c new file mode 100644 index 0000000..c68158d --- /dev/null +++ b/sortStation/sortStation/main.c @@ -0,0 +1,175 @@ +#include +#include +#include "stack.h" +#include "queue.h" +#include +#include + +void returnOperation(Stack* operatorActions, char symbol[], int* errorCode, Queue* queue) { + char operations = pop(operatorActions, errorCode); + symbol[0] = operations; + if (*errorCode != 0) { + clear(queue); + deleteStack(&operatorActions); + return false; + } + if (operations != '(') { + enqueue(queue, symbol); + } +} + +bool converterToPostfixForm(const char buffer[], char result[]) { + size_t sizeBuffer = strlen(buffer); + Queue queue = { NULL, NULL }; + Stack* operatorActions = createStack(); + size_t i = 0; + int errorCode = 0; + char number[100] = { '\0' }; + while (i < sizeBuffer) { + memset(number, 0, strlen(number)); + int walker = 0; + while (buffer[i] >= '0' && buffer[i] <= '9') { + number[walker] = buffer[i]; + ++i; + ++walker; + } + if (strlen(number) != 0) { + enqueue(&queue, number); + } + if (buffer[i] == '*' || buffer[i] == '+' || buffer[i] == '/' || buffer[i] == '-' || buffer[i] == '(' || buffer[i] == ')') { + if (isEmpty(operatorActions)) { + push(operatorActions, buffer[i]); + } + else if (buffer[i] == '(') { + push(operatorActions, buffer[i]); + } + else if (top(operatorActions) == '(') { + push(operatorActions, buffer[i]); + } + else if (buffer[i] == ')') { + while (!isEmpty(operatorActions)) { + char symbol[2] = { '\0' }; + returnOperation(operatorActions, symbol, &errorCode, &queue); + if (errorCode != 0) { + return false; + } + } + } + else if (buffer[i] == '*' || buffer[i] == '/') { + if (top(operatorActions) == '/' || top(operatorActions) == '*') { + while (!isEmpty(operatorActions) && (top(operatorActions) == '/' || top(operatorActions) == '*')) { + char symbol[2] = { '\0' }; + returnOperation(operatorActions, symbol, &errorCode, &queue); + if (errorCode != 0) { + return false; + } + } + } + push(operatorActions, buffer[i]); + } + else if (buffer[i] == '+' || buffer[i] == '-') { + while (!isEmpty(operatorActions)) { + char symbol[2] = { '\0' }; + returnOperation(operatorActions, symbol, &errorCode, &queue); + if (errorCode != 0) { + return false; + } + } + push(operatorActions, buffer[i]); + } + } + ++i; + } + while (!isEmpty(operatorActions)) { + char symbol[2] = { '\0' }; + returnOperation(operatorActions, symbol, &errorCode, &queue); + if (errorCode != 0) { + return false; + } + } + size_t j = 0; + while (!isEmptyQueue(queue)) { + char queueElements[100] = { '\0' }; + dequeue(&queue, &errorCode, queueElements); + if (errorCode != 0) { + clear(&queue); + return false; + } + size_t sizeQueueElements = strlen(queueElements); + size_t i = 0; + while (i < sizeQueueElements) { + result[j] = queueElements[i]; + ++i; + ++j; + } + result[j] = ' '; + ++j; + } + if (!isEmptyQueue(queue)) { + clear(&queue); + return false; + } + if (operatorActions != NULL) { + if (!isEmpty(operatorActions)) { + deleteStack(&operatorActions); + return false; + } + deleteStack(&operatorActions); + } + return true; +} + +bool firstTest() { + char buffer[] = "(1 + 2) * 2"; + char arrayOutput[100] = { '\0' }; + if (!converterToPostfixForm(buffer, arrayOutput)) { + return false; + } + char bufferNew[] = "1 2 + 2 *"; + size_t sizeArrayOutput = strlen(arrayOutput); + + for (int i = 0; i < sizeArrayOutput - 1; ++i) { + if (bufferNew[i] != arrayOutput[i]) { + return false; + } + } + return true; +} + +bool secondTest() { + char buffer[] = "(1 + 2) * (1 + 2)"; + char arrayOutput[100] = { '\0' }; + if (!converterToPostfixForm(buffer, arrayOutput)) { + return false; + } + char bufferNew[] = "1 2 + 1 2 + *"; + size_t sizeArrayOutput = strlen(arrayOutput); + + for (int i = 0; i < sizeArrayOutput - 1; ++i) { + if (bufferNew[i] != arrayOutput[i]) { + return false; + } + } + return true; +} + +int main() { + setlocale(LC_ALL, "RUS"); + + if (firstTest() && secondTest()) { + printf(" \n"); + } else { + printf(" ...\n"); + return -1; + } + + printf(" \n"); + char buffer[100] = { '\0' }; + gets_s(buffer, 100); + char result[100] = { '\0' }; + if (converterToPostfixForm(buffer, result)) { + printf("%s", result); + } else { + printf(" ...\n"); + } +} \ No newline at end of file diff --git a/sortStation/sortStation/queue.c b/sortStation/sortStation/queue.c new file mode 100644 index 0000000..9fd8b41 --- /dev/null +++ b/sortStation/sortStation/queue.c @@ -0,0 +1,65 @@ +#include "queue.h" +#include +#include +#include + +int enqueue(Queue* queue, char value[]) { + Node* temp = calloc(1, sizeof(Node)); + if (temp == NULL) { + printf("Problems with memory allocation"); + return -1; + } + size_t sizeValue = strlen(value); + memset(temp->value, 0, strlen(temp->value)); + strncpy(temp->value, value, sizeValue); + temp->next = NULL; + + if (queue->tail == NULL) { + queue->tail = temp; + queue->head = temp; + } + else { + queue->tail->next = temp; + queue->tail = temp; + } + + return 0; +} + +void dequeue(Queue* queue, int* errorCode, char value[]) { + if (queue->head == NULL) { + if (errorCode != NULL) { + *errorCode = -1; + } + + return 0; + } + if (errorCode != NULL) { + *errorCode = 0; + } + + size_t sizeQueueHeadValue = strlen(queue->head->value); + strncpy(value, queue->head->value, sizeQueueHeadValue); + + Node* next = queue->head->next; + //free(queue->head); + memset(queue->head->value, 0, strlen(queue->head->value)); + (queue->head)->next = NULL; + queue->head = NULL; + queue->head = next; + if (next == NULL) { + queue->tail = NULL; + } +} + +bool isEmptyQueue(Queue queue) { + return queue.head == NULL; +} + +void clear(Queue* queue) { + while (!isEmptyQueue(*queue)) { + int errorCode = 0; + dequeue(queue, &errorCode, ""); + } + queue = NULL; +} \ No newline at end of file diff --git a/sortStation/sortStation/queue.h b/sortStation/sortStation/queue.h new file mode 100644 index 0000000..9af3064 --- /dev/null +++ b/sortStation/sortStation/queue.h @@ -0,0 +1,20 @@ +#pragma once +#include + +typedef struct Node { + char value[100]; + struct Node* next; +} Node; + +typedef struct Queue { + struct Node* head; + struct Node* tail; +} Queue; + +int enqueue(Queue* queue, char value[]); + +void dequeue(Queue* queue, int* errorCode, char value[]); + +bool isEmptyQueue(Queue queue); + +void clear(Queue* queue); \ No newline at end of file diff --git a/sortStation/sortStation/sortStation.vcxproj b/sortStation/sortStation/sortStation.vcxproj new file mode 100644 index 0000000..a4d8ecd --- /dev/null +++ b/sortStation/sortStation/sortStation.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {6e239fd9-ad88-4ea0-b69a-cc5cf6417ad7} + sortStation + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + 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 + false + _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..c9b034f --- /dev/null +++ b/sortStation/sortStation/sortStation.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/sortStation/sortStation/stack.c b/sortStation/sortStation/stack.c new file mode 100644 index 0000000..59c7af5 --- /dev/null +++ b/sortStation/sortStation/stack.c @@ -0,0 +1,69 @@ +#include "stack.h" +#include +#include + +typedef struct Node { + char value; + struct Node* next; +} Node; + +typedef struct Stack { + Node* head; +} Stack; + +int push(Stack* stack, char value) { + Node* temp = calloc(1, sizeof(Node)); + if (temp == NULL) { + printf("Problems with memory allocation"); + return -1; + } + temp->value = value; + temp->next = stack->head; + + stack->head = temp; + return 0; +} + +char pop(Stack* stack, int* errorCode) { + if (stack->head == NULL) { + if (errorCode != NULL) { + *errorCode = -1; + } + + return 0; + } + if (errorCode != NULL) { + *errorCode = 0; + } + + char value = stack->head->value; + + Stack* next = stack->head->next; + free(stack->head); + stack->head = next; + + return value; +} + +bool isEmpty(Stack* stack) { + return stack->head == NULL; +} + +void deleteStack(Stack** stack) { + while (!isEmpty(*stack)) { + int errorCode = 0; + pop(*stack, &errorCode); + } + *stack = NULL; +} + +Stack* createStack(void) { + return calloc(1, sizeof(Stack)); +} + +char top(Stack* stack) { + if (stack->head == NULL) { + return ""; + } + return stack->head->value; +} \ No newline at end of file diff --git a/sortStation/sortStation/stack.h b/sortStation/sortStation/stack.h new file mode 100644 index 0000000..3073254 --- /dev/null +++ b/sortStation/sortStation/stack.h @@ -0,0 +1,25 @@ +#ifndef _STACK_H +#define _STACK_H +#include + +typedef struct Stack Stack; + +// +int push(Stack* stack, char value); + +// +char pop(Stack* stack, int* errorCode); + +// +bool isEmpty(Stack* stack); + +// +void deleteStack(Stack** stack); + +// +Stack* createStack(void); + +// +char top(Stack* stack); + +#endif \ No newline at end of file