-
Notifications
You must be signed in to change notification settings - Fork 0
Сортировочная станция #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Andrew-Kochanov
wants to merge
17
commits into
main
Choose a base branch
from
hw5sortStation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ed07fe0
Продвинутый баланс скобок
Andrew-Kochanov 8016d21
Чуть переделал продвинутый баланс скобок
Andrew-Kochanov 083b097
Реализация стека
Andrew-Kochanov 4cf30ef
Добавил функцию peek
Andrew-Kochanov 7609540
Добавил функцию peek
Andrew-Kochanov aeefeda
Сортировочная станция
Andrew-Kochanov 20ba75d
Добавил функцию deleteStack
Andrew-Kochanov 06228b9
Удалил старую программу продвинутого баланса скобок
Andrew-Kochanov ee37134
Поменял файлы в ветке
Andrew-Kochanov 03eea18
Удалил программу старого продвинутого баланса скобок
Andrew-Kochanov 349fb03
stack.h без функции deleteStack
Andrew-Kochanov 4831ff0
Добавил в stack.h функцию deleteStack
Andrew-Kochanov 00ebe99
CMakeLists.txt
Andrew-Kochanov 5f9a537
Папка build
Andrew-Kochanov e127ee8
Удалил папку build
Andrew-Kochanov 83efe9d
Переместил всё в папку
Andrew-Kochanov 8f32688
Папка со всем
Andrew-Kochanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| // значение | ||
| char value; | ||
| // ссылка на следующую "ячейку" | ||
| struct StackNode* next; | ||
| }; | ||
|
|
||
| // делаем ссsлку на голову | ||
| struct Stack { | ||
| struct StackNode* head; | ||
| }; | ||
|
|
||
| // создаем пустую структуру с пустой ссылкой на нулевую голову | ||
| struct Stack* new(void) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На |
||
| { | ||
| 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. До этого были просто |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно сделать typedef, будет проще