From 34b35c7c95cbfdb12a0324268db96174ce34c792 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Thu, 23 Oct 2025 01:45:50 +0300 Subject: [PATCH 01/22] one laba --- CMakeLists.txt | 8 ++-- Lab1CPP/.vscode/tasks.json | 28 ++++++++++++ Lab1CPP/CMakeLists.txt | 9 ++++ Lab1CPP/input.txt | 4 ++ Lab1CPP/lab1.cpp | 77 +++++++++++++++++++++++++++++++++ Lab1CPP/test2.txt | 4 ++ LibraryCPP/CMakeLists.txt | 2 +- LibraryCPP/Tests/CMakeLists.txt | 8 ++-- LibraryCPP/array.cpp | 15 +++++-- 9 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 Lab1CPP/.vscode/tasks.json create mode 100644 Lab1CPP/CMakeLists.txt create mode 100644 Lab1CPP/input.txt create mode 100644 Lab1CPP/lab1.cpp create mode 100644 Lab1CPP/test2.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index b40c0dd11c..b6bc75deaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,9 +10,9 @@ else() add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter) endif() -add_subdirectory(LibraryC) +#add_subdirectory(LibraryC) add_subdirectory(LibraryCPP) -add_subdirectory(LibraryCPPClass) -add_subdirectory(LibraryCPPTemplate) +#add_subdirectory(LibraryCPPClass) +#add_subdirectory(LibraryCPPTemplate) -add_subdirectory(Lab1C) +add_subdirectory(Lab1CPP) \ No newline at end of file diff --git a/Lab1CPP/.vscode/tasks.json b/Lab1CPP/.vscode/tasks.json new file mode 100644 index 0000000000..65323399c7 --- /dev/null +++ b/Lab1CPP/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: cl.exe build active file", + "command": "cl.exe", + "args": [ + "/Zi", + "/EHsc", + "/nologo", + "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe", + "${file}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$msCompile" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt new file mode 100644 index 0000000000..c7785cc993 --- /dev/null +++ b/Lab1CPP/CMakeLists.txt @@ -0,0 +1,9 @@ +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: 5 unique values: -1 2 3 4 6 ") \ No newline at end of file diff --git a/Lab1CPP/input.txt b/Lab1CPP/input.txt new file mode 100644 index 0000000000..2f173150b0 --- /dev/null +++ b/Lab1CPP/input.txt @@ -0,0 +1,4 @@ +10 +1 2 3 4 5 6 7 8 9 0 +4 +1 2 3 4 diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp new file mode 100644 index 0000000000..e94d36222d --- /dev/null +++ b/Lab1CPP/lab1.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include "../LibraryCPP/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) == 0) return; + + int count = 0; + for (size_t i = 0; i < array_size(arr) - 1; i++){ + if(array_get(arr, i) * array_get(arr, i + 1) < 0){ + count++; + } + } + cout << "change of sign: " << count << " "; +} + +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 == 1) { + cout << array_get(arr, i) << " "; + } + } + cout << endl; +} + + +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..7a49223c2b --- /dev/null +++ b/Lab1CPP/test2.txt @@ -0,0 +1,4 @@ +20 +1 -2 -3 4 5 -6 7 8 9 -0 1 2 3 -4 -5 -6 -7 -8 -9 -0 +8 +-1 2 3 4 5 5 5 6 0 diff --git a/LibraryCPP/CMakeLists.txt b/LibraryCPP/CMakeLists.txt index dbd7769feb..6ea3aafdf0 100644 --- a/LibraryCPP/CMakeLists.txt +++ b/LibraryCPP/CMakeLists.txt @@ -1,3 +1,3 @@ -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_subdirectory(Tests) diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index c903f9d7f3..ca5a0d2ce9 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -1,7 +1,7 @@ -# add_executable(TestArrayCPP array.cpp) -# target_include_directories(TestArrayCPP PUBLIC ..) -# target_link_libraries(TestArrayCPP LibraryCPP) -# add_test(TestArrayCPP TestArrayCPP) + add_executable(TestArrayCPP array.cpp) + target_include_directories(TestArrayCPP PUBLIC ..) + target_link_libraries(TestArrayCPP LibraryCPP) + add_test(TestArrayCPP TestArrayCPP) # add_executable(TestListCPP list.cpp) # target_include_directories(TestListCPP PUBLIC ..) diff --git a/LibraryCPP/array.cpp b/LibraryCPP/array.cpp index c773e5f167..4cf6d7dfae 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]{0}; + 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; } From 2953598a3c4b8b8fa2ce4df0d0e19fd2eef26aec Mon Sep 17 00:00:00 2001 From: Poni682 Date: Fri, 24 Oct 2025 00:51:31 +0300 Subject: [PATCH 02/22] :3 --- Lab1CPP/CMakeLists.txt | 2 +- Lab1CPP/lab1.cpp | 19 ++++++++++++------- Lab1CPP/test2.txt | 2 +- LibraryCPP/array.cpp | 2 +- ...1\200\320\276\320\262\320\270\321\207.txt" | 0 5 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 "\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" diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt index c7785cc993..aa3fad3aaa 100644 --- a/Lab1CPP/CMakeLists.txt +++ b/Lab1CPP/CMakeLists.txt @@ -6,4 +6,4 @@ 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: 5 unique values: -1 2 3 4 6 ") \ No newline at end of file +set_property(TEST TestLab1CPP2 PROPERTY PASS_REGULAR_EXPRESSION "change of sign: 5 unique values: 0 -1 2 3 4 6 ") \ No newline at end of file diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index e94d36222d..87cab5dac2 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -1,10 +1,11 @@ #include #include #include -#include "../LibraryCPP/array.h" +#include "array.h" using namespace std; + Array *array_create_and_read(istream &input) { int n; @@ -42,22 +43,26 @@ void task2(Array *arr) 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)) { + if(array_get(arr, i) == array_get(arr, j)){ count++; } - } - if (count == 1) { + if(count == 2){ + break; + } + } + if(count == 1){ cout << array_get(arr, i) << " "; } + } - cout << endl; + } int main(int argc, char **argv){ if (argc < 2) { - cerr << "Usage: " << argv[0] << " " << endl; - return 1; + cerr << "Usage: " << argv[0] << " " << endl; + return 1; } ifstream FILE(argv[1]); if (!FILE.is_open()){ diff --git a/Lab1CPP/test2.txt b/Lab1CPP/test2.txt index 7a49223c2b..71f85d7243 100644 --- a/Lab1CPP/test2.txt +++ b/Lab1CPP/test2.txt @@ -1,4 +1,4 @@ 20 1 -2 -3 4 5 -6 7 8 9 -0 1 2 3 -4 -5 -6 -7 -8 -9 -0 8 --1 2 3 4 5 5 5 6 0 +-1 2 3 4 5 5 6 0 diff --git a/LibraryCPP/array.cpp b/LibraryCPP/array.cpp index 4cf6d7dfae..ef6f110120 100644 --- a/LibraryCPP/array.cpp +++ b/LibraryCPP/array.cpp @@ -10,7 +10,7 @@ struct Array Array *array_create(size_t size) { Array* newArr = new Array; - newArr->data = new int[size]{0}; + newArr->data = new int[size]; newArr->size = size; return newArr; } 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 From 528a55e33347047b0512320b46cbc7c6c5f0be12 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Fri, 24 Oct 2025 01:05:49 +0300 Subject: [PATCH 03/22] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=B8=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D1=8F,=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=D1=8B,=20=D0=BA?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B5=20=D0=B1=D1=8B=D0=BB=D0=B8?= =?UTF-8?q?=20=D1=83=D0=BA=D0=B0=D0=B7=D0=B0=D0=BD=D1=8B=20=D0=B2=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lab1CPP/CMakeLists.txt | 2 +- Lab1CPP/lab1.cpp | 25 +++++++++++++++---------- Lab1CPP/test2.txt | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt index aa3fad3aaa..6000c32593 100644 --- a/Lab1CPP/CMakeLists.txt +++ b/Lab1CPP/CMakeLists.txt @@ -6,4 +6,4 @@ 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: 5 unique values: 0 -1 2 3 4 6 ") \ No newline at end of file +set_property(TEST TestLab1CPP2 PROPERTY PASS_REGULAR_EXPRESSION "change of sign: 1 unique values: -1 2 3 4 6 0 ") \ No newline at end of file diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index 87cab5dac2..0cb6cc57bf 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -22,22 +22,27 @@ Array *array_create_and_read(istream &input) return arr; } -void task1(Array *arr) -{ - if (arr == nullptr || array_size(arr) == 0) return; - - int count = 0; - for (size_t i = 0; i < array_size(arr) - 1; i++){ - if(array_get(arr, i) * array_get(arr, i + 1) < 0){ - count++; +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: " << count << " "; + + cout << "change of sign: " << signChanges; } void task2(Array *arr) { - cout << "unique values: "; + cout << " unique values: "; if (arr == nullptr || array_size(arr) == 0) return; for (size_t i = 0; i < array_size(arr); i++){ diff --git a/Lab1CPP/test2.txt b/Lab1CPP/test2.txt index 71f85d7243..5e5209a201 100644 --- a/Lab1CPP/test2.txt +++ b/Lab1CPP/test2.txt @@ -1,4 +1,4 @@ -20 -1 -2 -3 4 5 -6 7 8 9 -0 1 2 3 -4 -5 -6 -7 -8 -9 -0 +6 +-1 -1 0 0 1 1 8 -1 2 3 4 5 5 6 0 From 71151a9c68f1289f1a8e801888e98a1ebb5d7593 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Fri, 24 Oct 2025 11:07:11 +0300 Subject: [PATCH 04/22] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lab1CPP/CMakeLists.txt | 5 ++++- Lab1CPP/test3.txt | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Lab1CPP/test3.txt diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt index 6000c32593..79801a4290 100644 --- a/Lab1CPP/CMakeLists.txt +++ b/Lab1CPP/CMakeLists.txt @@ -6,4 +6,7 @@ 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 ") \ No newline at end of file +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/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 From c07b66bef6419d25cd2b326abbee645688e7599f Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 26 Oct 2025 20:01:07 +0300 Subject: [PATCH 05/22] delete .vscode --- Lab1CPP/.vscode/tasks.json | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 Lab1CPP/.vscode/tasks.json diff --git a/Lab1CPP/.vscode/tasks.json b/Lab1CPP/.vscode/tasks.json deleted file mode 100644 index 65323399c7..0000000000 --- a/Lab1CPP/.vscode/tasks.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: cl.exe build active file", - "command": "cl.exe", - "args": [ - "/Zi", - "/EHsc", - "/nologo", - "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe", - "${file}" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$msCompile" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "Task generated by Debugger." - } - ], - "version": "2.0.0" -} \ No newline at end of file From ea670f29bb242eb9bd391c6aaeb0792faf906b58 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Thu, 30 Oct 2025 17:32:42 +0300 Subject: [PATCH 06/22] =?UTF-8?q?Create=20=D0=92=D0=BE=D0=BF=D1=80=D0=BE?= =?UTF-8?q?=D1=81=D1=8B=20=D0=BF=D0=BE=20=D0=BF=D0=BE=D0=B2=D0=BE=D0=B4?= =?UTF-8?q?=D1=83=20=D0=B7=D0=B0=D0=BD=D1=8F=D1=82=D0=B8=D0=B9=2003.11.202?= =?UTF-8?q?5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Будет ли защита 03.11.2025? --- ...7\320\260\320\275\321\217\321\202\320\270\320\271 03.11.2025" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "\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" 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 @@ +защита на следующей неделе в понедельник будет? From 9bb014b41a41a835ee165fa083a2f02f3d1d621d Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 16:59:01 +0300 Subject: [PATCH 07/22] =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 3 +- Lab1C/CMakeLists.txt | 6 - Lab1C/input.txt | 4 - Lab1C/lab1.c | 40 ------- Lab2CPP/CMakeLists.txt | 36 ++++++ Lab2CPP/TestTXT/text1.txt | 1 + Lab2CPP/TestTXT/text10.txt | 1 + Lab2CPP/TestTXT/text2.txt | 1 + Lab2CPP/TestTXT/text3.txt | 1 + Lab2CPP/TestTXT/text4-1.txt | 1 + Lab2CPP/TestTXT/text4.txt | 1 + Lab2CPP/TestTXT/text5.txt | 1 + Lab2CPP/TestTXT/text6.txt | 1 + Lab2CPP/TestTXT/text7.txt | 1 + Lab2CPP/TestTXT/text8.txt | 1 + Lab2CPP/TestTXT/text9.txt | 1 + Lab2CPP/lab2.cpp | 202 ++++++++++++++++++++++++++++++++ LibraryCPP/CMakeLists.txt | 3 +- LibraryCPP/Tests/CMakeLists.txt | 26 ++-- LibraryCPP/stack.cpp | 32 ++++- LibraryCPP/stack.h | 8 ++ LibraryCPP/vector.cpp | 59 ++++++++-- LibraryCPP/vector.h | 8 +- 23 files changed, 358 insertions(+), 80 deletions(-) delete mode 100644 Lab1C/CMakeLists.txt delete mode 100644 Lab1C/input.txt delete mode 100644 Lab1C/lab1.c create mode 100644 Lab2CPP/CMakeLists.txt create mode 100644 Lab2CPP/TestTXT/text1.txt create mode 100644 Lab2CPP/TestTXT/text10.txt create mode 100644 Lab2CPP/TestTXT/text2.txt create mode 100644 Lab2CPP/TestTXT/text3.txt create mode 100644 Lab2CPP/TestTXT/text4-1.txt create mode 100644 Lab2CPP/TestTXT/text4.txt create mode 100644 Lab2CPP/TestTXT/text5.txt create mode 100644 Lab2CPP/TestTXT/text6.txt create mode 100644 Lab2CPP/TestTXT/text7.txt create mode 100644 Lab2CPP/TestTXT/text8.txt create mode 100644 Lab2CPP/TestTXT/text9.txt create mode 100644 Lab2CPP/lab2.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b6bc75deaf..511ed76648 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,4 +15,5 @@ add_subdirectory(LibraryCPP) #add_subdirectory(LibraryCPPClass) #add_subdirectory(LibraryCPPTemplate) -add_subdirectory(Lab1CPP) \ No newline at end of file +#add_subdirectory(Lab1CPP) +add_subdirectory(Lab2CPP) \ 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/input.txt b/Lab1C/input.txt deleted file mode 100644 index 2f173150b0..0000000000 --- a/Lab1C/input.txt +++ /dev/null @@ -1,4 +0,0 @@ -10 -1 2 3 4 5 6 7 8 9 0 -4 -1 2 3 4 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/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..257ddef71e --- /dev/null +++ b/Lab2CPP/lab2.cpp @@ -0,0 +1,202 @@ +#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; + } +//123 + int a3 = stack_take(stack); //3 + int a2 = stack_take(stack); //2 + int a1 = stack_take(stack); //1 + + 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 == "1") stack_push(stack, 1); + else if(command == "2") stack_push(stack, 2); + else if(command == "3") stack_push(stack, 3); + else if(command == "4") stack_push(stack, 4); + else if(command == "5") stack_push(stack, 5); + else if(command == "6") stack_push(stack, 6); + else if(command == "7") stack_push(stack, 7); + else if(command == "8") stack_push(stack, 8); + else if(command == "9") stack_push(stack, 9); + else if(command == "0") stack_push(stack, 0); + + 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); + ss << stroka; + while(ss >> command){ + determinant_operator(stack, command); + } + } + + else{ + getline(cin, stroka); + ss << stroka; + while(ss >> command){ + determinant_operator(stack, command); + } + } + + return 0; + stack_delete(stack); +} \ No newline at end of file diff --git a/LibraryCPP/CMakeLists.txt b/LibraryCPP/CMakeLists.txt index 6ea3aafdf0..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 ca5a0d2ce9..d037daf507 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -1,7 +1,7 @@ - add_executable(TestArrayCPP array.cpp) - target_include_directories(TestArrayCPP PUBLIC ..) - target_link_libraries(TestArrayCPP LibraryCPP) - add_test(TestArrayCPP TestArrayCPP) +# add_executable(TestArrayCPP array.cpp) +# target_include_directories(TestArrayCPP PUBLIC ..) +# target_link_libraries(TestArrayCPP LibraryCPP) +# add_test(TestArrayCPP TestArrayCPP) # add_executable(TestListCPP list.cpp) # target_include_directories(TestListCPP PUBLIC ..) @@ -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/stack.cpp b/LibraryCPP/stack.cpp index c090abba1c..6288a7948b 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 stack->vector->size == 0; +} + +/* +stack_size создан для проверки колличества элементов, так как stack_empty может быть не удобным +*/ +int stack_size(const Stack *stack){ + return stack->vector->size; } diff --git a/LibraryCPP/stack.h b/LibraryCPP/stack.h index a03c798df7..7a05bdccdf 100644 --- a/LibraryCPP/stack.h +++ b/LibraryCPP/stack.h @@ -1,6 +1,8 @@ #ifndef STACK_H #define STACK_H +#include "vector.h" + // Stack // Stores integer values inside @@ -22,6 +24,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 +34,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..90a8642f5a 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -1,34 +1,73 @@ #include "vector.h" - -struct Vector -{ -}; +#include Vector *vector_create() { - return new Vector; + Vector *vector = new Vector; + vector->arr = nullptr; + 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; + } + + // Нужно увеличить capacity + size_t new_capacity = std::max(size, vector->capacity * 2); + if (new_capacity == 0) new_capacity = 1; + + Data* new_data = new Data[new_capacity]; + + // Копируем старые данные + for (size_t i = 0; i < vector->size; i++) { + new_data[i] = vector->arr[i]; + } + + // Инициализируем новые элементы нулями + for (size_t i = vector->size; i < size; i++) { + new_data[i] = 0; + } + + delete[] vector->arr; + vector->arr = new_data; + 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..cf2b265916 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -2,6 +2,7 @@ #define VECTOR_H #include +#include // Vector (dynamic array) @@ -9,7 +10,12 @@ // Change it to desired type typedef int Data; -struct Vector; +struct Vector +{ + int* arr; + size_t size; + size_t capacity; +}; // Creates vector Vector *vector_create(); From 0caef735886ba4bebc204da136c447a983552960 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 17:35:19 +0300 Subject: [PATCH 08/22] mini update --- LibraryCPP/vector.cpp | 6 +----- LibraryCPP/vector.h | 1 + 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index 90a8642f5a..4318e054dd 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -1,5 +1,5 @@ #include "vector.h" -#include + Vector *vector_create() { @@ -41,7 +41,6 @@ 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; } @@ -50,18 +49,15 @@ void vector_resize(Vector *vector, size_t size) return; } - // Нужно увеличить capacity size_t new_capacity = std::max(size, vector->capacity * 2); if (new_capacity == 0) new_capacity = 1; Data* new_data = new Data[new_capacity]; - // Копируем старые данные for (size_t i = 0; i < vector->size; i++) { new_data[i] = vector->arr[i]; } - // Инициализируем новые элементы нулями for (size_t i = vector->size; i < size; i++) { new_data[i] = 0; } diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index cf2b265916..784f6aea39 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -4,6 +4,7 @@ #include #include + // Vector (dynamic array) // Stores integer values inside From 2b3a32241ad2903a75a48994fefe0ce745e1fb6b Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 17:40:57 +0300 Subject: [PATCH 09/22] test cmake git --- LibraryCPP/vector.h | 1 - 1 file changed, 1 deletion(-) diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index 784f6aea39..cf2b265916 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -4,7 +4,6 @@ #include #include - // Vector (dynamic array) // Stores integer values inside From e4e252a8c8bd039abbc899d0befd1cdab1d9a287 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 18:04:26 +0300 Subject: [PATCH 10/22] rewrite vector --- LibraryCPP/vector.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index 4318e054dd..acbdb4eda3 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -49,21 +49,27 @@ void vector_resize(Vector *vector, size_t size) return; } - size_t new_capacity = std::max(size, vector->capacity * 2); - if (new_capacity == 0) new_capacity = 1; + size_t new_capacity = vector->capacity * 2; + if (new_capacity < size) { + new_capacity = size; + } + + if (new_capacity == 0) { + new_capacity = 1; + } - Data* new_data = new Data[new_capacity]; + Data* new_arr = new Data[new_capacity]; for (size_t i = 0; i < vector->size; i++) { - new_data[i] = vector->arr[i]; + new_arr[i] = vector->arr[i]; } for (size_t i = vector->size; i < size; i++) { - new_data[i] = 0; + new_arr[i] = 0; } delete[] vector->arr; - vector->arr = new_data; + vector->arr = new_arr; vector->size = size; vector->capacity = new_capacity; } \ No newline at end of file From a8deca4a514106baddcf091dcc05dea553ae1e48 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 18:07:31 +0300 Subject: [PATCH 11/22] delete all librari from vector --- LibraryCPP/vector.cpp | 2 +- LibraryCPP/vector.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index acbdb4eda3..8686adfe5d 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -19,7 +19,7 @@ void vector_delete(Vector *vector) Data vector_get(const Vector *vector, size_t index) { if (index >= vector->size) { - throw std::out_of_range("Index out of range"); + return -1; } return vector->arr[index]; } diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index cf2b265916..7aca69bcdc 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -2,7 +2,6 @@ #define VECTOR_H #include -#include // Vector (dynamic array) From c5a00da68d1043ae79c2e269b07e05a31eb1b1d3 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 18:09:04 +0300 Subject: [PATCH 12/22] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LibraryCPP/vector.cpp | 2 +- LibraryCPP/vector.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index 8686adfe5d..acbdb4eda3 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -19,7 +19,7 @@ void vector_delete(Vector *vector) Data vector_get(const Vector *vector, size_t index) { if (index >= vector->size) { - return -1; + throw std::out_of_range("Index out of range"); } return vector->arr[index]; } diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index 7aca69bcdc..cf2b265916 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -2,6 +2,7 @@ #define VECTOR_H #include +#include // Vector (dynamic array) From e8a8065dcde086ae6b8a4492e8026a7f516ced06 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 18:09:50 +0300 Subject: [PATCH 13/22] 1 --- LibraryCPP/vector.h | 1 + 1 file changed, 1 insertion(+) diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index cf2b265916..784f6aea39 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -4,6 +4,7 @@ #include #include + // Vector (dynamic array) // Stores integer values inside From a431c66fe5ac97d982ef55afbd77577e03532f9f Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 18:20:38 +0300 Subject: [PATCH 14/22] main --- Lab2CPP/lab2.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index 257ddef71e..83bc3efebd 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -120,10 +120,10 @@ void rot(Stack* stack) { cout << "error rot\n"; return; } -//123 - int a3 = stack_take(stack); //3 - int a2 = stack_take(stack); //2 - int a1 = stack_take(stack); //1 + + int a3 = stack_take(stack); + int a2 = stack_take(stack); + int a1 = stack_take(stack); stack_push(stack, a2); stack_push(stack, a3); @@ -183,10 +183,12 @@ int main(int argc, char **argv){ } stroka = read_file(FILE); + FILE.close(); ss << stroka; while(ss >> command){ determinant_operator(stack, command); } + } else{ From 88c53358ade038fc3f3f3e377791df25cac7dd33 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 16 Nov 2025 18:26:03 +0300 Subject: [PATCH 15/22] =?UTF-8?q?:=D0=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lab2CPP/lab2.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index 83bc3efebd..79a183cbee 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -198,7 +198,6 @@ int main(int argc, char **argv){ determinant_operator(stack, command); } } - - return 0; stack_delete(stack); + return 0; } \ No newline at end of file From d997db14fa6417998a955efba39c1fe28e9abbe2 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Mon, 17 Nov 2025 14:14:30 +0300 Subject: [PATCH 16/22] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F=20=D0=B2=D0=B5=D1=80=D1=81?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D0=BE=D0=B4=20=D0=BA=D0=BE=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D0=B0=D1=80=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lab2CPP/lab2.cpp | 17 +++++------------ LibraryCPP/stack.cpp | 4 ++-- LibraryCPP/stack.h | 3 ++- LibraryCPP/vector.cpp | 7 +++++++ LibraryCPP/vector.h | 7 +------ 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index 79a183cbee..7f5f0a5004 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -145,17 +145,9 @@ void determinant_operator(Stack *stack, string command){ else if(command == "/") division(stack); else if(command == "%") remainder(stack); - else if(command == "1") stack_push(stack, 1); - else if(command == "2") stack_push(stack, 2); - else if(command == "3") stack_push(stack, 3); - else if(command == "4") stack_push(stack, 4); - else if(command == "5") stack_push(stack, 5); - else if(command == "6") stack_push(stack, 6); - else if(command == "7") stack_push(stack, 7); - else if(command == "8") stack_push(stack, 8); - else if(command == "9") stack_push(stack, 9); - else if(command == "0") stack_push(stack, 0); - + 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); @@ -200,4 +192,5 @@ int main(int argc, char **argv){ } stack_delete(stack); return 0; -} \ No newline at end of file +} + diff --git a/LibraryCPP/stack.cpp b/LibraryCPP/stack.cpp index 6288a7948b..f01f7c8e05 100644 --- a/LibraryCPP/stack.cpp +++ b/LibraryCPP/stack.cpp @@ -47,12 +47,12 @@ void stack_pop(Stack *stack) bool stack_empty(const Stack *stack) { - return stack->vector->size == 0; + return vector_size(stack->vector) == 0; } /* stack_size создан для проверки колличества элементов, так как stack_empty может быть не удобным */ int stack_size(const Stack *stack){ - return stack->vector->size; + return vector_size(stack->vector); } diff --git a/LibraryCPP/stack.h b/LibraryCPP/stack.h index 7a05bdccdf..cadd365e17 100644 --- a/LibraryCPP/stack.h +++ b/LibraryCPP/stack.h @@ -1,7 +1,6 @@ #ifndef STACK_H #define STACK_H -#include "vector.h" // Stack @@ -9,6 +8,8 @@ // Change it to desired type typedef int Data; +struct Vector; + struct Stack; // Creates empty stack diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index acbdb4eda3..b1d8830d8b 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -1,5 +1,12 @@ #include "vector.h" +struct Vector +{ + int* arr; + size_t size; + size_t capacity; +}; + Vector *vector_create() { diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index 784f6aea39..0a9a34c4e0 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -11,12 +11,7 @@ // Change it to desired type typedef int Data; -struct Vector -{ - int* arr; - size_t size; - size_t capacity; -}; +struct Vector; // Creates vector Vector *vector_create(); From 0b6ad6422eeb007b531bc932553eac4784cf2ac7 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Thu, 11 Dec 2025 05:34:42 +0300 Subject: [PATCH 17/22] laba3 --- CMakeLists.txt | 7 +- Lab3CPP/CMakeLists.txt | 21 +++++ Lab3CPP/lab3.cpp | 132 +++++++++++++++++++++++++++ Lab3CPP/test/test1.txt | 6 ++ Lab3CPP/test/test2.txt | 1 + Lab3CPP/test/test3.txt | 1 + Lab3CPP/test/test4.txt | 5 + Lab3CPP/test/test5.txt | 4 + Lab3CPP/test/test6.txt | 6 ++ LibraryCPP/Tests/CMakeLists.txt | 18 ++-- LibraryCPP/vector.cpp | 2 +- LibraryCPPClass/CMakeLists.txt | 2 +- LibraryCPPClass/Tests/CMakeLists.txt | 18 ++-- LibraryCPPClass/list.cpp | 124 +++++++++++++++++++++++-- LibraryCPPClass/list.h | 29 +++++- LibraryCPPClass/queue.cpp | 25 ++++- LibraryCPPClass/queue.h | 6 ++ 17 files changed, 369 insertions(+), 38 deletions(-) create mode 100644 Lab3CPP/CMakeLists.txt create mode 100644 Lab3CPP/lab3.cpp create mode 100644 Lab3CPP/test/test1.txt create mode 100644 Lab3CPP/test/test2.txt create mode 100644 Lab3CPP/test/test3.txt create mode 100644 Lab3CPP/test/test4.txt create mode 100644 Lab3CPP/test/test5.txt create mode 100644 Lab3CPP/test/test6.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 511ed76648..a0ccc07009 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,9 +11,10 @@ else() endif() #add_subdirectory(LibraryC) -add_subdirectory(LibraryCPP) -#add_subdirectory(LibraryCPPClass) +#add_subdirectory(LibraryCPP) +add_subdirectory(LibraryCPPClass) #add_subdirectory(LibraryCPPTemplate) #add_subdirectory(Lab1CPP) -add_subdirectory(Lab2CPP) \ No newline at end of file +#add_subdirectory(Lab2CPP) +add_subdirectory(Lab3CPP) \ No newline at end of file diff --git a/Lab3CPP/CMakeLists.txt b/Lab3CPP/CMakeLists.txt new file mode 100644 index 0000000000..1c219bd5ad --- /dev/null +++ b/Lab3CPP/CMakeLists.txt @@ -0,0 +1,21 @@ +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") \ No newline at end of file diff --git a/Lab3CPP/lab3.cpp b/Lab3CPP/lab3.cpp new file mode 100644 index 0000000000..ab7c2fbb7b --- /dev/null +++ b/Lab3CPP/lab3.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +#include "../LibraryCPPClass/queue.h" + +using namespace std; + +// Для улучшения читаемочти кода +#define X first +#define Y second + +// Одна функция для нахождения начала и конца, для улучшния читабельности кода сделал их +const char START = 'Q'; +const char END = 'E'; + +pair find_point(const vector& 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 {i, j}; + } + } + } + return {-1, -1}; +} +// Это перевод с pair в int, но используется только в очереди, такчто будет pair to queue +int ptoq(pair pos, int cols) { + return pos.X * cols + pos.Y; +} + +int bfs(const vector& maze, pair start, pair end) { + if (start.X == -1 || end.X == -1) { + return 0; + } + + if (start == end) return 1; + + 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 pair 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 (!inQueue[ny][nx] && !(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; + if (!inQueue[ny][nx]) { + q.insert(ptoq({ny, nx}, cols)); + inQueue[ny][nx] = true; + } + } + + } + } + } + + 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; + } + + vector 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/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index d037daf507..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/vector.cpp b/LibraryCPP/vector.cpp index b1d8830d8b..ec737ed6ed 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -11,7 +11,7 @@ struct Vector Vector *vector_create() { Vector *vector = new Vector; - vector->arr = nullptr; + vector->arr = NULL; vector->size = 0; vector->capacity = 0; return vector; 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..1939fddbc3 100644 --- a/LibraryCPPClass/list.cpp +++ b/LibraryCPPClass/list.cpp @@ -1,44 +1,154 @@ #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), size_(0) { } -List::List(const List &a) +List::List(const List &a) : head_(NULL), tail_(NULL), size_(0) { + if(a.head_ == NULL){ + return; + } + 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(); + } } List &List::operator=(const List &a) { + if(this != &a){ + while(head_ != NULL){ + this->erase_first(); + } + + 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(); + } + + } return *this; } List::~List() { + while (head_ != NULL) { + erase_first(); + } } 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; + this->size_ ++; + + return newItem; } List::Item *List::insert_after(Item *item, Data data) { - return nullptr; + if(item == NULL){ + return insert(data); + } + + + + Item *newItem = new Item(data, NULL, item); + if(item->next() != NULL){ + Item *nextItem = item->next(); + newItem->setNextPtr(nextItem); + nextItem->setPrevPtr(newItem); + } + + if(newItem->next() == NULL){ + this->tail_ = newItem; + } + item->setNextPtr(newItem); + this->size_++; + return newItem; } List::Item *List::erase_first() { - return nullptr; + if(this->head_ == NULL){ + return NULL; + } + + if(this->head_->next() == NULL){ + this->head_ = NULL; + this->tail_ = NULL; + this->size_--; + return NULL; + } + + Item *newHead = this->head_->next(); + delete this->head_; + this->head_ = newHead; + this->head_->setPrevPtr(NULL); + this->size_--; + return this->head_; } List::Item *List::erase_next(Item *item) { - return nullptr; + if(item == NULL){ + return NULL; + } + + if(item->next() == NULL || item->prev() == NULL){ + delete item; + this->head_ = NULL; + this->tail_ = NULL; + this->size_ --; + return NULL; + } + + Item *nextItem = item->next(); + item->setNextPtr(nextItem->next()); + delete nextItem; + return item->next(); +} +// обеспечивает добавление одного элемента O(1) так как он добавляет его в конец (для очереди) +List::Item *List::get_end_item() const { + return this->tail_; +} + +Data List::data_head() const { + return head_->data(); } diff --git a/LibraryCPPClass/list.h b/LibraryCPPClass/list.h index 67da6907f7..1aa2f77ee9 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,25 @@ 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; + private: // private data should be here + Item* head_; + Item* tail_; + size_t size_; }; #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 From 9c9a4ffa9d34dcc9abc588d8fe140ea9669b34ff Mon Sep 17 00:00:00 2001 From: Poni682 Date: Thu, 11 Dec 2025 05:54:33 +0300 Subject: [PATCH 18/22] fix list and queue --- LibraryCPPClass/list.cpp | 92 +++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/LibraryCPPClass/list.cpp b/LibraryCPPClass/list.cpp index 1939fddbc3..2e7124c680 100644 --- a/LibraryCPPClass/list.cpp +++ b/LibraryCPPClass/list.cpp @@ -35,15 +35,15 @@ List &List::operator=(const List &a) this->erase_first(); } - 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(); + if(a.head_ != NULL){ + 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(); + } } - } return *this; } @@ -76,7 +76,7 @@ List::Item *List::insert(Data data) } this->head_ = newItem; - this->size_ ++; + this->size_++; return newItem; } @@ -87,19 +87,20 @@ List::Item *List::insert_after(Item *item, Data data) return insert(data); } + Item *newItem = new Item(data); + + newItem->setNextPtr(item->next()); + newItem->setPrevPtr(item); - - Item *newItem = new Item(data, NULL, item); if(item->next() != NULL){ - Item *nextItem = item->next(); - newItem->setNextPtr(nextItem); - nextItem->setPrevPtr(newItem); + item->next()->setPrevPtr(newItem); } - - if(newItem->next() == NULL){ + item->setNextPtr(newItem); + + if(this->tail_ == item){ this->tail_ = newItem; } - item->setNextPtr(newItem); + this->size_++; return newItem; } @@ -110,39 +111,47 @@ List::Item *List::erase_first() return NULL; } - if(this->head_->next() == NULL){ - this->head_ = NULL; + Item *toDelete = this->head_; + this->head_ = this->head_->next(); + + if(this->head_ != NULL){ + this->head_->setPrevPtr(NULL); + } + else { this->tail_ = NULL; - this->size_--; - return NULL; } - - Item *newHead = this->head_->next(); - delete this->head_; - this->head_ = newHead; - this->head_->setPrevPtr(NULL); + + delete toDelete; this->size_--; + return this->head_; } List::Item *List::erase_next(Item *item) { if(item == NULL){ - return NULL; + return erase_first(); } - - if(item->next() == NULL || item->prev() == NULL){ - delete item; - this->head_ = NULL; - this->tail_ = NULL; - this->size_ --; + + if(item->next() == NULL){ return NULL; } - - Item *nextItem = item->next(); - item->setNextPtr(nextItem->next()); - delete nextItem; - return item->next(); + + Item *toDelete = item->next(); + Item *nextAfterDelete = toDelete->next(); + item->setNextPtr(nextAfterDelete); + + if(nextAfterDelete != NULL){ + nextAfterDelete->setPrevPtr(item); + } + else { + this->tail_ = item; + } + + delete toDelete; + this->size_--; + + return nextAfterDelete; } // обеспечивает добавление одного элемента O(1) так как он добавляет его в конец (для очереди) List::Item *List::get_end_item() const { @@ -150,5 +159,8 @@ List::Item *List::get_end_item() const { } Data List::data_head() const { - return head_->data(); -} + if(this->head_ != NULL){ + return head_->data(); + } + return Data(); +} \ No newline at end of file From c339e252848b202719c1b5517998d79ed98701d2 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Fri, 12 Dec 2025 05:09:59 +0300 Subject: [PATCH 19/22] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lab3CPP/CMakeLists.txt | 12 ++++---- Lab3CPP/lab3.cpp | 59 +++++++++++++++++++++++----------------- LibraryCPPClass/list.cpp | 8 ++---- LibraryCPPClass/list.h | 1 - 4 files changed, 42 insertions(+), 38 deletions(-) diff --git a/Lab3CPP/CMakeLists.txt b/Lab3CPP/CMakeLists.txt index 1c219bd5ad..637aad9d4c 100644 --- a/Lab3CPP/CMakeLists.txt +++ b/Lab3CPP/CMakeLists.txt @@ -3,19 +3,19 @@ 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") +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") +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") +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") +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") +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") \ No newline at end of file +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 index ab7c2fbb7b..051f833edf 100644 --- a/Lab3CPP/lab3.cpp +++ b/Lab3CPP/lab3.cpp @@ -1,39 +1,47 @@ #include #include #include -#include "../LibraryCPPClass/queue.h" +#include "queue.h" using namespace std; -// Для улучшения читаемочти кода -#define X first -#define Y second +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'; -pair find_point(const vector& maze, char point) { +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 {i, j}; + return {static_cast(i), static_cast(j)}; } } } return {-1, -1}; } -// Это перевод с pair в int, но используется только в очереди, такчто будет pair to queue -int ptoq(pair pos, int cols) { - return pos.X * cols + pos.Y; +// Это перевод с Point в int, но используется только в очереди, так что будет Point to queue +int ptoq(Point pos, int cols) { + return pos.x * cols + pos.y; } -int bfs(const vector& maze, pair start, pair end) { - if (start.X == -1 || end.X == -1) { +int bfs(const Maze& maze, Point start, Point end) { + if (start.x == -1 || end.x == -1) { return 0; } - - if (start == end) return 1; + // сделал для теста, чтобы показать, что выполняется условие на которое вы показали. Для удобства взял string + string complete = "Не выполнилось"; int rows = maze.size(); int cols = maze[0].size(); @@ -43,18 +51,18 @@ int bfs(const vector& maze, pair start, pair end) { vector> inQueue(rows, vector(cols, false)); // 8 направлений движения ферзя (x,y) - const pair moveQueen[8] = { + const Point moveQueen[8] = { {-1,-1}, {-1,0}, {-1,1}, - {0,-1}, /* Q */ {0,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; + dist[start.x][start.y] = 0; + count[start.x][start.y] = 1; q.insert(ptoq(start, cols)); - inQueue[start.X][start.Y] = true; + inQueue[start.x][start.y] = true; while (!q.empty()) { int currentIndex = q.take(); @@ -62,13 +70,13 @@ int bfs(const vector& maze, pair start, pair end) { 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; + int dy = dir.x; + int dx = dir.y; for (int step = 1; ; step++) { @@ -84,7 +92,7 @@ int bfs(const vector& maze, pair start, pair end) { dist[ny][nx] = newDistant; count[ny][nx] = c; - if (!inQueue[ny][nx] && !(ny == end.X && nx == end.Y)) { + if (!inQueue[ny][nx] && !(ny == end.x && nx == end.y)) { q.insert(ptoq({ny, nx}, cols)); inQueue[ny][nx] = true; } @@ -94,14 +102,15 @@ int bfs(const vector& maze, pair start, pair end) { if (!inQueue[ny][nx]) { q.insert(ptoq({ny, nx}, cols)); inQueue[ny][nx] = true; + complete = "Выполнилось"; } } } } } - - return count[end.X][end.Y]; + cout << complete << endl; + return count[end.x][end.y]; } int main(int argc, char **argv) { @@ -116,7 +125,7 @@ int main(int argc, char **argv) { return 1; } - vector maze; + Maze maze; string line; while (getline(file, line)) { maze.push_back(line); diff --git a/LibraryCPPClass/list.cpp b/LibraryCPPClass/list.cpp index 2e7124c680..51f82512bc 100644 --- a/LibraryCPPClass/list.cpp +++ b/LibraryCPPClass/list.cpp @@ -9,11 +9,11 @@ void List::Item::setPrevPtr(Item* prev){ this->prev_ = prev; } -List::List() : head_(NULL), tail_(NULL), size_(0) +List::List() : head_(NULL), tail_(NULL) { } -List::List(const List &a) : head_(NULL), tail_(NULL), size_(0) +List::List(const List &a) : head_(NULL), tail_(NULL) { if(a.head_ == NULL){ return; @@ -76,7 +76,6 @@ List::Item *List::insert(Data data) } this->head_ = newItem; - this->size_++; return newItem; } @@ -101,7 +100,6 @@ List::Item *List::insert_after(Item *item, Data data) this->tail_ = newItem; } - this->size_++; return newItem; } @@ -122,7 +120,6 @@ List::Item *List::erase_first() } delete toDelete; - this->size_--; return this->head_; } @@ -149,7 +146,6 @@ List::Item *List::erase_next(Item *item) } delete toDelete; - this->size_--; return nextAfterDelete; } diff --git a/LibraryCPPClass/list.h b/LibraryCPPClass/list.h index 1aa2f77ee9..e5d6aae759 100644 --- a/LibraryCPPClass/list.h +++ b/LibraryCPPClass/list.h @@ -72,7 +72,6 @@ class List // private data should be here Item* head_; Item* tail_; - size_t size_; }; #endif From 0bfe2e1821eca2ddace06876facace4b29da2e49 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 14 Dec 2025 16:28:06 +0300 Subject: [PATCH 20/22] corrected --- Lab3CPP/CMakeLists.txt | 12 ++++++------ Lab3CPP/lab3.cpp | 10 +--------- Lab3CPP/test/test7.txt | 3 +++ LibraryCPPClass/list.cpp | 37 +++++++++++++++++++------------------ LibraryCPPClass/list.h | 6 ++++++ 5 files changed, 35 insertions(+), 33 deletions(-) create mode 100644 Lab3CPP/test/test7.txt diff --git a/Lab3CPP/CMakeLists.txt b/Lab3CPP/CMakeLists.txt index 637aad9d4c..1c219bd5ad 100644 --- a/Lab3CPP/CMakeLists.txt +++ b/Lab3CPP/CMakeLists.txt @@ -3,19 +3,19 @@ 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") +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") +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") +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") +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") +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") \ No newline at end of file +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 index 051f833edf..5861fdd7b7 100644 --- a/Lab3CPP/lab3.cpp +++ b/Lab3CPP/lab3.cpp @@ -40,9 +40,7 @@ int bfs(const Maze& maze, Point start, Point end) { if (start.x == -1 || end.x == -1) { return 0; } - // сделал для теста, чтобы показать, что выполняется условие на которое вы показали. Для удобства взял string - string complete = "Не выполнилось"; - + int rows = maze.size(); int cols = maze[0].size(); @@ -99,17 +97,11 @@ int bfs(const Maze& maze, Point start, Point end) { } else if (newDistant == dist[ny][nx]) { count[ny][nx] += c; - if (!inQueue[ny][nx]) { - q.insert(ptoq({ny, nx}, cols)); - inQueue[ny][nx] = true; - complete = "Выполнилось"; - } } } } } - cout << complete << endl; return count[end.x][end.y]; } 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/LibraryCPPClass/list.cpp b/LibraryCPPClass/list.cpp index 51f82512bc..119ffd0ecf 100644 --- a/LibraryCPPClass/list.cpp +++ b/LibraryCPPClass/list.cpp @@ -13,11 +13,9 @@ List::List() : head_(NULL), tail_(NULL) { } -List::List(const List &a) : head_(NULL), tail_(NULL) -{ - if(a.head_ == NULL){ - return; - } +// Добавлена функция копирования +void List::copy(const List& a){ + Item* lastCopy = this->insert(a.head_->data()); Item* current = a.head_->next(); @@ -28,21 +26,26 @@ List::List(const List &a) : head_(NULL), tail_(NULL) } } +// Добавил еще одну функцию, так как повторялась удаление листа +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){ - while(head_ != NULL){ - this->erase_first(); - } + this->delete_list(); if(a.head_ != NULL){ - 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(); - } + copy(a); } } return *this; @@ -50,9 +53,7 @@ List &List::operator=(const List &a) List::~List() { - while (head_ != NULL) { - erase_first(); - } + this->delete_list(); } List::Item *List::first() diff --git a/LibraryCPPClass/list.h b/LibraryCPPClass/list.h index e5d6aae759..1f777ce1a4 100644 --- a/LibraryCPPClass/list.h +++ b/LibraryCPPClass/list.h @@ -67,11 +67,17 @@ class List 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 From a3b02d22c7d41b573d2032e3ea9c3634c068933d Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 14 Dec 2025 17:01:56 +0300 Subject: [PATCH 21/22] add test and small correction --- Lab3CPP/CMakeLists.txt | 3 +++ Lab3CPP/lab3.cpp | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Lab3CPP/CMakeLists.txt b/Lab3CPP/CMakeLists.txt index 1c219bd5ad..c7da3575e2 100644 --- a/Lab3CPP/CMakeLists.txt +++ b/Lab3CPP/CMakeLists.txt @@ -18,4 +18,7 @@ add_test(NAME diagonal_movement COMMAND Lab3CPP ${CMAKE_CURRENT_SOURCE_DIR}/test 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 index 5861fdd7b7..4a88e3eee9 100644 --- a/Lab3CPP/lab3.cpp +++ b/Lab3CPP/lab3.cpp @@ -40,7 +40,8 @@ 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(); @@ -90,7 +91,7 @@ int bfs(const Maze& maze, Point start, Point end) { dist[ny][nx] = newDistant; count[ny][nx] = c; - if (!inQueue[ny][nx] && !(ny == end.x && nx == end.y)) { + if (!inQueue[ny][nx]) { q.insert(ptoq({ny, nx}, cols)); inQueue[ny][nx] = true; } From a760556dd81454558bc50d06a62ce39d38f622a7 Mon Sep 17 00:00:00 2001 From: Poni682 Date: Sun, 14 Dec 2025 17:27:48 +0300 Subject: [PATCH 22/22] m --- Lab3CPP/lab3.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab3CPP/lab3.cpp b/Lab3CPP/lab3.cpp index 4a88e3eee9..942e77af28 100644 --- a/Lab3CPP/lab3.cpp +++ b/Lab3CPP/lab3.cpp @@ -91,7 +91,7 @@ int bfs(const Maze& maze, Point start, Point end) { dist[ny][nx] = newDistant; count[ny][nx] = c; - if (!inQueue[ny][nx]) { + if (!(ny == end.x && nx == end.y)) { q.insert(ptoq({ny, nx}, cols)); inQueue[ny][nx] = true; }