diff --git a/5/CMakeLists.txt b/5/CMakeLists.txt new file mode 100644 index 0000000..3af5bfa --- /dev/null +++ b/5/CMakeLists.txt @@ -0,0 +1,18 @@ +# Устанавливаем минимальную версию CMake +cmake_minimum_required(VERSION 3.25) +# Указываем название проекта и используемый язык(и) +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED ON) + +project(AdvancedBalance C) +project(ShuntingYard C) + +# Перечисляем библиотеки +add_library(Stack stack.c) + +# Указываем исполняемый файл +add_executable(AdvancedBalance advanced_balance.c) +add_executable(ShuntingYard shuntingYard.c) +# Связываемся с библиотеками +target_link_libraries(AdvancedBalance PRIVATE Stack) +target_link_libraries(ShuntingYard PRIVATE Stack) diff --git a/5/shuntingYard.c b/5/shuntingYard.c new file mode 100644 index 0000000..3001aa7 --- /dev/null +++ b/5/shuntingYard.c @@ -0,0 +1,86 @@ +#include "stack.h" + +int getPriority(char cur); + +void main() +{ + char string[200] = { ' ' }; + + Stack stack; + init(&stack); + fgets(string, sizeof(string), stdin); + + int countSpace = 0; + + for (int i = 0; i < 200; i++) { + char cur = string[i]; + if (cur == '\n') { + break; + } + + else if (cur == '(') { + push(&stack, cur); + } + + else if (cur == ')') { + while (stack.size > 0) { + Node* node = pop(&stack); + char value = (*node).value; + if (value == '(') { + break; + } + + printf(" %c", value); + } + } else if (cur == '+' || cur == '-' || cur == '*' || cur == '/') { + int curPriority = getPriority(cur); + while (stack.size > 0) { + Node* node = pop(&stack); + char value = (*node).value; + + int valuePriority = getPriority(value); + if (valuePriority >= curPriority) { + printf(" %c", value); + } else { + push(&stack, value); + free(node); + break; + } + } + push(&stack, cur); + } + + else { + if (cur == ' ' && countSpace == 0) { + printf("%c", cur); + countSpace += 1; + } else if (cur != ' ') { + printf("%c", cur); + countSpace = 0; + } + } + } + + while (stack.size > 0) { + Node* node = pop(&stack); + char value = (*node).value; + if (value == '(') { + break; + } + + printf(" %c", value); + } + + printf("\n"); +} + +int getPriority(char cur) +{ + if (cur == '+' || cur == '-') { + return 1; + } else if (cur == '*' || cur == '/') { + return 2; + } + + return -1000; +} diff --git a/5/stack.c b/5/stack.c new file mode 100644 index 0000000..447a011 --- /dev/null +++ b/5/stack.c @@ -0,0 +1,46 @@ +#include "stack.h" + +void init(Stack* stack) +{ + (*stack).top = NULL; + (*stack).size = 0; +} + +void push(Stack* stack, char value) +{ + Node* new_node = malloc(sizeof(Node)); + + (*new_node).value = value; + (*new_node).next = (*stack).top; + + (*stack).top = new_node; + (*stack).size++; +} + +Node* pop(Stack* stack) +{ + if ((*stack).top == NULL) { + return NULL; + } + + Node* out = (*stack).top; + (*stack).top = (*out).next; + + (*stack).size--; + + return out; +} + +void printStack(Stack* stack) +{ + if ((*stack).size == 0) { + printf("\n"); + return; + } + Node* current = (*stack).top; + do { + printf("%c ", (*current).value); + current = (*current).next; + } while (current != NULL); + printf("\n"); +} diff --git a/5/stack.h b/5/stack.h new file mode 100644 index 0000000..7609179 --- /dev/null +++ b/5/stack.h @@ -0,0 +1,17 @@ +#include +#include + +typedef struct Node { + char value; + struct Node* next; +} Node; + +typedef struct Stack { + Node* top; + int size; +} Stack; + +void init(Stack* stack); +void push(Stack* stack, char value); +Node* pop(Stack* stack); +void printStack(Stack* stack);