diff --git a/CMakeLists.txt b/CMakeLists.txt index b40c0dd11c..a0ccc07009 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,9 +10,11 @@ else() add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter) endif() -add_subdirectory(LibraryC) -add_subdirectory(LibraryCPP) +#add_subdirectory(LibraryC) +#add_subdirectory(LibraryCPP) add_subdirectory(LibraryCPPClass) -add_subdirectory(LibraryCPPTemplate) +#add_subdirectory(LibraryCPPTemplate) -add_subdirectory(Lab1C) +#add_subdirectory(Lab1CPP) +#add_subdirectory(Lab2CPP) +add_subdirectory(Lab3CPP) \ No newline at end of file diff --git a/Lab1C/CMakeLists.txt b/Lab1C/CMakeLists.txt deleted file mode 100644 index f4da4d8372..0000000000 --- a/Lab1C/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_executable(Lab1C lab1.c) -target_include_directories(Lab1C PUBLIC ../LibraryC) -target_link_libraries(Lab1C LibraryC) - -add_test(NAME TestLab1C COMMAND Lab1C ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) -set_property(TEST TestLab1C PROPERTY PASS_REGULAR_EXPRESSION "1 2 3 4 5") diff --git a/Lab1C/lab1.c b/Lab1C/lab1.c deleted file mode 100644 index fdf6ed1527..0000000000 --- a/Lab1C/lab1.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include "array.h" - -Array *array_create_and_read(FILE *input) -{ - int n; - fscanf(input, "%d", &n); - /* Create array */ - Array *arr = array_create(n, NULL); - /* Read array data */ - for (int i = 0 ; i < n ; ++i) - { - int x; - fscanf(input, "%d", &x); - array_set(arr, i, x); - } - return arr; -} - -void task1(Array *arr) -{ -} - -void task2(Array *arr) -{ -} - -int main(int argc, char **argv) -{ - Array *arr = NULL; - FILE *input = fopen(argv[1], "r"); - arr = array_create_and_read(input); - task1(arr); - array_delete(arr); - /* Create another array here */ - arr = array_create_and_read(input); - task2(arr); - array_delete(arr); - fclose(input); -} diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt new file mode 100644 index 0000000000..79801a4290 --- /dev/null +++ b/Lab1CPP/CMakeLists.txt @@ -0,0 +1,12 @@ +add_executable(Lab1CPP lab1.cpp) +target_include_directories(Lab1CPP PUBLIC ../LibraryCPP) +target_link_libraries(Lab1CPP LibraryCPP) + +add_test(NAME TestLab1CPP COMMAND Lab1CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +set_property(TEST TestLab1CPP PROPERTY PASS_REGULAR_EXPRESSION "change of sign: 0 unique values: 1 2 3 4 ") + +add_test(NAME TestLab1CPP2 COMMAND Lab1CPP ${CMAKE_CURRENT_SOURCE_DIR}/test2.txt) +set_property(TEST TestLab1CPP2 PROPERTY PASS_REGULAR_EXPRESSION "change of sign: 1 unique values: -1 2 3 4 6 0 ") + +add_test(NAME TestLab1CPP3 COMMAND Lab1CPP ${CMAKE_CURRENT_SOURCE_DIR}/test3.txt) +set_property(TEST TestLab1CPP3 PROPERTY PASS_REGULAR_EXPRESSION "change of sign: 2 unique values: -1 2 3 4 6 0 -9 ") \ No newline at end of file diff --git a/Lab1C/input.txt b/Lab1CPP/input.txt similarity index 100% rename from Lab1C/input.txt rename to Lab1CPP/input.txt diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp new file mode 100644 index 0000000000..0cb6cc57bf --- /dev/null +++ b/Lab1CPP/lab1.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include "array.h" + +using namespace std; + + +Array *array_create_and_read(istream &input) +{ + int n; + input >> n; + /* Create array */ + Array *arr = array_create(n); + /* Read array data */ + for (int i = 0 ; i < n ; ++i) + { + int x; + input >> x; + array_set(arr, i, x); + } + return arr; +} + +void task1(Array *arr) { + if (arr == nullptr || array_size(arr) < 2) return; + + int signChanges = 0; + int prevSign = (array_get(arr, 0) >= 0) ? 1 : -1; + + for (size_t i = 1; i < array_size(arr); i++) { + int currentSign = (array_get(arr, i) >= 0) ? 1 : -1; + + if (currentSign != prevSign) { + signChanges++; + prevSign = currentSign; + } + } + + cout << "change of sign: " << signChanges; +} + +void task2(Array *arr) +{ + cout << " unique values: "; + if (arr == nullptr || array_size(arr) == 0) return; + + for (size_t i = 0; i < array_size(arr); i++){ + int count = 0; + for (size_t j = 0; j < array_size(arr); j++){ + if(array_get(arr, i) == array_get(arr, j)){ + count++; + } + if(count == 2){ + break; + } + } + if(count == 1){ + cout << array_get(arr, i) << " "; + } + + } + +} + + +int main(int argc, char **argv){ + if (argc < 2) { + cerr << "Usage: " << argv[0] << " " << endl; + return 1; + } + ifstream FILE(argv[1]); + if (!FILE.is_open()){ + return -1; + } + Array *arr = array_create_and_read(FILE); + task1(arr); + array_delete(arr); + /* Create another array here */ + arr = array_create_and_read(FILE); + task2(arr); + array_delete(arr); + + + FILE.close(); + cout << "\n"; +} \ No newline at end of file diff --git a/Lab1CPP/test2.txt b/Lab1CPP/test2.txt new file mode 100644 index 0000000000..5e5209a201 --- /dev/null +++ b/Lab1CPP/test2.txt @@ -0,0 +1,4 @@ +6 +-1 -1 0 0 1 1 +8 +-1 2 3 4 5 5 6 0 diff --git a/Lab1CPP/test3.txt b/Lab1CPP/test3.txt new file mode 100644 index 0000000000..fe074a9d86 --- /dev/null +++ b/Lab1CPP/test3.txt @@ -0,0 +1,4 @@ +9 +0 0 -1 -1 0 0 1 1 0 +9 +-1 2 3 4 5 5 6 0 -9 \ No newline at end of file diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt new file mode 100644 index 0000000000..5314a0c949 --- /dev/null +++ b/Lab2CPP/CMakeLists.txt @@ -0,0 +1,36 @@ +add_executable(Lab2CPP lab2.cpp) +target_include_directories(Lab2CPP PUBLIC ../LibraryCPP) +target_link_libraries(Lab2CPP LibraryCPP) + +add_test(NAME your_first_test COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text1.txt) +set_property(TEST your_first_test PROPERTY PASS_REGULAR_EXPRESSION "1.*3.*2") + +add_test(NAME your_second_test COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text2.txt) +set_property(TEST your_second_test PROPERTY PASS_REGULAR_EXPRESSION "1.*2.*3.*6") + +add_test(NAME test_summ COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text3.txt) +set_property(TEST test_summ PROPERTY PASS_REGULAR_EXPRESSION "3.*18") + +add_test(NAME test_division COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text4.txt) +set_property(TEST test_division PROPERTY PASS_REGULAR_EXPRESSION "1.*1.*1.*2.*2") + +add_test(NAME test_division_by_zero COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text4-1.txt) +set_property(TEST test_division_by_zero PROPERTY PASS_REGULAR_EXPRESSION "division by zero error.*stack empty") + +add_test(NAME test_remainder COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text5.txt) +set_property(TEST test_remainder PROPERTY PASS_REGULAR_EXPRESSION "1.*0") + +add_test(NAME test_dup COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text6.txt) +set_property(TEST test_dup PROPERTY PASS_REGULAR_EXPRESSION "3.*3.*2") + +add_test(NAME test_swap COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text7.txt) +set_property(TEST test_swap PROPERTY PASS_REGULAR_EXPRESSION "1.*2.*0.*1") + +add_test(NAME test_drop COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text8.txt) +set_property(TEST test_drop PROPERTY PASS_REGULAR_EXPRESSION "1") + +add_test(NAME test_over COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text9.txt) +set_property(TEST test_over PROPERTY PASS_REGULAR_EXPRESSION "9.*5.*5") + +add_test(NAME test_rot COMMAND Lab2CPP ${CMAKE_CURRENT_SOURCE_DIR}/TestTXT/text10.txt) +set_property(TEST test_rot PROPERTY PASS_REGULAR_EXPRESSION "8.*0.*9.*7.*6.*5.*4.*3.*2.*1") \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text1.txt b/Lab2CPP/TestTXT/text1.txt new file mode 100644 index 0000000000..a7b3ff24b5 --- /dev/null +++ b/Lab2CPP/TestTXT/text1.txt @@ -0,0 +1 @@ +1 2 3 rot . . . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text10.txt b/Lab2CPP/TestTXT/text10.txt new file mode 100644 index 0000000000..b4d0cf7e82 --- /dev/null +++ b/Lab2CPP/TestTXT/text10.txt @@ -0,0 +1 @@ +1 2 3 4 5 6 7 8 9 0 rot . . . . . . . . . . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text2.txt b/Lab2CPP/TestTXT/text2.txt new file mode 100644 index 0000000000..0e80c227ed --- /dev/null +++ b/Lab2CPP/TestTXT/text2.txt @@ -0,0 +1 @@ +1 dup . 2 dup . 3 dup . + + . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text3.txt b/Lab2CPP/TestTXT/text3.txt new file mode 100644 index 0000000000..11133f6405 --- /dev/null +++ b/Lab2CPP/TestTXT/text3.txt @@ -0,0 +1 @@ +1 2 + . 9 9 + . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text4-1.txt b/Lab2CPP/TestTXT/text4-1.txt new file mode 100644 index 0000000000..dc5e5bb768 --- /dev/null +++ b/Lab2CPP/TestTXT/text4-1.txt @@ -0,0 +1 @@ +5 0 / . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text4.txt b/Lab2CPP/TestTXT/text4.txt new file mode 100644 index 0000000000..1ee7766557 --- /dev/null +++ b/Lab2CPP/TestTXT/text4.txt @@ -0,0 +1 @@ +1 1 / . 2 2 / . 3 3 / . 4 2 / . 5 2 / . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text5.txt b/Lab2CPP/TestTXT/text5.txt new file mode 100644 index 0000000000..0a202f163a --- /dev/null +++ b/Lab2CPP/TestTXT/text5.txt @@ -0,0 +1 @@ +3 2 % . 9 9 * 9 + 9 + 1 + 5 5 + % . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text6.txt b/Lab2CPP/TestTXT/text6.txt new file mode 100644 index 0000000000..d421f3d7f8 --- /dev/null +++ b/Lab2CPP/TestTXT/text6.txt @@ -0,0 +1 @@ +2 3 dup . . . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text7.txt b/Lab2CPP/TestTXT/text7.txt new file mode 100644 index 0000000000..fd65ee9cbe --- /dev/null +++ b/Lab2CPP/TestTXT/text7.txt @@ -0,0 +1 @@ +1 2 swap . . 0 1 swap . . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text8.txt b/Lab2CPP/TestTXT/text8.txt new file mode 100644 index 0000000000..1466a6b04c --- /dev/null +++ b/Lab2CPP/TestTXT/text8.txt @@ -0,0 +1 @@ +1 2 drop . \ No newline at end of file diff --git a/Lab2CPP/TestTXT/text9.txt b/Lab2CPP/TestTXT/text9.txt new file mode 100644 index 0000000000..576813afe4 --- /dev/null +++ b/Lab2CPP/TestTXT/text9.txt @@ -0,0 +1 @@ +5 9 over . . . \ No newline at end of file diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp new file mode 100644 index 0000000000..7f5f0a5004 --- /dev/null +++ b/Lab2CPP/lab2.cpp @@ -0,0 +1,196 @@ +#include +#include +#include +#include +#include "stack.h" + +using namespace std; + +void summa(Stack* stack){ + if((stack_size(stack) < 2)){ + cout << "error sum\n" << endl; + return; + } + int one, two, result; + two = stack_take(stack); + one = stack_take(stack); + result = one + two; + stack_push(stack, result); +} + +void minuss(Stack* stack){ + if((stack_size(stack) < 2)){ + cout << "error minus\n" << endl; + return; + } + int one, two, result; + two = stack_take(stack); + one = stack_take(stack); + result = one - two; + stack_push(stack, result); +} +// * +void increase(Stack* stack){ + if((stack_size(stack) < 2)){ + cout << "error increase\n" << endl; + return; + } + int one, two, result; + two = stack_take(stack); + one = stack_take(stack); + result = one * two; + stack_push(stack, result); +} +// / +void division(Stack* stack){ + if((stack_size(stack) < 2)){ + cout << "error division\n" << endl; + return; + } + int one, two, result; + two = stack_take(stack); + one = stack_take(stack); + if(two == 0){ + cout << "division by zero error\n"; + return; + } + else { + result = one / two; + stack_push(stack, result); + } + +} +// % +void remainder(Stack* stack){ + if((stack_size(stack) < 2)){ + cout << "error remainder\n" << endl; + return; + } + int one, two, result; + two = stack_take(stack); + one = stack_take(stack); + result = one % two; + stack_push(stack, result); +} + +void dup(Stack* stack){ + if(stack_empty(stack)){ + cout << "stack empty\n" << endl; + return; + } + int one; + one = stack_get(stack); + stack_push(stack, one); +} + +void drop(Stack* stack){ + if(stack_empty(stack)){ + cout << "stack empty\n" << endl; + return; + } + stack_pop(stack); +} +// поменять местами +void swap_stack(Stack* stack){ + if((stack_size(stack) < 2)){ + cout << "error swap\n" << endl; + return; + } + int one, two; + two = stack_take(stack); + one = stack_take(stack); + stack_push(stack, two); + stack_push(stack, one); +} +// копирование второго +void over(Stack* stack){ + if((stack_size(stack) < 2)){ + cout << "error over\n" << endl; + return; + } + int one, two; + two = stack_take(stack); + one = stack_get(stack); + stack_push(stack, one); + stack_push(stack, two); +} +// сдвиг +void rot(Stack* stack) { + if (stack_size(stack) < 3) { + cout << "error rot\n"; + return; + } + + int a3 = stack_take(stack); + int a2 = stack_take(stack); + int a1 = stack_take(stack); + + stack_push(stack, a2); + stack_push(stack, a3); + stack_push(stack, a1); +} + +void write(Stack* stack){ + if(stack_empty(stack)){ + cout << "stack empty\n"; + return; + } + cout << stack_take(stack) << '\n'; +} + +void determinant_operator(Stack *stack, string command){ + if(command == "+") summa(stack); + else if(command == "-") minuss(stack); + else if(command == "*") increase(stack); + else if(command == "/") division(stack); + else if(command == "%") remainder(stack); + + else if(command[0] >= '0' && command[0] <= '9' && command.size() == 1){ + stack_push(stack, stoi(command)); + } + else if(command == "dup") dup(stack); + else if(command == "drop") drop(stack); + else if(command == "swap") swap_stack(stack); + else if(command == "over") over(stack); + else if(command == "rot") rot(stack); + else if(command == ".") write(stack); + else cout << "ignore bad massage\n"; +} + +string read_file(istream &input){ + string a; + getline(input, a); + return a; +} + +int main(int argc, char **argv){ + string stroka, command; + Stack* stack = stack_create(); + stringstream ss; + if (argc > 1){ + + ifstream FILE(argv[1]); + if (!FILE.is_open()){ + return -1; + } + + stroka = read_file(FILE); + FILE.close(); + ss << stroka; + while(ss >> command){ + determinant_operator(stack, command); + } + + } + + else{ + getline(cin, stroka); + ss << stroka; + while(ss >> command){ + determinant_operator(stack, command); + } + } + stack_delete(stack); + return 0; +} + diff --git a/Lab3CPP/CMakeLists.txt b/Lab3CPP/CMakeLists.txt new file mode 100644 index 0000000000..c7da3575e2 --- /dev/null +++ b/Lab3CPP/CMakeLists.txt @@ -0,0 +1,24 @@ +add_executable(Lab3CPP lab3.cpp) +target_include_directories(Lab3CPP PUBLIC ../LibraryCPPClass) +target_link_libraries(Lab3CPP LibraryCPPClass) + +add_test(NAME your_first_test COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/test/test1.txt) +set_property(TEST your_first_test PROPERTY PASS_REGULAR_EXPRESSION "3") + +add_test(NAME simple_work_program COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/test/test2.txt) +set_property(TEST simple_work_program PROPERTY PASS_REGULAR_EXPRESSION "1") + +add_test(NAME checking_walls_and_fields COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/test/test3.txt) +set_property(TEST checking_walls_and_fields PROPERTY PASS_REGULAR_EXPRESSION "0") + +add_test(NAME аlmost_free_field COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/test/test4.txt) +set_property(TEST аlmost_free_field PROPERTY PASS_REGULAR_EXPRESSION "4") + +add_test(NAME diagonal_movement COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/test/test5.txt) +set_property(TEST diagonal_movement PROPERTY PASS_REGULAR_EXPRESSION "1") + +add_test(NAME hard_movment_test COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/test/test6.txt) +set_property(TEST hard_movment_test PROPERTY PASS_REGULAR_EXPRESSION "2") + +add_test(NAME test_unnecessary_function COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/test/test7.txt) +set_property(TEST hard_movment_test PROPERTY PASS_REGULAR_EXPRESSION "2") \ No newline at end of file diff --git a/Lab3CPP/lab3.cpp b/Lab3CPP/lab3.cpp new file mode 100644 index 0000000000..942e77af28 --- /dev/null +++ b/Lab3CPP/lab3.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include "queue.h" + +using namespace std; + +typedef vector Maze; + +struct Point +{ + int x, y; + Point(): x(0), y(0){} + Point(int x_, int y_): x(x_), y(y_){} + bool operator==(const Point &other) const{ + return this->x == other.x && this->y == other.y; + } +}; + +// Одна функция для нахождения начала и конца, для улучшния читабельности кода сделал их +const char START = 'Q'; +const char END = 'E'; + +Point find_point(const Maze& maze, char point) { + for (size_t i = 0; i < maze.size(); i++) { + for (size_t j = 0; j < maze[i].size(); j++) { + if (maze[i][j] == point) { + return {static_cast(i), static_cast(j)}; + } + } + } + return {-1, -1}; +} +// Это перевод с Point в int, но используется только в очереди, так что будет Point to queue +int ptoq(Point pos, int cols) { + return pos.x * cols + pos.y; +} + +int bfs(const Maze& maze, Point start, Point end) { + if (start.x == -1 || end.x == -1) { + return 0; + } + // сделал для теста, чтобы показать, что выполняется условие на которое вы показали. Для удобства взял string + + int rows = maze.size(); + int cols = maze[0].size(); + + vector> dist(rows, vector(cols, -1)); + vector> count(rows, vector(cols, 0)); + vector> inQueue(rows, vector(cols, false)); + + // 8 направлений движения ферзя (x,y) + const Point moveQueen[8] = { + {-1,-1}, {-1,0}, {-1,1}, + {0,-1}, /* Q */ {0,1}, + {1,-1}, {1,0}, {1,1} + }; + + Queue q; + dist[start.x][start.y] = 0; + count[start.x][start.y] = 1; + + q.insert(ptoq(start, cols)); + inQueue[start.x][start.y] = true; + + while (!q.empty()) { + int currentIndex = q.take(); + + int y = currentIndex / cols; + int x = currentIndex % cols; + inQueue[y][x] = false; + + int d = dist[y][x]; + int c = count[y][x]; + + for (const auto& dir : moveQueen) { + int dy = dir.x; + int dx = dir.y; + + + for (int step = 1; ; step++) { + int ny = y + dy * step; + int nx = x + dx * step; + + if (ny < 0 || ny >= rows || nx < 0 || nx >= cols) break; + if (maze[ny][nx] == '#') break; + + int newDistant = d + 1; + + if (dist[ny][nx] == -1) { + dist[ny][nx] = newDistant; + count[ny][nx] = c; + + if (!(ny == end.x && nx == end.y)) { + q.insert(ptoq({ny, nx}, cols)); + inQueue[ny][nx] = true; + } + } + else if (newDistant == dist[ny][nx]) { + count[ny][nx] += c; + } + + } + } + } + return count[end.x][end.y]; +} + +int main(int argc, char **argv) { + if (argc < 2) { + cerr << "Usage: " << argv[0] << " " << endl; + return 1; + } + + ifstream file(argv[1]); + if (!file.is_open()) { + cerr << "Cannot open file: " << argv[1] << endl; + return 1; + } + + Maze maze; + string line; + while (getline(file, line)) { + maze.push_back(line); + } + file.close(); + + auto start = find_point(maze, START); + auto end = find_point(maze, END); + + cout << bfs(maze, start, end) << endl; + + return 0; +} \ No newline at end of file diff --git a/Lab3CPP/test/test1.txt b/Lab3CPP/test/test1.txt new file mode 100644 index 0000000000..925a6eeeb1 --- /dev/null +++ b/Lab3CPP/test/test1.txt @@ -0,0 +1,6 @@ +######### +#..#...Q# +#.......# +#...#.### +#...#..E# +######### \ No newline at end of file diff --git a/Lab3CPP/test/test2.txt b/Lab3CPP/test/test2.txt new file mode 100644 index 0000000000..9e09775fc8 --- /dev/null +++ b/Lab3CPP/test/test2.txt @@ -0,0 +1 @@ +Q.E diff --git a/Lab3CPP/test/test3.txt b/Lab3CPP/test/test3.txt new file mode 100644 index 0000000000..6c53153a52 --- /dev/null +++ b/Lab3CPP/test/test3.txt @@ -0,0 +1 @@ +Q#E diff --git a/Lab3CPP/test/test4.txt b/Lab3CPP/test/test4.txt new file mode 100644 index 0000000000..f2e517548c --- /dev/null +++ b/Lab3CPP/test/test4.txt @@ -0,0 +1,5 @@ +..#...... +.Q....... +......... +.......#. +......E.. \ No newline at end of file diff --git a/Lab3CPP/test/test5.txt b/Lab3CPP/test/test5.txt new file mode 100644 index 0000000000..abac0cdbba --- /dev/null +++ b/Lab3CPP/test/test5.txt @@ -0,0 +1,4 @@ +#### +#Q## +##E# +#### \ No newline at end of file diff --git a/Lab3CPP/test/test6.txt b/Lab3CPP/test/test6.txt new file mode 100644 index 0000000000..6dfdd85c59 --- /dev/null +++ b/Lab3CPP/test/test6.txt @@ -0,0 +1,6 @@ +##.######## +#Q#.####### +####.###### +###.##...## +##.#..##E## +###.####### \ No newline at end of file diff --git a/Lab3CPP/test/test7.txt b/Lab3CPP/test/test7.txt new file mode 100644 index 0000000000..b6e0d5279f --- /dev/null +++ b/Lab3CPP/test/test7.txt @@ -0,0 +1,3 @@ +Q.# +.#. +#.E \ No newline at end of file diff --git a/LibraryCPP/CMakeLists.txt b/LibraryCPP/CMakeLists.txt index dbd7769feb..1ab778dc66 100644 --- a/LibraryCPP/CMakeLists.txt +++ b/LibraryCPP/CMakeLists.txt @@ -1,3 +1,4 @@ -add_library(LibraryCPP STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp) +#add_library(LibraryCPP STATIC array.cpp) list.cpp queue.cpp stack.cpp vector.cpp) +add_library(LibraryCPP STATIC stack.cpp vector.cpp) add_subdirectory(Tests) diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index c903f9d7f3..5df5059855 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -14,13 +14,13 @@ # add_test(TestQueueCPP TestQueueCPP) # set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10) -# add_executable(TestStackCPP stack.cpp) -# target_include_directories(TestStackCPP PUBLIC ..) -# target_link_libraries(TestStackCPP LibraryCPP) -# add_test(TestStackCPP TestStackCPP) +# add_executable(TestStackCPP stack.cpp) +# target_include_directories(TestStackCPP PUBLIC ..) +# target_link_libraries(TestStackCPP LibraryCPP) +# add_test(TestStackCPP TestStackCPP) -# add_executable(TestVectorCPP vector.cpp) -# target_include_directories(TestVectorCPP PUBLIC ..) -# target_link_libraries(TestVectorCPP LibraryCPP) -# add_test(TestVectorCPP TestVectorCPP) -# set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10) +# add_executable(TestVectorCPP vector.cpp) +# target_include_directories(TestVectorCPP PUBLIC ..) +# target_link_libraries(TestVectorCPP LibraryCPP) +# add_test(TestVectorCPP TestVectorCPP) +# set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10) diff --git a/LibraryCPP/array.cpp b/LibraryCPP/array.cpp index c773e5f167..ef6f110120 100644 --- a/LibraryCPP/array.cpp +++ b/LibraryCPP/array.cpp @@ -2,33 +2,42 @@ struct Array { + size_t size; + int *data; }; // create array Array *array_create(size_t size) { - return new Array; + Array* newArr = new Array; + newArr->data = new int[size]; + newArr->size = size; + return newArr; } // delete array, free memory void array_delete(Array *arr) { + delete[] arr->data; delete arr; } // returns specified array element Data array_get(const Array *arr, size_t index) { - return (Data)0; + return arr->data[index]; } // sets the specified array element to the value void array_set(Array *arr, size_t index, Data value) { + if(index < arr->size){ + arr->data[index] = value; + } } // returns array size size_t array_size(const Array *arr) { - return 0; + return arr->size; } diff --git a/LibraryCPP/stack.cpp b/LibraryCPP/stack.cpp index c090abba1c..f01f7c8e05 100644 --- a/LibraryCPP/stack.cpp +++ b/LibraryCPP/stack.cpp @@ -1,34 +1,58 @@ #include "stack.h" +#include "vector.h" struct Stack { + Vector* vector; }; Stack *stack_create() { - return new Stack; + Stack* stack = new Stack; + stack->vector = vector_create(); + return stack; } void stack_delete(Stack *stack) { - // TODO: free stack elements + vector_delete(stack->vector); delete stack; } void stack_push(Stack *stack, Data data) { + vector_set(stack->vector, vector_size(stack->vector), data); } Data stack_get(const Stack *stack) { - return (Data)0; + Data ret = vector_get(stack->vector, vector_size(stack->vector) - 1); + return ret; +} + +/* +stack_take создан, чтобы забирать и удалять одновременно из стека число +*/ +Data stack_take(Stack *stack) +{ + Data ret = vector_get(stack->vector, vector_size(stack->vector) - 1); + stack_pop(stack); + return ret; } void stack_pop(Stack *stack) { + vector_resize(stack->vector, vector_size(stack->vector) - 1); } bool stack_empty(const Stack *stack) { - return true; + return vector_size(stack->vector) == 0; +} + +/* +stack_size создан для проверки колличества элементов, так как stack_empty может быть не удобным +*/ +int stack_size(const Stack *stack){ + return vector_size(stack->vector); } diff --git a/LibraryCPP/stack.h b/LibraryCPP/stack.h index a03c798df7..cadd365e17 100644 --- a/LibraryCPP/stack.h +++ b/LibraryCPP/stack.h @@ -1,12 +1,15 @@ #ifndef STACK_H #define STACK_H + // Stack // Stores integer values inside // Change it to desired type typedef int Data; +struct Vector; + struct Stack; // Creates empty stack @@ -22,6 +25,9 @@ void stack_push(Stack *stack, Data data); // Retrieves the last element from the stack Data stack_get(const Stack *stack); +// Retrieves the last element from the stack and delete her +Data stack_take(Stack *stack); + // Removes the last element from the stack // Should be O(1) void stack_pop(Stack *stack); @@ -29,4 +35,7 @@ void stack_pop(Stack *stack); // Returns true if the stack is empty bool stack_empty(const Stack *stack); +//Returns size stack +int stack_size(const Stack *stack); + #endif diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index aee7157b54..ec737ed6ed 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -2,33 +2,81 @@ struct Vector { + int* arr; + size_t size; + size_t capacity; }; + Vector *vector_create() { - return new Vector; + Vector *vector = new Vector; + vector->arr = NULL; + vector->size = 0; + vector->capacity = 0; + return vector; } void vector_delete(Vector *vector) { - // TODO: free vector internals - delete vector; + delete[] vector->arr; + delete vector; } Data vector_get(const Vector *vector, size_t index) { - return (Data)0; + if (index >= vector->size) { + throw std::out_of_range("Index out of range"); + } + return vector->arr[index]; } void vector_set(Vector *vector, size_t index, Data value) { + if (index >= vector->size) { + vector_resize(vector, index + 1); + } + vector->arr[index] = value; } size_t vector_size(const Vector *vector) { - return 0; + return vector->size; } void vector_resize(Vector *vector, size_t size) { -} + if (size <= vector->capacity) { + if (size > vector->size) { + for (size_t i = vector->size; i < size; i++) { + vector->arr[i] = 0; + } + } + vector->size = size; + return; + } + + size_t new_capacity = vector->capacity * 2; + if (new_capacity < size) { + new_capacity = size; + } + + if (new_capacity == 0) { + new_capacity = 1; + } + + Data* new_arr = new Data[new_capacity]; + + for (size_t i = 0; i < vector->size; i++) { + new_arr[i] = vector->arr[i]; + } + + for (size_t i = vector->size; i < size; i++) { + new_arr[i] = 0; + } + + delete[] vector->arr; + vector->arr = new_arr; + vector->size = size; + vector->capacity = new_capacity; +} \ No newline at end of file diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index f191e7de03..0a9a34c4e0 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -2,6 +2,8 @@ #define VECTOR_H #include +#include + // Vector (dynamic array) diff --git a/LibraryCPPClass/CMakeLists.txt b/LibraryCPPClass/CMakeLists.txt index 0ddd51cba2..8b3d70e7d3 100644 --- a/LibraryCPPClass/CMakeLists.txt +++ b/LibraryCPPClass/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(LibraryCPPClass STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp) +add_library(LibraryCPPClass STATIC list.cpp queue.cpp)#array.cpp list.cpp queue.cpp stack.cpp vector.cpp) add_subdirectory(Tests) diff --git a/LibraryCPPClass/Tests/CMakeLists.txt b/LibraryCPPClass/Tests/CMakeLists.txt index 748ae652cc..6b0921ec98 100644 --- a/LibraryCPPClass/Tests/CMakeLists.txt +++ b/LibraryCPPClass/Tests/CMakeLists.txt @@ -3,16 +3,16 @@ # target_link_libraries(TestArrayCPPClass LibraryCPPClass) # add_test(TestArrayCPPClass TestArrayCPPClass) -# add_executable(TestListCPPClass list.cpp) -# target_include_directories(TestListCPPClass PUBLIC ..) -# target_link_libraries(TestListCPPClass LibraryCPPClass) -# add_test(TestListCPPClass TestListCPPClass) + add_executable(TestListCPPClass list.cpp) + target_include_directories(TestListCPPClass PUBLIC ..) + target_link_libraries(TestListCPPClass LibraryCPPClass) + add_test(TestListCPPClass TestListCPPClass) -# add_executable(TestQueueCPPClass queue.cpp) -# target_include_directories(TestQueueCPPClass PUBLIC ..) -# target_link_libraries(TestQueueCPPClass LibraryCPPClass) -# add_test(TestQueueCPPClass TestQueueCPPClass) -# set_tests_properties(TestQueueCPPClass PROPERTIES TIMEOUT 10) + add_executable(TestQueueCPPClass queue.cpp) + target_include_directories(TestQueueCPPClass PUBLIC ..) + target_link_libraries(TestQueueCPPClass LibraryCPPClass) + add_test(TestQueueCPPClass TestQueueCPPClass) + set_tests_properties(TestQueueCPPClass PROPERTIES TIMEOUT 10) # add_executable(TestStackCPPClass stack.cpp) # target_include_directories(TestStackCPPClass PUBLIC ..) diff --git a/LibraryCPPClass/list.cpp b/LibraryCPPClass/list.cpp index a08e44fad8..119ffd0ecf 100644 --- a/LibraryCPPClass/list.cpp +++ b/LibraryCPPClass/list.cpp @@ -1,44 +1,163 @@ #include #include "list.h" -List::List() +void List::Item::setNextPtr(Item* next){ + this->next_ = next; +} + +void List::Item::setPrevPtr(Item* prev){ + this->prev_ = prev; +} + +List::List() : head_(NULL), tail_(NULL) { } -List::List(const List &a) +// Добавлена функция копирования +void List::copy(const List& a){ + + Item* lastCopy = this->insert(a.head_->data()); + + Item* current = a.head_->next(); + + while(current != NULL){ + lastCopy = this->insert_after(lastCopy, current->data()); + current = current->next(); + } +} + +// Добавил еще одну функцию, так как повторялась удаление листа +void List::delete_list() { + while(this->erase_first() != NULL); +} + +List::List(const List &a) : head_(NULL), tail_(NULL) { + if(a.head_ == NULL){ + return; + } + copy(a); } List &List::operator=(const List &a) { + if(this != &a){ + this->delete_list(); + + if(a.head_ != NULL){ + copy(a); + } + } return *this; } List::~List() { + this->delete_list(); } List::Item *List::first() { - return nullptr; + return this->head_; +} + +bool List::list_empty() const +{ + return this->head_ == NULL; } List::Item *List::insert(Data data) { - return nullptr; + Item* newItem = new Item(data, this->head_, NULL); + if(this->head_ != NULL){ + this->head_->setPrevPtr(newItem); + } + else { + this->tail_ = newItem; + } + + this->head_ = newItem; + + return newItem; } List::Item *List::insert_after(Item *item, Data data) { - return nullptr; + if(item == NULL){ + return insert(data); + } + + Item *newItem = new Item(data); + + newItem->setNextPtr(item->next()); + newItem->setPrevPtr(item); + + if(item->next() != NULL){ + item->next()->setPrevPtr(newItem); + } + item->setNextPtr(newItem); + + if(this->tail_ == item){ + this->tail_ = newItem; + } + + return newItem; } List::Item *List::erase_first() { - return nullptr; + if(this->head_ == NULL){ + return NULL; + } + + Item *toDelete = this->head_; + this->head_ = this->head_->next(); + + if(this->head_ != NULL){ + this->head_->setPrevPtr(NULL); + } + else { + this->tail_ = NULL; + } + + delete toDelete; + + return this->head_; } List::Item *List::erase_next(Item *item) { - return nullptr; + if(item == NULL){ + return erase_first(); + } + + if(item->next() == NULL){ + return NULL; + } + + Item *toDelete = item->next(); + Item *nextAfterDelete = toDelete->next(); + item->setNextPtr(nextAfterDelete); + + if(nextAfterDelete != NULL){ + nextAfterDelete->setPrevPtr(item); + } + else { + this->tail_ = item; + } + + delete toDelete; + + return nextAfterDelete; } +// обеспечивает добавление одного элемента O(1) так как он добавляет его в конец (для очереди) +List::Item *List::get_end_item() const { + return this->tail_; +} + +Data List::data_head() const { + if(this->head_ != NULL){ + return head_->data(); + } + return Data(); +} \ No newline at end of file diff --git a/LibraryCPPClass/list.h b/LibraryCPPClass/list.h index 67da6907f7..1f777ce1a4 100644 --- a/LibraryCPPClass/list.h +++ b/LibraryCPPClass/list.h @@ -12,12 +12,21 @@ class List class Item { public: - Item *next() { return nullptr; } - Item *prev() { return nullptr; } - Data data() const { return Data(); } + Item(Data data, Item* next = nullptr, Item* prev = nullptr): data_(data), prev_(prev), next_(next) {} + + Item *next() { return next_; } + Item *prev() { return prev_; } + Data data() const { return data_; } + + void setNextPtr(Item *next); + void setPrevPtr(Item *prev); + private: // internal data here - }; + Data data_; + Item *prev_; + Item *next_; + }; // Creates new list List(); @@ -45,13 +54,30 @@ class List // Returns pointer to the item next to the deleted one. Item *erase_first(); + // Returns true if the list is empty + bool list_empty() const; + // Deletes the list item following the specified one. // Deletes the first element when item is null. // Returns pointer to the item next to the deleted one. // Should be O(1) Item *erase_next(Item *item); + + // Returns the element from the end + Item *get_end_item() const; + + Data data_head() const; + + // Deletes all item from list + void delete_list(); + private: // private data should be here + Item* head_; + Item* tail_; + // for only list + void copy(const List& a); + }; #endif diff --git a/LibraryCPPClass/queue.cpp b/LibraryCPPClass/queue.cpp index 395c05b7f2..d3b80883b8 100644 --- a/LibraryCPPClass/queue.cpp +++ b/LibraryCPPClass/queue.cpp @@ -2,37 +2,54 @@ Queue::Queue() { + } Queue::Queue(const Queue &a) { - // implement or disable this function + this->list = a.list; } Queue &Queue::operator=(const Queue &a) { - // implement or disable this function + this->list = a.list; return *this; } Queue::~Queue() { + } void Queue::insert(Data data) { + if(this->empty()){ + this->list.insert(data); + } + else{ + //обеспечивает добавление одного элемента O(1) так как он добавляет его в конец + this->list.insert_after(this->list.get_end_item() ,data); + } + } Data Queue::get() const { - return Data(); + return this->list.data_head(); } void Queue::remove() { + this->list.erase_first(); +} + +Data Queue::take(){ + Data data = this->get(); + this->remove(); + return data; } bool Queue::empty() const { - return true; + return this->list.list_empty(); } diff --git a/LibraryCPPClass/queue.h b/LibraryCPPClass/queue.h index b221979aae..aaec8d4325 100644 --- a/LibraryCPPClass/queue.h +++ b/LibraryCPPClass/queue.h @@ -2,6 +2,7 @@ #define QUEUE_H #include +#include "list.h" // Change it to desired type typedef int Data; @@ -32,11 +33,16 @@ class Queue // Should be O(1) on average void remove(); + // Retrieves first element from the queue and removes her + // Also O(1) on average + Data take(); + // Returns true if the queue is empty bool empty() const; private: // private data should be here + List list; }; #endif diff --git "a/\320\222\320\276\320\277\321\200\320\276\321\201\321\213 \320\277\320\276 \320\277\320\276\320\262\320\276\320\264\321\203 \320\267\320\260\320\275\321\217\321\202\320\270\320\271 03.11.2025" "b/\320\222\320\276\320\277\321\200\320\276\321\201\321\213 \320\277\320\276 \320\277\320\276\320\262\320\276\320\264\321\203 \320\267\320\260\320\275\321\217\321\202\320\270\320\271 03.11.2025" new file mode 100644 index 0000000000..a6d8abe8bc --- /dev/null +++ "b/\320\222\320\276\320\277\321\200\320\276\321\201\321\213 \320\277\320\276 \320\277\320\276\320\262\320\276\320\264\321\203 \320\267\320\260\320\275\321\217\321\202\320\270\320\271 03.11.2025" @@ -0,0 +1 @@ +защита на следующей неделе в понедельник будет? diff --git "a/\320\234\320\260\320\272\321\201\320\270\320\274_\320\241\320\260\320\273\321\214\320\275\320\270\320\272\320\276\320\262_\320\222\320\273\320\260\320\264\320\270\320\274\320\270\321\200\320\276\320\262\320\270\321\207.txt" "b/\320\234\320\260\320\272\321\201\320\270\320\274_\320\241\320\260\320\273\321\214\320\275\320\270\320\272\320\276\320\262_\320\222\320\273\320\260\320\264\320\270\320\274\320\270\321\200\320\276\320\262\320\270\321\207.txt" new file mode 100644 index 0000000000..e69de29bb2