diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..8616e4f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.25) +project(homeworks C) + +add_compile_options(-Wall -Wextra -pedantic) + +add_subdirectory(src/stack) \ No newline at end of file diff --git a/src/stack/CMakeLists.txt b/src/stack/CMakeLists.txt new file mode 100644 index 0000000..ae34397 --- /dev/null +++ b/src/stack/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(stack stack.c) + +add_executable(task1 task1.c) + +target_link_libraries(task1 PRIVATE stack) diff --git a/src/stack/instruction.txt b/src/stack/instruction.txt new file mode 100644 index 0000000..bb218c5 --- /dev/null +++ b/src/stack/instruction.txt @@ -0,0 +1,9 @@ +Task1: +gcc -Wall -Wextra -pedantic -c stack.c +gcc -Wall -Wextra -pedantic -c task1.c +gcc -Wall -Wextra -pedantic stack.o task1.o -o steakTask1 + +Task2: +gcc -Wall -Wextra -pedantic -c stack.c +gcc -Wall -Wextra -pedantic -c task2.c +gcc -Wall -Wextra -pedantic stack.o task2.o -o steakTask2 \ No newline at end of file diff --git a/src/stack/stack.c b/src/stack/stack.c new file mode 100644 index 0000000..4674854 --- /dev/null +++ b/src/stack/stack.c @@ -0,0 +1,54 @@ +#include "stack.h" +#include +#include + +struct StackNode { + int value; + struct StackNode* next; +}; + +struct Stack { + struct StackNode* head; +}; + +Stack* newStack() +{ + Stack* stack = (Stack*)malloc(sizeof(Stack)); + if (!stack) + return NULL; + stack->head = NULL; + return stack; +} + +void push(struct Stack* stack, int value) +{ + struct StackNode* node = (struct StackNode*)malloc(sizeof(struct StackNode)); + + node->value = value; + node->next = stack->head; + stack->head = node; +} + +int pop(Stack* stack) +{ + struct StackNode* oldNode = stack->head; + int res = oldNode->value; + + stack->head = oldNode->next; + free(oldNode); + + return res; +} + +int isEmpty(Stack* stack) +{ + return stack->head == NULL; +} + +void deleteStack(Stack* stack) +{ + while (!isEmpty(stack)) { + pop(stack); + } + free(stack); +} \ No newline at end of file diff --git a/src/stack/stack.h b/src/stack/stack.h new file mode 100644 index 0000000..d26ffea --- /dev/null +++ b/src/stack/stack.h @@ -0,0 +1,9 @@ +#pragma once + +typedef struct Stack Stack; + +Stack* newStack(); // Создание стека. +void push(Stack* stack, int value); // Добавляет элемент на вершину стека. +int pop(Stack* stack); // Убирает из стека элемент и возвращает его. Необходимо проверять isEmpty перед вызовом +int isEmpty(Stack* stack); +void deleteStack(Stack* stack); // Удаление стека. diff --git a/src/stack/task1.c b/src/stack/task1.c new file mode 100644 index 0000000..1a24271 --- /dev/null +++ b/src/stack/task1.c @@ -0,0 +1,44 @@ +#include "stack.h" +#include +#include + +bool isBalanced(const char* s) +{ + Stack* st = newStack(); + + for (int i = 0; s[i] != '\0'; ++i) { + char c = s[i]; + if ((c == '(' || c == '[' || c == '{')) { + push(st, c); + } else if (c == ')' || c == ']' || c == '}') { + if (isEmpty(st)) { + deleteStack(st); + return false; + } + char t = pop(st); + if ((c == ')' && t != '(') || (c == ']' && t != '[') || (c == '}' && t != '{')) { + deleteStack(&st); + return false; + } + } + } + + bool res = isEmpty(st); + deleteStack(st); + return res; +} + +int main(void) +{ + char str[100]; + + printf("строка: "); + scanf("%s", str); + + if (isBalanced(str)) + printf("верно\n"); + else + printf("неверно\n"); + + return 0; +} \ No newline at end of file