diff --git a/advBalStap/CMakeLists.txt b/advBalStap/CMakeLists.txt new file mode 100644 index 0000000..89783e9 --- /dev/null +++ b/advBalStap/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.25) + +project(advancBalancStap C) + +add_library(stack stack.c) + +add_executable(advancBalancStap advancBalancStap.c) + +target_link_libraries(advancBalancStap PRIVATE stack) \ No newline at end of file diff --git a/advBalStap/advancBalancStap.c b/advBalStap/advancBalancStap.c new file mode 100644 index 0000000..2cffc32 --- /dev/null +++ b/advBalStap/advancBalancStap.c @@ -0,0 +1,58 @@ +#include "stack.h" +#include +#include +#include +#include + +void balance(char* str, int length) +{ + struct Stack* staples = new(); + char openParent; + bool flag = true; + for (int ind = 0; ind < length; ind++) { + if (str[ind] == '(' || str[ind] == '{' || str[ind] == '[') { + push(staples, str[ind]); + } else { + switch (str[ind]) { + case ')': + openParent = pop(staples); + if (openParent != '(') { + flag = false; + } + break; + case '}': + openParent = pop(staples); + if (openParent != '{') { + flag = false; + } + break; + case ']': + openParent = pop(staples); + if (openParent != '[') { + flag = false; + } + break; + } + if (!flag) { + break; + } + } + } + if (isEmpty(staples) && flag) { + printf("Баланс есть\n"); + free(staples); + } else { + printf("Баланса нет\n"); + free(staples); + } +} + +int main(int argc, char** argv) +{ + char str[1000]; + printf("Введите строку: "); + fgets(str, sizeof(str), stdin); + balance(str, strlen(str)); + + return 0; +} diff --git a/advBalStap/stack.c b/advBalStap/stack.c new file mode 100644 index 0000000..1cf93a1 --- /dev/null +++ b/advBalStap/stack.c @@ -0,0 +1,77 @@ +#include "stack.h" +#include +#include + +struct StackNode { + // значение + char value; + // ссылка на следующую "ячейку" + struct StackNode* next; +}; + +// делаем ссылку на голову +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/advBalStap/stack.h b/advBalStap/stack.h new file mode 100644 index 0000000..b2f0838 --- /dev/null +++ b/advBalStap/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); \ No newline at end of file