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 sortStans/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.25)

project(sortStation C)

add_library(stack stack.c)

add_executable(sortStation sortStation.c)

target_link_libraries(sortStation PRIVATE stack)
82 changes: 82 additions & 0 deletions sortStans/sortStation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "stack.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// переносит все операторы от ")" до "(" из стека в очередь по индексу ind
int moveBtwStap(struct Stack* stack, char* queue, int ind)
{
int index = ind;
char operator = pop(stack);
while (operator != '(' && operator != '\0') {
index++;
// printf("%s\n", queue[ind]);
queue[index] = operator;
operator = pop(stack);
}
return index;
}

char* postfNotation(char* str, int lenght)
{
char* queue = (char*)malloc((lenght + 1) * sizeof(char));
int indQueue = 0;
struct Stack* operators = new();

for (int ind = 0; ind < lenght; ind++) {
if (str[ind] == '-' || str[ind] == ')' || str[ind] == '+' || str[ind] == '*' || str[ind] == '/') {
if (str[ind] == ')') {
indQueue = moveBtwStap(operators, queue, indQueue);
} else {
if (str[ind] == '*' || str[ind] == '/') {
while (!isEmpty(operators) && (peek(operators) == '*' || peek(operators) == '/')) {
indQueue++;
queue[indQueue] = pop(operators);
}
push(operators, str[ind]);
} else if (str[ind] == '+' || str[ind] == '-') {
while (!isEmpty(operators) && (peek(operators) == '-' || peek(operators) == '+' || peek(operators) == '*' || peek(operators) == '/')) {
indQueue++;
queue[indQueue] = pop(operators);
}
push(operators, str[ind]);
}
}
} else if (str[ind] == '(') {
push(operators, str[ind]);
} else if (str[ind] != ' ' && str[ind] != '\n') {
indQueue++;
queue[indQueue] = str[ind];
}
}

while (!isEmpty(operators)) {
char operator = pop(operators);
if (operator != '(') {
indQueue++;
queue[indQueue] = operator;
}
}
indQueue++;
queue[indQueue] = '\0';
deleteStack(operators);
return queue;
}

int main(int argc, char** argv)
{
char str[1000];
printf("Введите строку: ");
fgets(str, sizeof(str), stdin);
int lenght = strlen(str);

char* results = postfNotation(str, lenght);
for (int ind = 0; ind < lenght; ind++) {
printf("%c ", results[ind]);
}
printf("\n");
free(results);

return 0;
}
78 changes: 78 additions & 0 deletions sortStans/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
// #include<stdbool.h>

struct StackNode {

Choose a reason for hiding this comment

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

Можно сделать typedef, будет проще

// значение
char value;
// ссылка на следующую "ячейку"
struct StackNode* next;
};

// делаем ссsлку на голову
struct Stack {
struct StackNode* head;
};

// создаем пустую структуру с пустой ссылкой на нулевую голову
struct Stack* new(void)

Choose a reason for hiding this comment

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

На Stack больше не нужно ссылаться как stack Struct

{
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 sortStans/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);
Comment on lines +10 to +11

Choose a reason for hiding this comment

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

До этого были просто Stack, теперь struct Stack, хотелось бы чтобы было консистентно