From 32bea4f595caff439bde565b99f9e19055372ae5 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Tue, 9 Sep 2025 16:48:48 +0300 Subject: [PATCH 01/36] =?UTF-8?q?Create=20=D0=94=D0=B5=D0=BC=D0=B8=D0=B4?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=5F=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=5F4091.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...262\320\260_\320\241\320\276\321\204\321\214\321\217_4091.txt" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\320\224\320\265\320\274\320\270\320\264\320\276\320\262\320\260_\320\241\320\276\321\204\321\214\321\217_4091.txt" diff --git "a/\320\224\320\265\320\274\320\270\320\264\320\276\320\262\320\260_\320\241\320\276\321\204\321\214\321\217_4091.txt" "b/\320\224\320\265\320\274\320\270\320\264\320\276\320\262\320\260_\320\241\320\276\321\204\321\214\321\217_4091.txt" new file mode 100644 index 0000000000..e69de29bb2 From 45e110dc997728130ca19cf9719b2dc2712f198b Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Tue, 30 Sep 2025 21:05:27 +0300 Subject: [PATCH 02/36] LAB1 (update 1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлена Lab1CPP (те же файлы что и в Lab1C, все изменены) изменен LibraryCPP\array.cpp, закомментированы некоторые моменты в LibraryCPP\Tests\CMakeLists.txt --- Lab1CPP/CMakeLists.txt | 14 ++++ Lab1CPP/input.txt | 4 ++ Lab1CPP/lab1.cpp | 113 ++++++++++++++++++++++++++++++++ LibraryCPP/Tests/CMakeLists.txt | 8 +-- LibraryCPP/array.cpp | 16 ++++- 5 files changed, 148 insertions(+), 7 deletions(-) create mode 100644 Lab1CPP/CMakeLists.txt create mode 100644 Lab1CPP/input.txt create mode 100644 Lab1CPP/lab1.cpp diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt new file mode 100644 index 0000000000..9db1e378fa --- /dev/null +++ b/Lab1CPP/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.10) +project(Lab1CPP) + +enable_testing() + +add_subdirectory(../LibraryCPP LibraryCPP_build) + +add_executable(Lab1CPP lab1.cpp) +target_include_directories(Lab1CPP PUBLIC ../LibraryCPP) +target_link_libraries(Lab1CPP LibraryCPP) + +add_test(NAME TestLab1CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +set_property(TEST TestLab1CPP PROPERTY PASS_REGULAR_EXPRESSION "2 3 5 7 11") + 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..1f5fe406ba --- /dev/null +++ b/Lab1CPP/lab1.cpp @@ -0,0 +1,113 @@ +#include +#include +#include "array.h" + +Array *array_create_and_read(std::istream& input) +{ + size_t n; + input >> n; + + /* Create array */ + Array *arr = array_create(n); + /* Read array data */ + for (int i = 0 ; i < n ; ++i) + { + Data x; + input >> x; + array_set(arr, i, x); + } + return arr; +} + + +/* , . + , 2. .*/ +void task1(Array *arr) +{ + int num = 2; + for (size_t i = 0; i < array_size(arr); ++i) + { + while (true) + { + bool prime = true; + for (int j = 2; j * j <= num; ++j) + { + if (num % j == 0) + { + prime = false; + break; + } + } + if (prime) break; + ++num; + } + array_set(arr, i, num++); + } + + for (size_t i = 0; i < array_size(arr); ++i) + { + std::cout << array_get(arr, i) << " "; + } + std::cout << "\n"; + +} + +/* , + . , + . .*/ +void task2(Array *arr) +{ + size_t n = array_size(arr); + if (n < 5) + { + return; + } + + int maxSum = 0; + int currSum = 0; + size_t indx = 0; + + for (size_t i = 0; i < 5; ++i) + currSum += array_get(arr, i); + maxSum = currSum; + + for (size_t i = 5; i < n; ++i) + { + currSum = currSum - array_get(arr, i - 5) + array_get(arr, i); + if (currSum > maxSum) + { + maxSum = currSum; + indx = i - 4; + } + } + + for (size_t i = indx; i < indx + 5; ++i) + { + std::cout << array_get(arr, i) << " "; + } + std::cout << "\n"; +} + +int main(int argc, char **argv) +{ + if (argc < 2) + { + return 1; + } + + std::ifstream input(argv[1]); + if (!input) + { + return 1; + } + + Array* arr = array_create_and_read(input); + task1(arr); + array_delete(arr); + + arr = array_create_and_read(input); + task2(arr); + array_delete(arr); + + return 0; +} diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index c903f9d7f3..98d8c01fd6 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..0774c67eef 100644 --- a/LibraryCPP/array.cpp +++ b/LibraryCPP/array.cpp @@ -1,34 +1,44 @@ #include "array.h" +#include struct Array { + Data* data; + size_t size; }; // create array Array *array_create(size_t size) { - return new Array; + Array* arr = new Array; + arr->data = new Data[size]; + arr->size = size; + return arr; } // 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; + if (index >= arr->size) 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) return; + arr->data[index] = value; } // returns array size size_t array_size(const Array *arr) { - return 0; + return arr->size; } From 4862cc035ce1913a82e9d9c1fec7b40594f0e64a Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Wed, 1 Oct 2025 12:59:12 +0300 Subject: [PATCH 03/36] LAB1 (update 2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Заклментированы строки в Lab1C/CMakeLists.txt для работы тестов --- Lab1C/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lab1C/CMakeLists.txt b/Lab1C/CMakeLists.txt index f4da4d8372..f32851ad11 100644 --- a/Lab1C/CMakeLists.txt +++ b/Lab1C/CMakeLists.txt @@ -2,5 +2,5 @@ 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") +#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") From 8abe39b312f8a920ddd01f31205911b89d6c0d7b Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Thu, 2 Oct 2025 16:43:46 +0300 Subject: [PATCH 04/36] LAB1 (update 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Исправлено, что наворочено --- CMakeLists.txt | 1 + Lab1CPP/CMakeLists.txt | 7 ------- Lab1CPP/lab1.cpp | 2 +- LibraryC/Tests/CMakeLists.txt | 8 ++++---- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b40c0dd11c..b2a26a98f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,3 +16,4 @@ add_subdirectory(LibraryCPPClass) add_subdirectory(LibraryCPPTemplate) add_subdirectory(Lab1C) +add_subdirectory(Lab1CPP) diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt index 9db1e378fa..7cce5a0dbf 100644 --- a/Lab1CPP/CMakeLists.txt +++ b/Lab1CPP/CMakeLists.txt @@ -1,10 +1,3 @@ -cmake_minimum_required(VERSION 3.10) -project(Lab1CPP) - -enable_testing() - -add_subdirectory(../LibraryCPP LibraryCPP_build) - add_executable(Lab1CPP lab1.cpp) target_include_directories(Lab1CPP PUBLIC ../LibraryCPP) target_link_libraries(Lab1CPP LibraryCPP) diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index 1f5fe406ba..0d8f9e9823 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -10,7 +10,7 @@ Array *array_create_and_read(std::istream& input) /* Create array */ Array *arr = array_create(n); /* Read array data */ - for (int i = 0 ; i < n ; ++i) + for (size_t i = 0 ; i < n ; ++i) { Data x; input >> x; diff --git a/LibraryC/Tests/CMakeLists.txt b/LibraryC/Tests/CMakeLists.txt index b182dee615..dab8e15fd5 100644 --- a/LibraryC/Tests/CMakeLists.txt +++ b/LibraryC/Tests/CMakeLists.txt @@ -1,7 +1,7 @@ -add_executable(TestArrayC array.cpp) -target_include_directories(TestArrayC PUBLIC ..) -target_link_libraries(TestArrayC LibraryC) -add_test(TestArrayC TestArrayC) +#add_executable(TestArrayC array.cpp) +#target_include_directories(TestArrayC PUBLIC ..) +#target_link_libraries(TestArrayC LibraryC) +#add_test(TestArrayC TestArrayC) # add_executable(TestListC list.cpp) # target_include_directories(TestListC PUBLIC ..) From 4f7d75fee03b96d76f057d15557a03c30668e219 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Fri, 3 Oct 2025 21:31:19 +0300 Subject: [PATCH 05/36] LAB1 (update 4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Вторая подзадача теперь проверяется, добавлены новые функции: 1) создание массива с считанным из файла размером (ТОЛЬКО размером); 2) в отдельную функцию вынесена проверка числа на "простоту". --- Lab1CPP/CMakeLists.txt | 3 ++- Lab1CPP/lab1.cpp | 52 +++++++++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt index 7cce5a0dbf..4b50a7a26c 100644 --- a/Lab1CPP/CMakeLists.txt +++ b/Lab1CPP/CMakeLists.txt @@ -3,5 +3,6 @@ target_include_directories(Lab1CPP PUBLIC ../LibraryCPP) target_link_libraries(Lab1CPP LibraryCPP) add_test(NAME TestLab1CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) -set_property(TEST TestLab1CPP PROPERTY PASS_REGULAR_EXPRESSION "2 3 5 7 11") +set_property(TEST TestLab1CPP PROPERTY PASS_REGULAR_EXPRESSION "2 3 5 7 11.*5 6 7 8 9") + diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index 0d8f9e9823..1df50e8266 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -19,31 +19,40 @@ Array *array_create_and_read(std::istream& input) return arr; } +Array* array_create_read_size(std::istream& input) +{ + size_t n; + input >> n; + return array_create(n); +} + -/* , . - , 2. .*/ +bool is_prime(int n) +{ + if (n < 2) return false; + for (int i = 2; i * i <= n; ++i) + if (n % i == 0) + return false; + return true; +} + +/*Read an integer from a file, create an array of this size. +Fill the array with prime numbers, starting with 2. Display the array on the screen.*/ void task1(Array *arr) { - int num = 2; - for (size_t i = 0; i < array_size(arr); ++i) + array_set(arr, 0, 2); + int num = 3; + for (size_t i = 1; i < array_size(arr); ) { - while (true) + if (is_prime(num)) { - bool prime = true; - for (int j = 2; j * j <= num; ++j) - { - if (num % j == 0) - { - prime = false; - break; - } - } - if (prime) break; - ++num; + array_set(arr, i, num); + ++i; } - array_set(arr, i, num++); + ++num; } + for (size_t i = 0; i < array_size(arr); ++i) { std::cout << array_get(arr, i) << " "; @@ -52,9 +61,9 @@ void task1(Array *arr) } -/* , - . , - . .*/ +/*Read an integer from a file, create an array of this size and +fill it with numbers from the file. Find five neighboring elements, +the sum of whose values ??is maximum. Display the result on the screen.*/ void task2(Array *arr) { size_t n = array_size(arr); @@ -101,10 +110,11 @@ int main(int argc, char **argv) return 1; } - Array* arr = array_create_and_read(input); + Array* arr = array_create_read_size(input); task1(arr); array_delete(arr); + input.seekg(0); // to set the read pointer to the beginning of the file arr = array_create_and_read(input); task2(arr); array_delete(arr); From d75a2282603eb3da390fba7ef56b1b1d47837c62 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Fri, 3 Oct 2025 21:34:31 +0300 Subject: [PATCH 06/36] LAB1 (update 4.1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Меня раздражали вопросительные знаки --- Lab1CPP/lab1.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index 1df50e8266..22a9b61ae6 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -63,7 +63,7 @@ void task1(Array *arr) /*Read an integer from a file, create an array of this size and fill it with numbers from the file. Find five neighboring elements, -the sum of whose values ??is maximum. Display the result on the screen.*/ +the sum of whose values is maximum. Display the result on the screen.*/ void task2(Array *arr) { size_t n = array_size(arr); From a9271e3eb79bd0bf865cce16d58bc5019dd3e365 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Mon, 6 Oct 2025 14:39:21 +0300 Subject: [PATCH 07/36] LAB1 (update 5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Изменен input.txt, отредактирован момент в lab1.cpp --- Lab1CPP/input.txt | 4 ++-- Lab1CPP/lab1.cpp | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Lab1CPP/input.txt b/Lab1CPP/input.txt index 2f173150b0..e79d2ce3ce 100644 --- a/Lab1CPP/input.txt +++ b/Lab1CPP/input.txt @@ -1,4 +1,4 @@ 10 +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 index 22a9b61ae6..dc1e9c8e75 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -42,14 +42,14 @@ void task1(Array *arr) { array_set(arr, 0, 2); int num = 3; - for (size_t i = 1; i < array_size(arr); ) + for (size_t i = 1; i < array_size(arr); ++num) { if (is_prime(num)) { array_set(arr, i, num); ++i; } - ++num; + } @@ -114,7 +114,6 @@ int main(int argc, char **argv) task1(arr); array_delete(arr); - input.seekg(0); // to set the read pointer to the beginning of the file arr = array_create_and_read(input); task2(arr); array_delete(arr); From d37216ded15b07c88f9f181c8b36fe4af3acb582 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Fri, 10 Oct 2025 14:18:14 +0300 Subject: [PATCH 08/36] LAB2 (update 0) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлена папка (и прочее) для второй лабораторной --- CMakeLists.txt | 5 +- Lab2CPP/CMakeLists.txt | 8 +++ Lab2CPP/input.txt | 2 + Lab2CPP/input_commands.txt | 2 + Lab2CPP/lab1.cpp | 122 ++++++++++++++++++++++++++++++++ LibraryCPP/Tests/CMakeLists.txt | 24 +++---- 6 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 Lab2CPP/CMakeLists.txt create mode 100644 Lab2CPP/input.txt create mode 100644 Lab2CPP/input_commands.txt create mode 100644 Lab2CPP/lab1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b2a26a98f8..a5ee7332cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,5 +15,6 @@ add_subdirectory(LibraryCPP) add_subdirectory(LibraryCPPClass) add_subdirectory(LibraryCPPTemplate) -add_subdirectory(Lab1C) -add_subdirectory(Lab1CPP) +#add_subdirectory(Lab1C) +#add_subdirectory(Lab1CPP) +add_subdirectory(Lab2CPP) diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt new file mode 100644 index 0000000000..6798e756b7 --- /dev/null +++ b/Lab2CPP/CMakeLists.txt @@ -0,0 +1,8 @@ +add_executable(Lab2CPP lab1.cpp) +target_include_directories(Lab2CPP PUBLIC ../LibraryCPP) +target_link_libraries(Lab2CPP LibraryCPP) + +add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "2 3 5 7 11.*5 6 7 8 9") + + diff --git a/Lab2CPP/input.txt b/Lab2CPP/input.txt new file mode 100644 index 0000000000..f4c9ff8e92 --- /dev/null +++ b/Lab2CPP/input.txt @@ -0,0 +1,2 @@ +10 33 100 108 114 119 32 44 111 108 101 72 + diff --git a/Lab2CPP/input_commands.txt b/Lab2CPP/input_commands.txt new file mode 100644 index 0000000000..2cb9f8b0e6 --- /dev/null +++ b/Lab2CPP/input_commands.txt @@ -0,0 +1,2 @@ +peek setr peek setr peek peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr + diff --git a/Lab2CPP/lab1.cpp b/Lab2CPP/lab1.cpp new file mode 100644 index 0000000000..dc1e9c8e75 --- /dev/null +++ b/Lab2CPP/lab1.cpp @@ -0,0 +1,122 @@ +#include +#include +#include "array.h" + +Array *array_create_and_read(std::istream& input) +{ + size_t n; + input >> n; + + /* Create array */ + Array *arr = array_create(n); + /* Read array data */ + for (size_t i = 0 ; i < n ; ++i) + { + Data x; + input >> x; + array_set(arr, i, x); + } + return arr; +} + +Array* array_create_read_size(std::istream& input) +{ + size_t n; + input >> n; + return array_create(n); +} + + +bool is_prime(int n) +{ + if (n < 2) return false; + for (int i = 2; i * i <= n; ++i) + if (n % i == 0) + return false; + return true; +} + +/*Read an integer from a file, create an array of this size. +Fill the array with prime numbers, starting with 2. Display the array on the screen.*/ +void task1(Array *arr) +{ + array_set(arr, 0, 2); + int num = 3; + for (size_t i = 1; i < array_size(arr); ++num) + { + if (is_prime(num)) + { + array_set(arr, i, num); + ++i; + } + + } + + + for (size_t i = 0; i < array_size(arr); ++i) + { + std::cout << array_get(arr, i) << " "; + } + std::cout << "\n"; + +} + +/*Read an integer from a file, create an array of this size and +fill it with numbers from the file. Find five neighboring elements, +the sum of whose values is maximum. Display the result on the screen.*/ +void task2(Array *arr) +{ + size_t n = array_size(arr); + if (n < 5) + { + return; + } + + int maxSum = 0; + int currSum = 0; + size_t indx = 0; + + for (size_t i = 0; i < 5; ++i) + currSum += array_get(arr, i); + maxSum = currSum; + + for (size_t i = 5; i < n; ++i) + { + currSum = currSum - array_get(arr, i - 5) + array_get(arr, i); + if (currSum > maxSum) + { + maxSum = currSum; + indx = i - 4; + } + } + + for (size_t i = indx; i < indx + 5; ++i) + { + std::cout << array_get(arr, i) << " "; + } + std::cout << "\n"; +} + +int main(int argc, char **argv) +{ + if (argc < 2) + { + return 1; + } + + std::ifstream input(argv[1]); + if (!input) + { + return 1; + } + + Array* arr = array_create_read_size(input); + task1(arr); + array_delete(arr); + + arr = array_create_and_read(input); + task2(arr); + array_delete(arr); + + return 0; +} diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index 98d8c01fd6..d871c096be 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -1,12 +1,12 @@ -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 ..) -# target_link_libraries(TestListCPP LibraryCPP) -# add_test(TestListCPP TestListCPP) +add_executable(TestListCPP list.cpp) +target_include_directories(TestListCPP PUBLIC ..) +target_link_libraries(TestListCPP LibraryCPP) +add_test(TestListCPP TestListCPP) # add_executable(TestQueueCPP queue.cpp) # target_include_directories(TestQueueCPP PUBLIC ..) @@ -14,10 +14,10 @@ add_test(TestArrayCPP TestArrayCPP) # 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 ..) From 004b95760d9a432b455de53797c0dce8a830bf7a Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Thu, 16 Oct 2025 23:52:03 +0300 Subject: [PATCH 09/36] LAB2 (update 1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Контейнеры реализованы, тест (по примеру) сделан и проходится p.s Добавление дополнительных тестов будет в новом обновлении 😟 --- Lab2CPP/CMakeLists.txt | 6 +- Lab2CPP/lab1.cpp | 122 -------------------------------- Lab2CPP/lab2.cpp | 118 ++++++++++++++++++++++++++++++ LibraryCPP/Tests/CMakeLists.txt | 8 +-- LibraryCPP/list.cpp | 68 ++++++++++++++---- LibraryCPP/list.h | 2 +- LibraryCPP/stack.cpp | 15 +++- 7 files changed, 194 insertions(+), 145 deletions(-) delete mode 100644 Lab2CPP/lab1.cpp create mode 100644 Lab2CPP/lab2.cpp diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt index 6798e756b7..75b9509dd4 100644 --- a/Lab2CPP/CMakeLists.txt +++ b/Lab2CPP/CMakeLists.txt @@ -1,8 +1,8 @@ -add_executable(Lab2CPP lab1.cpp) +add_executable(Lab2CPP lab2.cpp) target_include_directories(Lab2CPP PUBLIC ../LibraryCPP) target_link_libraries(Lab2CPP LibraryCPP) -add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) -set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "2 3 5 7 11.*5 6 7 8 9") +add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt ${CMAKE_CURRENT_SOURCE_DIR}/input_commands.txt) +set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "72 101 108 111") diff --git a/Lab2CPP/lab1.cpp b/Lab2CPP/lab1.cpp deleted file mode 100644 index dc1e9c8e75..0000000000 --- a/Lab2CPP/lab1.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include -#include -#include "array.h" - -Array *array_create_and_read(std::istream& input) -{ - size_t n; - input >> n; - - /* Create array */ - Array *arr = array_create(n); - /* Read array data */ - for (size_t i = 0 ; i < n ; ++i) - { - Data x; - input >> x; - array_set(arr, i, x); - } - return arr; -} - -Array* array_create_read_size(std::istream& input) -{ - size_t n; - input >> n; - return array_create(n); -} - - -bool is_prime(int n) -{ - if (n < 2) return false; - for (int i = 2; i * i <= n; ++i) - if (n % i == 0) - return false; - return true; -} - -/*Read an integer from a file, create an array of this size. -Fill the array with prime numbers, starting with 2. Display the array on the screen.*/ -void task1(Array *arr) -{ - array_set(arr, 0, 2); - int num = 3; - for (size_t i = 1; i < array_size(arr); ++num) - { - if (is_prime(num)) - { - array_set(arr, i, num); - ++i; - } - - } - - - for (size_t i = 0; i < array_size(arr); ++i) - { - std::cout << array_get(arr, i) << " "; - } - std::cout << "\n"; - -} - -/*Read an integer from a file, create an array of this size and -fill it with numbers from the file. Find five neighboring elements, -the sum of whose values is maximum. Display the result on the screen.*/ -void task2(Array *arr) -{ - size_t n = array_size(arr); - if (n < 5) - { - return; - } - - int maxSum = 0; - int currSum = 0; - size_t indx = 0; - - for (size_t i = 0; i < 5; ++i) - currSum += array_get(arr, i); - maxSum = currSum; - - for (size_t i = 5; i < n; ++i) - { - currSum = currSum - array_get(arr, i - 5) + array_get(arr, i); - if (currSum > maxSum) - { - maxSum = currSum; - indx = i - 4; - } - } - - for (size_t i = indx; i < indx + 5; ++i) - { - std::cout << array_get(arr, i) << " "; - } - std::cout << "\n"; -} - -int main(int argc, char **argv) -{ - if (argc < 2) - { - return 1; - } - - std::ifstream input(argv[1]); - if (!input) - { - return 1; - } - - Array* arr = array_create_read_size(input); - task1(arr); - array_delete(arr); - - arr = array_create_and_read(input); - task2(arr); - array_delete(arr); - - return 0; -} diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp new file mode 100644 index 0000000000..ae19c3e6d6 --- /dev/null +++ b/Lab2CPP/lab2.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include "stack.h" + +int command_to_num(const std::string& cmd) { + if (cmd == "add") return 1; + if (cmd == "sub") return 2; + if (cmd == "mul") return 3; + if (cmd == "div") return 4; + if (cmd == "sqrt") return 5; + if (cmd == "sq") return 6; + if (cmd == "get") return 7; + if (cmd == "peek") return 8; + if (cmd == "cond") return 9; + if (cmd == "end") return 10; + if (cmd == "setr") return 11; + if (cmd == "repeat") return 12; + return 0; +} + +void read_commands_stack_from_file(const char* filename, Stack* cmds) { + std::ifstream file(filename); + std::string cmd; + while (file >> cmd) { + int code = command_to_num(cmd); + stack_push(cmds, code); + } +} + +void read_numbers_stack_from_file(const char* filename, Stack* nums) { + Stack* temp = stack_create(); + std::ifstream file(filename); + int num; + + while (file >> num) { + stack_push(temp, num); + } + + while (!stack_empty(temp)) { + Data val = stack_get(temp); + stack_pop(temp); + stack_push(nums, val); + } + stack_delete(temp); +} + +void reverse_stack(Stack* stack) { + Stack* temp = stack_create(); + while (!stack_empty(stack)) { + Data val = stack_get(stack); + stack_pop(stack); + stack_push(temp, val); + } + while (!stack_empty(temp)) { + Data val = stack_get(temp); + stack_pop(temp); + stack_push(stack, val); + } + stack_delete(temp); +} + +int main(int argc, char* argv[]) { + const char* num_file = argc > 1 ? argv[1] : "input.txt"; + const char* cmd_file = argc > 2 ? argv[2] : "input_commands.txt"; + + Stack* nums = stack_create(); + Stack* cmds = stack_create(); + Stack* res = stack_create(); + + read_numbers_stack_from_file(num_file, nums); + read_commands_stack_from_file(cmd_file, cmds); + reverse_stack(cmds); + + while (!stack_empty(cmds)) { + int cmd = stack_get(cmds); + stack_pop(cmds); + + switch (cmd) { + case 8: // peek + + break; + case 11: // setr + if (!stack_empty(nums)) { + Data val = stack_get(nums); + stack_pop(nums); + stack_push(res, val); + } + break; + default: + + break; + } + } + + reverse_stack(res); + + while (!stack_empty(res)) { + Data val = stack_get(res); + stack_pop(res); + std::cout << val << " "; + } + std::cout << std::endl; + + stack_delete(nums); + stack_delete(cmds); + stack_delete(res); + + return 0; +} + + + + + + + + diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index d871c096be..a828ed2212 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -3,10 +3,10 @@ #target_link_libraries(TestArrayCPP LibraryCPP) #add_test(TestArrayCPP TestArrayCPP) -add_executable(TestListCPP list.cpp) -target_include_directories(TestListCPP PUBLIC ..) -target_link_libraries(TestListCPP LibraryCPP) -add_test(TestListCPP TestListCPP) +#add_executable(TestListCPP list.cpp) +#target_include_directories(TestListCPP PUBLIC ..) +#target_link_libraries(TestListCPP LibraryCPP) +#add_test(TestListCPP TestListCPP) # add_executable(TestQueueCPP queue.cpp) # target_include_directories(TestQueueCPP PUBLIC ..) diff --git a/LibraryCPP/list.cpp b/LibraryCPP/list.cpp index a8946b5793..a5fbea953a 100644 --- a/LibraryCPP/list.cpp +++ b/LibraryCPP/list.cpp @@ -3,59 +3,103 @@ struct ListItem { + Data data; + ListItem* next; }; struct List { + ListItem* first; }; List *list_create() { - return new List; + List* list = new List; + list->first = nullptr; + return list; } -void list_delete(List *list) +void list_delete(List* list) { - // TODO: free items + ListItem* curr = list->first; + while (curr != nullptr) + { + ListItem* next = curr->next; + delete curr; + curr = next; + } delete list; } ListItem *list_first(List *list) { - return NULL; + return list->first; } Data list_item_data(const ListItem *item) { - return (Data)0; + return item->data; } ListItem *list_item_next(ListItem *item) { - return NULL; + if (item == nullptr) + return nullptr; + return item->next; } -ListItem *list_item_prev(ListItem *item) +ListItem *list_item_prev(List* list, ListItem *item) { - return NULL; + if (list->first == nullptr || list->first == item) + return nullptr; + + ListItem* current = list->first; + while (current != nullptr && current->next != item) + { + current = current->next; + } + return current; } ListItem *list_insert(List *list, Data data) { - return NULL; + ListItem* new_item = new ListItem; + new_item->data = data; + new_item->next = list->first; + list->first = new_item; + return new_item; } ListItem *list_insert_after(List *list, ListItem *item, Data data) { - return NULL; + if (item == nullptr) + return list_insert(list, data); + + ListItem* new_item = new ListItem; + new_item->data = data; + new_item->next = item->next; + item->next = new_item; + return new_item; } ListItem *list_erase_first(List *list) { - return NULL; + if (list->first == nullptr) + return nullptr; + + ListItem* rm = list->first; + list->first = rm->next; + delete rm; + return list->first; } ListItem *list_erase_next(List *list, ListItem *item) { - return NULL; + return NULL; if (item == nullptr || item->next == nullptr) + return nullptr; + + ListItem* rm = item->next; + item->next = rm->next; + delete rm; + return item->next; } diff --git a/LibraryCPP/list.h b/LibraryCPP/list.h index 2a02d84eb5..71c12d6d6f 100644 --- a/LibraryCPP/list.h +++ b/LibraryCPP/list.h @@ -27,7 +27,7 @@ ListItem *list_item_next(ListItem *item); // Returns previous element for the specified item. // Not applicable for the singly linked lists. -ListItem *list_item_prev(ListItem *item); +ListItem *list_item_prev(List* list, ListItem* item); // Inserts new list item into the beginning ListItem *list_insert(List *list, Data data); diff --git a/LibraryCPP/stack.cpp b/LibraryCPP/stack.cpp index c090abba1c..2d7032f98d 100644 --- a/LibraryCPP/stack.cpp +++ b/LibraryCPP/stack.cpp @@ -1,34 +1,43 @@ #include "stack.h" +#include "list.h" struct Stack { + List* list; }; Stack *stack_create() { - return new Stack; + Stack* stack = new Stack; + stack->list = list_create(); + return stack; } void stack_delete(Stack *stack) { - // TODO: free stack elements + list_delete(stack->list); delete stack; } void stack_push(Stack *stack, Data data) { + list_insert(stack->list, data); } Data stack_get(const Stack *stack) { + ListItem* first = list_first(stack->list); + if (first) + return list_item_data(first); return (Data)0; } void stack_pop(Stack *stack) { + list_erase_first(stack->list); } bool stack_empty(const Stack *stack) { - return true; + return list_first(stack->list) == nullptr; } From 31b1bdde9ecca21c83dd8627816e7433b582d497 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Fri, 17 Oct 2025 14:36:55 +0300 Subject: [PATCH 10/36] LAB2 (update 1.2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Раскомментирован тест list --- LibraryCPP/Tests/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index a828ed2212..d871c096be 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -3,10 +3,10 @@ #target_link_libraries(TestArrayCPP LibraryCPP) #add_test(TestArrayCPP TestArrayCPP) -#add_executable(TestListCPP list.cpp) -#target_include_directories(TestListCPP PUBLIC ..) -#target_link_libraries(TestListCPP LibraryCPP) -#add_test(TestListCPP TestListCPP) +add_executable(TestListCPP list.cpp) +target_include_directories(TestListCPP PUBLIC ..) +target_link_libraries(TestListCPP LibraryCPP) +add_test(TestListCPP TestListCPP) # add_executable(TestQueueCPP queue.cpp) # target_include_directories(TestQueueCPP PUBLIC ..) From 84533b7f9a16f461b8fa813a6e7f89ddc6d915d0 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Sun, 19 Oct 2025 19:57:55 +0300 Subject: [PATCH 11/36] LAB2 (update 2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Новые тесты --- Lab2CPP/CMakeLists.txt | 3 +++ Lab2CPP/input2.txt | 2 ++ Lab2CPP/input_commands2.txt | 2 ++ Lab2CPP/lab2.cpp | 47 ++++++++++++++++++++++++++++++++++++- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 Lab2CPP/input2.txt create mode 100644 Lab2CPP/input_commands2.txt diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt index 75b9509dd4..a08c186a3b 100644 --- a/Lab2CPP/CMakeLists.txt +++ b/Lab2CPP/CMakeLists.txt @@ -5,4 +5,7 @@ target_link_libraries(Lab2CPP LibraryCPP) add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt ${CMAKE_CURRENT_SOURCE_DIR}/input_commands.txt) set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "72 101 108 111") +add_test(NAME TestLab2CPP2 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt ${CMAKE_CURRENT_SOURCE_DIR}/input_commands2.txt) +set_property(TEST TestLab2CPP2 PROPERTY PASS_REGULAR_EXPRESSION "6 5 4 3") + diff --git a/Lab2CPP/input2.txt b/Lab2CPP/input2.txt new file mode 100644 index 0000000000..39270dfee3 --- /dev/null +++ b/Lab2CPP/input2.txt @@ -0,0 +1,2 @@ +1 2 3 4 5 6 7 8 9 10 + diff --git a/Lab2CPP/input_commands2.txt b/Lab2CPP/input_commands2.txt new file mode 100644 index 0000000000..0e4f45bf8a --- /dev/null +++ b/Lab2CPP/input_commands2.txt @@ -0,0 +1,2 @@ +peek setr setr peek peek setr peek setr setr setr + diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index ae19c3e6d6..63a838553d 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -78,7 +78,7 @@ int main(int argc, char* argv[]) { switch (cmd) { case 8: // peek - + break; case 11: // setr if (!stack_empty(nums)) { @@ -106,6 +106,51 @@ int main(int argc, char* argv[]) { stack_delete(cmds); stack_delete(res); + const char* num_file2 = argc > 1 ? argv[1] : "input2.txt"; + const char* cmd_file2 = argc > 2 ? argv[2] : "input_commands2.txt"; + + Stack* nums2 = stack_create(); + Stack* cmds2 = stack_create(); + Stack* res2 = stack_create(); + + read_numbers_stack_from_file(num_file2, nums2); + read_commands_stack_from_file(cmd_file2, cmds2); + reverse_stack(cmds2); + + while (!stack_empty(cmds2)) { + int cmd2 = stack_get(cmds2); + stack_pop(cmds2); + + switch (cmd2) { + case 8: // peek + + break; + case 11: // setr + if (!stack_empty(nums2)) { + Data val2 = stack_get(nums2); + stack_pop(nums2); + stack_push(res2, val2); + } + break; + default: + + break; + } + } + + reverse_stack(res2); + + while (!stack_empty(res2)) { + Data val2 = stack_get(res2); + stack_pop(res2); + std::cout << val2 << " "; + } + std::cout << std::endl; + + stack_delete(nums2); + stack_delete(cmds2); + stack_delete(res2); + return 0; } From 31739b63e3fccfcc7e37e789a2f776de2466b03b Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Tue, 21 Oct 2025 19:55:55 +0300 Subject: [PATCH 12/36] LAB2 (update 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit В файле теперь и числа, и скрипт... Много изменений в lab2.cpp. Реализация всех команд в процессе --- Lab2CPP/CMakeLists.txt | 6 +- Lab2CPP/input.txt | 2 +- Lab2CPP/input2.txt | 2 +- Lab2CPP/input_commands.txt | 2 - Lab2CPP/input_commands2.txt | 2 - Lab2CPP/lab2.cpp | 265 +++++++++++++++++++++--------------- 6 files changed, 159 insertions(+), 120 deletions(-) delete mode 100644 Lab2CPP/input_commands.txt delete mode 100644 Lab2CPP/input_commands2.txt diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt index a08c186a3b..1efe6ea12d 100644 --- a/Lab2CPP/CMakeLists.txt +++ b/Lab2CPP/CMakeLists.txt @@ -2,10 +2,10 @@ add_executable(Lab2CPP lab2.cpp) target_include_directories(Lab2CPP PUBLIC ../LibraryCPP) target_link_libraries(Lab2CPP LibraryCPP) -add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt ${CMAKE_CURRENT_SOURCE_DIR}/input_commands.txt) -set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "72 101 108 111") +add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "72 101 108") add_test(NAME TestLab2CPP2 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt ${CMAKE_CURRENT_SOURCE_DIR}/input_commands2.txt) -set_property(TEST TestLab2CPP2 PROPERTY PASS_REGULAR_EXPRESSION "6 5 4 3") +set_property(TEST TestLab2CPP2 PROPERTY PASS_REGULAR_EXPRESSION "10 9 8") diff --git a/Lab2CPP/input.txt b/Lab2CPP/input.txt index f4c9ff8e92..9cab2b69c5 100644 --- a/Lab2CPP/input.txt +++ b/Lab2CPP/input.txt @@ -1,2 +1,2 @@ -10 33 100 108 114 119 32 44 111 108 101 72 +10 33 100 108 114 119 32 44 111 108 101 72 peek setr peek setr peek peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr diff --git a/Lab2CPP/input2.txt b/Lab2CPP/input2.txt index 39270dfee3..66b220d377 100644 --- a/Lab2CPP/input2.txt +++ b/Lab2CPP/input2.txt @@ -1,2 +1,2 @@ -1 2 3 4 5 6 7 8 9 10 +1 2 3 4 5 6 7 8 9 10 peek setr peek setr peek setr diff --git a/Lab2CPP/input_commands.txt b/Lab2CPP/input_commands.txt deleted file mode 100644 index 2cb9f8b0e6..0000000000 --- a/Lab2CPP/input_commands.txt +++ /dev/null @@ -1,2 +0,0 @@ -peek setr peek setr peek peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr - diff --git a/Lab2CPP/input_commands2.txt b/Lab2CPP/input_commands2.txt deleted file mode 100644 index 0e4f45bf8a..0000000000 --- a/Lab2CPP/input_commands2.txt +++ /dev/null @@ -1,2 +0,0 @@ -peek setr setr peek peek setr peek setr setr setr - diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index 63a838553d..431c8a33cc 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -1,155 +1,196 @@ #include #include +#include #include #include "stack.h" -int command_to_num(const std::string& cmd) { - if (cmd == "add") return 1; - if (cmd == "sub") return 2; - if (cmd == "mul") return 3; - if (cmd == "div") return 4; - if (cmd == "sqrt") return 5; - if (cmd == "sq") return 6; - if (cmd == "get") return 7; - if (cmd == "peek") return 8; - if (cmd == "cond") return 9; - if (cmd == "end") return 10; - if (cmd == "setr") return 11; - if (cmd == "repeat") return 12; - return 0; -} - -void read_commands_stack_from_file(const char* filename, Stack* cmds) { - std::ifstream file(filename); - std::string cmd; - while (file >> cmd) { - int code = command_to_num(cmd); - stack_push(cmds, code); - } +enum Command { + cmd_none = 0, + cmd_add = 1, //done + cmd_sub = 2, //done + cmd_mul = 3, //done + cmd_div = 4, //done + cmd_sqrt = 5, //done + cmd_sq = 6, //done + cmd_get = 7, //done + cmd_peek = 8, //done + cmd_cond = 9, + cmd_end = 10, + cmd_setr = 11, //half done + cmd_repeat = 12 +}; + +Command command_to_num(const std::string& cmd) { + if (cmd == "add") return cmd_add; + if (cmd == "sub") return cmd_sub; + if (cmd == "mul") return cmd_mul; + if (cmd == "div") return cmd_div; + if (cmd == "sqrt") return cmd_sqrt; + if (cmd == "sq") return cmd_sq; + if (cmd == "get") return cmd_get; + if (cmd == "peek") return cmd_peek; + if (cmd == "cond") return cmd_cond; + if (cmd == "end") return cmd_end; + if (cmd == "setr") return cmd_setr; + if (cmd == "repeat") return cmd_repeat; + return cmd_none; } -void read_numbers_stack_from_file(const char* filename, Stack* nums) { - Stack* temp = stack_create(); +void read_numbers_and_commands_from_file(const char* filename, Stack* nums, Stack* cmds) { std::ifstream file(filename); - int num; - - while (file >> num) { - stack_push(temp, num); - } - - while (!stack_empty(temp)) { - Data val = stack_get(temp); - stack_pop(temp); - stack_push(nums, val); + std::string token; + bool reading_commands = false; + + while (file >> token) { + if (!reading_commands) { + try { + int num = std::stoi(token); + stack_push(nums, num); + } + catch (...) { + reading_commands = true; + Command cmd_code = command_to_num(token); + if (cmd_code != cmd_none) { + stack_push(cmds, static_cast(cmd_code)); + } + } + } + else { + Command cmd_code = command_to_num(token); + if (cmd_code != cmd_none) { + stack_push(cmds, static_cast(cmd_code)); + } + } } - stack_delete(temp); } -void reverse_stack(Stack* stack) { +Stack* copy_stack_with_reverse(Stack* original) { Stack* temp = stack_create(); - while (!stack_empty(stack)) { - Data val = stack_get(stack); - stack_pop(stack); - stack_push(temp, val); - } - while (!stack_empty(temp)) { - Data val = stack_get(temp); - stack_pop(temp); - stack_push(stack, val); + while (!stack_empty(original)) { + stack_push(temp, stack_get(original)); + stack_pop(original); } - stack_delete(temp); + return temp; } -int main(int argc, char* argv[]) { - const char* num_file = argc > 1 ? argv[1] : "input.txt"; - const char* cmd_file = argc > 2 ? argv[2] : "input_commands.txt"; - - Stack* nums = stack_create(); - Stack* cmds = stack_create(); - Stack* res = stack_create(); - - read_numbers_stack_from_file(num_file, nums); - read_commands_stack_from_file(cmd_file, cmds); - reverse_stack(cmds); - - while (!stack_empty(cmds)) { - int cmd = stack_get(cmds); - stack_pop(cmds); +void execute_commands(Stack* temp_cmds, Stack* nums) { + while (!stack_empty(temp_cmds)) { + Command cmd = static_cast(stack_get(temp_cmds)); + stack_pop(temp_cmds); switch (cmd) { - case 8: // peek - - break; - case 11: // setr + case cmd_peek: if (!stack_empty(nums)) { Data val = stack_get(nums); + std::cout << val << " "; + } + break; + case cmd_setr: + if (!stack_empty(nums)) { + stack_pop(nums); + } + break; + case cmd_add: + if (!stack_empty(nums)) { + int a = stack_get(nums); + stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, a + b); + } + } + break; + case cmd_sub: + if (!stack_empty(nums)) { + int a = stack_get(nums); + stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, b - a); + } + } + break; + case cmd_mul: + if (!stack_empty(nums)) { + int a = stack_get(nums); + stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, a * b); + } + } + break; + case cmd_div: + if (!stack_empty(nums)) { + int a = stack_get(nums); + stack_pop(nums); + if (!stack_empty(nums) && a != 0) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, b / a); + } + } + break; + case cmd_sqrt: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); - stack_push(res, val); + stack_push(nums, (int)sqrt(a)); } break; + case cmd_sq: + if (!stack_empty(nums)) { + int a = stack_get(nums); + stack_pop(nums); + stack_push(nums, a * a); + } + break; + case cmd_get: + { + int val; + std::cout << "Enter number: "; + std::cin >> val; + stack_push(nums, val); + } + break; default: - break; } } +} - reverse_stack(res); +int main(int argc, char* argv[]) { + const char* input_file = argc > 1 ? argv[1] : "input.txt"; - while (!stack_empty(res)) { - Data val = stack_get(res); - stack_pop(res); - std::cout << val << " "; - } - std::cout << std::endl; + Stack* nums = stack_create(); + Stack* cmds = stack_create(); - stack_delete(nums); - stack_delete(cmds); - stack_delete(res); + read_numbers_and_commands_from_file(input_file, nums, cmds); - const char* num_file2 = argc > 1 ? argv[1] : "input2.txt"; - const char* cmd_file2 = argc > 2 ? argv[2] : "input_commands2.txt"; + Stack* temp_cmds = copy_stack_with_reverse(cmds); - Stack* nums2 = stack_create(); - Stack* cmds2 = stack_create(); - Stack* res2 = stack_create(); + execute_commands(temp_cmds, nums); - read_numbers_stack_from_file(num_file2, nums2); - read_commands_stack_from_file(cmd_file2, cmds2); - reverse_stack(cmds2); + stack_delete(nums); + stack_delete(cmds); + stack_delete(temp_cmds); - while (!stack_empty(cmds2)) { - int cmd2 = stack_get(cmds2); - stack_pop(cmds2); - switch (cmd2) { - case 8: // peek - break; - case 11: // setr - if (!stack_empty(nums2)) { - Data val2 = stack_get(nums2); - stack_pop(nums2); - stack_push(res2, val2); - } - break; - default: + const char* input_file2 = argc > 1 ? argv[1] : "input2.txt"; - break; - } - } + Stack* nums2 = stack_create(); + Stack* cmds2 = stack_create(); - reverse_stack(res2); + read_numbers_and_commands_from_file(input_file2, nums2, cmds2); - while (!stack_empty(res2)) { - Data val2 = stack_get(res2); - stack_pop(res2); - std::cout << val2 << " "; - } - std::cout << std::endl; + Stack* temp_cmds2 = copy_stack_with_reverse(cmds2); + + execute_commands(temp_cmds2, nums2); stack_delete(nums2); stack_delete(cmds2); - stack_delete(res2); + stack_delete(temp_cmds2); + return 0; } @@ -161,3 +202,5 @@ int main(int argc, char* argv[]) { + + From d0128e37259f02dc0fc67978948c80c4582b4fc1 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Wed, 22 Oct 2025 18:12:07 +0300 Subject: [PATCH 13/36] LAB2 (update 4) --- Lab2CPP/CMakeLists.txt | 4 +- Lab2CPP/input3.txt | 2 + Lab2CPP/inputnum.txt | 2 + Lab2CPP/lab2.cpp | 241 ++++++++++++++++++++++++++++------------- 4 files changed, 175 insertions(+), 74 deletions(-) create mode 100644 Lab2CPP/input3.txt create mode 100644 Lab2CPP/inputnum.txt diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt index 1efe6ea12d..2e044d688e 100644 --- a/Lab2CPP/CMakeLists.txt +++ b/Lab2CPP/CMakeLists.txt @@ -5,7 +5,9 @@ target_link_libraries(Lab2CPP LibraryCPP) add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "72 101 108") -add_test(NAME TestLab2CPP2 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt ${CMAKE_CURRENT_SOURCE_DIR}/input_commands2.txt) +add_test(NAME TestLab2CPP2 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt) set_property(TEST TestLab2CPP2 PROPERTY PASS_REGULAR_EXPRESSION "10 9 8") +add_test(NAME TestLab2CPP3 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input3.txt) +set_property(TEST TestLab2CPP3 PROPERTY PASS_REGULAR_EXPRESSION "0") diff --git a/Lab2CPP/input3.txt b/Lab2CPP/input3.txt new file mode 100644 index 0000000000..eddde0050f --- /dev/null +++ b/Lab2CPP/input3.txt @@ -0,0 +1,2 @@ +0 0 div peek + diff --git a/Lab2CPP/inputnum.txt b/Lab2CPP/inputnum.txt new file mode 100644 index 0000000000..77ac542d4f --- /dev/null +++ b/Lab2CPP/inputnum.txt @@ -0,0 +1,2 @@ +0 + diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index 431c8a33cc..691da070a2 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -1,22 +1,25 @@ #include #include -#include +#include #include +#include "array.h" #include "stack.h" +std::ifstream inputnum("inputnum"); + enum Command { cmd_none = 0, - cmd_add = 1, //done - cmd_sub = 2, //done - cmd_mul = 3, //done - cmd_div = 4, //done - cmd_sqrt = 5, //done - cmd_sq = 6, //done - cmd_get = 7, //done - cmd_peek = 8, //done + cmd_add = 1, + cmd_sub = 2, + cmd_mul = 3, + cmd_div = 4, + cmd_sqrt = 5, + cmd_sq = 6, + cmd_get = 7, + cmd_peek = 8, cmd_cond = 9, cmd_end = 10, - cmd_setr = 11, //half done + cmd_setr = 11, cmd_repeat = 12 }; @@ -36,11 +39,14 @@ Command command_to_num(const std::string& cmd) { return cmd_none; } -void read_numbers_and_commands_from_file(const char* filename, Stack* nums, Stack* cmds) { +void read_numbers_and_commands_from_file(const char* filename, Stack* nums, Array* cmds_array, size_t& cmds_count) { std::ifstream file(filename); std::string token; bool reading_commands = false; + const size_t max_cmds = array_size(cmds_array); + cmds_count = 0; + while (file >> token) { if (!reading_commands) { try { @@ -49,34 +55,24 @@ void read_numbers_and_commands_from_file(const char* filename, Stack* nums, Stac } catch (...) { reading_commands = true; - Command cmd_code = command_to_num(token); - if (cmd_code != cmd_none) { - stack_push(cmds, static_cast(cmd_code)); - } } } - else { + if (reading_commands) { Command cmd_code = command_to_num(token); - if (cmd_code != cmd_none) { - stack_push(cmds, static_cast(cmd_code)); + if (cmd_code != cmd_none && cmds_count < max_cmds) { + array_set(cmds_array, cmds_count, static_cast(cmd_code)); + cmds_count++; } } } } -Stack* copy_stack_with_reverse(Stack* original) { - Stack* temp = stack_create(); - while (!stack_empty(original)) { - stack_push(temp, stack_get(original)); - stack_pop(original); - } - return temp; -} +void execute_commands(Array* cmds, size_t count, Stack* nums) { + int loop_var = 0; + size_t i = 0; -void execute_commands(Stack* temp_cmds, Stack* nums) { - while (!stack_empty(temp_cmds)) { - Command cmd = static_cast(stack_get(temp_cmds)); - stack_pop(temp_cmds); + while (i < count) { + Command cmd = static_cast(array_get(cmds, i)); switch (cmd) { case cmd_peek: @@ -84,114 +80,211 @@ void execute_commands(Stack* temp_cmds, Stack* nums) { Data val = stack_get(nums); std::cout << val << " "; } + ++i; break; + case cmd_setr: if (!stack_empty(nums)) { + loop_var = stack_get(nums); stack_pop(nums); } + ++i; break; + case cmd_add: if (!stack_empty(nums)) { - int a = stack_get(nums); + int a = stack_get(nums); stack_pop(nums); if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); + int b = stack_get(nums); + stack_pop(nums); stack_push(nums, a + b); } } + ++i; break; + case cmd_sub: if (!stack_empty(nums)) { - int a = stack_get(nums); + int a = stack_get(nums); stack_pop(nums); if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); + int b = stack_get(nums); + stack_pop(nums); stack_push(nums, b - a); } } + ++i; break; + case cmd_mul: if (!stack_empty(nums)) { - int a = stack_get(nums); + int a = stack_get(nums); stack_pop(nums); if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); + int b = stack_get(nums); + stack_pop(nums); stack_push(nums, a * b); } } + ++i; break; + case cmd_div: if (!stack_empty(nums)) { int a = stack_get(nums); stack_pop(nums); if (!stack_empty(nums) && a != 0) { - int b = stack_get(nums); stack_pop(nums); + int b = stack_get(nums); + stack_pop(nums); stack_push(nums, b / a); } } + ++i; break; + case cmd_sqrt: if (!stack_empty(nums)) { - int a = stack_get(nums); + int a = stack_get(nums); stack_pop(nums); stack_push(nums, (int)sqrt(a)); } + ++i; break; + case cmd_sq: if (!stack_empty(nums)) { - int a = stack_get(nums); + int a = stack_get(nums); stack_pop(nums); stack_push(nums, a * a); } + ++i; break; - case cmd_get: - { + + case cmd_get: { int val; - std::cout << "Enter number: "; - std::cin >> val; - stack_push(nums, val); + if (inputnum >> val) { + stack_push(nums, val); + } + else { + return; + } + ++i; + break; } - break; + + case cmd_cond: { + if (!stack_empty(nums)) { + int a = stack_get(nums); + stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); + stack_pop(nums); + if (a == b) { + ++i; + } + else { + size_t nested = 0; + do { + ++i; + if (i >= count) break; + Command cur = static_cast(array_get(cmds, i)); + if (cur == cmd_cond) nested++; + if (cur == cmd_end) { + if (nested == 0) break; + else nested--; + } + } while (true); + ++i; + } + } + else { + size_t nested = 0; + do { + ++i; + if (i >= count) break; + Command cur = static_cast(array_get(cmds, i)); + if (cur == cmd_cond) nested++; + if (cur == cmd_end) { + if (nested == 0) break; + else nested--; + } + } while (true); + ++i; + } + } + else { + size_t nested = 0; + do { + ++i; + if (i >= count) break; + Command cur = static_cast(array_get(cmds, i)); + if (cur == cmd_cond) nested++; + if (cur == cmd_end) { + if (nested == 0) break; + else nested--; + } + } while (true); + ++i; + } + break; + } + + case cmd_end: + ++i; + break; + + case cmd_repeat: + if (loop_var > 0) { + loop_var--; + ssize_t j = i - 1; + while (j >= 0) { + Command c = static_cast(array_get(cmds, j)); + if (c == cmd_setr) + break; + j--; + } + if (j >= 0) { + i = j + 1; + } + else { + ++i; + } + } + else { + ++i; + } + break; + default: + ++i; break; } } } int main(int argc, char* argv[]) { - const char* input_file = argc > 1 ? argv[1] : "input.txt"; + + const char* input_files[3] = { + argc > 1 ? argv[1] : "input.txt", + argc > 2 ? argv[2] : "input2.txt", + argc > 3 ? argv[3] : "input3.txt" + }; - Stack* nums = stack_create(); - Stack* cmds = stack_create(); + for (int t = 0; t < 3; ++t) { + Stack* nums = stack_create(); + Array* cmds = array_create(64); + size_t cmds_count = 0; - read_numbers_and_commands_from_file(input_file, nums, cmds); + read_numbers_and_commands_from_file(input_files[t], nums, cmds, cmds_count); - Stack* temp_cmds = copy_stack_with_reverse(cmds); - - execute_commands(temp_cmds, nums); - - stack_delete(nums); - stack_delete(cmds); - stack_delete(temp_cmds); - - - - const char* input_file2 = argc > 1 ? argv[1] : "input2.txt"; - - Stack* nums2 = stack_create(); - Stack* cmds2 = stack_create(); - - read_numbers_and_commands_from_file(input_file2, nums2, cmds2); - - Stack* temp_cmds2 = copy_stack_with_reverse(cmds2); - - execute_commands(temp_cmds2, nums2); - - stack_delete(nums2); - stack_delete(cmds2); - stack_delete(temp_cmds2); + execute_commands(cmds, cmds_count, nums); + stack_delete(nums); + array_delete(cmds); + } + inputnum.close(); return 0; } @@ -204,3 +297,5 @@ int main(int argc, char* argv[]) { + + From afb33cb0b228e4fcc9bab2f4fc25046c6b92d894 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Thu, 23 Oct 2025 18:39:42 +0300 Subject: [PATCH 14/36] LAB2 (update 5) Skrillex Feat. Sirah - Bangarang --- Lab2CPP/CMakeLists.txt | 11 ++- Lab2CPP/input2.txt | 3 +- Lab2CPP/input4.txt | 1 + Lab2CPP/inputnum.txt | 2 +- Lab2CPP/lab2.cpp | 170 +++++++++++++++-------------------------- 5 files changed, 72 insertions(+), 115 deletions(-) create mode 100644 Lab2CPP/input4.txt diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt index 2e044d688e..339e3b75cf 100644 --- a/Lab2CPP/CMakeLists.txt +++ b/Lab2CPP/CMakeLists.txt @@ -2,12 +2,15 @@ add_executable(Lab2CPP lab2.cpp) target_include_directories(Lab2CPP PUBLIC ../LibraryCPP) target_link_libraries(Lab2CPP LibraryCPP) -add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +add_test(NAME TestLab2CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt ${CMAKE_CURRENT_SOURCE_DIR}/inputnum.txt) set_property(TEST TestLab2CPP PROPERTY PASS_REGULAR_EXPRESSION "72 101 108") -add_test(NAME TestLab2CPP2 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt) -set_property(TEST TestLab2CPP2 PROPERTY PASS_REGULAR_EXPRESSION "10 9 8") +add_test(NAME TestLab2CPP2 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt ${CMAKE_CURRENT_SOURCE_DIR}/inputnum.txt) +set_property(TEST TestLab2CPP2 PROPERTY PASS_REGULAR_EXPRESSION "5 10") -add_test(NAME TestLab2CPP3 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input3.txt) +add_test(NAME TestLab2CPP3 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input3.txt ${CMAKE_CURRENT_SOURCE_DIR}/inputnum.txt) set_property(TEST TestLab2CPP3 PROPERTY PASS_REGULAR_EXPRESSION "0") +add_test(NAME TestLab2CPP4 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input4.txt ${CMAKE_CURRENT_SOURCE_DIR}/inputnum.txt) +set_property(TEST TestLab2CPP4 PROPERTY PASS_REGULAR_EXPRESSION "10") + diff --git a/Lab2CPP/input2.txt b/Lab2CPP/input2.txt index 66b220d377..5ef23ccd7c 100644 --- a/Lab2CPP/input2.txt +++ b/Lab2CPP/input2.txt @@ -1,2 +1,3 @@ -1 2 3 4 5 6 7 8 9 10 peek setr peek setr peek setr +2 3 add peek sub peek mul peek div peek sqrt peek sq peek get peek cond end setr repeat peek + diff --git a/Lab2CPP/input4.txt b/Lab2CPP/input4.txt new file mode 100644 index 0000000000..c4950d3f0f --- /dev/null +++ b/Lab2CPP/input4.txt @@ -0,0 +1 @@ +10 peek 20 diff --git a/Lab2CPP/inputnum.txt b/Lab2CPP/inputnum.txt index 77ac542d4f..021ea30c0e 100644 --- a/Lab2CPP/inputnum.txt +++ b/Lab2CPP/inputnum.txt @@ -1,2 +1,2 @@ -0 +10 diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index 691da070a2..a3a0609fbd 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -2,11 +2,9 @@ #include #include #include -#include "array.h" +#include #include "stack.h" -std::ifstream inputnum("inputnum"); - enum Command { cmd_none = 0, cmd_add = 1, @@ -39,14 +37,11 @@ Command command_to_num(const std::string& cmd) { return cmd_none; } -void read_numbers_and_commands_from_file(const char* filename, Stack* nums, Array* cmds_array, size_t& cmds_count) { +void read_numbers_and_commands_from_file(const char* filename, Stack* nums, std::vector& cmds) { std::ifstream file(filename); std::string token; bool reading_commands = false; - const size_t max_cmds = array_size(cmds_array); - cmds_count = 0; - while (file >> token) { if (!reading_commands) { try { @@ -59,20 +54,34 @@ void read_numbers_and_commands_from_file(const char* filename, Stack* nums, Arra } if (reading_commands) { Command cmd_code = command_to_num(token); - if (cmd_code != cmd_none && cmds_count < max_cmds) { - array_set(cmds_array, cmds_count, static_cast(cmd_code)); - cmds_count++; + if (cmd_code != cmd_none) { + cmds.push_back(cmd_code); } } } } -void execute_commands(Array* cmds, size_t count, Stack* nums) { +size_t skip_cond_block(const std::vector& cmds, size_t current_index, size_t count) { + size_t nested = 0; + size_t i = current_index + 1; + while (i < count) { + if (cmds[i] == cmd_cond) nested++; + else if (cmds[i] == cmd_end) { + if (nested == 0) break; + nested--; + } + i++; + } + return i + 1; +} + +void execute_commands(const std::vector& cmds, Stack* nums, std::ifstream& inputnum) { int loop_var = 0; size_t i = 0; + size_t count = cmds.size(); while (i < count) { - Command cmd = static_cast(array_get(cmds, i)); + Command cmd = cmds[i]; switch (cmd) { case cmd_peek: @@ -93,11 +102,9 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { case cmd_add: if (!stack_empty(nums)) { - int a = stack_get(nums); - stack_pop(nums); + int a = stack_get(nums); stack_pop(nums); if (!stack_empty(nums)) { - int b = stack_get(nums); - stack_pop(nums); + int b = stack_get(nums); stack_pop(nums); stack_push(nums, a + b); } } @@ -106,11 +113,9 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { case cmd_sub: if (!stack_empty(nums)) { - int a = stack_get(nums); - stack_pop(nums); + int a = stack_get(nums); stack_pop(nums); if (!stack_empty(nums)) { - int b = stack_get(nums); - stack_pop(nums); + int b = stack_get(nums); stack_pop(nums); stack_push(nums, b - a); } } @@ -119,11 +124,9 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { case cmd_mul: if (!stack_empty(nums)) { - int a = stack_get(nums); - stack_pop(nums); + int a = stack_get(nums); stack_pop(nums); if (!stack_empty(nums)) { - int b = stack_get(nums); - stack_pop(nums); + int b = stack_get(nums); stack_pop(nums); stack_push(nums, a * b); } } @@ -132,11 +135,9 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { case cmd_div: if (!stack_empty(nums)) { - int a = stack_get(nums); - stack_pop(nums); + int a = stack_get(nums); stack_pop(nums); if (!stack_empty(nums) && a != 0) { - int b = stack_get(nums); - stack_pop(nums); + int b = stack_get(nums); stack_pop(nums); stack_push(nums, b / a); } } @@ -145,8 +146,7 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { case cmd_sqrt: if (!stack_empty(nums)) { - int a = stack_get(nums); - stack_pop(nums); + int a = stack_get(nums); stack_pop(nums); stack_push(nums, (int)sqrt(a)); } ++i; @@ -154,8 +154,7 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { case cmd_sq: if (!stack_empty(nums)) { - int a = stack_get(nums); - stack_pop(nums); + int a = stack_get(nums); stack_pop(nums); stack_push(nums, a * a); } ++i; @@ -174,59 +173,23 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { } case cmd_cond: { - if (!stack_empty(nums)) { - int a = stack_get(nums); - stack_pop(nums); - if (!stack_empty(nums)) { - int b = stack_get(nums); - stack_pop(nums); - if (a == b) { - ++i; - } - else { - size_t nested = 0; - do { - ++i; - if (i >= count) break; - Command cur = static_cast(array_get(cmds, i)); - if (cur == cmd_cond) nested++; - if (cur == cmd_end) { - if (nested == 0) break; - else nested--; - } - } while (true); - ++i; - } - } - else { - size_t nested = 0; - do { - ++i; - if (i >= count) break; - Command cur = static_cast(array_get(cmds, i)); - if (cur == cmd_cond) nested++; - if (cur == cmd_end) { - if (nested == 0) break; - else nested--; - } - } while (true); - ++i; - } + if (stack_empty(nums)) { + i = skip_cond_block(cmds, i, count); + break; } - else { - size_t nested = 0; - do { - ++i; - if (i >= count) break; - Command cur = static_cast(array_get(cmds, i)); - if (cur == cmd_cond) nested++; - if (cur == cmd_end) { - if (nested == 0) break; - else nested--; - } - } while (true); + int a = stack_get(nums); stack_pop(nums); + if (stack_empty(nums)) { + i = skip_cond_block(cmds, i, count); + break; + } + int b = stack_get(nums); stack_pop(nums); + + if (a == b) { ++i; } + else { + i = skip_cond_block(cmds, i, count); + } break; } @@ -238,18 +201,8 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { if (loop_var > 0) { loop_var--; ssize_t j = i - 1; - while (j >= 0) { - Command c = static_cast(array_get(cmds, j)); - if (c == cmd_setr) - break; - j--; - } - if (j >= 0) { - i = j + 1; - } - else { - ++i; - } + while (j >= 0 && cmds[j] != cmd_setr) j--; + i = (j >= 0) ? j + 1 : i + 1; } else { ++i; @@ -264,27 +217,25 @@ void execute_commands(Array* cmds, size_t count, Stack* nums) { } int main(int argc, char* argv[]) { - - const char* input_files[3] = { - argc > 1 ? argv[1] : "input.txt", - argc > 2 ? argv[2] : "input2.txt", - argc > 3 ? argv[3] : "input3.txt" - }; - for (int t = 0; t < 3; ++t) { - Stack* nums = stack_create(); - Array* cmds = array_create(64); - size_t cmds_count = 0; + if (argc < 3) { + return 1; + } - read_numbers_and_commands_from_file(input_files[t], nums, cmds, cmds_count); + std::ifstream inputnum(argv[2]); + if (!inputnum) { + return 1; + } - execute_commands(cmds, cmds_count, nums); + Stack* nums = stack_create(); + std::vector cmds; - stack_delete(nums); - array_delete(cmds); - } + read_numbers_and_commands_from_file(argv[1], nums, cmds); + execute_commands(cmds, nums, inputnum); + stack_delete(nums); inputnum.close(); + return 0; } @@ -299,3 +250,4 @@ int main(int argc, char* argv[]) { + From eb15b97001331ae2ce9e84f856074ac82ea8d0dd Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Fri, 24 Oct 2025 12:31:50 +0300 Subject: [PATCH 15/36] LAB2 (update 6) No comments --- Lab2CPP/CMakeLists.txt | 4 +- Lab2CPP/input5.txt | 1 + Lab2CPP/lab2.cpp | 414 ++++++++++++++++++++--------------------- 3 files changed, 208 insertions(+), 211 deletions(-) create mode 100644 Lab2CPP/input5.txt diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt index 339e3b75cf..0b0d429dbf 100644 --- a/Lab2CPP/CMakeLists.txt +++ b/Lab2CPP/CMakeLists.txt @@ -12,5 +12,7 @@ add_test(NAME TestLab2CPP3 COMMAND $ ${CMAKE_CURRENT_SOURCE set_property(TEST TestLab2CPP3 PROPERTY PASS_REGULAR_EXPRESSION "0") add_test(NAME TestLab2CPP4 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input4.txt ${CMAKE_CURRENT_SOURCE_DIR}/inputnum.txt) -set_property(TEST TestLab2CPP4 PROPERTY PASS_REGULAR_EXPRESSION "10") +set_property(TEST TestLab2CPP4 PROPERTY PASS_REGULAR_EXPRESSION "20") +add_test(NAME TestLab2CPP5 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input5.txt ${CMAKE_CURRENT_SOURCE_DIR}/inputnum.txt) +set_property(TEST TestLab2CPP5 PROPERTY PASS_REGULAR_EXPRESSION "20 20") \ No newline at end of file diff --git a/Lab2CPP/input5.txt b/Lab2CPP/input5.txt new file mode 100644 index 0000000000..3cd31a8019 --- /dev/null +++ b/Lab2CPP/input5.txt @@ -0,0 +1 @@ +10 peek 20 peek diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index a3a0609fbd..f14c70c9cd 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -6,237 +6,231 @@ #include "stack.h" enum Command { - cmd_none = 0, - cmd_add = 1, - cmd_sub = 2, - cmd_mul = 3, - cmd_div = 4, - cmd_sqrt = 5, - cmd_sq = 6, - cmd_get = 7, - cmd_peek = 8, - cmd_cond = 9, - cmd_end = 10, - cmd_setr = 11, - cmd_repeat = 12 + cmd_none = 0, + cmd_add = 1, + cmd_sub = 2, + cmd_mul = 3, + cmd_div = 4, + cmd_sqrt = 5, + cmd_sq = 6, + cmd_get = 7, + cmd_peek = 8, + cmd_cond = 9, + cmd_end = 10, + cmd_setr = 11, + cmd_repeat = 12 }; Command command_to_num(const std::string& cmd) { - if (cmd == "add") return cmd_add; - if (cmd == "sub") return cmd_sub; - if (cmd == "mul") return cmd_mul; - if (cmd == "div") return cmd_div; - if (cmd == "sqrt") return cmd_sqrt; - if (cmd == "sq") return cmd_sq; - if (cmd == "get") return cmd_get; - if (cmd == "peek") return cmd_peek; - if (cmd == "cond") return cmd_cond; - if (cmd == "end") return cmd_end; - if (cmd == "setr") return cmd_setr; - if (cmd == "repeat") return cmd_repeat; - return cmd_none; + if (cmd == "add") return cmd_add; + if (cmd == "sub") return cmd_sub; + if (cmd == "mul") return cmd_mul; + if (cmd == "div") return cmd_div; + if (cmd == "sqrt") return cmd_sqrt; + if (cmd == "sq") return cmd_sq; + if (cmd == "get") return cmd_get; + if (cmd == "peek") return cmd_peek; + if (cmd == "cond") return cmd_cond; + if (cmd == "end") return cmd_end; + if (cmd == "setr") return cmd_setr; + if (cmd == "repeat") return cmd_repeat; + return cmd_none; } void read_numbers_and_commands_from_file(const char* filename, Stack* nums, std::vector& cmds) { - std::ifstream file(filename); - std::string token; - bool reading_commands = false; - - while (file >> token) { - if (!reading_commands) { - try { - int num = std::stoi(token); - stack_push(nums, num); - } - catch (...) { - reading_commands = true; - } - } - if (reading_commands) { - Command cmd_code = command_to_num(token); - if (cmd_code != cmd_none) { - cmds.push_back(cmd_code); - } - } - } + std::ifstream file(filename); + std::string token; + + while (file >> token) { + Command cmd_code = command_to_num(token); + if (cmd_code != cmd_none) { + cmds.push_back(cmd_code); + } + else { + int num = std::stoi(token); + stack_push(nums, num); + + } + } } + size_t skip_cond_block(const std::vector& cmds, size_t current_index, size_t count) { - size_t nested = 0; - size_t i = current_index + 1; - while (i < count) { - if (cmds[i] == cmd_cond) nested++; - else if (cmds[i] == cmd_end) { - if (nested == 0) break; - nested--; - } - i++; - } - return i + 1; + size_t nested = 0; + size_t i = current_index + 1; + while (i < count) { + if (cmds[i] == cmd_cond) nested++; + else if (cmds[i] == cmd_end) { + if (nested == 0) break; + nested--; + } + i++; + } + return i + 1; } void execute_commands(const std::vector& cmds, Stack* nums, std::ifstream& inputnum) { - int loop_var = 0; - size_t i = 0; - size_t count = cmds.size(); - - while (i < count) { - Command cmd = cmds[i]; - - switch (cmd) { - case cmd_peek: - if (!stack_empty(nums)) { - Data val = stack_get(nums); - std::cout << val << " "; - } - ++i; - break; - - case cmd_setr: - if (!stack_empty(nums)) { - loop_var = stack_get(nums); - stack_pop(nums); - } - ++i; - break; - - case cmd_add: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); - stack_push(nums, a + b); - } - } - ++i; - break; - - case cmd_sub: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); - stack_push(nums, b - a); - } - } - ++i; - break; - - case cmd_mul: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); - stack_push(nums, a * b); - } - } - ++i; - break; - - case cmd_div: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - if (!stack_empty(nums) && a != 0) { - int b = stack_get(nums); stack_pop(nums); - stack_push(nums, b / a); - } - } - ++i; - break; - - case cmd_sqrt: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - stack_push(nums, (int)sqrt(a)); - } - ++i; - break; - - case cmd_sq: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - stack_push(nums, a * a); - } - ++i; - break; - - case cmd_get: { - int val; - if (inputnum >> val) { - stack_push(nums, val); - } - else { - return; - } - ++i; - break; - } - - case cmd_cond: { - if (stack_empty(nums)) { - i = skip_cond_block(cmds, i, count); - break; - } - int a = stack_get(nums); stack_pop(nums); - if (stack_empty(nums)) { - i = skip_cond_block(cmds, i, count); - break; - } - int b = stack_get(nums); stack_pop(nums); - - if (a == b) { - ++i; - } - else { - i = skip_cond_block(cmds, i, count); - } - break; - } - - case cmd_end: - ++i; - break; - - case cmd_repeat: - if (loop_var > 0) { - loop_var--; - ssize_t j = i - 1; - while (j >= 0 && cmds[j] != cmd_setr) j--; - i = (j >= 0) ? j + 1 : i + 1; - } - else { - ++i; - } - break; - - default: - ++i; - break; - } - } + int loop_var = 0; + size_t i = 0; + size_t count = cmds.size(); + + while (i < count) { + Command cmd = cmds[i]; + + switch (cmd) { + case cmd_peek: + if (!stack_empty(nums)) { + Data val = stack_get(nums); + std::cout << val << " "; + } + ++i; + break; + + case cmd_setr: + if (!stack_empty(nums)) { + loop_var = stack_get(nums); + stack_pop(nums); + } + ++i; + break; + + case cmd_add: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, a + b); + } + } + ++i; + break; + + case cmd_sub: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, b - a); + } + } + ++i; + break; + + case cmd_mul: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, a * b); + } + } + ++i; + break; + + case cmd_div: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + if (!stack_empty(nums) && a != 0) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, b / a); + } + } + ++i; + break; + + case cmd_sqrt: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + stack_push(nums, (int)sqrt(a)); + } + ++i; + break; + + case cmd_sq: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + stack_push(nums, a * a); + } + ++i; + break; + + case cmd_get: { + int val; + if (inputnum >> val) { + stack_push(nums, val); + } + else { + return; + } + ++i; + break; + } + + case cmd_cond: { + if (stack_empty(nums)) { + i = skip_cond_block(cmds, i, count); + break; + } + int a = stack_get(nums); stack_pop(nums); + if (stack_empty(nums)) { + i = skip_cond_block(cmds, i, count); + break; + } + int b = stack_get(nums); stack_pop(nums); + + if (a == b) { + ++i; + } + else { + i = skip_cond_block(cmds, i, count); + } + break; + } + + case cmd_end: + ++i; + break; + + case cmd_repeat: + if (loop_var > 0) { + loop_var--; + ssize_t j = i - 1; + while (j >= 0 && cmds[j] != cmd_setr) j--; + i = (j >= 0) ? j + 1 : i + 1; + } + else { + ++i; + } + break; + + default: + ++i; + break; + } + } } int main(int argc, char* argv[]) { - if (argc < 3) { - return 1; - } + if (argc < 3) { + return 1; + } - std::ifstream inputnum(argv[2]); - if (!inputnum) { - return 1; - } + std::ifstream inputnum(argv[2]); + if (!inputnum) { + return 1; + } - Stack* nums = stack_create(); - std::vector cmds; + Stack* nums = stack_create(); + std::vector cmds; - read_numbers_and_commands_from_file(argv[1], nums, cmds); - execute_commands(cmds, nums, inputnum); + read_numbers_and_commands_from_file(argv[1], nums, cmds); + execute_commands(cmds, nums, inputnum); - stack_delete(nums); - inputnum.close(); + stack_delete(nums); + inputnum.close(); - return 0; + return 0; } From fd995891d37cbdc4bce9c137aff76eaece7ad95e Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Tue, 28 Oct 2025 14:31:53 +0300 Subject: [PATCH 16/36] LAB2 (update 7) im tired boss.... --- Lab2CPP/CMakeLists.txt | 4 +- Lab2CPP/lab2.cpp | 419 +++++++++++++++++++++-------------------- 2 files changed, 213 insertions(+), 210 deletions(-) diff --git a/Lab2CPP/CMakeLists.txt b/Lab2CPP/CMakeLists.txt index 0b0d429dbf..64313094bf 100644 --- a/Lab2CPP/CMakeLists.txt +++ b/Lab2CPP/CMakeLists.txt @@ -12,7 +12,7 @@ add_test(NAME TestLab2CPP3 COMMAND $ ${CMAKE_CURRENT_SOURCE set_property(TEST TestLab2CPP3 PROPERTY PASS_REGULAR_EXPRESSION "0") add_test(NAME TestLab2CPP4 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input4.txt ${CMAKE_CURRENT_SOURCE_DIR}/inputnum.txt) -set_property(TEST TestLab2CPP4 PROPERTY PASS_REGULAR_EXPRESSION "20") +set_property(TEST TestLab2CPP4 PROPERTY PASS_REGULAR_EXPRESSION "10") add_test(NAME TestLab2CPP5 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input5.txt ${CMAKE_CURRENT_SOURCE_DIR}/inputnum.txt) -set_property(TEST TestLab2CPP5 PROPERTY PASS_REGULAR_EXPRESSION "20 20") \ No newline at end of file +set_property(TEST TestLab2CPP5 PROPERTY PASS_REGULAR_EXPRESSION "10 20") \ No newline at end of file diff --git a/Lab2CPP/lab2.cpp b/Lab2CPP/lab2.cpp index f14c70c9cd..5552a3092c 100644 --- a/Lab2CPP/lab2.cpp +++ b/Lab2CPP/lab2.cpp @@ -6,231 +6,233 @@ #include "stack.h" enum Command { - cmd_none = 0, - cmd_add = 1, - cmd_sub = 2, - cmd_mul = 3, - cmd_div = 4, - cmd_sqrt = 5, - cmd_sq = 6, - cmd_get = 7, - cmd_peek = 8, - cmd_cond = 9, - cmd_end = 10, - cmd_setr = 11, - cmd_repeat = 12 + cmd_none = 0, + cmd_add = 1, + cmd_sub = 2, + cmd_mul = 3, + cmd_div = 4, + cmd_sqrt = 5, + cmd_sq = 6, + cmd_get = 7, + cmd_peek = 8, + cmd_cond = 9, + cmd_end = 10, + cmd_setr = 11, + cmd_repeat = 12, + cmd_push = 13 +}; + +struct CommandItem { + Command cmd; + int value; }; Command command_to_num(const std::string& cmd) { - if (cmd == "add") return cmd_add; - if (cmd == "sub") return cmd_sub; - if (cmd == "mul") return cmd_mul; - if (cmd == "div") return cmd_div; - if (cmd == "sqrt") return cmd_sqrt; - if (cmd == "sq") return cmd_sq; - if (cmd == "get") return cmd_get; - if (cmd == "peek") return cmd_peek; - if (cmd == "cond") return cmd_cond; - if (cmd == "end") return cmd_end; - if (cmd == "setr") return cmd_setr; - if (cmd == "repeat") return cmd_repeat; - return cmd_none; + if (cmd == "add") return cmd_add; + if (cmd == "sub") return cmd_sub; + if (cmd == "mul") return cmd_mul; + if (cmd == "div") return cmd_div; + if (cmd == "sqrt") return cmd_sqrt; + if (cmd == "sq") return cmd_sq; + if (cmd == "get") return cmd_get; + if (cmd == "peek") return cmd_peek; + if (cmd == "cond") return cmd_cond; + if (cmd == "end") return cmd_end; + if (cmd == "setr") return cmd_setr; + if (cmd == "repeat") return cmd_repeat; + return cmd_none; } -void read_numbers_and_commands_from_file(const char* filename, Stack* nums, std::vector& cmds) { - std::ifstream file(filename); - std::string token; - - while (file >> token) { - Command cmd_code = command_to_num(token); - if (cmd_code != cmd_none) { - cmds.push_back(cmd_code); - } - else { - int num = std::stoi(token); - stack_push(nums, num); - - } - } +void read_numbers_and_commands_from_file(const char* filename, std::vector& cmds) { + std::ifstream file(filename); + std::string token; + + while (file >> token) { + Command cmd_code = command_to_num(token); + if (cmd_code != cmd_none) { + cmds.push_back({ cmd_code, 0 }); + } + else { + int num = std::stoi(token); + cmds.push_back({ cmd_push, num }); + } + } } - -size_t skip_cond_block(const std::vector& cmds, size_t current_index, size_t count) { - size_t nested = 0; - size_t i = current_index + 1; - while (i < count) { - if (cmds[i] == cmd_cond) nested++; - else if (cmds[i] == cmd_end) { - if (nested == 0) break; - nested--; - } - i++; - } - return i + 1; +size_t skip_cond_block(const std::vector& cmds, size_t current_index, size_t count) { + size_t nested = 0; + size_t i = current_index + 1; + while (i < count) { + if (cmds[i].cmd == cmd_cond) nested++; + else if (cmds[i].cmd == cmd_end) { + if (nested == 0) break; + nested--; + } + i++; + } + return i + 1; } -void execute_commands(const std::vector& cmds, Stack* nums, std::ifstream& inputnum) { - int loop_var = 0; - size_t i = 0; - size_t count = cmds.size(); - - while (i < count) { - Command cmd = cmds[i]; - - switch (cmd) { - case cmd_peek: - if (!stack_empty(nums)) { - Data val = stack_get(nums); - std::cout << val << " "; - } - ++i; - break; - - case cmd_setr: - if (!stack_empty(nums)) { - loop_var = stack_get(nums); - stack_pop(nums); - } - ++i; - break; - - case cmd_add: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); - stack_push(nums, a + b); - } - } - ++i; - break; - - case cmd_sub: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); - stack_push(nums, b - a); - } - } - ++i; - break; - - case cmd_mul: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - if (!stack_empty(nums)) { - int b = stack_get(nums); stack_pop(nums); - stack_push(nums, a * b); - } - } - ++i; - break; - - case cmd_div: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - if (!stack_empty(nums) && a != 0) { - int b = stack_get(nums); stack_pop(nums); - stack_push(nums, b / a); - } - } - ++i; - break; - - case cmd_sqrt: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - stack_push(nums, (int)sqrt(a)); - } - ++i; - break; - - case cmd_sq: - if (!stack_empty(nums)) { - int a = stack_get(nums); stack_pop(nums); - stack_push(nums, a * a); - } - ++i; - break; - - case cmd_get: { - int val; - if (inputnum >> val) { - stack_push(nums, val); - } - else { - return; - } - ++i; - break; - } - - case cmd_cond: { - if (stack_empty(nums)) { - i = skip_cond_block(cmds, i, count); - break; - } - int a = stack_get(nums); stack_pop(nums); - if (stack_empty(nums)) { - i = skip_cond_block(cmds, i, count); - break; - } - int b = stack_get(nums); stack_pop(nums); - - if (a == b) { - ++i; - } - else { - i = skip_cond_block(cmds, i, count); - } - break; - } - - case cmd_end: - ++i; - break; - - case cmd_repeat: - if (loop_var > 0) { - loop_var--; - ssize_t j = i - 1; - while (j >= 0 && cmds[j] != cmd_setr) j--; - i = (j >= 0) ? j + 1 : i + 1; - } - else { - ++i; - } - break; - - default: - ++i; - break; - } - } +void execute_commands(const std::vector& cmds, Stack* nums, std::ifstream& inputnum) { + int loop_var = 0; + size_t i = 0; + size_t count = cmds.size(); + + while (i < count) { + CommandItem cmdItem = cmds[i]; + switch (cmdItem.cmd) { + + case cmd_push: + stack_push(nums, cmdItem.value); + ++i; + break; + + case cmd_peek: + if (!stack_empty(nums)) { + Data val = stack_get(nums); + std::cout << val << " "; + } + ++i; + break; + + case cmd_setr: + if (!stack_empty(nums)) { + loop_var = stack_get(nums); + stack_pop(nums); + } + ++i; + break; + + case cmd_add: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, a + b); + } + } + ++i; + break; + + case cmd_sub: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, b - a); + } + } + ++i; + break; + + case cmd_mul: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + if (!stack_empty(nums)) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, a * b); + } + } + ++i; + break; + + case cmd_div: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + if (!stack_empty(nums) && a != 0) { + int b = stack_get(nums); stack_pop(nums); + stack_push(nums, b / a); + } + } + ++i; + break; + + case cmd_sqrt: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + stack_push(nums, (int)sqrt(a)); + } + ++i; + break; + + case cmd_sq: + if (!stack_empty(nums)) { + int a = stack_get(nums); stack_pop(nums); + stack_push(nums, a * a); + } + ++i; + break; + + case cmd_get: { + int val; + if (inputnum >> val) { + stack_push(nums, val); + } + else { + return; + } + ++i; + break; + } + + case cmd_cond: { + if (stack_empty(nums)) { + i = skip_cond_block(cmds, i, count); + break; + } + int a = stack_get(nums); stack_pop(nums); + if (stack_empty(nums)) { + i = skip_cond_block(cmds, i, count); + break; + } + int b = stack_get(nums); stack_pop(nums); + + if (a == b) { + ++i; + } + else { + i = skip_cond_block(cmds, i, count); + } + break; + } + + case cmd_end: + ++i; + break; + + case cmd_repeat: + if (loop_var > 0) { + loop_var--; + ssize_t j = i - 1; + while (j >= 0 && cmds[j].cmd != cmd_setr) j--; + i = (j >= 0) ? j + 1 : i + 1; + } + else { + ++i; + } + break; + + default: + ++i; + break; + } + } } int main(int argc, char* argv[]) { - if (argc < 3) { - return 1; - } + std::ifstream inputnum(argv[2]); - std::ifstream inputnum(argv[2]); - if (!inputnum) { - return 1; - } + Stack* nums = stack_create(); + std::vector cmds; - Stack* nums = stack_create(); - std::vector cmds; + read_numbers_and_commands_from_file(argv[1], cmds); + execute_commands(cmds, nums, inputnum); - read_numbers_and_commands_from_file(argv[1], nums, cmds); - execute_commands(cmds, nums, inputnum); + stack_delete(nums); + inputnum.close(); - stack_delete(nums); - inputnum.close(); - - return 0; + return 0; } @@ -245,3 +247,4 @@ int main(int argc, char* argv[]) { + From 9271cfc846b4d0957c139892f7be85c3c7a1e44b Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Wed, 29 Oct 2025 17:35:24 +0300 Subject: [PATCH 17/36] LAB2 (update 8) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлен тест ЧИСТО для get --- Lab2CPP/input4.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab2CPP/input4.txt b/Lab2CPP/input4.txt index c4950d3f0f..60b4bf67ec 100644 --- a/Lab2CPP/input4.txt +++ b/Lab2CPP/input4.txt @@ -1 +1 @@ -10 peek 20 +get peek From 22d925441ec4ceb6e3ad6e8f88dc4f2f22711fa6 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Wed, 12 Nov 2025 20:26:37 +0300 Subject: [PATCH 18/36] LAB3 (update 1) --- CMakeLists.txt | 9 ++- Lab3CPP/CMakeLists.txt | 7 ++ Lab3CPP/input.txt | 8 ++ Lab3CPP/lab3.cpp | 130 ++++++++++++++++++++++++++++++++ LibraryCPP/Tests/CMakeLists.txt | 36 ++++----- LibraryCPP/queue.cpp | 42 ++++++++++- LibraryCPP/vector.cpp | 42 +++++++++-- 7 files changed, 243 insertions(+), 31 deletions(-) create mode 100644 Lab3CPP/CMakeLists.txt create mode 100644 Lab3CPP/input.txt create mode 100644 Lab3CPP/lab3.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a5ee7332cf..83719b61f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,11 +10,12 @@ 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) -add_subdirectory(Lab2CPP) +#add_subdirectory(Lab2CPP) +add_subdirectory(Lab3CPP) diff --git a/Lab3CPP/CMakeLists.txt b/Lab3CPP/CMakeLists.txt new file mode 100644 index 0000000000..4e591e9784 --- /dev/null +++ b/Lab3CPP/CMakeLists.txt @@ -0,0 +1,7 @@ +add_executable(Lab3CPP lab3.cpp) +target_include_directories(Lab3CPP PUBLIC ../LibraryCPP) +target_link_libraries(Lab3CPP LibraryCPP) + +add_test(NAME TestLab3CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +set_property(TEST TestLab3CPP PROPERTY PASS_REGULAR_EXPRESSION "0\\.######") + diff --git a/Lab3CPP/input.txt b/Lab3CPP/input.txt new file mode 100644 index 0000000000..af91529241 --- /dev/null +++ b/Lab3CPP/input.txt @@ -0,0 +1,8 @@ +K.###### +..#E.... +..#..... +..#.###. +..#..... +.....### +.#####.. +##...... \ No newline at end of file diff --git a/Lab3CPP/lab3.cpp b/Lab3CPP/lab3.cpp new file mode 100644 index 0000000000..3cedfbcca9 --- /dev/null +++ b/Lab3CPP/lab3.cpp @@ -0,0 +1,130 @@ +#include "queue.h" +#include "vector.h" +#include +#include +#include +#include + +using namespace std; + +int encode(int row, int col, int cols) { + return row * cols + col; +} + +void decode(int code, int cols, int& row, int& col) { + row = code / cols; + col = code % cols; +} + +int main(int argc, char* argv[]) { + if (argc < 2) { + return 1; + } + + ifstream fin(argv[1]); + if (!fin) { + return 1; + } + + vector board; + string line; + while (getline(fin, line)) { + board.push_back(line); + } + fin.close(); + + int rows = (int)board.size(); + int cols = rows > 0 ? (int)board[0].size() : 0; + + int start = -1; + int end = -1; + + for (int r = 0; r < rows; ++r) { + for (int c = 0; c < cols; ++c) { + if (board[r][c] == 'K') start = encode(r, c, cols); + if (board[r][c] == 'E') end = encode(r, c, cols); + } + } + if (start == -1 || end == -1) { + return 1; + } + + const int dr[8] = { -2,-1,1,2,2,1,-1,-2 }; + const int dc[8] = { 1,2,2,1,-1,-2,-2,-1 }; + + vector> dist(rows, vector(cols, -1)); + vector> parent(rows, vector(cols, -1)); + + int sr, sc; + decode(start, cols, sr, sc); + dist[sr][sc] = 0; + + Queue* queue = queue_create(); + queue_insert(queue, start); + + while (!queue_empty(queue)) { + int cur = queue_get(queue); + queue_remove(queue); + + if (cur == end) break; + + int r, c; + decode(cur, cols, r, c); + + for (int i = 0; i < 8; ++i) { + int nr = r + dr[i]; + int nc = c + dc[i]; + if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && + board[nr][nc] != '#' && dist[nr][nc] == -1) { + + dist[nr][nc] = dist[r][c] + 1; + parent[nr][nc] = cur; + queue_insert(queue, encode(nr, nc, cols)); + } + } + } + + queue_delete(queue); + + vector output = board; + + if (dist[end / cols][end % cols] == -1) { + + } + else { + int cur = end; + while (cur != -1) { + int r, c; + decode(cur, cols, r, c); + int step = dist[r][c]; + output[r][c] = '0' + (step % 10); + cur = parent[r][c]; + } + } + + for (const auto& row : output) { + cout << row << '\n'; + } + + return 0; +} + + + + + + + + + + + + + + + + + + + + diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index d871c096be..b06a60b4be 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -3,24 +3,24 @@ #target_link_libraries(TestArrayCPP LibraryCPP) #add_test(TestArrayCPP TestArrayCPP) -add_executable(TestListCPP list.cpp) -target_include_directories(TestListCPP PUBLIC ..) -target_link_libraries(TestListCPP LibraryCPP) -add_test(TestListCPP TestListCPP) +#add_executable(TestListCPP list.cpp) +#target_include_directories(TestListCPP PUBLIC ..) +#target_link_libraries(TestListCPP LibraryCPP) +#add_test(TestListCPP TestListCPP) -# add_executable(TestQueueCPP queue.cpp) -# target_include_directories(TestQueueCPP PUBLIC ..) -# target_link_libraries(TestQueueCPP LibraryCPP) -# add_test(TestQueueCPP TestQueueCPP) -# set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10) +add_executable(TestQueueCPP queue.cpp) +target_include_directories(TestQueueCPP PUBLIC ..) +target_link_libraries(TestQueueCPP LibraryCPP) +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/queue.cpp b/LibraryCPP/queue.cpp index a9b4730488..9b8e5e8c6d 100644 --- a/LibraryCPP/queue.cpp +++ b/LibraryCPP/queue.cpp @@ -1,34 +1,68 @@ #include "queue.h" +#include "vector.h" +#include struct Queue { + Vector* vector; + size_t head; + size_t tail; + size_t count; }; Queue *queue_create() { - return new Queue; + Queue* queue = new Queue; + queue->vector = vector_create(); + vector_resize(queue->vector, 4); + queue->head = 0; + queue->tail = 0; + queue->count = 0; + return queue; } void queue_delete(Queue *queue) { - // TODO: free queue items + vector_delete(queue->vector); delete queue; } void queue_insert(Queue *queue, Data data) { + if (queue->count == vector_size(queue->vector)) { + size_t old_size = vector_size(queue->vector); + vector_resize(queue->vector, old_size * 2); + + for (size_t i = 0; i < queue->count; ++i) { + size_t from_idx = (queue->head + i) % old_size; + vector_set(queue->vector, i, vector_get(queue->vector, from_idx)); + } + + queue->head = 0; + queue->tail = queue->count; + } + + vector_set(queue->vector, queue->tail, data); + queue->tail = (queue->tail + 1) % vector_size(queue->vector); + queue->count++; } Data queue_get(const Queue *queue) { - return (Data)0; + if (queue_empty(queue)) + throw std::out_of_range("Queue is empty"); + return vector_get(queue->vector, queue->head); } void queue_remove(Queue *queue) { + if (queue_empty(queue)) + throw std::out_of_range("Queue is empty"); + queue->head = (queue->head + 1) % vector_size(queue->vector); + queue->count--; } bool queue_empty(const Queue *queue) { - return true; + return queue->count == 0; } diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index aee7157b54..bfedddfbe3 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -1,34 +1,66 @@ #include "vector.h" +#include struct Vector { + Data* data; + size_t size; + size_t capacity; }; Vector *vector_create() { - return new Vector; + Vector* vec = new Vector; + vec->data = nullptr; + vec->size = 0; + vec->capacity = 0; + return vec; } void vector_delete(Vector *vector) { - // TODO: free vector internals - delete vector; + delete[] vector->data; + 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->data[index]; } void vector_set(Vector *vector, size_t index, Data value) { + if (index >= vector->size) { + throw std::out_of_range("Index out of range"); + } + vector->data[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) { + vector->size = size; + } + else { + size_t new_capacity = vector->capacity ? vector->capacity * 2 : 1; + while (new_capacity < size) { + new_capacity *= 2; + } + Data* new_data = new Data[new_capacity]; + for (size_t i = 0; i < vector->size; ++i) { + new_data[i] = vector->data[i]; + } + delete[] vector->data; + vector->data = new_data; + vector->capacity = new_capacity; + vector->size = size; + } } From 6fc1b47d29ab96ab8b43cc4630dc7bfa39da45a0 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Thu, 13 Nov 2025 21:27:42 +0300 Subject: [PATCH 19/36] LAB3 (update 2) --- Lab3CPP/CMakeLists.txt | 5 ++++- Lab3CPP/input2.txt | 8 ++++++++ Lab3CPP/lab3.cpp | 9 ++++++--- LibraryCPP/queue.cpp | 18 +++++++++--------- 4 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 Lab3CPP/input2.txt diff --git a/Lab3CPP/CMakeLists.txt b/Lab3CPP/CMakeLists.txt index 4e591e9784..34859d523a 100644 --- a/Lab3CPP/CMakeLists.txt +++ b/Lab3CPP/CMakeLists.txt @@ -3,5 +3,8 @@ target_include_directories(Lab3CPP PUBLIC ../LibraryCPP) target_link_libraries(Lab3CPP LibraryCPP) add_test(NAME TestLab3CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) -set_property(TEST TestLab3CPP PROPERTY PASS_REGULAR_EXPRESSION "0\\.######") +set_property(TEST TestLab3CPP PROPERTY PASS_REGULAR_EXPRESSION "\\.\\.\\#2\\.\\.\\.") + +add_test(NAME TestLab3CPP2 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt) +set_property(TEST TestLab3CPP2 PROPERTY PASS_REGULAR_EXPRESSION "K\\.\\######") diff --git a/Lab3CPP/input2.txt b/Lab3CPP/input2.txt new file mode 100644 index 0000000000..6a1a64882a --- /dev/null +++ b/Lab3CPP/input2.txt @@ -0,0 +1,8 @@ +K.###### +..##.... +.###.... +..#####. +####.... +.....### +.#####.. +##.....E diff --git a/Lab3CPP/lab3.cpp b/Lab3CPP/lab3.cpp index 3cedfbcca9..4a7f52b1c7 100644 --- a/Lab3CPP/lab3.cpp +++ b/Lab3CPP/lab3.cpp @@ -88,8 +88,10 @@ int main(int argc, char* argv[]) { vector output = board; - if (dist[end / cols][end % cols] == -1) { - + int er, ec; + decode(end, cols, er, ec); + if (dist[er][ec] == -1) { + } else { int cur = end; @@ -97,7 +99,7 @@ int main(int argc, char* argv[]) { int r, c; decode(cur, cols, r, c); int step = dist[r][c]; - output[r][c] = '0' + (step % 10); + output[r][c] = '0' + (step % 10); cur = parent[r][c]; } } @@ -127,4 +129,5 @@ int main(int argc, char* argv[]) { + diff --git a/LibraryCPP/queue.cpp b/LibraryCPP/queue.cpp index 9b8e5e8c6d..469eacba73 100644 --- a/LibraryCPP/queue.cpp +++ b/LibraryCPP/queue.cpp @@ -5,8 +5,7 @@ struct Queue { Vector* vector; - size_t head; - size_t tail; + size_t head; size_t count; }; @@ -16,7 +15,6 @@ Queue *queue_create() queue->vector = vector_create(); vector_resize(queue->vector, 4); queue->head = 0; - queue->tail = 0; queue->count = 0; return queue; } @@ -29,21 +27,23 @@ void queue_delete(Queue *queue) void queue_insert(Queue *queue, Data data) { - if (queue->count == vector_size(queue->vector)) { - size_t old_size = vector_size(queue->vector); + size_t capacity = vector_size(queue->vector); + + if (queue->count == capacity) { + size_t old_size = capacity; vector_resize(queue->vector, old_size * 2); for (size_t i = 0; i < queue->count; ++i) { size_t from_idx = (queue->head + i) % old_size; - vector_set(queue->vector, i, vector_get(queue->vector, from_idx)); + Data val = vector_get(queue->vector, from_idx); + vector_set(queue->vector, i, val); } queue->head = 0; - queue->tail = queue->count; } - vector_set(queue->vector, queue->tail, data); - queue->tail = (queue->tail + 1) % vector_size(queue->vector); + size_t tail = (queue->head + queue->count) % vector_size(queue->vector); + vector_set(queue->vector, tail, data); queue->count++; } From a6750e973cf21c1f53dbc89219c35b903da020d4 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Sun, 16 Nov 2025 18:06:23 +0300 Subject: [PATCH 20/36] LAB3 (update 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit пупупу..... --- Lab3CPP/lab3.cpp | 11 +++++------ LibraryCPP/queue.cpp | 8 +++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Lab3CPP/lab3.cpp b/Lab3CPP/lab3.cpp index 4a7f52b1c7..41947b1323 100644 --- a/Lab3CPP/lab3.cpp +++ b/Lab3CPP/lab3.cpp @@ -90,16 +90,15 @@ int main(int argc, char* argv[]) { int er, ec; decode(end, cols, er, ec); - if (dist[er][ec] == -1) { - - } - else { + + if (dist[er][ec] != -1) { int cur = end; while (cur != -1) { int r, c; decode(cur, cols, r, c); - int step = dist[r][c]; - output[r][c] = '0' + (step % 10); + + output[r][c] = '0' + (dist[r][c] % 10); + cur = parent[r][c]; } } diff --git a/LibraryCPP/queue.cpp b/LibraryCPP/queue.cpp index 469eacba73..62c3252918 100644 --- a/LibraryCPP/queue.cpp +++ b/LibraryCPP/queue.cpp @@ -25,7 +25,7 @@ void queue_delete(Queue *queue) delete queue; } -void queue_insert(Queue *queue, Data data) +void queue_insert(Queue* queue, Data data) { size_t capacity = vector_size(queue->vector); @@ -39,14 +39,16 @@ void queue_insert(Queue *queue, Data data) vector_set(queue->vector, i, val); } - queue->head = 0; + queue->head = 0; } - size_t tail = (queue->head + queue->count) % vector_size(queue->vector); + size_t capacity_after = vector_size(queue->vector); + size_t tail = (queue->head + queue->count) % capacity_after; vector_set(queue->vector, tail, data); queue->count++; } + Data queue_get(const Queue *queue) { if (queue_empty(queue)) From eab485034abca4a92507332da8353f2c8e6a3abc Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Mon, 17 Nov 2025 11:59:25 +0300 Subject: [PATCH 21/36] LAB2 (p.s 1) --- LibraryCPP/list.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LibraryCPP/list.cpp b/LibraryCPP/list.cpp index a5fbea953a..8cb926849f 100644 --- a/LibraryCPP/list.cpp +++ b/LibraryCPP/list.cpp @@ -95,7 +95,7 @@ ListItem *list_erase_first(List *list) ListItem *list_erase_next(List *list, ListItem *item) { - return NULL; if (item == nullptr || item->next == nullptr) + if (item == nullptr || item->next == nullptr) return nullptr; ListItem* rm = item->next; From 0e56ac4788aca9cfa52ea5a9cfd9fba6ff3e0724 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Mon, 17 Nov 2025 22:20:16 +0300 Subject: [PATCH 22/36] LAB3 (update 4) --- LibraryCPP/queue.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/LibraryCPP/queue.cpp b/LibraryCPP/queue.cpp index 62c3252918..e5f8558d7f 100644 --- a/LibraryCPP/queue.cpp +++ b/LibraryCPP/queue.cpp @@ -29,22 +29,26 @@ void queue_insert(Queue* queue, Data data) { size_t capacity = vector_size(queue->vector); - if (queue->count == capacity) { + if (queue->count >= capacity) { size_t old_size = capacity; - vector_resize(queue->vector, old_size * 2); + size_t new_size = old_size * 2; + + Vector* new_vector = vector_create(); + vector_resize(new_vector, new_size); for (size_t i = 0; i < queue->count; ++i) { size_t from_idx = (queue->head + i) % old_size; Data val = vector_get(queue->vector, from_idx); - vector_set(queue->vector, i, val); + vector_set(new_vector, i, val); } - queue->head = 0; + vector_delete(queue->vector); + queue->vector = new_vector; + queue->head = 0; } - size_t capacity_after = vector_size(queue->vector); - size_t tail = (queue->head + queue->count) % capacity_after; - vector_set(queue->vector, tail, data); + size_t insert_pos = (queue->head + queue->count) % vector_size(queue->vector); + vector_set(queue->vector, insert_pos, data); queue->count++; } From 461d516253a0374794f7cbd0d0b54e9927511fd5 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Wed, 26 Nov 2025 00:39:58 +0300 Subject: [PATCH 23/36] LAB4 (update 0) --- LibraryCPPTemplate/dirgraph.h | 195 ++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 LibraryCPPTemplate/dirgraph.h diff --git a/LibraryCPPTemplate/dirgraph.h b/LibraryCPPTemplate/dirgraph.h new file mode 100644 index 0000000000..9b42df3161 --- /dev/null +++ b/LibraryCPPTemplate/dirgraph.h @@ -0,0 +1,195 @@ +#ifndef DIRGRAPH_TEMPLATE_H +#define DIRGRAPH_TEMPLATE_H + +#include +#include + +template +class DirGraph { +private: + struct Edge { + size_t to; + edge label; + + Edge(size_t t, const edge& l = edge()) : to(t), label(l) {} + }; + + std::vector vertexMarks; + std::vector> edges; + + void checkVertex(size_t v) const { + if (v >= vertexMarks.size()) + throw std::out_of_range("Invalid vertex id"); + } + +public: + typedef size_t VertexId; + + // Constructor. Creates graph with specified number of vertices + DirGraph(size_t vertexCount = 0) { + vertexMarks.resize(vertexCount); + edges.resize(vertexCount); + } + + // Add new vertex to the graph + VertexId addVertex(const vertex& mark = vertex()) { + VertexId id = vertexMarks.size(); + vertexMarks.push_back(mark); + edges.push_back(std::vector()); + return id; + } + + // Remove vertex from the graph + void removeVertex(VertexId v) { + checkVertex(v); + + vertexMarks.erase(vertexMarks.begin() + v); + edges.erase(edges.begin() + v); + + for (size_t i = 0; i < edges.size(); ++i) { + auto& neighbors = edges[i]; + for (auto it = neighbors.begin(); it != neighbors.end(); ) { + if (it->to == v) { + it = neighbors.erase(it); + } + else { + if (it->to > v) { + it->to--; + } + ++it; + } + } + } + } + + // Add edge between two vertices + bool addEdge(VertexId from, VertexId to, const edge& mark = edge()) { + checkVertex(from); + checkVertex(to); + + for (const auto& e : edges[from]) { + if (e.to == to) { + return false; + } + } + + edges[from].push_back(Edge(to, mark)); + return true; + } + + // Remove edge between two vertices + void removeEdge(VertexId from, VertexId to) { + checkVertex(from); + checkVertex(to); + + auto& neighbors = edges[from]; + for (auto it = neighbors.begin(); it != neighbors.end(); ++it) { + if (it->to == to) { + neighbors.erase(it); + return; + } + } + } + + // Check if edge exists between two vertices + bool edgeExists(VertexId from, VertexId to) const { + checkVertex(from); + checkVertex(to); + + for (const auto& e : edges[from]) { + if (e.to == to) { + return true; + } + } + return false; + } + + // Set label for edge + void setEdgeMark(VertexId from, VertexId to, const edge& mark) { + checkVertex(from); + checkVertex(to); + + for (auto& e : edges[from]) { + if (e.to == to) { + e.label = mark; + return; + } + } + throw std::out_of_range("Edge does not exist"); + } + + // Get label of edge + edge getEdgeMark(VertexId from, VertexId to) const { + checkVertex(from); + checkVertex(to); + + for (const auto& e : edges[from]) { + if (e.to == to) { + return e.label; + } + } + throw std::out_of_range("Edge does not exist"); + } + + // Set label for vertex + void setVertexMark(VertexId v, const vertex& mark) { + checkVertex(v); + vertexMarks[v] = mark; + } + + // Get label of vertex + vertex getVertexMark(VertexId v) const { + checkVertex(v); + return vertexMarks[v]; + } + + // Get labels of all vertices + std::vector getAllVertexMarks() const { + return vertexMarks; + } + + // Get total number of vertices + size_t getVertexCount() const { + return vertexMarks.size(); + } + + // Iterator for neighbors of a vertex + class NeighborIterator { + private: + typename std::vector::const_iterator current; + typename std::vector::const_iterator end; + + public: + NeighborIterator(typename std::vector::const_iterator begin, + typename std::vector::const_iterator end) + : current(begin), end(end) { + } + + bool operator!=(const NeighborIterator& other) const { + return current != other.current; + } + + NeighborIterator& operator++() { + ++current; + return *this; + } + + VertexId operator*() const { + return current->to; + } + }; + + // Get iterator to first neighbor + NeighborIterator neighborsBegin(VertexId v) const { + checkVertex(v); + return NeighborIterator(edges[v].begin(), edges[v].end()); + } + + // Get iterator to end of neighbors + NeighborIterator neighborsEnd(VertexId v) const { + checkVertex(v); + return NeighborIterator(edges[v].end(), edges[v].end()); + } +}; + +#endif \ No newline at end of file From bb9a72cda5c2dfd33997c4d96b5fec9cd83ed0f9 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Wed, 26 Nov 2025 21:22:09 +0300 Subject: [PATCH 24/36] LAB4 (update 0.1) --- CMakeLists.txt | 6 +- LibraryCPPTemplate/Tests/CMakeLists.txt | 11 +- LibraryCPPTemplate/Tests/dirgraph.cpp | 46 ++++++++ LibraryCPPTemplate/dirgraph.h | 134 +++++++++++++++-------- LibraryCPPTemplate/list.h | 140 ++++++++++++++++++------ 5 files changed, 253 insertions(+), 84 deletions(-) create mode 100644 LibraryCPPTemplate/Tests/dirgraph.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 83719b61f0..13baa28e85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,11 +11,11 @@ else() endif() #add_subdirectory(LibraryC) -add_subdirectory(LibraryCPP) +#add_subdirectory(LibraryCPP) #add_subdirectory(LibraryCPPClass) -#add_subdirectory(LibraryCPPTemplate) +add_subdirectory(LibraryCPPTemplate) #add_subdirectory(Lab1C) #add_subdirectory(Lab1CPP) #add_subdirectory(Lab2CPP) -add_subdirectory(Lab3CPP) +#add_subdirectory(Lab3CPP) diff --git a/LibraryCPPTemplate/Tests/CMakeLists.txt b/LibraryCPPTemplate/Tests/CMakeLists.txt index cbee1c72d5..eed25e4670 100644 --- a/LibraryCPPTemplate/Tests/CMakeLists.txt +++ b/LibraryCPPTemplate/Tests/CMakeLists.txt @@ -2,9 +2,9 @@ # target_include_directories(TestArrayCPPTemplate PUBLIC ..) # add_test(TestArrayCPPTemplate TestArrayCPPTemplate) -# add_executable(TestListCPPTemplate list.cpp) -# target_include_directories(TestListCPPTemplate PUBLIC ..) -# add_test(TestListCPPTemplate TestListCPPTemplate) +add_executable(TestListCPPTemplate list.cpp) +target_include_directories(TestListCPPTemplate PUBLIC ..) +add_test(TestListCPPTemplate TestListCPPTemplate) # add_executable(TestQueueCPPTemplate queue.cpp) # target_include_directories(TestQueueCPPTemplate PUBLIC ..) @@ -19,3 +19,8 @@ # target_include_directories(TestVectorCPPTemplate PUBLIC ..) # add_test(TestVectorCPPTemplate TestVectorCPPTemplate) # set_tests_properties(TestVectorCPPTemplate PROPERTIES TIMEOUT 10) + +add_executable(TestDirGraphCPPTemplate dirgraph.cpp) +target_include_directories(TestDirGraphCPPTemplate PUBLIC ..) +add_test(TestDirGraphCPPTemplate TestDirGraphCPPTemplate) +set_tests_properties(TestDirGraphCPPTemplate PROPERTIES PASS_REGULAR_EXPRESSION "All tests passed") diff --git a/LibraryCPPTemplate/Tests/dirgraph.cpp b/LibraryCPPTemplate/Tests/dirgraph.cpp new file mode 100644 index 0000000000..8c966c1dd7 --- /dev/null +++ b/LibraryCPPTemplate/Tests/dirgraph.cpp @@ -0,0 +1,46 @@ +#include +#include "dirgraph.h" + +struct VertexData { + int id = 0; + VertexData(int i = 0) : id(i) {} + bool operator==(const VertexData& other) const { return id == other.id; } +}; +struct EdgeData { + int weight = 0; + EdgeData(int w = 0) : weight(w) {} + bool operator==(const EdgeData& other) const { return weight == other.weight; } +}; + +int main() { + DirGraph graph; + + auto v0 = graph.addVertex(VertexData(0)); + auto v1 = graph.addVertex(VertexData(10)); + auto v2 = graph.addVertex(VertexData(20)); + + std::cout << "Created vertices: " << v0 << ", " << v1 << ", " << v2 << std::endl; + + if (!(graph.getVertexMark(v0) == VertexData(0))) return 1; + if (!graph.addEdge(v0, v1, EdgeData(5))) return 1; + if (!graph.edgeExists(v0, v1)) return 1; + if (!(graph.getEdgeMark(v0, v1) == EdgeData(5))) return 1; + + graph.removeEdge(v0, v1); + if (graph.edgeExists(v0, v1)) return 1; + + graph.removeVertex(v2); + if (graph.getVertexCount() != 2) return 1; + + graph.addEdge(v0, v1, EdgeData(10)); + if (!graph.edgeExists(v0, v1)) return 1; + + std::cout << "Neighbors of vertex " << v0 << ": "; + for (auto it = graph.neighborsBegin(v0); it != graph.neighborsEnd(v0); ++it) { + std::cout << *it << " "; + } + std::cout << std::endl; + + std::cout << "All tests passed"; + return 0; +} \ No newline at end of file diff --git a/LibraryCPPTemplate/dirgraph.h b/LibraryCPPTemplate/dirgraph.h index 9b42df3161..64f9f1854e 100644 --- a/LibraryCPPTemplate/dirgraph.h +++ b/LibraryCPPTemplate/dirgraph.h @@ -1,6 +1,7 @@ -#ifndef DIRGRAPH_TEMPLATE_H -#define DIRGRAPH_TEMPLATE_H +#ifndef DIRGRAPH_WITH_LIST_H +#define DIRGRAPH_WITH_LIST_H +#include "list.h" #include #include @@ -15,7 +16,7 @@ class DirGraph { }; std::vector vertexMarks; - std::vector> edges; + std::vector> adjacencyLists; void checkVertex(size_t v) const { if (v >= vertexMarks.size()) @@ -28,38 +29,66 @@ class DirGraph { // Constructor. Creates graph with specified number of vertices DirGraph(size_t vertexCount = 0) { vertexMarks.resize(vertexCount); - edges.resize(vertexCount); + adjacencyLists.resize(vertexCount); } // Add new vertex to the graph VertexId addVertex(const vertex& mark = vertex()) { VertexId id = vertexMarks.size(); vertexMarks.push_back(mark); - edges.push_back(std::vector()); + adjacencyLists.push_back(List()); return id; } - // Remove vertex from the graph + // Remove vertex from the graph - void removeVertex(VertexId v) { checkVertex(v); - vertexMarks.erase(vertexMarks.begin() + v); - edges.erase(edges.begin() + v); + adjacencyLists[v] = List(); + + for (size_t i = 0; i < adjacencyLists.size(); ++i) { + if (i == v) continue; + + List& neighbors = adjacencyLists[i]; + typename List::Item* current = neighbors.first(); + typename List::Item* prevItem = nullptr; - for (size_t i = 0; i < edges.size(); ++i) { - auto& neighbors = edges[i]; - for (auto it = neighbors.begin(); it != neighbors.end(); ) { - if (it->to == v) { - it = neighbors.erase(it); + while (current != nullptr) { + if (current->data().to == v) { + + if (prevItem == nullptr) { + neighbors.erase_first(); + current = neighbors.first(); + } + else { + current = neighbors.erase_next(prevItem); + } } - else { - if (it->to > v) { - it->to--; + else if (current->data().to > v) { + + Edge correctedEdge(current->data().to - 1, current->data().label); + + if (prevItem == nullptr) { + neighbors.erase_first(); + neighbors.insert(correctedEdge); + current = neighbors.first(); + } + else { + typename List::Item* nextItem = current->next(); + neighbors.erase_next(prevItem); + neighbors.insert_after(prevItem, correctedEdge); + current = nextItem; } - ++it; + } + else { + prevItem = current; + current = current->next(); } } } + + vertexMarks.erase(vertexMarks.begin() + v); + adjacencyLists.erase(adjacencyLists.begin() + v); } // Add edge between two vertices @@ -67,13 +96,16 @@ class DirGraph { checkVertex(from); checkVertex(to); - for (const auto& e : edges[from]) { - if (e.to == to) { - return false; + typename List::Item* current = adjacencyLists[from].first(); + while (current != nullptr) { + if (current->data().to == to) { + return false; // edge already exists } + current = current->next(); } - edges[from].push_back(Edge(to, mark)); + // Insert new edge + adjacencyLists[from].insert(Edge(to, mark)); return true; } @@ -82,12 +114,22 @@ class DirGraph { checkVertex(from); checkVertex(to); - auto& neighbors = edges[from]; - for (auto it = neighbors.begin(); it != neighbors.end(); ++it) { - if (it->to == to) { - neighbors.erase(it); + List& neighbors = adjacencyLists[from]; + typename List::Item* current = neighbors.first(); + typename List::Item* prevItem = nullptr; + + while (current != nullptr) { + if (current->data().to == to) { + if (prevItem == nullptr) { + neighbors.erase_first(); + } + else { + neighbors.erase_next(prevItem); + } return; } + prevItem = current; + current = current->next(); } } @@ -96,10 +138,12 @@ class DirGraph { checkVertex(from); checkVertex(to); - for (const auto& e : edges[from]) { - if (e.to == to) { + const typename List::Item* current = adjacencyLists[from].first(); + while (current != nullptr) { + if (current->data().to == to) { return true; } + current = current->next(); } return false; } @@ -109,11 +153,16 @@ class DirGraph { checkVertex(from); checkVertex(to); - for (auto& e : edges[from]) { - if (e.to == to) { - e.label = mark; + typename List::Item* current = adjacencyLists[from].first(); + while (current != nullptr) { + if (current->data().to == to) { + // Remove and reinsert with new mark + Edge newEdge(to, mark); + removeEdge(from, to); + adjacencyLists[from].insert(newEdge); return; } + current = current->next(); } throw std::out_of_range("Edge does not exist"); } @@ -123,10 +172,12 @@ class DirGraph { checkVertex(from); checkVertex(to); - for (const auto& e : edges[from]) { - if (e.to == to) { - return e.label; + const typename List::Item* current = adjacencyLists[from].first(); + while (current != nullptr) { + if (current->data().to == to) { + return current->data().label; } + current = current->next(); } throw std::out_of_range("Edge does not exist"); } @@ -156,39 +207,36 @@ class DirGraph { // Iterator for neighbors of a vertex class NeighborIterator { private: - typename std::vector::const_iterator current; - typename std::vector::const_iterator end; + const typename List::Item* current; public: - NeighborIterator(typename std::vector::const_iterator begin, - typename std::vector::const_iterator end) - : current(begin), end(end) { - } + NeighborIterator(const typename List::Item* start) : current(start) {} bool operator!=(const NeighborIterator& other) const { return current != other.current; } NeighborIterator& operator++() { - ++current; + if (current != nullptr) + current = current->next(); return *this; } VertexId operator*() const { - return current->to; + return current->data().to; } }; // Get iterator to first neighbor NeighborIterator neighborsBegin(VertexId v) const { checkVertex(v); - return NeighborIterator(edges[v].begin(), edges[v].end()); + return NeighborIterator(adjacencyLists[v].first()); } // Get iterator to end of neighbors NeighborIterator neighborsEnd(VertexId v) const { checkVertex(v); - return NeighborIterator(edges[v].end(), edges[v].end()); + return NeighborIterator(nullptr); } }; diff --git a/LibraryCPPTemplate/list.h b/LibraryCPPTemplate/list.h index ef8cb1dd38..6196cc7b1c 100644 --- a/LibraryCPPTemplate/list.h +++ b/LibraryCPPTemplate/list.h @@ -1,76 +1,146 @@ #ifndef LIST_TEMPLATE_H #define LIST_TEMPLATE_H -template class List -{ +template +class List { public: - class Item - { + class Item { public: - Item *next() { return nullptr; } - Item *prev() { return nullptr; } - Data data() const { return Data(); } + Item* next() { return nxt; } + const Item* next() const { return nxt; } + Item* prev() { return prv; } + const Item* prev() const { return prv; } + Data data() const { return dt; } private: - // internal data here + friend class List; + Item* nxt = nullptr; + Item* prv = nullptr; + Data dt; + + Item(Data val) : nxt(nullptr), prv(nullptr), dt(val) {} }; // Creates new list - List() - { - } + List() : head(nullptr), tail(nullptr) {} // copy constructor - List(const List &a) - { + List(const List& a) : head(nullptr), tail(nullptr) { + Item* cur = a.head; + while (cur != nullptr) { + insert(cur->data()); + cur = cur->next(); + } } // assignment operator - List &operator=(const List &a) - { + List& operator=(const List& a) { + if (this == &a) + return *this; + clear(); + Item* cur = a.head; + while (cur != nullptr) { + insert(cur->data()); + cur = cur->next(); + } return *this; } // Destroys the list and frees the memory - ~List() - { + ~List() { + clear(); } // Retrieves the first item from the list - Item *first() - { - return nullptr; + Item* first() { + return head; + } + + // Const version of first() + const Item* first() const { + return head; } // Inserts new list item into the beginning - Item *insert(Data data) - { - return nullptr; + Item* insert(Data data) { + Item* newItem = new Item(data); + if (head == nullptr) { + head = tail = newItem; + } + else { + newItem->nxt = head; + head->prv = newItem; + head = newItem; + } + return newItem; } // Inserts new list item after the specified item // Inserts first element if item is null - Item *insert_after(Item *item, Data data) - { - return nullptr; + Item* insert_after(Item* item, Data data) { + if (item == nullptr) { + return insert(data); + } + Item* newItem = new Item(data); + newItem->prv = item; + newItem->nxt = item->nxt; + if (item->nxt != nullptr) { + item->nxt->prv = newItem; + } + item->nxt = newItem; + if (item == tail) { + tail = newItem; + } + return newItem; } // Deletes the first list item. // Returns pointer to the item next to the deleted one. - Item *erase_first() - { - return nullptr; + Item* erase_first() { + if (head == nullptr) + return nullptr; + Item* toDelete = head; + head = head->nxt; + if (head != nullptr) + head->prv = nullptr; + else + tail = nullptr; + Item* nextItem = head; + delete toDelete; + return nextItem; } // 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) - { - return nullptr; + Item* erase_next(Item* item) { + if (item == nullptr) { + // Erase first + return erase_first(); + } + Item* toDelete = item->nxt; + if (toDelete == nullptr) + return nullptr; + item->nxt = toDelete->nxt; + if (toDelete->nxt != nullptr) { + toDelete->nxt->prv = item; + } + else { + tail = item; + } + Item* nextItem = toDelete->nxt; + delete toDelete; + return nextItem; } + private: - // private data should be here + Item* head; + Item* tail; + + void clear() { + while (head != nullptr) { + erase_first(); + } + } }; -#endif +#endif \ No newline at end of file From c489bd6f19abd59fc7b957eb2c6e5b3bdaa7a3c7 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Thu, 27 Nov 2025 22:58:19 +0300 Subject: [PATCH 25/36] LAB4 (update 1) --- CMakeLists.txt | 1 + Lab4CPPTemplate/CMakeLists.txt | 8 ++ Lab4CPPTemplate/input.txt | 6 ++ Lab4CPPTemplate/lab4.cpp | 162 +++++++++++++++++++++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 Lab4CPPTemplate/CMakeLists.txt create mode 100644 Lab4CPPTemplate/input.txt create mode 100644 Lab4CPPTemplate/lab4.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 13baa28e85..ffd1152be4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,3 +19,4 @@ add_subdirectory(LibraryCPPTemplate) #add_subdirectory(Lab1CPP) #add_subdirectory(Lab2CPP) #add_subdirectory(Lab3CPP) +add_subdirectory(Lab4CPPTemplate) diff --git a/Lab4CPPTemplate/CMakeLists.txt b/Lab4CPPTemplate/CMakeLists.txt new file mode 100644 index 0000000000..23cd10da97 --- /dev/null +++ b/Lab4CPPTemplate/CMakeLists.txt @@ -0,0 +1,8 @@ +add_executable(Lab4CPP lab4.cpp) +target_include_directories(Lab4CPP PUBLIC ../LibraryCPPTemplate) + +add_test(NAME TestLab4CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +set_property(TEST TestLab4CPP PROPERTY PASS_REGULAR_EXPRESSION "A B 1") + + + diff --git a/Lab4CPPTemplate/input.txt b/Lab4CPPTemplate/input.txt new file mode 100644 index 0000000000..810f30bf62 --- /dev/null +++ b/Lab4CPPTemplate/input.txt @@ -0,0 +1,6 @@ +4 5 +A B 1 +A C 4 +B C 2 +B D 5 +C D 3 diff --git a/Lab4CPPTemplate/lab4.cpp b/Lab4CPPTemplate/lab4.cpp new file mode 100644 index 0000000000..4095e200ee --- /dev/null +++ b/Lab4CPPTemplate/lab4.cpp @@ -0,0 +1,162 @@ +#include "dirgraph.h" +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct EdgeKruskal { + size_t from; + size_t to; + int weight; + + EdgeKruskal(size_t f, size_t t, int w) : from(f), to(t), weight(w) {} + + bool operator<(const EdgeKruskal& other) const { + return weight < other.weight; + } +}; + +class DisjointSet { +private: + vector parent; + vector rank; + +public: + DisjointSet(size_t size) { + parent.resize(size); + rank.resize(size, 0); + for (size_t i = 0; i < size; ++i) { + parent[i] = i; + } + } + + size_t find(size_t x) { + if (parent[x] != x) { + parent[x] = find(parent[x]); + } + return parent[x]; + } + + bool unite(size_t x, size_t y) { + size_t px = find(x); + size_t py = find(y); + + if (px == py) { + return false; + } + + if (rank[px] < rank[py]) { + parent[px] = py; + } + else { + parent[py] = px; + if (rank[px] == rank[py]) { + ++rank[px]; + } + } + return true; + } +}; + +vector getAllEdges(const DirGraph& graph) { + vector edges; + for (size_t from = 0; from < graph.getVertexCount(); ++from) { + auto it = graph.neighborsBegin(from); + auto end = graph.neighborsEnd(from); + for (; it != end; ++it) { + size_t to = *it; + + if (from < to) { + int weight = graph.getEdgeMark(from, to); + edges.emplace_back(from, to, weight); + } + } + } + return edges; +} + +vector kruskalMST(DirGraph& graph) { + size_t n = graph.getVertexCount(); + if (n < 2) return {}; + + vector edges = getAllEdges(graph); + sort(edges.begin(), edges.end()); + + DisjointSet dsu(n); + vector mst; + + for (const auto& edge : edges) { + if (dsu.unite(edge.from, edge.to)) { + mst.push_back(edge); + if (mst.size() == n - 1) { + break; + } + } + } + + return mst; +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + return 1; + } + + ifstream file(argv[1]); + if (!file.is_open()) { + return 1; + } + + DirGraph graph; + map vertexMap; + string line; + + while (getline(file, line)) { + if (!line.empty()) { + size_t pos1 = line.find(' '); + if (pos1 != string::npos) { + size_t pos2 = line.find(' ', pos1 + 1); + if (pos2 != string::npos) { + string vertex1 = line.substr(0, pos1); + string vertex2 = line.substr(pos1 + 1, pos2 - pos1 - 1); + int weight = stoi(line.substr(pos2 + 1)); + + if (vertexMap.find(vertex1) == vertexMap.end()) { + size_t id = graph.addVertex(vertex1); + vertexMap[vertex1] = id; + } + if (vertexMap.find(vertex2) == vertexMap.end()) { + size_t id = graph.addVertex(vertex2); + vertexMap[vertex2] = id; + } + + size_t fromId = vertexMap[vertex1]; + size_t toId = vertexMap[vertex2]; + graph.addEdge(fromId, toId, weight); + graph.addEdge(toId, fromId, weight); + } + } + } + } + + file.close(); + + vector mst = kruskalMST(graph); + + int totalWeight = 0; + for (const auto& e : mst) { + totalWeight += e.weight; + } + + for (const auto& e : mst) { + cout << graph.getVertexMark(e.from) << " " + << graph.getVertexMark(e.to) << " " + << e.weight << endl; + } + + return 0; +} \ No newline at end of file From 7ff22aafeabbd83bbaeb35a393c599a8e1ba1bb1 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Mon, 1 Dec 2025 23:06:44 +0300 Subject: [PATCH 26/36] LAB4 (update 2) --- Lab4CPPTemplate/CMakeLists.txt | 2 +- Lab4CPPTemplate/lab4.cpp | 12 ++++-- LibraryCPPTemplate/Tests/dirgraph.cpp | 8 +++- LibraryCPPTemplate/dirgraph.h | 53 +++++---------------------- 4 files changed, 24 insertions(+), 51 deletions(-) diff --git a/Lab4CPPTemplate/CMakeLists.txt b/Lab4CPPTemplate/CMakeLists.txt index 23cd10da97..7f25db4749 100644 --- a/Lab4CPPTemplate/CMakeLists.txt +++ b/Lab4CPPTemplate/CMakeLists.txt @@ -2,7 +2,7 @@ add_executable(Lab4CPP lab4.cpp) target_include_directories(Lab4CPP PUBLIC ../LibraryCPPTemplate) add_test(NAME TestLab4CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) -set_property(TEST TestLab4CPP PROPERTY PASS_REGULAR_EXPRESSION "A B 1") +set_property(TEST TestLab4CPP PROPERTY PASS_REGULAR_EXPRESSION ".*A B 1.*B C 2.*C D 3.*") diff --git a/Lab4CPPTemplate/lab4.cpp b/Lab4CPPTemplate/lab4.cpp index 4095e200ee..9c7c5d963a 100644 --- a/Lab4CPPTemplate/lab4.cpp +++ b/Lab4CPPTemplate/lab4.cpp @@ -62,15 +62,19 @@ class DisjointSet { } }; +template +using NeighborIterator = typename DirGraph::NeighborIterator; + vector getAllEdges(const DirGraph& graph) { vector edges; for (size_t from = 0; from < graph.getVertexCount(); ++from) { - auto it = graph.neighborsBegin(from); - auto end = graph.neighborsEnd(from); + NeighborIterator it = graph.neighborsBegin(from); + NeighborIterator end = graph.neighborsEnd(from); + for (; it != end; ++it) { size_t to = *it; - if (from < to) { + if (from < to) { int weight = graph.getEdgeMark(from, to); edges.emplace_back(from, to, weight); } @@ -159,4 +163,4 @@ int main(int argc, char* argv[]) { } return 0; -} \ No newline at end of file +} diff --git a/LibraryCPPTemplate/Tests/dirgraph.cpp b/LibraryCPPTemplate/Tests/dirgraph.cpp index 8c966c1dd7..298f16972a 100644 --- a/LibraryCPPTemplate/Tests/dirgraph.cpp +++ b/LibraryCPPTemplate/Tests/dirgraph.cpp @@ -36,11 +36,15 @@ int main() { if (!graph.edgeExists(v0, v1)) return 1; std::cout << "Neighbors of vertex " << v0 << ": "; + bool hasNeighbor = false; for (auto it = graph.neighborsBegin(v0); it != graph.neighborsEnd(v0); ++it) { std::cout << *it << " "; + if (*it == v1) hasNeighbor = true; } std::cout << std::endl; - std::cout << "All tests passed"; + if (!hasNeighbor) return 1; + + std::cout << "All tests passed" << std::endl; return 0; -} \ No newline at end of file +} diff --git a/LibraryCPPTemplate/dirgraph.h b/LibraryCPPTemplate/dirgraph.h index 64f9f1854e..99b39daf1a 100644 --- a/LibraryCPPTemplate/dirgraph.h +++ b/LibraryCPPTemplate/dirgraph.h @@ -40,55 +40,22 @@ class DirGraph { return id; } - // Remove vertex from the graph - + // Remove vertex from the graph void removeVertex(VertexId v) { checkVertex(v); - adjacencyLists[v] = List(); + adjacencyLists[v] = List(); for (size_t i = 0; i < adjacencyLists.size(); ++i) { - if (i == v) continue; - - List& neighbors = adjacencyLists[i]; - typename List::Item* current = neighbors.first(); - typename List::Item* prevItem = nullptr; - - while (current != nullptr) { - if (current->data().to == v) { - - if (prevItem == nullptr) { - neighbors.erase_first(); - current = neighbors.first(); - } - else { - current = neighbors.erase_next(prevItem); - } - } - else if (current->data().to > v) { - - Edge correctedEdge(current->data().to - 1, current->data().label); - - if (prevItem == nullptr) { - neighbors.erase_first(); - neighbors.insert(correctedEdge); - current = neighbors.first(); - } - else { - typename List::Item* nextItem = current->next(); - neighbors.erase_next(prevItem); - neighbors.insert_after(prevItem, correctedEdge); - current = nextItem; - } - } - else { - prevItem = current; - current = current->next(); - } + if (i != v && edgeExists(i, v)) { + removeEdge(i, v); } } + vertexMarks.erase(vertexMarks.begin() + v); adjacencyLists.erase(adjacencyLists.begin() + v); + } // Add edge between two vertices @@ -154,12 +121,10 @@ class DirGraph { checkVertex(to); typename List::Item* current = adjacencyLists[from].first(); + while (current != nullptr) { if (current->data().to == to) { - // Remove and reinsert with new mark - Edge newEdge(to, mark); - removeEdge(from, to); - adjacencyLists[from].insert(newEdge); + current->data().label = mark; return; } current = current->next(); @@ -240,4 +205,4 @@ class DirGraph { } }; -#endif \ No newline at end of file +#endif From 2b4c9ce0f90bc4fb0fe9d82156669ca43a5c2a2a Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Wed, 3 Dec 2025 00:44:18 +0300 Subject: [PATCH 27/36] LAB4 (update 3) --- LibraryCPPTemplate/Tests/CMakeLists.txt | 8 ++-- LibraryCPPTemplate/dirgraph.h | 28 +++++++----- LibraryCPPTemplate/vector.h | 59 ++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/LibraryCPPTemplate/Tests/CMakeLists.txt b/LibraryCPPTemplate/Tests/CMakeLists.txt index eed25e4670..00cb415187 100644 --- a/LibraryCPPTemplate/Tests/CMakeLists.txt +++ b/LibraryCPPTemplate/Tests/CMakeLists.txt @@ -15,10 +15,10 @@ add_test(TestListCPPTemplate TestListCPPTemplate) # target_include_directories(TestStackCPPTemplate PUBLIC ..) # add_test(TestStackCPPTemplate TestStackCPPTemplate) -# add_executable(TestVectorCPPTemplate vector.cpp) -# target_include_directories(TestVectorCPPTemplate PUBLIC ..) -# add_test(TestVectorCPPTemplate TestVectorCPPTemplate) -# set_tests_properties(TestVectorCPPTemplate PROPERTIES TIMEOUT 10) +add_executable(TestVectorCPPTemplate vector.cpp) +target_include_directories(TestVectorCPPTemplate PUBLIC ..) +add_test(TestVectorCPPTemplate TestVectorCPPTemplate) +set_tests_properties(TestVectorCPPTemplate PROPERTIES TIMEOUT 10) add_executable(TestDirGraphCPPTemplate dirgraph.cpp) target_include_directories(TestDirGraphCPPTemplate PUBLIC ..) diff --git a/LibraryCPPTemplate/dirgraph.h b/LibraryCPPTemplate/dirgraph.h index 99b39daf1a..354fcfd0db 100644 --- a/LibraryCPPTemplate/dirgraph.h +++ b/LibraryCPPTemplate/dirgraph.h @@ -1,6 +1,7 @@ #ifndef DIRGRAPH_WITH_LIST_H #define DIRGRAPH_WITH_LIST_H +#include "vector.h" #include "list.h" #include #include @@ -15,7 +16,7 @@ class DirGraph { Edge(size_t t, const edge& l = edge()) : to(t), label(l) {} }; - std::vector vertexMarks; + Vector vertexMarks; std::vector> adjacencyLists; void checkVertex(size_t v) const { @@ -35,7 +36,8 @@ class DirGraph { // Add new vertex to the graph VertexId addVertex(const vertex& mark = vertex()) { VertexId id = vertexMarks.size(); - vertexMarks.push_back(mark); + vertexMarks.resize(id + 1); + vertexMarks.set(id, mark); adjacencyLists.push_back(List()); return id; } @@ -47,15 +49,17 @@ class DirGraph { adjacencyLists[v] = List(); for (size_t i = 0; i < adjacencyLists.size(); ++i) { - if (i != v && edgeExists(i, v)) { - removeEdge(i, v); - } + removeEdge(i, v); } - - vertexMarks.erase(vertexMarks.begin() + v); - adjacencyLists.erase(adjacencyLists.begin() + v); + // Shift all elements after v in vertexMarks + for (size_t i = v; i + 1 < vertexMarks.size(); ++i) { + vertexMarks.set(i, vertexMarks.get(i + 1)); + } + vertexMarks.resize(vertexMarks.size() - 1); + // Remove from adjacencyLists + adjacencyLists.erase(adjacencyLists.begin() + v); } // Add edge between two vertices @@ -124,7 +128,7 @@ class DirGraph { while (current != nullptr) { if (current->data().to == to) { - current->data().label = mark; + current->data().label = mark; return; } current = current->next(); @@ -150,17 +154,17 @@ class DirGraph { // Set label for vertex void setVertexMark(VertexId v, const vertex& mark) { checkVertex(v); - vertexMarks[v] = mark; + vertexMarks.set(v, mark); } // Get label of vertex vertex getVertexMark(VertexId v) const { checkVertex(v); - return vertexMarks[v]; + return vertexMarks.get(v); } // Get labels of all vertices - std::vector getAllVertexMarks() const { + Vector getAllVertexMarks() const { return vertexMarks; } diff --git a/LibraryCPPTemplate/vector.h b/LibraryCPPTemplate/vector.h index f4872259e1..e6ead3051a 100644 --- a/LibraryCPPTemplate/vector.h +++ b/LibraryCPPTemplate/vector.h @@ -2,56 +2,101 @@ #define VECTOR_TEMPLATE_H #include +#include +#include template class Vector { public: // Creates vector - Vector() + Vector() : data(nullptr), sz(0), capacity(0) { } // copy constructor - Vector(const Vector &a) + Vector(const Vector& a) : sz(a.sz), capacity(a.capacity) { + if (capacity == 0) { + data = nullptr; + } + else { + data = new Data[capacity]; + std::copy(a.data, a.data + sz, data); + } } // assignment operator - Vector &operator=(const Vector &a) + Vector& operator=(const Vector& a) { + if (this != &a) { + delete[] data; + sz = a.sz; + capacity = a.capacity; + if (capacity == 0) { + data = nullptr; + } + else { + data = new Data[capacity]; + std::copy(a.data, a.data + sz, data); + } + } return *this; } // Deletes vector structure and internal data ~Vector() { + delete[] data; } // Retrieves vector element with the specified index Data get(size_t index) const { - return Data(); + if (index >= sz) { + throw std::out_of_range("Index out of bounds"); + } + return data[index]; } // Sets vector element with the specified index void set(size_t index, Data value) { + if (index >= sz) { + throw std::out_of_range("Index out of bounds"); + } + data[index] = value; } // Retrieves current vector size size_t size() const { - return 0; + return sz; } // Changes the vector size (may increase or decrease) // Should be O(1) on average - void resize(size_t size) + void resize(size_t new_size) { + if (new_size > capacity) { + size_t new_capacity = capacity == 0 ? 1 : capacity * 2; + while (new_capacity < new_size) { + new_capacity *= 2; + } + Data* new_data = new Data[new_capacity]; + if (data) { + std::copy(data, data + sz, new_data); + delete[] data; + } + data = new_data; + capacity = new_capacity; + } + sz = new_size; } private: - // private data should be here + Data* data; + size_t sz; + size_t capacity; }; #endif From 5abc04384cb72557162d0961f1446015b459cf7d Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Wed, 3 Dec 2025 22:02:17 +0300 Subject: [PATCH 28/36] LAB4 (update 4) --- Lab4CPPTemplate/lab4.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Lab4CPPTemplate/lab4.cpp b/Lab4CPPTemplate/lab4.cpp index 9c7c5d963a..2c6ecfed83 100644 --- a/Lab4CPPTemplate/lab4.cpp +++ b/Lab4CPPTemplate/lab4.cpp @@ -62,14 +62,13 @@ class DisjointSet { } }; -template -using NeighborIterator = typename DirGraph::NeighborIterator; +typedef typename DirGraph::NeighborIterator NeighborIterator; vector getAllEdges(const DirGraph& graph) { vector edges; for (size_t from = 0; from < graph.getVertexCount(); ++from) { - NeighborIterator it = graph.neighborsBegin(from); - NeighborIterator end = graph.neighborsEnd(from); + NeighborIterator it = graph.neighborsBegin(from); + NeighborIterator end = graph.neighborsEnd(from); for (; it != end; ++it) { size_t to = *it; From 5c18cf3660c2a6f0600f727586fbd57e066f34d9 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Thu, 4 Dec 2025 21:38:12 +0300 Subject: [PATCH 29/36] LAB4 (update 5) --- Lab4CPPTemplate/lab4.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lab4CPPTemplate/lab4.cpp b/Lab4CPPTemplate/lab4.cpp index 2c6ecfed83..7735d500a2 100644 --- a/Lab4CPPTemplate/lab4.cpp +++ b/Lab4CPPTemplate/lab4.cpp @@ -62,18 +62,16 @@ class DisjointSet { } }; -typedef typename DirGraph::NeighborIterator NeighborIterator; - vector getAllEdges(const DirGraph& graph) { vector edges; for (size_t from = 0; from < graph.getVertexCount(); ++from) { - NeighborIterator it = graph.neighborsBegin(from); - NeighborIterator end = graph.neighborsEnd(from); + typename DirGraph::NeighborIterator it = graph.neighborsBegin(from); + typename DirGraph::NeighborIterator end = graph.neighborsEnd(from); for (; it != end; ++it) { size_t to = *it; - if (from < to) { + if (from < to) { int weight = graph.getEdgeMark(from, to); edges.emplace_back(from, to, weight); } From 7e88c16e9634abd351f6f662491460fc468fcb21 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Fri, 5 Dec 2025 21:12:13 +0300 Subject: [PATCH 30/36] LAB4 (update 6) --- Lab4CPPTemplate/lab4.cpp | 68 ++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/Lab4CPPTemplate/lab4.cpp b/Lab4CPPTemplate/lab4.cpp index 7735d500a2..d035728809 100644 --- a/Lab4CPPTemplate/lab4.cpp +++ b/Lab4CPPTemplate/lab4.cpp @@ -5,6 +5,7 @@ #include #include #include +#include using namespace std; @@ -64,22 +65,19 @@ class DisjointSet { vector getAllEdges(const DirGraph& graph) { vector edges; - for (size_t from = 0; from < graph.getVertexCount(); ++from) { - typename DirGraph::NeighborIterator it = graph.neighborsBegin(from); - typename DirGraph::NeighborIterator end = graph.neighborsEnd(from); - - for (; it != end; ++it) { - size_t to = *it; - - if (from < to) { - int weight = graph.getEdgeMark(from, to); - edges.emplace_back(from, to, weight); + for (size_t i = 0; i < graph.getVertexCount(); ++i) { + for (auto it = graph.neighborsBegin(i); it != graph.neighborsEnd(i); ++it) { + size_t j = *it; + if (i < j) { + int weight = graph.getEdgeMark(i, j); + edges.emplace_back(i, j, weight); } } } return edges; } + vector kruskalMST(DirGraph& graph) { size_t n = graph.getVertexCount(); if (n < 2) return {}; @@ -116,31 +114,29 @@ int main(int argc, char* argv[]) { map vertexMap; string line; + getline(file, line); + while (getline(file, line)) { - if (!line.empty()) { - size_t pos1 = line.find(' '); - if (pos1 != string::npos) { - size_t pos2 = line.find(' ', pos1 + 1); - if (pos2 != string::npos) { - string vertex1 = line.substr(0, pos1); - string vertex2 = line.substr(pos1 + 1, pos2 - pos1 - 1); - int weight = stoi(line.substr(pos2 + 1)); - - if (vertexMap.find(vertex1) == vertexMap.end()) { - size_t id = graph.addVertex(vertex1); - vertexMap[vertex1] = id; - } - if (vertexMap.find(vertex2) == vertexMap.end()) { - size_t id = graph.addVertex(vertex2); - vertexMap[vertex2] = id; - } - - size_t fromId = vertexMap[vertex1]; - size_t toId = vertexMap[vertex2]; - graph.addEdge(fromId, toId, weight); - graph.addEdge(toId, fromId, weight); - } + if (line.empty()) continue; + + istringstream iss(line); + string v1, v2; + int weight; + + if (iss >> v1 >> v2 >> weight) { + + if (vertexMap.find(v1) == vertexMap.end()) { + vertexMap[v1] = graph.addVertex(v1); } + + if (vertexMap.find(v2) == vertexMap.end()) { + vertexMap[v2] = graph.addVertex(v2); + } + + size_t fromId = vertexMap[v1]; + size_t toId = vertexMap[v2]; + graph.addEdge(fromId, toId, weight); + graph.addEdge(toId, fromId, weight); } } @@ -148,11 +144,6 @@ int main(int argc, char* argv[]) { vector mst = kruskalMST(graph); - int totalWeight = 0; - for (const auto& e : mst) { - totalWeight += e.weight; - } - for (const auto& e : mst) { cout << graph.getVertexMark(e.from) << " " << graph.getVertexMark(e.to) << " " @@ -161,3 +152,4 @@ int main(int argc, char* argv[]) { return 0; } + From 1ade65a11f4bab1dbcad007b4abfe5ed9cb95a89 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Thu, 11 Dec 2025 20:34:39 +0300 Subject: [PATCH 31/36] LAB4 (update 7) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit п.с если честно не знаю в ту сторону мне подумалось или не в ту, пожалуйста напишите подробнее прошу :( --- Lab4CPPTemplate/CMakeLists.txt | 3 ++- Lab4CPPTemplate/input.txt | 1 - Lab4CPPTemplate/input2.txt | 9 +++++++ Lab4CPPTemplate/lab4.cpp | 49 ++++++++++++++++++++-------------- LibraryCPPTemplate/dirgraph.h | 23 ++++++++++------ LibraryCPPTemplate/list.h | 3 ++- 6 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 Lab4CPPTemplate/input2.txt diff --git a/Lab4CPPTemplate/CMakeLists.txt b/Lab4CPPTemplate/CMakeLists.txt index 7f25db4749..8930956332 100644 --- a/Lab4CPPTemplate/CMakeLists.txt +++ b/Lab4CPPTemplate/CMakeLists.txt @@ -4,5 +4,6 @@ target_include_directories(Lab4CPP PUBLIC ../LibraryCPPTemplate) add_test(NAME TestLab4CPP COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) set_property(TEST TestLab4CPP PROPERTY PASS_REGULAR_EXPRESSION ".*A B 1.*B C 2.*C D 3.*") - +add_test(NAME TestLab4CPP2 COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/input2.txt) +set_property(TEST TestLab4CPP2 PROPERTY PASS_REGULAR_EXPRESSION "A B 4") diff --git a/Lab4CPPTemplate/input.txt b/Lab4CPPTemplate/input.txt index 810f30bf62..40dc7610cd 100644 --- a/Lab4CPPTemplate/input.txt +++ b/Lab4CPPTemplate/input.txt @@ -1,4 +1,3 @@ -4 5 A B 1 A C 4 B C 2 diff --git a/Lab4CPPTemplate/input2.txt b/Lab4CPPTemplate/input2.txt new file mode 100644 index 0000000000..145a279b45 --- /dev/null +++ b/Lab4CPPTemplate/input2.txt @@ -0,0 +1,9 @@ +A B 4 +A C 8 +B C 2 +B D 11 +C D 6 +C E 7 +D E 3 +F G 5 + diff --git a/Lab4CPPTemplate/lab4.cpp b/Lab4CPPTemplate/lab4.cpp index d035728809..611a65735f 100644 --- a/Lab4CPPTemplate/lab4.cpp +++ b/Lab4CPPTemplate/lab4.cpp @@ -9,18 +9,21 @@ using namespace std; +// Structure to store edge info for Kruskal's algorithm struct EdgeKruskal { - size_t from; - size_t to; - int weight; + size_t from; // start vertex id + size_t to; // end vertex id + int weight; // edge weight EdgeKruskal(size_t f, size_t t, int w) : from(f), to(t), weight(w) {} + // For sorting edges by weight bool operator<(const EdgeKruskal& other) const { return weight < other.weight; } }; +// Disjoint set (union-find) data structure for Kruskal class DisjointSet { private: vector parent; @@ -35,6 +38,7 @@ class DisjointSet { } } + // Find root of set containing x with path compression size_t find(size_t x) { if (parent[x] != x) { parent[x] = find(parent[x]); @@ -42,14 +46,16 @@ class DisjointSet { return parent[x]; } + // Unite two sets, return true if merged bool unite(size_t x, size_t y) { size_t px = find(x); size_t py = find(y); if (px == py) { - return false; + return false; // already in same set } + // Union by rank if (rank[px] < rank[py]) { parent[px] = py; } @@ -63,46 +69,50 @@ class DisjointSet { } }; +// Get all edges of the graph as vector of EdgeKruskal vector getAllEdges(const DirGraph& graph) { vector edges; - for (size_t i = 0; i < graph.getVertexCount(); ++i) { + size_t n = graph.getVertexCount(); + + // Iterate over all vertices + for (size_t i = 0; i < n; ++i) { + // Iterate over all neighbors of vertex i for (auto it = graph.neighborsBegin(i); it != graph.neighborsEnd(i); ++it) { size_t j = *it; - if (i < j) { - int weight = graph.getEdgeMark(i, j); - edges.emplace_back(i, j, weight); - } + int weight = graph.getEdgeMark(i, j); + edges.emplace_back(i, j, weight); } } return edges; } - +// Compute MST with Kruskal algorithm on given graph vector kruskalMST(DirGraph& graph) { size_t n = graph.getVertexCount(); if (n < 2) return {}; + // Get all edges and sort by weight ascending (stonks) vector edges = getAllEdges(graph); sort(edges.begin(), edges.end()); DisjointSet dsu(n); vector mst; + // Iterate edges, add to MST if they connect two different sets for (const auto& edge : edges) { if (dsu.unite(edge.from, edge.to)) { mst.push_back(edge); if (mst.size() == n - 1) { - break; + break; // MST complete } } } - return mst; } int main(int argc, char* argv[]) { if (argc != 2) { - return 1; + return 1; } ifstream file(argv[1]); @@ -111,11 +121,10 @@ int main(int argc, char* argv[]) { } DirGraph graph; - map vertexMap; + map vertexMap; // Map vertex name to id string line; - getline(file, line); - + // Read each line: "vertex1 vertex2 weight" while (getline(file, line)) { if (line.empty()) continue; @@ -124,26 +133,27 @@ int main(int argc, char* argv[]) { int weight; if (iss >> v1 >> v2 >> weight) { - + // Add vertex if not exists if (vertexMap.find(v1) == vertexMap.end()) { vertexMap[v1] = graph.addVertex(v1); } - if (vertexMap.find(v2) == vertexMap.end()) { vertexMap[v2] = graph.addVertex(v2); } size_t fromId = vertexMap[v1]; size_t toId = vertexMap[v2]; + // Add edge in both directions to simulate undirected graph!!! graph.addEdge(fromId, toId, weight); graph.addEdge(toId, fromId, weight); } } - file.close(); + // Find minimal spanning tree edges with Kruskal vector mst = kruskalMST(graph); + // Output MST edges with vertex names and weights for (const auto& e : mst) { cout << graph.getVertexMark(e.from) << " " << graph.getVertexMark(e.to) << " " @@ -152,4 +162,3 @@ int main(int argc, char* argv[]) { return 0; } - diff --git a/LibraryCPPTemplate/dirgraph.h b/LibraryCPPTemplate/dirgraph.h index 354fcfd0db..f2e4c10f93 100644 --- a/LibraryCPPTemplate/dirgraph.h +++ b/LibraryCPPTemplate/dirgraph.h @@ -46,20 +46,27 @@ class DirGraph { void removeVertex(VertexId v) { checkVertex(v); - adjacencyLists[v] = List(); - for (size_t i = 0; i < adjacencyLists.size(); ++i) { removeEdge(i, v); } - // Shift all elements after v in vertexMarks - for (size_t i = v; i + 1 < vertexMarks.size(); ++i) { - vertexMarks.set(i, vertexMarks.get(i + 1)); + size_t last = vertexMarks.size() - 1; + if (v < last) { + vertexMarks.set(v, vertexMarks.get(last)); + adjacencyLists[v] = std::move(adjacencyLists[last]); } - vertexMarks.resize(vertexMarks.size() - 1); + vertexMarks.resize(last); + adjacencyLists.pop_back(); - // Remove from adjacencyLists - adjacencyLists.erase(adjacencyLists.begin() + v); + for (size_t i = 0; i < adjacencyLists.size(); ++i) { + auto* it = adjacencyLists[i].first(); + while (it != nullptr) { + if (it->data().to > v) { + it->data().to--; + } + it = it->next(); + } + } } // Add edge between two vertices diff --git a/LibraryCPPTemplate/list.h b/LibraryCPPTemplate/list.h index 6196cc7b1c..8078f6299c 100644 --- a/LibraryCPPTemplate/list.h +++ b/LibraryCPPTemplate/list.h @@ -10,7 +10,8 @@ class List { const Item* next() const { return nxt; } Item* prev() { return prv; } const Item* prev() const { return prv; } - Data data() const { return dt; } + Data& data() { return dt; } + const Data& data() const { return dt; } private: friend class List; Item* nxt = nullptr; From c5fd952e6444b5ca1c9e5c41d6d54efbb365d141 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Fri, 12 Dec 2025 20:14:18 +0300 Subject: [PATCH 32/36] LAB4 (update 8) --- LibraryCPPTemplate/dirgraph.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/LibraryCPPTemplate/dirgraph.h b/LibraryCPPTemplate/dirgraph.h index f2e4c10f93..53d16eda37 100644 --- a/LibraryCPPTemplate/dirgraph.h +++ b/LibraryCPPTemplate/dirgraph.h @@ -1,5 +1,5 @@ -#ifndef DIRGRAPH_WITH_LIST_H -#define DIRGRAPH_WITH_LIST_H +#ifndef DIRGRAPH_TEMPLATE_H +#define DIRGRAPH_TEMPLATE_H #include "vector.h" #include "list.h" @@ -26,6 +26,8 @@ class DirGraph { public: typedef size_t VertexId; + typedef typename List::Item ListItem; + typedef const typename List::Item ConstListItem; // Constructor. Creates graph with specified number of vertices DirGraph(size_t vertexCount = 0) { @@ -59,7 +61,7 @@ class DirGraph { adjacencyLists.pop_back(); for (size_t i = 0; i < adjacencyLists.size(); ++i) { - auto* it = adjacencyLists[i].first(); + ListItem* it = adjacencyLists[i].first(); while (it != nullptr) { if (it->data().to > v) { it->data().to--; @@ -74,7 +76,7 @@ class DirGraph { checkVertex(from); checkVertex(to); - typename List::Item* current = adjacencyLists[from].first(); + ConstListItem* current = adjacencyLists[from].first(); while (current != nullptr) { if (current->data().to == to) { return false; // edge already exists @@ -93,8 +95,8 @@ class DirGraph { checkVertex(to); List& neighbors = adjacencyLists[from]; - typename List::Item* current = neighbors.first(); - typename List::Item* prevItem = nullptr; + ListItem* current = neighbors.first(); + ListItem* prevItem = nullptr; while (current != nullptr) { if (current->data().to == to) { @@ -116,7 +118,7 @@ class DirGraph { checkVertex(from); checkVertex(to); - const typename List::Item* current = adjacencyLists[from].first(); + ConstListItem* current = adjacencyLists[from].first(); while (current != nullptr) { if (current->data().to == to) { return true; @@ -131,7 +133,7 @@ class DirGraph { checkVertex(from); checkVertex(to); - typename List::Item* current = adjacencyLists[from].first(); + ListItem* current = adjacencyLists[from].first(); while (current != nullptr) { if (current->data().to == to) { @@ -148,7 +150,7 @@ class DirGraph { checkVertex(from); checkVertex(to); - const typename List::Item* current = adjacencyLists[from].first(); + ConstListItem* current = adjacencyLists[from].first(); while (current != nullptr) { if (current->data().to == to) { return current->data().label; @@ -183,10 +185,10 @@ class DirGraph { // Iterator for neighbors of a vertex class NeighborIterator { private: - const typename List::Item* current; + ConstListItem* current; public: - NeighborIterator(const typename List::Item* start) : current(start) {} + NeighborIterator(ConstListItem* start) : current(start) {} bool operator!=(const NeighborIterator& other) const { return current != other.current; From d10ef44d0dd2b915ff6011644428b5f35c7e9738 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Tue, 16 Dec 2025 00:36:08 +0300 Subject: [PATCH 33/36] LAB4 (update 9) --- LibraryCPPTemplate/list.h | 43 +++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/LibraryCPPTemplate/list.h b/LibraryCPPTemplate/list.h index 8078f6299c..082671c69f 100644 --- a/LibraryCPPTemplate/list.h +++ b/LibraryCPPTemplate/list.h @@ -10,7 +10,7 @@ class List { const Item* next() const { return nxt; } Item* prev() { return prv; } const Item* prev() const { return prv; } - Data& data() { return dt; } + Data& data() { return dt; } const Data& data() const { return dt; } private: friend class List; @@ -21,24 +21,27 @@ class List { Item(Data val) : nxt(nullptr), prv(nullptr), dt(val) {} }; + typedef Item* ItemPtr; + typedef const Item* ConstItemPtr; + // Creates new list List() : head(nullptr), tail(nullptr) {} - // copy constructor + // Copy constructor List(const List& a) : head(nullptr), tail(nullptr) { - Item* cur = a.head; + ItemPtr cur = a.head; while (cur != nullptr) { insert(cur->data()); cur = cur->next(); } } - // assignment operator + // Assignment operator List& operator=(const List& a) { if (this == &a) return *this; clear(); - Item* cur = a.head; + ItemPtr cur = a.head; while (cur != nullptr) { insert(cur->data()); cur = cur->next(); @@ -52,18 +55,18 @@ class List { } // Retrieves the first item from the list - Item* first() { + ItemPtr first() { return head; } // Const version of first() - const Item* first() const { + ConstItemPtr first() const { return head; } // Inserts new list item into the beginning - Item* insert(Data data) { - Item* newItem = new Item(data); + ItemPtr insert(Data data) { + ItemPtr newItem = new Item(data); if (head == nullptr) { head = tail = newItem; } @@ -77,11 +80,11 @@ class List { // Inserts new list item after the specified item // Inserts first element if item is null - Item* insert_after(Item* item, Data data) { + ItemPtr insert_after(ItemPtr item, Data data) { if (item == nullptr) { return insert(data); } - Item* newItem = new Item(data); + ItemPtr newItem = new Item(data); newItem->prv = item; newItem->nxt = item->nxt; if (item->nxt != nullptr) { @@ -96,16 +99,16 @@ class List { // Deletes the first list item. // Returns pointer to the item next to the deleted one. - Item* erase_first() { + ItemPtr erase_first() { if (head == nullptr) return nullptr; - Item* toDelete = head; + ItemPtr toDelete = head; head = head->nxt; if (head != nullptr) head->prv = nullptr; else tail = nullptr; - Item* nextItem = head; + ItemPtr nextItem = head; delete toDelete; return nextItem; } @@ -113,12 +116,12 @@ class List { // 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. - Item* erase_next(Item* item) { + ItemPtr erase_next(ItemPtr item) { if (item == nullptr) { // Erase first return erase_first(); } - Item* toDelete = item->nxt; + ItemPtr toDelete = item->nxt; if (toDelete == nullptr) return nullptr; item->nxt = toDelete->nxt; @@ -128,14 +131,14 @@ class List { else { tail = item; } - Item* nextItem = toDelete->nxt; + ItemPtr nextItem = toDelete->nxt; delete toDelete; return nextItem; } private: - Item* head; - Item* tail; + ItemPtr head; + ItemPtr tail; void clear() { while (head != nullptr) { @@ -144,4 +147,4 @@ class List { } }; -#endif \ No newline at end of file +#endif From 30d1691b168f4b40c3d5da64e8e9ece9277ce289 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Thu, 18 Dec 2025 19:29:30 +0300 Subject: [PATCH 34/36] LAB4 (update 10) --- Lab4CPPTemplate/lab4.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Lab4CPPTemplate/lab4.cpp b/Lab4CPPTemplate/lab4.cpp index 611a65735f..f8f085e779 100644 --- a/Lab4CPPTemplate/lab4.cpp +++ b/Lab4CPPTemplate/lab4.cpp @@ -9,6 +9,9 @@ using namespace std; +typedef DirGraph Graph; +typedef Graph::VertexId VertexId; + // Structure to store edge info for Kruskal's algorithm struct EdgeKruskal { size_t from; // start vertex id @@ -69,16 +72,14 @@ class DisjointSet { } }; -// Get all edges of the graph as vector of EdgeKruskal -vector getAllEdges(const DirGraph& graph) { +vector getAllEdges(const Graph& graph) { vector edges; size_t n = graph.getVertexCount(); // Iterate over all vertices for (size_t i = 0; i < n; ++i) { - // Iterate over all neighbors of vertex i for (auto it = graph.neighborsBegin(i); it != graph.neighborsEnd(i); ++it) { - size_t j = *it; + VertexId j = *it; int weight = graph.getEdgeMark(i, j); edges.emplace_back(i, j, weight); } @@ -86,8 +87,7 @@ vector getAllEdges(const DirGraph& graph) { return edges; } -// Compute MST with Kruskal algorithm on given graph -vector kruskalMST(DirGraph& graph) { +vector kruskalMST(Graph& graph) { size_t n = graph.getVertexCount(); if (n < 2) return {}; @@ -112,7 +112,7 @@ vector kruskalMST(DirGraph& graph) { int main(int argc, char* argv[]) { if (argc != 2) { - return 1; + return 1; } ifstream file(argv[1]); @@ -120,8 +120,8 @@ int main(int argc, char* argv[]) { return 1; } - DirGraph graph; - map vertexMap; // Map vertex name to id + Graph graph; + map vertexMap; string line; // Read each line: "vertex1 vertex2 weight" @@ -141,8 +141,8 @@ int main(int argc, char* argv[]) { vertexMap[v2] = graph.addVertex(v2); } - size_t fromId = vertexMap[v1]; - size_t toId = vertexMap[v2]; + VertexId fromId = vertexMap[v1]; + VertexId toId = vertexMap[v2]; // Add edge in both directions to simulate undirected graph!!! graph.addEdge(fromId, toId, weight); graph.addEdge(toId, fromId, weight); From f80505cbe4bba21078155291dcc6c4af75b77c9d Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Sat, 20 Dec 2025 02:02:25 +0300 Subject: [PATCH 35/36] LAB4 (update 11) --- Lab4CPPTemplate/lab4.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Lab4CPPTemplate/lab4.cpp b/Lab4CPPTemplate/lab4.cpp index f8f085e779..3219184dab 100644 --- a/Lab4CPPTemplate/lab4.cpp +++ b/Lab4CPPTemplate/lab4.cpp @@ -26,6 +26,8 @@ struct EdgeKruskal { } }; +typedef vector EdgeVector; + // Disjoint set (union-find) data structure for Kruskal class DisjointSet { private: @@ -72,8 +74,8 @@ class DisjointSet { } }; -vector getAllEdges(const Graph& graph) { - vector edges; +EdgeVector getAllEdges(const Graph& graph) { + EdgeVector edges; size_t n = graph.getVertexCount(); // Iterate over all vertices @@ -87,16 +89,16 @@ vector getAllEdges(const Graph& graph) { return edges; } -vector kruskalMST(Graph& graph) { +EdgeVector kruskalMST(Graph& graph) { size_t n = graph.getVertexCount(); if (n < 2) return {}; // Get all edges and sort by weight ascending (stonks) - vector edges = getAllEdges(graph); + EdgeVector edges = getAllEdges(graph); sort(edges.begin(), edges.end()); DisjointSet dsu(n); - vector mst; + EdgeVector mst; // Iterate edges, add to MST if they connect two different sets for (const auto& edge : edges) { @@ -121,7 +123,7 @@ int main(int argc, char* argv[]) { } Graph graph; - map vertexMap; + map vertexMap; string line; // Read each line: "vertex1 vertex2 weight" @@ -151,7 +153,7 @@ int main(int argc, char* argv[]) { file.close(); // Find minimal spanning tree edges with Kruskal - vector mst = kruskalMST(graph); + EdgeVector mst = kruskalMST(graph); // Output MST edges with vertex names and weights for (const auto& e : mst) { From 7c7d13b86fe66018a46233eb1d163adff39e1106 Mon Sep 17 00:00:00 2001 From: S0NIC-the-hedgehog Date: Tue, 13 Jan 2026 19:02:04 +0300 Subject: [PATCH 36/36] =?UTF-8?q?=D0=B4=D0=B5=D0=B9=D1=81=D1=82=D0=B2?= =?UTF-8?q?=D0=B8=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D0=B0=D1=8F=20=D1=83=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=B0=D1=8F=20=D1=81=D1=81=D1=8B=D0=BB=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BD=D0=B0=20=D1=81=D1=82=D0=B0=D1=82=D1=8C=D1=8E=20?= =?UTF-8?q?=D0=98=D0=A2=D0=9C=D0=9E=20=D0=BE=20=D0=B0=D0=BB=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D1=82=D0=BC=D0=B5=20A*?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://neerc.ifmo.ru/wiki/index.php?title=Алгоритм_A* --- ...\263\320\276\321\200\320\270\321\202\320\274\320\220star.txt" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "\320\260\320\273\320\263\320\276\321\200\320\270\321\202\320\274\320\220star.txt" diff --git "a/\320\260\320\273\320\263\320\276\321\200\320\270\321\202\320\274\320\220star.txt" "b/\320\260\320\273\320\263\320\276\321\200\320\270\321\202\320\274\320\220star.txt" new file mode 100644 index 0000000000..22aa721af3 --- /dev/null +++ "b/\320\260\320\273\320\263\320\276\321\200\320\270\321\202\320\274\320\220star.txt" @@ -0,0 +1 @@ +https://neerc.ifmo.ru/wiki/index.php?title=Алгоритм_A* \ No newline at end of file