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
16 changes: 16 additions & 0 deletions 05_stack_and_queue/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.10)
project(Stack_and_queue)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)

add_library(Stack stack.c)

add_executable(sortStation sortStation.c)
add_executable(bracketsChecker bracketsChecker.c)

target_link_libraries(sortStation Stack)
target_link_libraries(bracketsChecker Stack)

add_compile_options(-Wall -Wextra -pedantic)
44 changes: 44 additions & 0 deletions 05_stack_and_queue/src/bracketsChecker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "stack.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int bracketsChecker(char* str, bool* res)
{
Stack* stack = newStack();
for (unsigned long i = 0; i < strlen(str); i++) {
if ((str[i] == '(') || (str[i] == '[') || (str[i] == '{')) {
push(stack, str[i]);
} else if ((str[i] == ')') || (str[i] == ']') || (str[i] == '}')) {
if (!isEmpty(stack)) {
char popped = pop(stack);
if (((str[i] == ')') && (popped != '(')) || ((str[i] == ']') && (popped != '[')) || ((str[i] == '}') && (popped != '{'))) {
*res = false;
}
} else {
*res = false;
}
}
}
*res = *res && isEmpty(stack);
deleteStack(stack);
return 0;
}

int main(void)
{
int n = 0;
scanf("%d\n", &n);

char* input = calloc(n + 1, sizeof(char));
fgets(input, n + 1, stdin);

bool res = true;
bracketsChecker(input, &res);

printf("%d\n", res);
free(input);
return 0;
}
119 changes: 119 additions & 0 deletions 05_stack_and_queue/src/sortStation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "stack.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getPriority(char operation)
{
if (operation == '*' || operation == '/') {
return 2;
} else if (operation == '+' || operation == '-') {
return 1;
}
return 0;
}

int sortStation(char* str, char* res)
{
unsigned lastUsedResIndex = 0;
Stack* stack = newStack();
unsigned long len = strlen(str);
for (unsigned long i = 0; i < len; i++) {
if (isdigit(str[i])) {
if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') {
res[lastUsedResIndex] = ' ';
lastUsedResIndex++;
}
while (i < len && isdigit(str[i])) {
res[lastUsedResIndex] = str[i];
lastUsedResIndex++;
i++;
}
if (i < len) {
i--;
}
} else if ((str[i] == '+') || (str[i] == '-') || (str[i] == '*') || (str[i] == '/')) {
while (!isEmpty(stack)) {
char peeked = peek(stack);
if (peeked == '(') {
break;
}
if (getPriority(peeked) >= getPriority(str[i])) {
if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') {
res[lastUsedResIndex] = ' ';
lastUsedResIndex++;
}
res[lastUsedResIndex] = pop(stack);
lastUsedResIndex++;
} else {
break;
}
}
push(stack, str[i]);
} else if (str[i] == '(') {
push(stack, str[i]);
} else if (str[i] == ')') {
char peeked = 0;
if (isEmpty(stack)) {
deleteStack(stack);
return 1;
}
peeked = peek(stack);
while (peeked != '(') {
if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') {
res[lastUsedResIndex] = ' ';
lastUsedResIndex++;
}
res[lastUsedResIndex] = peeked;
lastUsedResIndex++;
pop(stack);

if (isEmpty(stack)) {
deleteStack(stack);
return 1;
}
peeked = peek(stack);
}
pop(stack);
}
}
while (!isEmpty(stack)) {
char popped = pop(stack);
if (popped == '(') {
deleteStack(stack);
return 1;
}
if (lastUsedResIndex > 0 && res[lastUsedResIndex - 1] != ' ') {
res[lastUsedResIndex] = ' ';
lastUsedResIndex++;
}
res[lastUsedResIndex] = popped;
lastUsedResIndex++;
}
res[lastUsedResIndex] = '\0';
deleteStack(stack);
return 0;
}

int main(void)
{
int n = 0;
scanf("%d\n", &n);

char* input = calloc(n + 1, sizeof(char));
fgets(input, n + 1, stdin);

char* res = calloc(n + 1, sizeof(char));

if (sortStation(input, res) == 0) {
printf("%s\n", res);
} else {
printf("Входная строка некорректна");
}

free(input);
free(res);
return 0;
}
64 changes: 64 additions & 0 deletions 05_stack_and_queue/src/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "stack.h"
#include <stdbool.h>
#include <stdlib.h>

typedef struct StackNode {
char data;
struct StackNode* next;
} StackNode;

typedef struct Stack {
StackNode* head;
} Stack;

Stack* newStack(void)
{
Stack* stack = calloc(1, sizeof(Stack));
return stack;
}

bool isEmpty(Stack* stack)
{
return stack->head == NULL;
}

void push(Stack* stack, char data)
{
StackNode* element = (StackNode*)calloc(1, sizeof(StackNode));

element->data = data;
element->next = stack->head;
stack->head = element;
}

char pop(Stack* stack)
{
if (isEmpty(stack)) {
return '\0';
}

StackNode* oldNode = stack->head;
char data = oldNode->data;
stack->head = stack->head->next;

free(oldNode);

return data;
}

char peek(Stack* stack)
{
if (isEmpty(stack)) {
return '\0';
}

return stack->head->data;
}

void deleteStack(Stack* stack)
{
while (!isEmpty(stack)) {
pop(stack);
}
free(stack);
}
11 changes: 11 additions & 0 deletions 05_stack_and_queue/src/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* newStack(void);
bool isEmpty(Stack*);
void push(Stack* stack, char data);
char pop(Stack* stack);
char peek(Stack* stack);
void deleteStack(Stack* stack);