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
9 changes: 9 additions & 0 deletions advBalStap/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
58 changes: 58 additions & 0 deletions advBalStap/advancBalancStap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "stack.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
77 changes: 77 additions & 0 deletions advBalStap/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

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);
}
11 changes: 11 additions & 0 deletions advBalStap/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include <stdbool.h>

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);