-
Notifications
You must be signed in to change notification settings - Fork 0
Домашнее задание 5.1. Продвинутый баланс скобок. Разгуляева А.И. #2
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
ada1ra
wants to merge
11
commits into
main
Choose a base branch
from
hw_5-1_proBrackets
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
11 commits
Select commit
Hold shift + click to select a range
bb1be3c
Add .c file with stack
ada1ra 9283e17
Add .h file with stack
ada1ra 97cd461
Add .c file with homework
ada1ra 3e06a67
Add .h file with homework
ada1ra 021ec94
Add build instruction
ada1ra 54b2770
Updated stack.c with names changes according to WebKit
ada1ra a3a5e6d
Updated stack.h with names changes according to WebKit
ada1ra 53cf4a6
Merge branch 'stack' into hw_5-1_proBrackets
ada1ra 66bdad9
Updated hw_5-1_proBrackets.c with names changes according to WebKit
ada1ra 2f1c321
Rename instruction.md to README.md
ada1ra 3444d8c
Update README.md
ada1ra 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,19 @@ | ||
| # Инструкция по сборке | ||
|
|
||
| Домашнее задание 5.1 Продвинутый баланс скобок | ||
|
|
||
| 1. Откройте терминал и перейдите в директорию с файлами приложения (hw_5-1_proBrackets) | ||
| 2. Выполните команду компиляции: | ||
| ```console | ||
| gcc hw_5-1_proBrackets.c ../stack/stack.c -I.. -o hw_5-1_proBrackets | ||
| ``` | ||
| 3. Запустите программу: | ||
| ```console | ||
| ./hw_5-1_proBrackets | ||
| ``` | ||
| ### Использование | ||
| 1. Введите строку, состоящую из скобок (не более 100 символов) | ||
| 2. Нажмите Enter для завершения ввода | ||
|
|
||
| 3. Программа выведет, корректно ли расположение скобок | ||
|
|
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,48 @@ | ||
| #include "hw_5-1_proBrackets/hw_5-1_proBrackets.h" | ||
|
Collaborator
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. тут не надо так длинно, если используете -I |
||
| #include "stack/stack.h" | ||
|
Collaborator
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. обычно идет после #include<...> |
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| int isValidBrackets(char originalString[]) | ||
| { | ||
| Stack* bracketsStack = stackNew(); | ||
|
|
||
| for (int i = 0; i < strlen(originalString); i++) { | ||
| char s = originalString[i]; | ||
|
|
||
| if ((s == '(') || (s == '{') || (s == '[')) { | ||
| stackPush(bracketsStack, s); | ||
|
|
||
| } else if ((s == ')' && bracketsStack == NULL) | ||
| || (s == '}' && bracketsStack == NULL) | ||
| || (s == ']' && bracketsStack == NULL)) { | ||
| return 0; | ||
|
|
||
| } else if ((s == ')' && stackPeek(bracketsStack) == '(') | ||
| || (s == '}' && stackPeek(bracketsStack) == '{') | ||
| || (s == ']' && stackPeek(bracketsStack) == '[')) { | ||
| stackPop(bracketsStack); | ||
|
|
||
| } else { | ||
| return 0; | ||
| } | ||
| } | ||
| if (bracketsStack != NULL && bracketsStack->top != NULL) { | ||
| return 0; | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| char originalString[100]; | ||
| scanf("%99s", originalString); | ||
| int result = isValidBrackets(originalString); | ||
|
|
||
| if (result == 0) { | ||
| printf("String doesn't follow rules.\n"); | ||
| return -1; | ||
| } | ||
| printf("Balance of brackets is keep.\n"); | ||
| 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,10 @@ | ||
| #ifndef PROBRACKETS_H | ||
| #define PROBRACKETS_H | ||
|
|
||
| // проверка корректности расстановки скобок | ||
| int isValidBrackets(char originalString[]); | ||
|
|
||
| // ввод данных и вывод результата | ||
| int main(void); | ||
|
|
||
| #endif |
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,81 @@ | ||
| #include "stack.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| // создание пустого стека | ||
| Stack* stackNew(void) { | ||
| Stack* stack = (Stack*)malloc(sizeof(Stack)); | ||
| if (stack != NULL) { | ||
| stack->top = NULL; | ||
| } | ||
| return stack; | ||
| } | ||
|
|
||
| // добавление элемента на стек | ||
| int stackPush(Stack* stack, int value) { | ||
| if (stack == NULL) { | ||
| printf("Stack is NULL!\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| // создаем новый узел | ||
| Node* new_node = (Node*)malloc(sizeof(Node)); | ||
| if (new_node == NULL) { | ||
| printf("Memory failed!\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| // заполняем узел | ||
| new_node->data = value; | ||
| new_node->next = stack->top; | ||
|
|
||
| // обновляем вершину стека | ||
| stack->top = new_node; | ||
| } | ||
|
|
||
| // взятие элемента со стека | ||
| int stackPop(Stack* stack) { | ||
| if (stack == NULL || stack->top == NULL) { | ||
| printf("Stack is empty!\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| // сохраняем данные из вершины | ||
| Node* temp = stack->top; | ||
| int value = temp->data; | ||
|
|
||
| // перемещаем вершину на следующий элемент | ||
| stack->top = stack->top->next; | ||
|
|
||
| // освобождаем память удаляемого узла | ||
| free(temp); | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| // просмотр элемента на вершине стека | ||
| int stackPeek(Stack* stack) { | ||
| if (stack == NULL || stack->top == NULL) { | ||
| printf("Stack is empty!\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| return stack->top->data; | ||
| } | ||
|
|
||
| // удаление всего стека и освобождение памяти | ||
| void stackDelete(Stack* stack) { | ||
| if (stack == NULL) { | ||
| return; | ||
| } | ||
|
|
||
| // освобождаем все узлы | ||
| while (stack->top != NULL) { | ||
| Node* temp = stack->top; | ||
| stack->top = stack->top->next; | ||
| free(temp); | ||
| } | ||
|
|
||
| // освобождаем саму структуру стека | ||
| 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,29 @@ | ||
| #ifndef STACK_H | ||
| #define STACK_H | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| // максимальный размер стека | ||
| #define STACK_MAX_SIZE 100 | ||
|
|
||
| // структура узла стека | ||
| typedef struct Node { | ||
| int data; // целочисленные данные | ||
| struct Node* next; // указатель на следующий узел | ||
| } Node; | ||
|
|
||
| // структура стека | ||
| typedef struct { | ||
| Node* top; // указатель на вершину стека | ||
| } Stack; | ||
|
|
||
| // основные операции | ||
| int stackPush(Stack* stack, int value); // положить элемент на стек | ||
| int stackPop(Stack* stack); // взять элемент со стека | ||
| int stackPeek(Stack* stack); // посмотреть на элемент на вершине стека | ||
|
|
||
| // служебные функции | ||
| Stack* stackNew(void); // создать пустой стек | ||
| void stackDelete(Stack* stack); // удалить весь стек (освободить память) | ||
|
|
||
| #endif |
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.
лучше README.md