-
Notifications
You must be signed in to change notification settings - Fork 0
Homework5 task1 #3
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
shannami
wants to merge
20
commits into
main
Choose a base branch
from
homework5Task1
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
20 commits
Select commit
Hold shift + click to select a range
5db5e35
add src
shannami d0c6211
add stack
shannami 268813d
add dir stack/
shannami 63f39a6
add task1.c
shannami ba11e7d
move files to stack/
shannami 14cfea6
add CMake for stack
shannami 79b6ae9
Merge branch 'homework5' into homework5Task1
shannami 3669845
correction cmake
shannami 5da790e
fixed files
shannami 16e57d9
fixed stack.c
shannami 2049e5e
fixed stack.h
shannami a26f9bd
formatting changes
shannami 84cc932
formatting changes
shannami 0631b73
formatting changes
shannami 652c43c
added comments for a function, changed the names of the deletion func…
shannami 407860e
resolve merge conflict
shannami 11289a8
fixed the file
shannami 0b3fcea
fixed again
shannami 33a950e
fixed format
shannami b0e727d
fixed task1
shannami 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
Some comments aren't visible on the classic Files Changed page.
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,6 @@ | ||
| cmake_minimum_required(VERSION 3.25) | ||
| project(homeworks C) | ||
|
|
||
| add_compile_options(-Wall -Wextra -pedantic) | ||
|
|
||
| add_subdirectory(src/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,5 @@ | ||
| add_library(stack stack.c) | ||
|
|
||
| add_executable(task1 task1.c) | ||
|
|
||
| target_link_libraries(task1 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,9 @@ | ||
| Task1: | ||
| gcc -Wall -Wextra -pedantic -c stack.c | ||
| gcc -Wall -Wextra -pedantic -c task1.c | ||
| gcc -Wall -Wextra -pedantic stack.o task1.o -o steakTask1 | ||
|
|
||
| Task2: | ||
| gcc -Wall -Wextra -pedantic -c stack.c | ||
| gcc -Wall -Wextra -pedantic -c task2.c | ||
| gcc -Wall -Wextra -pedantic stack.o task2.o -o steakTask2 |
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,54 @@ | ||
| #include "stack.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| struct StackNode { | ||
| int value; | ||
| struct StackNode* next; | ||
| }; | ||
|
|
||
| struct Stack { | ||
| struct StackNode* head; | ||
| }; | ||
|
|
||
| Stack* newStack() | ||
| { | ||
| Stack* stack = (Stack*)malloc(sizeof(Stack)); | ||
| if (!stack) | ||
| return NULL; | ||
| stack->head = NULL; | ||
| return stack; | ||
| } | ||
|
|
||
| void push(struct Stack* stack, int value) | ||
| { | ||
| struct StackNode* node = (struct StackNode*)malloc(sizeof(struct StackNode)); | ||
|
|
||
| node->value = value; | ||
| node->next = stack->head; | ||
| stack->head = node; | ||
| } | ||
|
|
||
| int pop(Stack* stack) | ||
| { | ||
| struct StackNode* oldNode = stack->head; | ||
| int res = oldNode->value; | ||
|
|
||
| stack->head = oldNode->next; | ||
| free(oldNode); | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| int isEmpty(Stack* stack) | ||
| { | ||
| return stack->head == NULL; | ||
| } | ||
|
|
||
| void deleteStack(Stack* stack) | ||
| { | ||
| 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,9 @@ | ||
| #pragma once | ||
|
|
||
| typedef struct Stack Stack; | ||
|
|
||
| Stack* newStack(); // Создание стека. | ||
| void push(Stack* stack, int value); // Добавляет элемент на вершину стека. | ||
| int pop(Stack* stack); // Убирает из стека элемент и возвращает его. Необходимо проверять isEmpty перед вызовом | ||
| int isEmpty(Stack* stack); | ||
| void deleteStack(Stack* 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,44 @@ | ||
| #include "stack.h" | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
|
|
||
| bool isBalanced(const char* s) | ||
| { | ||
| Stack* st = newStack(); | ||
|
|
||
| for (int i = 0; s[i] != '\0'; ++i) { | ||
| char c = s[i]; | ||
| if ((c == '(' || c == '[' || c == '{')) { | ||
| push(st, c); | ||
| } else if (c == ')' || c == ']' || c == '}') { | ||
| if (isEmpty(st)) { | ||
| deleteStack(st); | ||
| return false; | ||
| } | ||
| char t = pop(st); | ||
| if ((c == ')' && t != '(') || (c == ']' && t != '[') || (c == '}' && t != '{')) { | ||
| deleteStack(&st); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool res = isEmpty(st); | ||
| deleteStack(st); | ||
| return res; | ||
|
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. Содержательно правда, но вот память течёт. Это нужно исправить. При этом дублировать код для очистки памяти возможно не очень хорошо, но флаг может помочь. |
||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| char str[100]; | ||
|
|
||
| printf("строка: "); | ||
| scanf("%s", str); | ||
|
|
||
| if (isBalanced(str)) | ||
| printf("верно\n"); | ||
| else | ||
| printf("неверно\n"); | ||
|
|
||
| return 0; | ||
| } | ||
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.
Хорошо бы ещё уметь удалять стек