-
Notifications
You must be signed in to change notification settings - Fork 280
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
Lab2 #922
Closed
Closed
Lab2 #922
Changes from 60 commits
Commits
Show all changes
73 commits
Select commit
Hold shift + click to select a range
93ae4a0
Create Lab1CPP
tolstosup f54d5e7
Update CMakeLists.txt
tolstosup bbced65
Delete Lab1CPP
tolstosup 10d0458
Create 1
tolstosup e08eb2b
Delete Lab1CPP/1
tolstosup be9b3b3
Create CMakeList.txt
tolstosup 4ca63f4
Create input.txt
tolstosup 17b6188
Add files via upload
tolstosup ee95690
Update CMakeLists.txt
tolstosup 0f95d0a
Update array.cpp
tolstosup 06d4f59
Update array.h
tolstosup 78f8113
Create Михайлов Роман 3091
tolstosup 5250111
Update CMakeList.txt
tolstosup a25e470
Update CMakeList.txt
tolstosup 496f8ce
Rename CMakeList.txt to CMakeLists.txt
tolstosup 0283fa5
Update CMakeLists.txt
tolstosup eb3fc8a
Add files via upload
tolstosup b68f54c
Add files via upload
tolstosup ccd9dca
Add files via upload
tolstosup b2ac8dc
Update array.cpp
tolstosup a74eb68
Update array.h
tolstosup 14c22ad
Update lab1.cpp
tolstosup 78c1f63
Update input.txt
tolstosup cfdb35d
Update array.cpp
tolstosup 5edd363
Update array.h
tolstosup 2df5416
Update array.cpp
tolstosup 98cbf4f
Add files via upload
tolstosup 6f15302
Create 1
tolstosup 1e02640
Add files via upload
tolstosup e5d8c45
Add files via upload
tolstosup 70cb6b9
Update CMakeLists.txt
tolstosup 09ed8ef
Create lab2.cpp
tolstosup 0264cad
Delete Lab2CPP/1
tolstosup 6adbe17
Update lab2.cpp
tolstosup b7dac41
Rename CMakeLists .txt to CMakeLists.txt
tolstosup 535e839
Update list.cpp
tolstosup cdab269
Update list.cpp
tolstosup 26b451e
Update list.cpp
tolstosup bd28b9a
Update stack.h
tolstosup d526cb2
Update stack.cpp
tolstosup e346e9c
Update CMakeLists.txt
tolstosup fb6564c
Update list.cpp
tolstosup 27c441c
Update stack.cpp
tolstosup e287c51
Delete Lab1CPP directory
tolstosup 8f7e77a
Update lab2.cpp
tolstosup 0d677f9
Add files via upload
tolstosup 4daacc0
Update stack.cpp
tolstosup 8ad0d1a
Update list.cpp
tolstosup ad26d88
Update stack.cpp
tolstosup 6f287e7
Update list.cpp
tolstosup b39aaa8
Update CMakeLists.txt
tolstosup 058efc7
Update lab2.cpp
tolstosup 6730125
Update stack.cpp
tolstosup 8e4a696
Update lab2.cpp
tolstosup 69bb0b8
Update input.txt
tolstosup 0eb1662
Update list.cpp
tolstosup 0e78e12
Update stack.h
tolstosup 39cead9
Update lab2.cpp
tolstosup 0264738
Update lab2.cpp
tolstosup 969b0b1
Update stack.cpp
tolstosup 0a10a0f
Update list.cpp
tolstosup f52b3cf
Update array.cpp
tolstosup a77c95e
Update list.cpp
tolstosup 5bf832e
Update list.cpp
tolstosup 0f6f2a2
Update lab2.cpp
tolstosup 3acf5d9
Update CMakeLists.txt
tolstosup 8a43462
Delete Lab2CPP/input.txt
tolstosup 2f2fea7
Update lab2.cpp
tolstosup 6dacaf4
Update CMakeLists.txt
tolstosup c18867c
Update CMakeLists.txt
tolstosup dbcf9c8
Update lab2.cpp
tolstosup 2a773a7
Update list.cpp
tolstosup 831dfe2
Update list.cpp
tolstosup 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 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
add_executable(Lab2CPP lab2.cpp) | ||
target_include_directories(Lab2CPP PUBLIC ../LibraryCPP) | ||
target_link_libraries(Lab2CPP LibraryCPP) | ||
|
||
add_test(NAME TestLab2CPP COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) |
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,2 @@ | ||
(1+2)*3 | ||
1+2*3 |
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,125 @@ | ||
#include <iostream> | ||
Check notice on line 1 in Lab2CPP/lab2.cpp GitHub Actions / cpp-linterRun clang-format on Lab2CPP/lab2.cpp
|
||
#include <string> | ||
#include <fstream> | ||
#include "stack.h" | ||
|
||
unsigned int priority(const char symbol) | ||
{ | ||
if (symbol == '*' || symbol == '/') | ||
return 3; | ||
if (symbol == '+' || symbol == '-') | ||
return 2; | ||
if (symbol == '(') | ||
return 1; | ||
|
||
return 0; | ||
} | ||
|
||
std::string RPN(std::string data) | ||
{ | ||
Stack* stack = stack_create(); | ||
std::string res = ""; | ||
for (size_t i = 0; i < data.size(); i++) | ||
{ | ||
if (isdigit(data[i])) | ||
{ | ||
res += data[i]; | ||
} | ||
else | ||
{ | ||
if (data[i] == '(') | ||
{ | ||
stack_push(stack, data[i]); | ||
} | ||
else if (data[i] == ')') | ||
{ | ||
while (!stack_empty(stack) && stack_get(stack) != '(') | ||
{ | ||
res += stack_get(stack); | ||
stack_pop(stack); | ||
} | ||
if (!stack_empty(stack) && stack_get(stack) == '(') | ||
{ | ||
stack_pop(stack); | ||
} | ||
} | ||
else | ||
{ | ||
while (!stack_empty(stack) && priority(stack_get(stack)) >= priority(data[i])) | ||
{ | ||
res += stack_get(stack); | ||
stack_pop(stack); | ||
} | ||
stack_push(stack, data[i]); | ||
} | ||
} | ||
} | ||
|
||
while (!stack_empty(stack)) | ||
{ | ||
res += stack_get(stack); | ||
stack_pop(stack); | ||
} | ||
|
||
stack_delete(stack); | ||
return res; | ||
} | ||
|
||
Data solve(std::string data) | ||
{ | ||
Stack* stack = stack_create(); | ||
for (size_t i = 0; i < data.size(); i++) | ||
{ | ||
if (isdigit(data[i])) | ||
stack_push(stack, data[i] - '0'); | ||
else | ||
{ | ||
Data val1 = stack_get(stack); | ||
stack_pop(stack); | ||
Data val2 = stack_get(stack); | ||
stack_pop(stack); | ||
Data res = 0; | ||
|
||
switch (data[i]) | ||
{ | ||
case '+': res = val1 + val2; | ||
break; | ||
case '-': res = val1 - val2; | ||
break; | ||
case '*': res = val1 * val2; | ||
break; | ||
case '/': res = val1 / val2; | ||
break; | ||
} | ||
|
||
stack_push(stack, res); | ||
} | ||
} | ||
|
||
Data res = stack_get(stack); | ||
stack_delete(stack); | ||
return res; | ||
} | ||
|
||
std::string calculate(std::string expr) | ||
{ | ||
expr = RPN(expr); | ||
expr = std::to_string(solve(expr)); | ||
|
||
return expr; | ||
} | ||
|
||
int main() | ||
{ | ||
std::ifstream in("input.txt"); | ||
|
||
std::string temp = ""; | ||
in >> temp; | ||
std::cout << temp << " = " << calculate(temp); | ||
|
||
in >> temp; | ||
std::cout << temp << " = " << calculate(temp); | ||
|
||
in.close(); | ||
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
add_library(LibraryCPP STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp) | ||
add_library(LibraryCPP STATIC list.cpp stack.cpp) | ||
|
||
add_subdirectory(Tests) |
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,34 +1,73 @@ | ||
#include "array.h" | ||
Check notice on line 1 in LibraryCPP/array.cpp GitHub Actions / cpp-linterRun clang-format on LibraryCPP/array.cpp
|
||
|
||
struct Array | ||
{ | ||
Data* array; | ||
size_t size; | ||
}; | ||
|
||
// create array | ||
Array *array_create(size_t size) | ||
Array* array_create(size_t size) | ||
{ | ||
return new Array; | ||
if (size != 0) | ||
{ | ||
Array* array = new Array; | ||
array->size = size; | ||
array->array = new Data[size]; | ||
return array; | ||
} | ||
else | ||
return nullptr; | ||
} | ||
|
||
// delete array, free memory | ||
void array_delete(Array *arr) | ||
void array_delete(Array* arr) | ||
{ | ||
delete arr; | ||
if (arr->array != nullptr) | ||
{ | ||
delete[] arr->array; | ||
delete arr; | ||
} | ||
} | ||
|
||
// returns specified array element | ||
Data array_get(const Array *arr, size_t index) | ||
Data array_get(const Array* arr, size_t index) | ||
{ | ||
return (Data)0; | ||
if (index < arr->size) | ||
return arr->array[index]; | ||
else | ||
return 0; | ||
} | ||
|
||
// sets the specified array element to the value | ||
void array_set(Array *arr, size_t index, Data value) | ||
void array_set(Array* arr, size_t index, Data value) | ||
{ | ||
if (index < arr->size) | ||
{ | ||
arr->array[index] = value; | ||
} | ||
} | ||
|
||
// returns array size | ||
size_t array_size(const Array *arr) | ||
size_t array_size(const Array* arr) | ||
{ | ||
return 0; | ||
if (arr->array != nullptr) | ||
return arr->size; | ||
return 0; | ||
} | ||
|
||
// fill array from file | ||
Array* fill_array(std::ifstream &in) | ||
{ | ||
size_t size; | ||
Data value; | ||
in >> size; | ||
Array* array = array_create(size); | ||
for (size_t i = 0; i < size; i++) | ||
{ | ||
in >> value; | ||
array_set(array, i, value); | ||
} | ||
|
||
return array; | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Лучше добавить два теста.
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.
Не исправлено.