-
Notifications
You must be signed in to change notification settings - Fork 0
Homework5 task2 #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
shannami
wants to merge
17
commits into
main
Choose a base branch
from
homework5Task2
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
5db5e35
add src
shannami d0c6211
add stack
shannami 268813d
add dir stack/
shannami 9103dd9
add task2.c (stack)
shannami de6c8bb
add task2.c to stack/
shannami 6116661
move task2.c to stack/
shannami 177d461
move instruction.txt to stack/
shannami 14cfea6
add CMake for stack
shannami 16e57d9
fixed stack.c
shannami 2049e5e
fixed stack.h
shannami a26f9bd
formatting changes
shannami 652c43c
added comments for a function, changed the names of the deletion func…
shannami 0587e32
Merge branch 'homework5' into homework5Task2.
shannami 6e43bcf
fixed task2.c
shannami e4474a7
add CMake
shannami 3f75526
fixed CMake
shannami eec838c
fixed cmake again..
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(task2 task2.c) | ||
|
|
||
| target_link_libraries(task2 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 |
|
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. Претензии к стеку такие же, как и в соседнем PR |
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,105 @@ | ||
| #include "stack.h" | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| int precedence(char op) | ||
| { | ||
| if (op == '*' || op == '/') | ||
| return 2; | ||
| if (op == '+' || op == '-') | ||
| return 1; | ||
| return 0; | ||
| } | ||
|
|
||
| int isOperator(char c) | ||
| { | ||
| return c == '+' || c == '-' || c == '*' || c == '/'; | ||
| } | ||
|
|
||
| int isDigit(char c) | ||
|
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. |
||
| { | ||
| return c >= '0' && c <= '9'; | ||
| } | ||
|
|
||
| /* | ||
| Пользователь должен выделить минимум 2 * strlen(infix) символ. | ||
| return 1 — если преобразование прошло успешно, | ||
| return 0 — если ошибка (неправильные скобки или неподдерживаемый символ). | ||
| */ | ||
| int infixToPostfix(char* infix, char* postfix) | ||
| { | ||
| Stack* st = newStack(); | ||
| int rpn = 0; // индекс для записи в reverse polish notation(постфиксная запись) | ||
|
|
||
| for (int i = 0; infix[i]; ++i) { | ||
| char c = infix[i]; | ||
|
|
||
| if (isDigit(c)) { | ||
| postfix[rpn++] = c; | ||
| } else if (c == '(') { | ||
| push(st, c); | ||
| } else if (c == ')') { | ||
| int found = 0; | ||
| while (!isEmpty(st)) { | ||
| int top = pop(st); | ||
| if (top == '(') { | ||
| found = 1; | ||
| break; | ||
| } | ||
| postfix[rpn++] = (char)top; | ||
| } | ||
|
|
||
| if (!found) { | ||
| printf("лишняя закрывающая скобка ')'\n"); | ||
| deleteStack(st); | ||
| return 0; | ||
| } | ||
| } else if (isOperator(c)) { | ||
| while (!isEmpty(st)) { | ||
| int top = pop(st); | ||
| if (precedence(top) < precedence(c)) { | ||
| push(st, top); | ||
| break; | ||
| } | ||
| postfix[rpn++] = (char)top; | ||
| } | ||
| push(st, c); | ||
| } else { | ||
| printf("неподдерживаемый символ '%c'\n", c); | ||
| deleteStack(st); | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| // проверка не осталось ли незакрытых скобок | ||
| while (!isEmpty(st)) { | ||
| int top = pop(st); | ||
| if (top == '(') { | ||
| printf("лишняя открывающая скобка '('\n"); | ||
| deleteStack(st); | ||
| return 0; | ||
| } | ||
| postfix[rpn++] = (char)top; | ||
| } | ||
|
|
||
| postfix[rpn] = '\0'; | ||
| deleteStack(st); | ||
| return 1; | ||
| } | ||
|
|
||
| int main(void) | ||
| { | ||
| char input[100]; | ||
| char output[2 * 100]; | ||
|
|
||
| printf("инфиксное выражение:\n"); | ||
| scanf("%99s", input); | ||
|
|
||
| if (!infixToPostfix(input, output)) { | ||
| printf("ошибка.\n"); | ||
| return 1; | ||
| } | ||
| printf("постфиксное выражение: %s\n", output); | ||
|
|
||
| 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.
Интересно, а здесь CMake файлов нет (да и не надо уже, наверное, сделаете отдельный PR для нужной домашки)