diff --git a/sortStans/CMakeLists.txt b/sortStans/CMakeLists.txt new file mode 100644 index 0000000..f3c5454 --- /dev/null +++ b/sortStans/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.25) + +project(sortStation C) + +add_library(stack stack.c) + +add_executable(sortStation sortStation.c) + +target_link_libraries(sortStation PRIVATE stack) diff --git a/sortStans/sortStation.c b/sortStans/sortStation.c new file mode 100644 index 0000000..077e818 --- /dev/null +++ b/sortStans/sortStation.c @@ -0,0 +1,82 @@ +#include "stack.h" +#include +#include +#include +#include + +// переносит все операторы от ")" до "(" из стека в очередь по индексу ind +int moveBtwStap(struct Stack* stack, char* queue, int ind) +{ + int index = ind; + char operator = pop(stack); + while (operator != '(' && operator != '\0') { + index++; + // printf("%s\n", queue[ind]); + queue[index] = operator; + operator = pop(stack); + } + return index; +} + +char* postfNotation(char* str, int lenght) +{ + char* queue = (char*)malloc((lenght + 1) * sizeof(char)); + int indQueue = 0; + struct Stack* operators = new(); + + for (int ind = 0; ind < lenght; ind++) { + if (str[ind] == '-' || str[ind] == ')' || str[ind] == '+' || str[ind] == '*' || str[ind] == '/') { + if (str[ind] == ')') { + indQueue = moveBtwStap(operators, queue, indQueue); + } else { + if (str[ind] == '*' || str[ind] == '/') { + while (!isEmpty(operators) && (peek(operators) == '*' || peek(operators) == '/')) { + indQueue++; + queue[indQueue] = pop(operators); + } + push(operators, str[ind]); + } else if (str[ind] == '+' || str[ind] == '-') { + while (!isEmpty(operators) && (peek(operators) == '-' || peek(operators) == '+' || peek(operators) == '*' || peek(operators) == '/')) { + indQueue++; + queue[indQueue] = pop(operators); + } + push(operators, str[ind]); + } + } + } else if (str[ind] == '(') { + push(operators, str[ind]); + } else if (str[ind] != ' ' && str[ind] != '\n') { + indQueue++; + queue[indQueue] = str[ind]; + } + } + + while (!isEmpty(operators)) { + char operator = pop(operators); + if (operator != '(') { + indQueue++; + queue[indQueue] = operator; + } + } + indQueue++; + queue[indQueue] = '\0'; + deleteStack(operators); + return queue; +} + +int main(int argc, char** argv) +{ + char str[1000]; + printf("Введите строку: "); + fgets(str, sizeof(str), stdin); + int lenght = strlen(str); + + char* results = postfNotation(str, lenght); + for (int ind = 0; ind < lenght; ind++) { + printf("%c ", results[ind]); + } + printf("\n"); + free(results); + + return 0; +} \ No newline at end of file diff --git a/sortStans/stack.c b/sortStans/stack.c new file mode 100644 index 0000000..3240054 --- /dev/null +++ b/sortStans/stack.c @@ -0,0 +1,78 @@ +#include "stack.h" +#include +#include +// #include + +struct StackNode { + // значение + char value; + // ссылка на следующую "ячейку" + struct StackNode* next; +}; + +// делаем ссsлку на голову +struct Stack { + struct StackNode* head; +}; + +// создаем пустую структуру с пустой ссылкой на нулевую голову +struct Stack* new(void) +{ + struct Stack* stack = calloc(1, sizeof(*stack)); + return stack; +} + +// положить элемент +void push(struct Stack* stack, char value) +{ + // выделяем память для нового стека + struct StackNode* node = (struct StackNode*)malloc(sizeof(struct StackNode)); + + // пихаем в него значение + node->value = value; + + // пихаем в него ссылку на предыдущий стек + node->next = stack->head; + + // делаем ссылку на этот же стек ссылкой на голову + stack->head = node; +} + +bool isEmpty(struct Stack* stack) +{ + return stack->head == NULL; +} + +char pop(struct Stack* stack) +{ + if (isEmpty(stack)) { + return '\0'; + } + // получаем ссылку на текущую голову(стек) + struct StackNode* oldNode = stack->head; + + // переприсваиваем ссылку головы на предыдущую от текущего стека + stack->head = oldNode->next; + + // вытаскиваем значение из текущего стека + char result = oldNode->value; + free(oldNode); + return result; +} + +char peek(struct Stack* stack) +{ + char result = stack->head->value; + return result; +} + +void deleteStack(struct Stack* stack) +{ + if (stack == NULL) { + return; + } + while (!isEmpty(stack)) { + pop(stack); + } + free(stack); +} diff --git a/sortStans/stack.h b/sortStans/stack.h new file mode 100644 index 0000000..cde3aab --- /dev/null +++ b/sortStans/stack.h @@ -0,0 +1,11 @@ +#pragma once +#include + +typedef struct Stack Stack; + +Stack* new(void); +void push(Stack* stack, char value); +char pop(Stack* stack); +bool isEmpty(Stack* stack); +char peek(struct Stack* stack); +void deleteStack(struct Stack* stack);