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
Выполнено индивидуальное задание 2 #85
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,35 +1,46 @@ | ||
#include <stdlib.h> | ||
#include "stack.h" | ||
#include "list.h" | ||
#include <iostream> | ||
|
||
struct Stack | ||
{ | ||
List* list; | ||
}; | ||
|
||
Stack *stack_create() | ||
{ | ||
return new Stack; | ||
Stack* stack = new Stack; | ||
stack->list = list_create(); | ||
return stack; | ||
} | ||
|
||
void stack_delete(Stack *stack) | ||
{ | ||
// TODO: free stack elements | ||
delete stack; | ||
list_delete(stack->list); | ||
delete stack; | ||
} | ||
|
||
void stack_push(Stack *stack, Data data) | ||
{ | ||
list_insert(stack->list, data); | ||
} | ||
|
||
Data stack_get(const Stack *stack) | ||
{ | ||
return (Data)0; | ||
return list_item_data(list_first(stack->list)); | ||
} | ||
|
||
void stack_pop(Stack *stack) | ||
{ | ||
if (list_first(stack->list) != NULL) | ||
list_erase(stack->list, list_first(stack->list)); | ||
} | ||
|
||
bool stack_empty(const Stack *stack) | ||
{ | ||
return true; | ||
if (list_first(stack->list) == NULL) | ||
return true; | ||
else | ||
return false; | ||
} |
This file contains 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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
// Stack | ||
// Stores integer values inside | ||
typedef int Data; | ||
typedef char Data; | ||
|
||
struct Stack; | ||
|
||
|
This file contains 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,3 @@ | ||
add_executable(Task main.cpp) | ||
target_include_directories(Task PUBLIC ../LibraryCPP) | ||
target_link_libraries(Task LibraryCPP) |
This file contains 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,73 @@ | ||
#include <iostream> | ||
#include "stack.h" | ||
|
||
void buildBackPolishExpression(const std::string& text) | ||
{ | ||
Stack* stack = stack_create(); | ||
|
||
for (int i = 0; i < text.size(); i++) | ||
{ | ||
if (text[i] != '*' && text[i] != '/' && text[i] != '+' && text[i] != '-') | ||
{ | ||
std::cout << text[i]; | ||
} | ||
else | ||
{ | ||
if (stack_empty(stack)) | ||
{ | ||
stack_push(stack, text[i]); | ||
} | ||
else | ||
{ | ||
if (stack_get(stack) == '*' || stack_get(stack) == '/') | ||
{ | ||
std::cout << stack_get(stack); | ||
stack_pop(stack); | ||
if (!stack_empty(stack) && (text[i] == '+' || text[i] == '-')) | ||
{ | ||
std::cout << stack_get(stack); | ||
stack_pop(stack); | ||
stack_push(stack, text[i]); | ||
} | ||
else | ||
{ | ||
stack_push(stack, text[i]); | ||
} | ||
} | ||
else | ||
{ | ||
if (text[i] == '*' || text[i] == '/') | ||
{ | ||
stack_push(stack, text[i]); | ||
} | ||
else | ||
{ | ||
std::cout << stack_get(stack); | ||
stack_pop(stack); | ||
stack_push(stack, text[i]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
while (!stack_empty(stack)) | ||
{ | ||
std::cout << stack_get(stack); | ||
stack_pop(stack); | ||
} | ||
|
||
stack_delete(stack); | ||
} | ||
|
||
int main() | ||
{ | ||
std::string text; | ||
|
||
std::cout << "Enter an expression: "; | ||
std::cin >> text; | ||
|
||
buildBackPolishExpression(text); | ||
|
||
return 0; | ||
} |
This file contains 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,3 @@ | ||
add_executable(TestListCPP main.cpp) | ||
target_include_directories(TestListCPP PUBLIC ../LibraryCPP) | ||
target_link_libraries(TestListCPP LibraryCPP) |
This file contains 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,36 @@ | ||
#include <iostream> | ||
#include "list.h" | ||
|
||
int main() | ||
{ | ||
List *list = list_create(); | ||
|
||
if (!list) | ||
std::cout << "List creation error\n"; | ||
|
||
list_insert(list, 1); | ||
list_insert(list, 2); | ||
list_insert(list, 3); | ||
|
||
if (list_item_data(list_first(list)) != 3) | ||
std::cout << "list_insert error\n"; | ||
|
||
list_insert_after(list, list_first(list), 4); | ||
|
||
if (list_item_data(list_item_next(list_first(list))) != 4) | ||
std::cout << "list_insert_after error\n"; | ||
|
||
list_erase(list, list_first(list)); | ||
|
||
if (list_item_data(list_first(list)) != 4) | ||
std::cout << "list_erase error\n"; | ||
|
||
std::cout << "List: "; | ||
for (ListItem *item = list_first(list) ; item ; item = list_item_next(item)) | ||
{ | ||
std::cout << list_item_data(item) << " "; | ||
} | ||
std::cout << "\n"; | ||
|
||
list_delete(list); | ||
} |
This file contains 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,3 @@ | ||
add_executable(TestStackCPP main.cpp) | ||
target_include_directories(TestStackCPP PUBLIC ../LibraryCPP) | ||
target_link_libraries(TestStackCPP LibraryCPP) |
This file contains 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,41 @@ | ||
#include <iostream> | ||
#include "stack.h" | ||
|
||
int main() | ||
{ | ||
Stack *stack = stack_create(); | ||
|
||
stack_push(stack, 1); | ||
stack_push(stack, 2); | ||
stack_push(stack, 3); | ||
|
||
if (stack_get(stack) != 3) | ||
std::cout << "Invalid stack top after push\n"; | ||
|
||
std::cout << "Get: " << stack_get(stack) << "\n"; | ||
stack_pop(stack); | ||
|
||
if (stack_get(stack) != 2) | ||
std::cout << "Invalid stack top after pop\n"; | ||
|
||
std::cout << "Get: " << stack_get(stack) << "\n"; | ||
stack_pop(stack); | ||
|
||
if (stack_get(stack) != 1) | ||
std::cout << "Invalid stack top after pop\n"; | ||
|
||
std::cout << "Get: " << stack_get(stack) << "\n"; | ||
stack_push(stack, 4); | ||
stack_push(stack, 5); | ||
|
||
if (stack_get(stack) != 5) | ||
std::cout << "Invalid stack top after push\n"; | ||
|
||
while (!stack_empty(stack)) | ||
{ | ||
std::cout << "Get: " << stack_get(stack) << "\n"; | ||
stack_pop(stack); | ||
} | ||
|
||
stack_delete(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.
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.
Где-то надо ещё и память от удаленного элемента освободить.