-
Notifications
You must be signed in to change notification settings - Fork 0
ДЗ № 5.2 Сортировочная станция, Шестаков Николай #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
345bc50
Add stack library header file
NicholayShestakov 4737d32
Add stack library realisation file
NicholayShestakov 405bf4c
Update stack header file commentaries
NicholayShestakov ce89c99
Update stack realisation file
NicholayShestakov f4d1e69
Update stack realisation file
NicholayShestakov d389c7d
Update stack header file
NicholayShestakov ceeeb02
Update stack header file
NicholayShestakov f344512
Update stack realisation file
NicholayShestakov caa1bf9
Update stack header file
NicholayShestakov 0d99e19
Update stack realisation file
NicholayShestakov 8f9bc54
Add sorting station algorithm
NicholayShestakov 851d7c8
Add compile instruction for sorting station
NicholayShestakov 5871294
Update .gitignore with adding ignoring CMake build directory
NicholayShestakov fd01840
Add CMakeLists.txt for build sorting station
NicholayShestakov 6fe8041
Update instructions how to build
NicholayShestakov 1038190
Update grammary in documentations
NicholayShestakov 74218cf
Update stack realization
NicholayShestakov 60792cf
Update stack header documentation
NicholayShestakov 46eac81
Add build instruction in README
NicholayShestakov 2a35f61
Delete build instruction from stack folder
NicholayShestakov 8646f0d
Update sorting station
NicholayShestakov 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 |
|---|---|---|
|
|
@@ -55,4 +55,7 @@ Module.symvers | |
| Mkfile.old | ||
| dkms.conf | ||
|
|
||
| # CMake | ||
| build | ||
|
|
||
| # End of https://www.toptal.com/developers/gitignore/api/c | ||
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,8 @@ | ||
| cmake_minimum_required(VERSION 3.25) | ||
| project(sorting_station C) | ||
|
|
||
| add_library(stack src/stack_and_queue/stack/stack.c) | ||
|
|
||
| add_executable(sorting_station src/stack_and_queue/stack/sorting_station.c) | ||
| target_link_libraries(sorting_station PRIVATE stack) | ||
| target_compile_options(sorting_station PRIVATE -Wall -Wextra -pedantic) |
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
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,156 @@ | ||
| #include "stack.h" | ||
| #include <assert.h> | ||
| #include <ctype.h> | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| bool isOperatorFirstPriority(char c) | ||
| { | ||
| return c == '*' || c == '/'; | ||
| } | ||
|
|
||
| bool isOperatorSecondPriority(char c) | ||
| { | ||
| return c == '+' || c == '-'; | ||
| } | ||
|
|
||
| // Takes infix form of expression as string and returns pointer to postfix form string. Please, dont forget free memory. | ||
| // Can track unbalanced brackets and correctness of order of numbers and operators. Cant track extraneous symbols. | ||
| // If the error is tracked, returns pointer to the string with the error description. | ||
| char* sortingStation(char* infixForm) | ||
WoWaster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| char* output = calloc(2 * strlen(infixForm), sizeof(output)); | ||
| Stack* operators = newStack(); | ||
|
|
||
| bool isNumberTurn = true; // For check correctness of order. | ||
| char* errorMessage = calloc(64, sizeof(*errorMessage)); // For error returns. | ||
| int indexOfOutput = 0; | ||
| for (int indexOfInfix = 0; infixForm[indexOfInfix] != '\0'; ++indexOfInfix) { | ||
| if (isdigit(infixForm[indexOfInfix])) { | ||
| if (!isNumberTurn) { | ||
| strcat(errorMessage, "Error! Order of numbers and operators is incorrect."); | ||
| } | ||
| isNumberTurn = !isNumberTurn; | ||
|
|
||
| output[indexOfOutput] = infixForm[indexOfInfix]; | ||
| output[indexOfOutput + 1] = ' '; | ||
| indexOfOutput += 2; | ||
WoWaster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| if (isOperatorFirstPriority(infixForm[indexOfInfix])) { | ||
| if (isNumberTurn) { | ||
| strcat(errorMessage, "Error! Order of numbers and operators is incorrect."); | ||
| } | ||
| isNumberTurn = !isNumberTurn; | ||
|
|
||
| push(operators, infixForm[indexOfInfix]); | ||
| } | ||
| if (isOperatorSecondPriority(infixForm[indexOfInfix])) { | ||
| if (isNumberTurn) { | ||
| strcat(errorMessage, "Error! Order of numbers and operators is incorrect."); | ||
| } | ||
| isNumberTurn = !isNumberTurn; | ||
|
|
||
| while (!isEmpty(operators) && peek(operators) != '(') { | ||
| output[indexOfOutput++] = pop(operators); | ||
| output[indexOfOutput++] = ' '; | ||
| } | ||
| push(operators, infixForm[indexOfInfix]); | ||
| } | ||
| if (infixForm[indexOfInfix] == '(') { | ||
| if (!isNumberTurn) { | ||
| strcat(errorMessage, "Error! Number before opening bracket."); | ||
| } | ||
|
|
||
| push(operators, infixForm[indexOfInfix]); | ||
| } | ||
| // Многоступенчатость в целях предотвращения одновременного добавления сразу нескольких сообщений об ошибке. | ||
| if (infixForm[indexOfInfix] == ')') { | ||
| if (isEmpty(operators)) { | ||
| strcat(errorMessage, "Error! Brackets not opened."); | ||
| } else { | ||
| if (isNumberTurn) { | ||
| strcat(errorMessage, "Error! Operator before closing bracket."); | ||
| } else { | ||
| while (peek(operators) != '(') { | ||
| output[indexOfOutput++] = pop(operators); | ||
| output[indexOfOutput++] = ' '; | ||
|
|
||
| if (isEmpty(operators)) { | ||
| strcat(errorMessage, "Error! Brackets not opened."); | ||
| break; | ||
| } | ||
| } | ||
| if (!isEmpty(operators)) { | ||
| pop(operators); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Данное условие истинно, когда строки не равны. | ||
| if (strcmp(errorMessage, "")) { | ||
| free(output); | ||
| deleteStack(operators); | ||
| return errorMessage; | ||
| } | ||
| } | ||
|
|
||
| while (!isEmpty(operators)) { | ||
| output[indexOfOutput++] = pop(operators); | ||
| output[indexOfOutput++] = ' '; | ||
|
|
||
| if (output[indexOfOutput - 2] == '(') { | ||
| free(output); | ||
| deleteStack(operators); | ||
| strcat(errorMessage, "Error! Brackets not closed."); | ||
| return errorMessage; | ||
| } | ||
| } | ||
|
|
||
| deleteStack(operators); | ||
| return output; | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
| char* testSimple = sortingStation("(1 + (4 + 5 / 2) - 3) + (6 + 8)"); | ||
| char* testNoSpaces = sortingStation("4/2+6"); | ||
| char* testManyBrackets = sortingStation("((((1))))"); | ||
| char* testOnlyNumber = sortingStation("1"); | ||
| char* testEmpty = sortingStation(""); | ||
| char* testIncorrectOrder = sortingStation("1 + + 1"); | ||
| char* testIncorrectOrder2 = sortingStation("+ 1"); | ||
| char* testNumberBeforeBracket = sortingStation("1 (+ 3)"); | ||
| char* testOperatorBeforeBracket = sortingStation("1 + (2 + ) 3"); | ||
| char* testBracketsNotOpened = sortingStation("1 + 2)"); | ||
| char* testBracketsNotClosed = sortingStation("1 + (2 + 3"); | ||
|
|
||
| assert(!strcmp(testSimple, "1 4 5 2 / + + 3 - 6 8 + + ") && "testSimple incorrect."); | ||
| assert(!strcmp(testNoSpaces, "4 2 / 6 + ") && "testNoSpaces incorrect."); | ||
| assert(!strcmp(testManyBrackets, "1 ") && "testManyBrackets incorrect."); | ||
| assert(!strcmp(testOnlyNumber, "1 ") && "testOnlyNumber incorrect."); | ||
| assert(!strcmp(testEmpty, "") && "testEmpty incorrect."); | ||
| assert(!strcmp(testIncorrectOrder, "Error! Order of numbers and operators is incorrect.") && "testIncorrectOrder incorrect."); | ||
| assert(!strcmp(testIncorrectOrder2, "Error! Order of numbers and operators is incorrect.") && "testIncorrectOrder2 incorrect."); | ||
| assert(!strcmp(testNumberBeforeBracket, "Error! Number before opening bracket.") && "testNumberBeforeBracket incorrect."); | ||
| assert(!strcmp(testOperatorBeforeBracket, "Error! Operator before closing bracket.") && "testOperatorBeforeBracket incorrect."); | ||
| assert(!strcmp(testBracketsNotOpened, "Error! Brackets not opened.") && "testBracketsNotOpened incorrect."); | ||
| assert(!strcmp(testBracketsNotClosed, "Error! Brackets not closed.") && "testBracketsNotClosed incorrect."); | ||
|
|
||
| printf("All tests passed successfully.\n"); | ||
|
|
||
| free(testSimple); | ||
| free(testNoSpaces); | ||
| free(testManyBrackets); | ||
| free(testOnlyNumber); | ||
| free(testEmpty); | ||
| free(testIncorrectOrder); | ||
| free(testIncorrectOrder2); | ||
| free(testNumberBeforeBracket); | ||
| free(testOperatorBeforeBracket); | ||
| free(testBracketsNotOpened); | ||
| free(testBracketsNotClosed); | ||
| return 0; | ||
| } | ||
NicholayShestakov marked this conversation as resolved.
Show resolved
Hide resolved
|
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,60 @@ | ||
| #include "stack.h" | ||
|
|
||
| #include <assert.h> | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| typedef struct StackNode { | ||
| int value; | ||
| struct StackNode* next; | ||
| } StackNode; | ||
|
|
||
| struct Stack { | ||
| StackNode* head; | ||
| }; | ||
|
|
||
| Stack* newStack() | ||
| { | ||
| Stack* stack = calloc(1, sizeof(*stack)); | ||
| assert(stack != NULL && "Error! Memory allocated incorrect."); | ||
| return stack; | ||
| } | ||
|
|
||
| void deleteStack(Stack* stack) | ||
| { | ||
| while (!isEmpty(stack)) { | ||
| pop(stack); | ||
| } | ||
| free(stack); | ||
| } | ||
|
|
||
| void push(Stack* stack, int value) | ||
| { | ||
| StackNode* newNode = malloc(sizeof(*newNode)); | ||
| assert(newNode != NULL && "Error! Memory allocated incorrect."); | ||
| newNode->value = value; | ||
| newNode->next = stack->head; | ||
| stack->head = newNode; | ||
| } | ||
|
|
||
| int pop(Stack* stack) | ||
| { | ||
| assert(!isEmpty(stack) && "Error! Stack is empty. Can`t pop value."); | ||
| StackNode* poppedNode = stack->head; | ||
| int value = poppedNode->value; | ||
| stack->head = poppedNode->next; | ||
| free(poppedNode); | ||
| return value; | ||
| } | ||
|
|
||
| int peek(Stack* stack) | ||
| { | ||
| assert(!isEmpty(stack) && "Error! Stack is empty. Can`t peek value."); | ||
| return stack->head->value; | ||
| } | ||
|
|
||
| bool isEmpty(Stack* stack) | ||
| { | ||
| return stack->head == NULL; | ||
| } |
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,33 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| typedef struct Stack Stack; | ||
|
|
||
| // Creates new stack. | ||
| // Returns pointer to the created stack. | ||
| Stack* newStack(); | ||
|
|
||
| // Deletes stack. Frees all memory used in it. | ||
| // Takes pointer to the stack. | ||
| void deleteStack(Stack* stack); | ||
|
|
||
| // Pushes new value on the top of the stack. | ||
| // Takes pointer to the stack and value for push. | ||
| void push(Stack* stack, int value); | ||
|
|
||
| // Deletes the top value of the stack and returns it. Frees memory used for top node. | ||
| // Takes pointer to the stack. | ||
| // Don`t use if the stack is empty. It causes the error. | ||
| // Check this with isEmpty before use this function. | ||
| int pop(Stack* stack); | ||
|
|
||
| // Returns the top value of the stack. | ||
| // Takes pointer to the stack. | ||
| // Don`t use if the stack is empty. It causes the error. | ||
| // Check this with isEmpty before use this function. | ||
| int peek(Stack* stack); | ||
|
|
||
| // Returns true if the stack is empty and false if not. | ||
| // Takes pointer to the stack. | ||
| bool isEmpty(Stack* stack); |
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.
Uh oh!
There was an error while loading. Please reload this page.