From 345bc50da4bd95691dfc61981e264e969c33a881 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 20:23:21 +0300 Subject: [PATCH 01/21] Add stack library header file --- src/stack_and_queue/stack/stack.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/stack_and_queue/stack/stack.h diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h new file mode 100644 index 0000000..0822bbf --- /dev/null +++ b/src/stack_and_queue/stack/stack.h @@ -0,0 +1,30 @@ +#pragma once + +typedef struct StackNode { + int value; + struct StackNode* next; +} StackNode; + +typedef struct Stack { + StackNode* head; +} Stack; + +// Creates new stack. +// Returns pointer on the created stack. +Stack* newStack(); + +// Deletes stack. +// Takes pointer on the stack. +void deleteStack(Stack* stack); + +// Pushes new value on the top of the stack. +// Takes pointer on the stack and value for push. +void push(Stack* stack, int value); + +// Deletes the top value of the stack and returns it. +// Takes pointer on the stack. +int pop(Stack* stack); + +// Prints the top value of the stack. +// Takes pointer on the stack. +void peek(Stack* stack); From 4737d32eed5973489422ffb1074afc99a74a9abe Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 20:23:52 +0300 Subject: [PATCH 02/21] Add stack library realisation file --- src/stack_and_queue/stack/stack.c | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/stack_and_queue/stack/stack.c diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c new file mode 100644 index 0000000..e61ca28 --- /dev/null +++ b/src/stack_and_queue/stack/stack.c @@ -0,0 +1,54 @@ +#include "stack.h" + +#include +#include + +Stack* newStack() +{ + Stack* stack = malloc(sizeof(Stack)); + stack->head = NULL; + return stack; +} + +void deleteStack(Stack* stack) +{ + StackNode* highest = stack->head; + while (highest != NULL) { + StackNode* newHighest = highest->next; + free(highest); + highest = newHighest; + } + free(stack); +} + +void push(Stack* stack, int value) +{ + StackNode* newNode = malloc(sizeof(StackNode)); + newNode->value = value; + newNode->next = stack->head; + stack->head = newNode; +} + +int pop(Stack* stack) +{ + if (stack->head == NULL) { + printf("The stack is empty. \n"); + return -1; + } + + StackNode* poppedNode = stack->head; + int value = poppedNode->value; + stack->head = poppedNode->next; + free(poppedNode); + return value; +} + +void peek(Stack* stack) +{ + if (stack->head == NULL) { + printf("The stack is empty. \n"); + return; + } + + printf("The highest element: %d \n", stack->head->value); +} From 405bf4c1c824f79f37d9c5bb3d1867adb39a2e66 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 20:49:07 +0300 Subject: [PATCH 03/21] Update stack header file commentaries --- src/stack_and_queue/stack/stack.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index 0822bbf..cbbfc15 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -23,8 +23,10 @@ void push(Stack* stack, int value); // Deletes the top value of the stack and returns it. // Takes pointer on the stack. +// If the stack is empty returns -1. int pop(Stack* stack); // Prints the top value of the stack. // Takes pointer on the stack. +// Prints message if the stack is empty. void peek(Stack* stack); From ce89c997bf05d3bf5641b96e76c43c96e38cc9ad Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 14 Oct 2025 20:49:36 +0300 Subject: [PATCH 04/21] Update stack realisation file --- src/stack_and_queue/stack/stack.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index e61ca28..c745db1 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -32,7 +32,6 @@ void push(Stack* stack, int value) int pop(Stack* stack) { if (stack->head == NULL) { - printf("The stack is empty. \n"); return -1; } From f4d1e691c16db183d8f85ce3a3c8b5f183c4a9ff Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 20 Oct 2025 14:59:09 +0300 Subject: [PATCH 05/21] Update stack realisation file --- src/stack_and_queue/stack/stack.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index c745db1..3b2b2c2 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -3,9 +3,18 @@ #include #include +typedef struct StackNode { + int value; + struct StackNode* next; +} StackNode; + +typedef struct Stack { + StackNode* head; +} Stack; + Stack* newStack() { - Stack* stack = malloc(sizeof(Stack)); + Stack* stack = malloc(sizeof(*stack)); stack->head = NULL; return stack; } @@ -23,7 +32,7 @@ void deleteStack(Stack* stack) void push(Stack* stack, int value) { - StackNode* newNode = malloc(sizeof(StackNode)); + StackNode* newNode = malloc(sizeof(*newNode)); newNode->value = value; newNode->next = stack->head; stack->head = newNode; @@ -42,12 +51,11 @@ int pop(Stack* stack) return value; } -void peek(Stack* stack) +int peek(Stack* stack) { if (stack->head == NULL) { - printf("The stack is empty. \n"); - return; + return -1; } - printf("The highest element: %d \n", stack->head->value); + return stack->head->value; } From d389c7db73352e9106299573aedd4817a7017bd2 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Mon, 20 Oct 2025 15:00:04 +0300 Subject: [PATCH 06/21] Update stack header file --- src/stack_and_queue/stack/stack.h | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index cbbfc15..b0a355f 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -1,13 +1,8 @@ #pragma once -typedef struct StackNode { - int value; - struct StackNode* next; -} StackNode; +typedef struct StackNode StackNode; -typedef struct Stack { - StackNode* head; -} Stack; +typedef struct Stack Stack; // Creates new stack. // Returns pointer on the created stack. @@ -26,7 +21,7 @@ void push(Stack* stack, int value); // If the stack is empty returns -1. int pop(Stack* stack); -// Prints the top value of the stack. +// Returns the top value of the stack. // Takes pointer on the stack. -// Prints message if the stack is empty. -void peek(Stack* stack); +// If the stack is empty returns -1. +int peek(Stack* stack); From ceeeb02df60f409dd711964c0f065f73ac0447b5 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 19:58:39 +0300 Subject: [PATCH 07/21] Update stack header file --- src/stack_and_queue/stack/stack.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index b0a355f..4439245 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -1,5 +1,7 @@ #pragma once +#include + typedef struct StackNode StackNode; typedef struct Stack Stack; @@ -8,7 +10,7 @@ typedef struct Stack Stack; // Returns pointer on the created stack. Stack* newStack(); -// Deletes stack. +// Deletes stack. Frees all memory used in it. // Takes pointer on the stack. void deleteStack(Stack* stack); @@ -16,7 +18,7 @@ void deleteStack(Stack* stack); // Takes pointer on the stack and value for push. void push(Stack* stack, int value); -// Deletes the top value of the stack and returns it. +// Deletes the top value of the stack and returns it. Frees memory used for top node. // Takes pointer on the stack. // If the stack is empty returns -1. int pop(Stack* stack); @@ -25,3 +27,7 @@ int pop(Stack* stack); // Takes pointer on the stack. // If the stack is empty returns -1. int peek(Stack* stack); + +// Returns true if the stack is empty and false if not. +// Takes pointer on the stack. +bool isEmpty(Stack* stack); From f344512d86c6205ed189fc76e409be20f046ad8d Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 19:59:13 +0300 Subject: [PATCH 08/21] Update stack realisation file --- src/stack_and_queue/stack/stack.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index 3b2b2c2..ff1989f 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -1,5 +1,6 @@ #include "stack.h" +#include #include #include @@ -21,11 +22,8 @@ Stack* newStack() void deleteStack(Stack* stack) { - StackNode* highest = stack->head; - while (highest != NULL) { - StackNode* newHighest = highest->next; - free(highest); - highest = newHighest; + while (stack->head != NULL) { + pop(stack); } free(stack); } @@ -40,7 +38,7 @@ void push(Stack* stack, int value) int pop(Stack* stack) { - if (stack->head == NULL) { + if (isEmpty(stack)) { return -1; } @@ -53,9 +51,14 @@ int pop(Stack* stack) int peek(Stack* stack) { - if (stack->head == NULL) { + if (isEmpty(stack)) { return -1; } return stack->head->value; } + +bool isEmpty(Stack* stack) +{ + return stack->head == NULL; +} From caa1bf95e1910f6be4efd5797263ddf2c071bbc5 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 20:13:52 +0300 Subject: [PATCH 09/21] Update stack header file --- src/stack_and_queue/stack/stack.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index 4439245..536c186 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -2,8 +2,6 @@ #include -typedef struct StackNode StackNode; - typedef struct Stack Stack; // Creates new stack. From 0d99e196eefce8fde90f718f2f5037ca77dc654d Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Tue, 21 Oct 2025 20:14:07 +0300 Subject: [PATCH 10/21] Update stack realisation file --- src/stack_and_queue/stack/stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index ff1989f..7dfb49a 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -9,9 +9,9 @@ typedef struct StackNode { struct StackNode* next; } StackNode; -typedef struct Stack { +struct Stack { StackNode* head; -} Stack; +}; Stack* newStack() { From 8f9bc54e5d43e2767de22f368a24a7c36a880293 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Wed, 22 Oct 2025 20:47:46 +0300 Subject: [PATCH 11/21] Add sorting station algorithm --- src/stack_and_queue/stack/sorting_station.c | 95 +++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/stack_and_queue/stack/sorting_station.c diff --git a/src/stack_and_queue/stack/sorting_station.c b/src/stack_and_queue/stack/sorting_station.c new file mode 100644 index 0000000..2f7872d --- /dev/null +++ b/src/stack_and_queue/stack/sorting_station.c @@ -0,0 +1,95 @@ +#include "stack.h" +#include +#include +#include +#include +#include + +bool isOperatorFirstPriority(char c) +{ + return c == '*' || c == '/'; +} + +bool isOperatorSecondPriority(char c) +{ + return c == '+' || c == '-'; +} + +// Takes infix form of expression as string and returns pointer on postfix form string. +char* sortingStation(char* infixForm) +{ + char* output = calloc(strlen(infixForm), sizeof(output) + 1); + Stack* operators = newStack(); + + int indexOfOutput = 0; + for (int indexOfInfix = 0; infixForm[indexOfInfix] != '\0'; ++indexOfInfix) { + if (isdigit(infixForm[indexOfInfix])) { + output[indexOfOutput] = infixForm[indexOfInfix]; + output[indexOfOutput + 1] = ' '; + indexOfOutput += 2; + } + if (isOperatorFirstPriority(infixForm[indexOfInfix]) || infixForm[indexOfInfix] == '(') { + push(operators, infixForm[indexOfInfix]); + } + if (isOperatorSecondPriority(infixForm[indexOfInfix])) { + while (!isEmpty(operators) && peek(operators) != '(') { + output[indexOfOutput] = pop(operators); + output[indexOfOutput + 1] = ' '; + indexOfOutput += 2; + } + push(operators, infixForm[indexOfInfix]); + } + if (infixForm[indexOfInfix] == ')') { + while (peek(operators) != '(') { + output[indexOfOutput] = pop(operators); + output[indexOfOutput + 1] = ' '; + indexOfOutput += 2; + } + pop(operators); + } + } + + while (!isEmpty(operators)) { + output[indexOfOutput] = pop(operators); + output[indexOfOutput + 1] = ' '; + indexOfOutput += 2; + } + + deleteStack(operators); + return output; +} + +int main() +{ + char* input1 = "(1 + 1) * 2"; + char* output1 = sortingStation(input1); + char* input2 = "4/2+6"; + char* output2 = sortingStation(input2); + char* input3 = "((1 + 2) * 3) - 4"; + char* output3 = sortingStation(input3); + char* input4 = "((((1))))"; + char* output4 = sortingStation(input4); + char* input5 = "(1 + (4 + 5 + 2) - 3) + (6 + 8)"; + char* output5 = sortingStation(input5); + char* input6 = "1"; + char* output6 = sortingStation(input6); + char* input7 = ""; + char* output7 = sortingStation(input7); + + printf("%s -> %s\n", input1, output1); + printf("%s -> %s\n", input2, output2); + printf("%s -> %s\n", input3, output3); + printf("%s -> %s\n", input4, output4); + printf("%s -> %s\n", input5, output5); + printf("%s -> %s\n", input6, output6); + printf("%s -> %s\n", input7, output7); + + free(output1); + free(output2); + free(output3); + free(output4); + free(output5); + free(output6); + free(output7); + return 0; +} From 851d7c894e97322c36b51cb05753a55198e0d3dc Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Wed, 22 Oct 2025 20:48:27 +0300 Subject: [PATCH 12/21] Add compile instruction for sorting station --- src/stack_and_queue/stack/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/stack_and_queue/stack/README.md diff --git a/src/stack_and_queue/stack/README.md b/src/stack_and_queue/stack/README.md new file mode 100644 index 0000000..5a16c22 --- /dev/null +++ b/src/stack_and_queue/stack/README.md @@ -0,0 +1,4 @@ +## Compile instruction for sorting station +gcc -Wall -Wextra -pedantic -O2 stack.c -c + +gcc -Wall -Wextra -pedantic -O2 stack.o sorting_station.c From 587129448b6279e1bc066edde350f434b7383953 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 9 Nov 2025 00:12:08 +0300 Subject: [PATCH 13/21] Update .gitignore with adding ignoring CMake build directory --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index b48d248..b1e499b 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,7 @@ Module.symvers Mkfile.old dkms.conf +# CMake +build + # End of https://www.toptal.com/developers/gitignore/api/c From fd01840bdd414b8a242a11fd2a32f10e76c570f6 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 9 Nov 2025 00:12:37 +0300 Subject: [PATCH 14/21] Add CMakeLists.txt for build sorting station --- CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6ea464c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.25) +project(sorting_station C) + +add_library(stack src/stack_and_queue/stack/stack.c) + +add_executable(sorting_station src/stack_and_queue/stack/sorting_station.c) +target_link_libraries(sorting_station PRIVATE stack) +target_compile_options(sorting_station PRIVATE -Wall -Wextra -pedantic) From 6fe80418e664593e3df92423c2e938b920a0fe0e Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Sun, 9 Nov 2025 00:12:59 +0300 Subject: [PATCH 15/21] Update instructions how to build --- src/stack_and_queue/stack/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/stack_and_queue/stack/README.md b/src/stack_and_queue/stack/README.md index 5a16c22..18568b1 100644 --- a/src/stack_and_queue/stack/README.md +++ b/src/stack_and_queue/stack/README.md @@ -1,4 +1,3 @@ ## Compile instruction for sorting station -gcc -Wall -Wextra -pedantic -O2 stack.c -c - -gcc -Wall -Wextra -pedantic -O2 stack.o sorting_station.c +cmake . -B build\ +cmake --build build From 10381902aa36c7eac84c43d75fa2887fa5a03e18 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 11 Dec 2025 11:15:51 +0300 Subject: [PATCH 16/21] Update grammary in documentations --- src/stack_and_queue/stack/stack.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index 536c186..dcec5bf 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -5,27 +5,27 @@ typedef struct Stack Stack; // Creates new stack. -// Returns pointer on the created stack. +// Returns pointer to the created stack. Stack* newStack(); // Deletes stack. Frees all memory used in it. -// Takes pointer on the stack. +// Takes pointer to the stack. void deleteStack(Stack* stack); // Pushes new value on the top of the stack. -// Takes pointer on the stack and value for push. +// Takes pointer to the stack and value for push. void push(Stack* stack, int value); // Deletes the top value of the stack and returns it. Frees memory used for top node. -// Takes pointer on the stack. +// Takes pointer to the stack. // If the stack is empty returns -1. int pop(Stack* stack); // Returns the top value of the stack. -// Takes pointer on the stack. +// Takes pointer to the stack. // If the stack is empty returns -1. int peek(Stack* stack); // Returns true if the stack is empty and false if not. -// Takes pointer on the stack. +// Takes pointer to the stack. bool isEmpty(Stack* stack); From 74218cf2ef038d8cbeeffd984c78b89a19cec0f9 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 11 Dec 2025 14:41:54 +0300 Subject: [PATCH 17/21] Update stack realization Remove pop and peek -1 return if stack is empty. Add asserts for memory allocation. Add asserts for not empty stack in pop and peek. --- src/stack_and_queue/stack/stack.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/stack_and_queue/stack/stack.c b/src/stack_and_queue/stack/stack.c index 7dfb49a..2878e96 100644 --- a/src/stack_and_queue/stack/stack.c +++ b/src/stack_and_queue/stack/stack.c @@ -1,5 +1,6 @@ #include "stack.h" +#include #include #include #include @@ -15,14 +16,14 @@ struct Stack { Stack* newStack() { - Stack* stack = malloc(sizeof(*stack)); - stack->head = NULL; + Stack* stack = calloc(1, sizeof(*stack)); + assert(stack != NULL && "Error! Memory allocated incorrect."); return stack; } void deleteStack(Stack* stack) { - while (stack->head != NULL) { + while (!isEmpty(stack)) { pop(stack); } free(stack); @@ -31,6 +32,7 @@ void deleteStack(Stack* stack) void push(Stack* stack, int value) { StackNode* newNode = malloc(sizeof(*newNode)); + assert(newNode != NULL && "Error! Memory allocated incorrect."); newNode->value = value; newNode->next = stack->head; stack->head = newNode; @@ -38,10 +40,7 @@ void push(Stack* stack, int value) int pop(Stack* stack) { - if (isEmpty(stack)) { - return -1; - } - + assert(!isEmpty(stack) && "Error! Stack is empty. Can`t pop value."); StackNode* poppedNode = stack->head; int value = poppedNode->value; stack->head = poppedNode->next; @@ -51,10 +50,7 @@ int pop(Stack* stack) int peek(Stack* stack) { - if (isEmpty(stack)) { - return -1; - } - + assert(!isEmpty(stack) && "Error! Stack is empty. Can`t peek value."); return stack->head->value; } From 60792cf6241a3b946c15f558786fa104053a29d6 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 11 Dec 2025 14:45:32 +0300 Subject: [PATCH 18/21] Update stack header documentation --- src/stack_and_queue/stack/stack.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/stack_and_queue/stack/stack.h b/src/stack_and_queue/stack/stack.h index dcec5bf..984d7e7 100644 --- a/src/stack_and_queue/stack/stack.h +++ b/src/stack_and_queue/stack/stack.h @@ -18,12 +18,14 @@ void push(Stack* stack, int value); // Deletes the top value of the stack and returns it. Frees memory used for top node. // Takes pointer to the stack. -// If the stack is empty returns -1. +// Don`t use if the stack is empty. It causes the error. +// Check this with isEmpty before use this function. int pop(Stack* stack); // Returns the top value of the stack. // Takes pointer to the stack. -// If the stack is empty returns -1. +// Don`t use if the stack is empty. It causes the error. +// Check this with isEmpty before use this function. int peek(Stack* stack); // Returns true if the stack is empty and false if not. From 46eac817a7a717a1db5828ccc099f039a4276ab6 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 11 Dec 2025 19:01:10 +0300 Subject: [PATCH 19/21] Add build instruction in README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 8d586e5..ab05d77 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,9 @@ Основная почта: nicholas.shestakov@gmail.com Телеграм: @Kolya_shesterka + +## Инструкции по сборке: +```console +$ cmake . -B build +$ cmake --build build +``` From 2a35f6138bbc77292dfd3b704a06c0437b1ff249 Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 11 Dec 2025 19:02:28 +0300 Subject: [PATCH 20/21] Delete build instruction from stack folder --- src/stack_and_queue/stack/README.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 src/stack_and_queue/stack/README.md diff --git a/src/stack_and_queue/stack/README.md b/src/stack_and_queue/stack/README.md deleted file mode 100644 index 18568b1..0000000 --- a/src/stack_and_queue/stack/README.md +++ /dev/null @@ -1,3 +0,0 @@ -## Compile instruction for sorting station -cmake . -B build\ -cmake --build build From 8646f0dd007f83b8ae849d4ce82ff3f22d033f6a Mon Sep 17 00:00:00 2001 From: Nicholay Shestakov Date: Thu, 11 Dec 2025 22:04:09 +0300 Subject: [PATCH 21/21] Update sorting station Add error messages for incorrect infix forms. --- src/stack_and_queue/stack/sorting_station.c | 149 ++++++++++++++------ 1 file changed, 105 insertions(+), 44 deletions(-) diff --git a/src/stack_and_queue/stack/sorting_station.c b/src/stack_and_queue/stack/sorting_station.c index 2f7872d..594698b 100644 --- a/src/stack_and_queue/stack/sorting_station.c +++ b/src/stack_and_queue/stack/sorting_station.c @@ -1,4 +1,5 @@ #include "stack.h" +#include #include #include #include @@ -15,44 +16,97 @@ bool isOperatorSecondPriority(char c) return c == '+' || c == '-'; } -// Takes infix form of expression as string and returns pointer on postfix form string. +// Takes infix form of expression as string and returns pointer to postfix form string. Please, dont forget free memory. +// Can track unbalanced brackets and correctness of order of numbers and operators. Cant track extraneous symbols. +// If the error is tracked, returns pointer to the string with the error description. char* sortingStation(char* infixForm) { - char* output = calloc(strlen(infixForm), sizeof(output) + 1); + char* output = calloc(2 * strlen(infixForm), sizeof(output)); Stack* operators = newStack(); + bool isNumberTurn = true; // For check correctness of order. + char* errorMessage = calloc(64, sizeof(*errorMessage)); // For error returns. int indexOfOutput = 0; for (int indexOfInfix = 0; infixForm[indexOfInfix] != '\0'; ++indexOfInfix) { if (isdigit(infixForm[indexOfInfix])) { + if (!isNumberTurn) { + strcat(errorMessage, "Error! Order of numbers and operators is incorrect."); + } + isNumberTurn = !isNumberTurn; + output[indexOfOutput] = infixForm[indexOfInfix]; output[indexOfOutput + 1] = ' '; indexOfOutput += 2; } - if (isOperatorFirstPriority(infixForm[indexOfInfix]) || infixForm[indexOfInfix] == '(') { + if (isOperatorFirstPriority(infixForm[indexOfInfix])) { + if (isNumberTurn) { + strcat(errorMessage, "Error! Order of numbers and operators is incorrect."); + } + isNumberTurn = !isNumberTurn; + push(operators, infixForm[indexOfInfix]); } if (isOperatorSecondPriority(infixForm[indexOfInfix])) { + if (isNumberTurn) { + strcat(errorMessage, "Error! Order of numbers and operators is incorrect."); + } + isNumberTurn = !isNumberTurn; + while (!isEmpty(operators) && peek(operators) != '(') { - output[indexOfOutput] = pop(operators); - output[indexOfOutput + 1] = ' '; - indexOfOutput += 2; + output[indexOfOutput++] = pop(operators); + output[indexOfOutput++] = ' '; + } + push(operators, infixForm[indexOfInfix]); + } + if (infixForm[indexOfInfix] == '(') { + if (!isNumberTurn) { + strcat(errorMessage, "Error! Number before opening bracket."); } + push(operators, infixForm[indexOfInfix]); } + // Многоступенчатость в целях предотвращения одновременного добавления сразу нескольких сообщений об ошибке. if (infixForm[indexOfInfix] == ')') { - while (peek(operators) != '(') { - output[indexOfOutput] = pop(operators); - output[indexOfOutput + 1] = ' '; - indexOfOutput += 2; + if (isEmpty(operators)) { + strcat(errorMessage, "Error! Brackets not opened."); + } else { + if (isNumberTurn) { + strcat(errorMessage, "Error! Operator before closing bracket."); + } else { + while (peek(operators) != '(') { + output[indexOfOutput++] = pop(operators); + output[indexOfOutput++] = ' '; + + if (isEmpty(operators)) { + strcat(errorMessage, "Error! Brackets not opened."); + break; + } + } + if (!isEmpty(operators)) { + pop(operators); + } + } } - pop(operators); + } + + // Данное условие истинно, когда строки не равны. + if (strcmp(errorMessage, "")) { + free(output); + deleteStack(operators); + return errorMessage; } } while (!isEmpty(operators)) { - output[indexOfOutput] = pop(operators); - output[indexOfOutput + 1] = ' '; - indexOfOutput += 2; + output[indexOfOutput++] = pop(operators); + output[indexOfOutput++] = ' '; + + if (output[indexOfOutput - 2] == '(') { + free(output); + deleteStack(operators); + strcat(errorMessage, "Error! Brackets not closed."); + return errorMessage; + } } deleteStack(operators); @@ -61,35 +115,42 @@ char* sortingStation(char* infixForm) int main() { - char* input1 = "(1 + 1) * 2"; - char* output1 = sortingStation(input1); - char* input2 = "4/2+6"; - char* output2 = sortingStation(input2); - char* input3 = "((1 + 2) * 3) - 4"; - char* output3 = sortingStation(input3); - char* input4 = "((((1))))"; - char* output4 = sortingStation(input4); - char* input5 = "(1 + (4 + 5 + 2) - 3) + (6 + 8)"; - char* output5 = sortingStation(input5); - char* input6 = "1"; - char* output6 = sortingStation(input6); - char* input7 = ""; - char* output7 = sortingStation(input7); - - printf("%s -> %s\n", input1, output1); - printf("%s -> %s\n", input2, output2); - printf("%s -> %s\n", input3, output3); - printf("%s -> %s\n", input4, output4); - printf("%s -> %s\n", input5, output5); - printf("%s -> %s\n", input6, output6); - printf("%s -> %s\n", input7, output7); - - free(output1); - free(output2); - free(output3); - free(output4); - free(output5); - free(output6); - free(output7); + char* testSimple = sortingStation("(1 + (4 + 5 / 2) - 3) + (6 + 8)"); + char* testNoSpaces = sortingStation("4/2+6"); + char* testManyBrackets = sortingStation("((((1))))"); + char* testOnlyNumber = sortingStation("1"); + char* testEmpty = sortingStation(""); + char* testIncorrectOrder = sortingStation("1 + + 1"); + char* testIncorrectOrder2 = sortingStation("+ 1"); + char* testNumberBeforeBracket = sortingStation("1 (+ 3)"); + char* testOperatorBeforeBracket = sortingStation("1 + (2 + ) 3"); + char* testBracketsNotOpened = sortingStation("1 + 2)"); + char* testBracketsNotClosed = sortingStation("1 + (2 + 3"); + + assert(!strcmp(testSimple, "1 4 5 2 / + + 3 - 6 8 + + ") && "testSimple incorrect."); + assert(!strcmp(testNoSpaces, "4 2 / 6 + ") && "testNoSpaces incorrect."); + assert(!strcmp(testManyBrackets, "1 ") && "testManyBrackets incorrect."); + assert(!strcmp(testOnlyNumber, "1 ") && "testOnlyNumber incorrect."); + assert(!strcmp(testEmpty, "") && "testEmpty incorrect."); + assert(!strcmp(testIncorrectOrder, "Error! Order of numbers and operators is incorrect.") && "testIncorrectOrder incorrect."); + assert(!strcmp(testIncorrectOrder2, "Error! Order of numbers and operators is incorrect.") && "testIncorrectOrder2 incorrect."); + assert(!strcmp(testNumberBeforeBracket, "Error! Number before opening bracket.") && "testNumberBeforeBracket incorrect."); + assert(!strcmp(testOperatorBeforeBracket, "Error! Operator before closing bracket.") && "testOperatorBeforeBracket incorrect."); + assert(!strcmp(testBracketsNotOpened, "Error! Brackets not opened.") && "testBracketsNotOpened incorrect."); + assert(!strcmp(testBracketsNotClosed, "Error! Brackets not closed.") && "testBracketsNotClosed incorrect."); + + printf("All tests passed successfully.\n"); + + free(testSimple); + free(testNoSpaces); + free(testManyBrackets); + free(testOnlyNumber); + free(testEmpty); + free(testIncorrectOrder); + free(testIncorrectOrder2); + free(testNumberBeforeBracket); + free(testOperatorBeforeBracket); + free(testBracketsNotOpened); + free(testBracketsNotClosed); return 0; }