Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.25)
project(homeworks C)

add_compile_options(-Wall -Wextra -pedantic)

add_subdirectory(src/stack)
5 changes: 5 additions & 0 deletions src/stack/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_library(stack stack.c)

add_executable(task1 task1.c)

target_link_libraries(task1 PRIVATE stack)
9 changes: 9 additions & 0 deletions src/stack/instruction.txt
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions src/stack/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошо бы ещё уметь удалять стек


void deleteStack(Stack* stack)
{
while (!isEmpty(stack)) {
pop(stack);
}
free(stack);
}
9 changes: 9 additions & 0 deletions src/stack/stack.h
Original file line number Diff line number Diff line change
@@ -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); // Удаление стека.
44 changes: 44 additions & 0 deletions src/stack/task1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "stack.h"
#include <stdbool.h>
#include <stdio.h>

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Содержательно правда, но вот память течёт. Это нужно исправить. При этом дублировать код для очистки памяти возможно не очень хорошо, но флаг может помочь.

}

int main(void)
{
char str[100];

printf("строка: ");
scanf("%s", str);

if (isBalanced(str))
printf("верно\n");
else
printf("неверно\n");

return 0;
}