From 7793a27992acb5184ab545fcd727c942c60975b6 Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Thu, 12 Sep 2024 23:13:23 +0300 Subject: [PATCH 1/9] =?UTF-8?q?Create=20=D0=90=D0=BD=D0=B4=D1=80=D0=B5?= =?UTF-8?q?=D0=B5=D0=B2=20=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=203092?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20\262 \320\241\320\265\321\200\320\263\320\265\320\271 3092" | 1 + 1 file changed, 1 insertion(+) create mode 100644 "\320\220\320\275\320\264\321\200\320\265\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271 3092" diff --git "a/\320\220\320\275\320\264\321\200\320\265\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271 3092" "b/\320\220\320\275\320\264\321\200\320\265\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271 3092" new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ "b/\320\220\320\275\320\264\321\200\320\265\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271 3092" @@ -0,0 +1 @@ + From a1a2084234b7636fdf5268ae2772227c7ec0f035 Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Thu, 21 Nov 2024 04:00:13 +0300 Subject: [PATCH 2/9] Add files via upload --- CMakeLists.txt | 36 ++++---- Lab1CPP/CMakeLists.txt | 5 ++ Lab1CPP/input.txt | 3 + Lab1CPP/lab1.cpp | 96 +++++++++++++++++++++ LibraryCPP/CMakeLists.txt | 6 +- LibraryCPP/Tests/CMakeLists.txt | 52 +++++------ LibraryCPP/Tests/array.cpp | 59 ++++++------- LibraryCPP/Tests/list.cpp | 96 ++++++++++----------- LibraryCPP/Tests/queue.cpp | 102 +++++++++++----------- LibraryCPP/Tests/stack.cpp | 114 ++++++++++++------------ LibraryCPP/Tests/vector.cpp | 148 ++++++++++++++++---------------- LibraryCPP/array.cpp | 19 +++- LibraryCPP/array.h | 54 ++++++------ LibraryCPP/list.cpp | 122 +++++++++++++------------- LibraryCPP/list.h | 90 +++++++++---------- LibraryCPP/queue.cpp | 68 +++++++-------- LibraryCPP/queue.h | 60 ++++++------- LibraryCPP/stack.cpp | 68 +++++++-------- LibraryCPP/stack.h | 60 ++++++------- LibraryCPP/vector.cpp | 68 +++++++-------- LibraryCPP/vector.h | 62 ++++++------- 21 files changed, 754 insertions(+), 634 deletions(-) create mode 100644 Lab1CPP/CMakeLists.txt create mode 100644 Lab1CPP/input.txt create mode 100644 Lab1CPP/lab1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b40c0dd11c..32beb34385 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,18 @@ -cmake_minimum_required(VERSION 3.15) -project(Algorithms) - -enable_testing() -set(CMAKE_COMPILE_WARNING_AS_ERROR ON) -if (MSVC) - # needs at least cmake 3.15 - add_compile_options(/W4 /WX) -else() - add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter) -endif() - -add_subdirectory(LibraryC) -add_subdirectory(LibraryCPP) -add_subdirectory(LibraryCPPClass) -add_subdirectory(LibraryCPPTemplate) - -add_subdirectory(Lab1C) +cmake_minimum_required(VERSION 3.15) +project(Algorithms-1) + +enable_testing() +set(CMAKE_COMPILE_WARNING_AS_ERROR ON) +if (MSVC) + # needs at least cmake 3.15 + add_compile_options(/W4 /WX) +else() + add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter) +endif() + +#add_subdirectory(LibraryC) +add_subdirectory(LibraryCPP) +#add_subdirectory(LibraryCPPClass) +#add_subdirectory(LibraryCPPTemplate) + +add_subdirectory(Lab1CPP) diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt new file mode 100644 index 0000000000..5522d3c408 --- /dev/null +++ b/Lab1CPP/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(Lab1CPP lab1.cpp) +target_include_directories(Lab1CPP PUBLIC ../LibraryCPP) +target_link_libraries(Lab1CPP LibraryCPP) + +add_test(NAME TestLab1CPP COMMAND Lab1CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) diff --git a/Lab1CPP/input.txt b/Lab1CPP/input.txt new file mode 100644 index 0000000000..3bba6dd2a0 --- /dev/null +++ b/Lab1CPP/input.txt @@ -0,0 +1,3 @@ +30 2 5 +50 -10 10 +-10 0 \ No newline at end of file diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp new file mode 100644 index 0000000000..a62bd3217a --- /dev/null +++ b/Lab1CPP/lab1.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include "array.h" + +Array *array_create_and_read(std::ifstream& input) +{ + //std::cout << "acar"; //для дебага + size_t n; + int start; + int end; + + if (!(input >> n) || n < 1) { + std::cout << "Failed"; + return 0; + } + if (!(input >> start)) { + std::cout << "Failed"; + return 0; + } + if (!(input >> end) || end < start) { + std::cout << "Failed"; + return 0; + } + Array* arr = array_create(n); + for (size_t i = 0; i < n; ++i) { + int x = rand() % (end - start + 1) + start; + array_set(arr, i, x); + } + return arr; +} + +void task1(std::ifstream& input) +{ + //std::cout << "t1\n"; //для дебага + Array* arr = array_create_and_read(input); + size_t c5 = 0, c4 = 0, c3= 0, c2 = 0; + + for(int i =0 ; i < array_size(arr); i++) { + std::cout << array_get(arr, i) << " "; + if(array_get(arr, i) == 5) {c5++;} + else if(array_get(arr, i) == 4) {c4++;} + else if(array_get(arr, i) == 3) {c3++;} + else if(array_get(arr, i) == 2) {c2++;} + } + + std::cout << "\nколичество 5: " << c5 << "\nколичество 4: " << c4 << "\nколичество 3: " << c3 <<"\nколичество 2: " << c2; +} + +void task2(std::ifstream& input) +{ + //std::cout << "t2\n"; //для дебага + + Array* arr = array_create_and_read(input); + int start, end, counter = 0; + if (!(input >> start)) { + std::cout << "Failed"; + return; + } + if (!(input >> end || end < start)) { + std::cout << "Failed"; + return; + } + + for(int i =0 ; i < array_size(arr); i++) { + std::cout << array_get(arr, i) << " "; + int temp = array_get(arr, i); + array_set(arr, i, 0); + if( temp > end || temp < start) { + array_set(arr, counter, temp); + counter++; + } + } + std::cout << "\n\n"; + for(int i =0 ; i < array_size(arr); i++) { + std::cout << array_get(arr, i) << " "; + } +} + +int main() +{ + srand((unsigned int)time(NULL)); + //std::cout << "Hello world"; + std::ifstream input("input.txt"); + if (input.is_open()) { + + std::cout << " task 1:\n"; + task1(input); + + std::cout << "\n---------\n\n task 2:\n"; + task2(input); + } + input.close(); + return 0; +} \ No newline at end of file diff --git a/LibraryCPP/CMakeLists.txt b/LibraryCPP/CMakeLists.txt index dbd7769feb..bf0e7448fd 100644 --- a/LibraryCPP/CMakeLists.txt +++ b/LibraryCPP/CMakeLists.txt @@ -1,3 +1,3 @@ -add_library(LibraryCPP STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp) - -add_subdirectory(Tests) +add_library(LibraryCPP STATIC array.cpp) + +add_subdirectory(Tests) diff --git a/LibraryCPP/Tests/CMakeLists.txt b/LibraryCPP/Tests/CMakeLists.txt index c903f9d7f3..825aa4ebb7 100644 --- a/LibraryCPP/Tests/CMakeLists.txt +++ b/LibraryCPP/Tests/CMakeLists.txt @@ -1,26 +1,26 @@ -# 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(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(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(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(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(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/Tests/array.cpp b/LibraryCPP/Tests/array.cpp index 69f54e42c8..ffe1aa8600 100644 --- a/LibraryCPP/Tests/array.cpp +++ b/LibraryCPP/Tests/array.cpp @@ -1,29 +1,30 @@ -#include -#include "array.h" - -int main() -{ - Array *arr = array_create(10); - - if (array_size(arr) != 10) - { - std::cout << "Invalid array size\n"; - array_delete(arr); - return 1; - } - - for (int i = 0 ; i < 10 ; ++i) - array_set(arr, i, i * 2); - - for (int i = 0 ; i < 10 ; ++i) - { - if (array_get(arr, i) != i * 2) - { - std::cout << "Invalid array element " << i << "\n"; - array_delete(arr); - return 1; - } - } - - array_delete(arr); -} +#include +#include "array.h" + +int main() +{ + Array *arr = array_create(10); + + if (array_size(arr) != 10) + { + std::cout << "Invalid array size\n"; + array_delete(arr); + return 1; + } + + for (int i = 0 ; i < 10 ; ++i) + array_set(arr, i, i * 2); + + for (int i = 0 ; i < 10 ; ++i) + { + if (array_get(arr, i) != i * 2) + { + std::cout << "Invalid array element " << i << "\n"; + array_delete(arr); + return 1; + } + } + std::cout << "\nTest end\n"; + + array_delete(arr); +} diff --git a/LibraryCPP/Tests/list.cpp b/LibraryCPP/Tests/list.cpp index 26862d4820..d67615c9e4 100644 --- a/LibraryCPP/Tests/list.cpp +++ b/LibraryCPP/Tests/list.cpp @@ -1,48 +1,48 @@ -#include -#include "list.h" - -int main() -{ - List *list = list_create(); - - if (!list) - { - std::cout << "List creation error\n"; - return 1; - } - - list_insert(list, 1); - list_insert(list, 2); - list_insert(list, 3); - - if (list_item_data(list_first(list)) != 3) - { - std::cout << "list_insert error\n"; - return 1; - } - - list_insert_after(list, list_first(list), 4); - - if (list_item_data(list_item_next(list_first(list))) != 4) - { - std::cout << "list_insert_after error\n"; - return 1; - } - - list_erase_first(list); - - if (list_item_data(list_first(list)) != 4) - { - std::cout << "list_erase error\n"; - return 1; - } - - std::cout << "List: "; - for (ListItem *item = list_first(list) ; item ; item = list_item_next(item)) - { - std::cout << list_item_data(item) << " "; - } - std::cout << "\n"; - - list_delete(list); -} +#include +#include "list.h" + +int main() +{ + List *list = list_create(); + + if (!list) + { + std::cout << "List creation error\n"; + return 1; + } + + list_insert(list, 1); + list_insert(list, 2); + list_insert(list, 3); + + if (list_item_data(list_first(list)) != 3) + { + std::cout << "list_insert error\n"; + return 1; + } + + list_insert_after(list, list_first(list), 4); + + if (list_item_data(list_item_next(list_first(list))) != 4) + { + std::cout << "list_insert_after error\n"; + return 1; + } + + list_erase_first(list); + + if (list_item_data(list_first(list)) != 4) + { + std::cout << "list_erase error\n"; + return 1; + } + + std::cout << "List: "; + for (ListItem *item = list_first(list) ; item ; item = list_item_next(item)) + { + std::cout << list_item_data(item) << " "; + } + std::cout << "\n"; + + list_delete(list); +} diff --git a/LibraryCPP/Tests/queue.cpp b/LibraryCPP/Tests/queue.cpp index 8645b911a4..ac7b34218e 100644 --- a/LibraryCPP/Tests/queue.cpp +++ b/LibraryCPP/Tests/queue.cpp @@ -1,51 +1,51 @@ -#include -#include "queue.h" - -int main() -{ - Queue *queue = queue_create(); - - queue_insert(queue, 1); - queue_insert(queue, 2); - queue_insert(queue, 3); - - if (queue_get(queue) != 1) - { - std::cout << "Invalid first element of the queue\n"; - return 1; - } - - std::cout << "Get: " << queue_get(queue) << "\n"; - queue_remove(queue); - - if (queue_get(queue) != 2) - { - std::cout << "Invalid second element of the queue\n"; - return 1; - } - - std::cout << "Get: " << queue_get(queue) << "\n"; - queue_remove(queue); - - if (queue_get(queue) != 3) - { - std::cout << "Invalid third element of the queue\n"; - return 1; - } - - std::cout << "Get: " << queue_get(queue) << "\n"; - queue_insert(queue, 4); - while (!queue_empty(queue)) - { - std::cout << "Get: " << queue_get(queue) << "\n"; - queue_remove(queue); - } - - // Performance test - for (int i = 1 ; i <= 10000000 ; ++i) - { - queue_insert(queue, i); - } - - queue_delete(queue); -} +#include +#include "queue.h" + +int main() +{ + Queue *queue = queue_create(); + + queue_insert(queue, 1); + queue_insert(queue, 2); + queue_insert(queue, 3); + + if (queue_get(queue) != 1) + { + std::cout << "Invalid first element of the queue\n"; + return 1; + } + + std::cout << "Get: " << queue_get(queue) << "\n"; + queue_remove(queue); + + if (queue_get(queue) != 2) + { + std::cout << "Invalid second element of the queue\n"; + return 1; + } + + std::cout << "Get: " << queue_get(queue) << "\n"; + queue_remove(queue); + + if (queue_get(queue) != 3) + { + std::cout << "Invalid third element of the queue\n"; + return 1; + } + + std::cout << "Get: " << queue_get(queue) << "\n"; + queue_insert(queue, 4); + while (!queue_empty(queue)) + { + std::cout << "Get: " << queue_get(queue) << "\n"; + queue_remove(queue); + } + + // Performance test + for (int i = 1 ; i <= 10000000 ; ++i) + { + queue_insert(queue, i); + } + + queue_delete(queue); +} diff --git a/LibraryCPP/Tests/stack.cpp b/LibraryCPP/Tests/stack.cpp index 0fac2b5813..759ec1d3ff 100644 --- a/LibraryCPP/Tests/stack.cpp +++ b/LibraryCPP/Tests/stack.cpp @@ -1,57 +1,57 @@ -#include -#include "stack.h" - -int main() -{ - Stack *stack = stack_create(); - - stack_push(stack, 1); - stack_push(stack, 2); - stack_push(stack, 3); - - if (stack_get(stack) != 3) - { - std::cout << "Invalid stack top after push\n"; - stack_delete(stack); - return 1; - } - - std::cout << "Get: " << stack_get(stack) << "\n"; - stack_pop(stack); - - if (stack_get(stack) != 2) - { - std::cout << "Invalid stack top after pop\n"; - stack_delete(stack); - return 1; - } - - std::cout << "Get: " << stack_get(stack) << "\n"; - stack_pop(stack); - - if (stack_get(stack) != 1) - { - std::cout << "Invalid stack top after pop\n"; - stack_delete(stack); - return 1; - } - - std::cout << "Get: " << stack_get(stack) << "\n"; - stack_push(stack, 4); - stack_push(stack, 5); - - if (stack_get(stack) != 5) - { - std::cout << "Invalid stack top after push\n"; - stack_delete(stack); - return 1; - } - - while (!stack_empty(stack)) - { - std::cout << "Get: " << stack_get(stack) << "\n"; - stack_pop(stack); - } - - stack_delete(stack); -} +#include +#include "stack.h" + +int main() +{ + Stack *stack = stack_create(); + + stack_push(stack, 1); + stack_push(stack, 2); + stack_push(stack, 3); + + if (stack_get(stack) != 3) + { + std::cout << "Invalid stack top after push\n"; + stack_delete(stack); + return 1; + } + + std::cout << "Get: " << stack_get(stack) << "\n"; + stack_pop(stack); + + if (stack_get(stack) != 2) + { + std::cout << "Invalid stack top after pop\n"; + stack_delete(stack); + return 1; + } + + std::cout << "Get: " << stack_get(stack) << "\n"; + stack_pop(stack); + + if (stack_get(stack) != 1) + { + std::cout << "Invalid stack top after pop\n"; + stack_delete(stack); + return 1; + } + + std::cout << "Get: " << stack_get(stack) << "\n"; + stack_push(stack, 4); + stack_push(stack, 5); + + if (stack_get(stack) != 5) + { + std::cout << "Invalid stack top after push\n"; + stack_delete(stack); + return 1; + } + + while (!stack_empty(stack)) + { + std::cout << "Get: " << stack_get(stack) << "\n"; + stack_pop(stack); + } + + stack_delete(stack); +} diff --git a/LibraryCPP/Tests/vector.cpp b/LibraryCPP/Tests/vector.cpp index 525ed56ef9..14bb99be27 100644 --- a/LibraryCPP/Tests/vector.cpp +++ b/LibraryCPP/Tests/vector.cpp @@ -1,74 +1,74 @@ -#include -#include "vector.h" - -int main() -{ - Vector *vector = vector_create(); - - vector_resize(vector, 5); - if (vector_size(vector) != 5) - { - std::cout << "Invalid resize\n"; - return 1; - } - - for (size_t i = 0 ; i < vector_size(vector) ; ++i) - vector_set(vector, i, i); - - for (size_t i = 0 ; i < vector_size(vector) ; ++i) - { - if (vector_get(vector, i) != (int)i) - { - std::cout << "Invalid vector element " << i << "\n"; - return 1; - } - } - - vector_resize(vector, 10); - if (vector_size(vector) != 10) - { - std::cout << "Invalid resize\n"; - return 1; - } - - std::cout << "Vector: "; - for (size_t i = 0 ; i < vector_size(vector) ; ++i) - std::cout << vector_get(vector, i) << " "; - std::cout << "\n"; - - vector_resize(vector, 3); - if (vector_size(vector) != 3) - { - std::cout << "Invalid resize\n"; - return 1; - } - - for (size_t i = 0 ; i < vector_size(vector) ; ++i) - { - if (vector_get(vector, i) != (int)i) - { - std::cout << "Invalid vector element " << i << "\n"; - return 1; - } - } - - std::cout << "Vector: "; - for (size_t i = 0 ; i < vector_size(vector) ; ++i) - std::cout << vector_get(vector, i) << " "; - std::cout << "\n"; - - // Performance test - for (int i = 1 ; i <= 10000000 ; ++i) - { - vector_resize(vector, i); - vector_set(vector, i - 1, i); - } - - long long sum = 0; - for (int i = 0 ; i < 10000000 ; ++i) - sum += vector_get(vector, i); - - std::cout << sum << "\n"; - - vector_delete(vector); -} +#include +#include "vector.h" + +int main() +{ + Vector *vector = vector_create(); + + vector_resize(vector, 5); + if (vector_size(vector) != 5) + { + std::cout << "Invalid resize\n"; + return 1; + } + + for (size_t i = 0 ; i < vector_size(vector) ; ++i) + vector_set(vector, i, i); + + for (size_t i = 0 ; i < vector_size(vector) ; ++i) + { + if (vector_get(vector, i) != (int)i) + { + std::cout << "Invalid vector element " << i << "\n"; + return 1; + } + } + + vector_resize(vector, 10); + if (vector_size(vector) != 10) + { + std::cout << "Invalid resize\n"; + return 1; + } + + std::cout << "Vector: "; + for (size_t i = 0 ; i < vector_size(vector) ; ++i) + std::cout << vector_get(vector, i) << " "; + std::cout << "\n"; + + vector_resize(vector, 3); + if (vector_size(vector) != 3) + { + std::cout << "Invalid resize\n"; + return 1; + } + + for (size_t i = 0 ; i < vector_size(vector) ; ++i) + { + if (vector_get(vector, i) != (int)i) + { + std::cout << "Invalid vector element " << i << "\n"; + return 1; + } + } + + std::cout << "Vector: "; + for (size_t i = 0 ; i < vector_size(vector) ; ++i) + std::cout << vector_get(vector, i) << " "; + std::cout << "\n"; + + // Performance test + for (int i = 1 ; i <= 10000000 ; ++i) + { + vector_resize(vector, i); + vector_set(vector, i - 1, i); + } + + long long sum = 0; + for (int i = 0 ; i < 10000000 ; ++i) + sum += vector_get(vector, i); + + std::cout << sum << "\n"; + + vector_delete(vector); +} diff --git a/LibraryCPP/array.cpp b/LibraryCPP/array.cpp index c773e5f167..3308957540 100644 --- a/LibraryCPP/array.cpp +++ b/LibraryCPP/array.cpp @@ -2,33 +2,48 @@ struct Array { + Data* values; + size_t size; }; // create array Array *array_create(size_t size) { - return new Array; + if (size != 0) { + Array* arr = new Array; + arr->size = size; + arr->values = new Data[size]; + return arr; + } + return 0; } // delete array, free memory void array_delete(Array *arr) { + delete[] arr->values; delete arr; } // returns specified array element Data array_get(const Array *arr, size_t index) { + if (index < arr->size) { + return arr->values[index]; + } return (Data)0; } // sets the specified array element to the value void array_set(Array *arr, size_t index, Data value) { + if (index < arr->size) { + arr->values[index] = value; + } } // returns array size size_t array_size(const Array *arr) { - return 0; + return arr->size; } diff --git a/LibraryCPP/array.h b/LibraryCPP/array.h index b541ebe8b4..d38b701a75 100644 --- a/LibraryCPP/array.h +++ b/LibraryCPP/array.h @@ -1,27 +1,27 @@ -#ifndef ARRAY_H -#define ARRAY_H - -#include - -// Non-resizeable array -// Stores integer values inside -typedef int Data; - -struct Array; - -// create array -Array *array_create(size_t size); - -// delete array, free memory -void array_delete(Array *arr); - -// returns specified array element -Data array_get(const Array *arr, size_t index); - -// sets the specified array element to the value -void array_set(Array *arr, size_t index, Data value); - -// returns array size -size_t array_size(const Array *arr); - -#endif +#ifndef ARRAY_H +#define ARRAY_H + +#include + +// Non-resizeable array +// Stores integer values inside +typedef int Data; + +struct Array; + +// create array +Array *array_create(size_t size); + +// delete array, free memory +void array_delete(Array *arr); + +// returns specified array element +Data array_get(const Array *arr, size_t index); + +// sets the specified array element to the value +void array_set(Array *arr, size_t index, Data value); + +// returns array size +size_t array_size(const Array *arr); + +#endif diff --git a/LibraryCPP/list.cpp b/LibraryCPP/list.cpp index a8946b5793..06dff38fae 100644 --- a/LibraryCPP/list.cpp +++ b/LibraryCPP/list.cpp @@ -1,61 +1,61 @@ -#include -#include "list.h" - -struct ListItem -{ -}; - -struct List -{ -}; - -List *list_create() -{ - return new List; -} - -void list_delete(List *list) -{ - // TODO: free items - delete list; -} - -ListItem *list_first(List *list) -{ - return NULL; -} - -Data list_item_data(const ListItem *item) -{ - return (Data)0; -} - -ListItem *list_item_next(ListItem *item) -{ - return NULL; -} - -ListItem *list_item_prev(ListItem *item) -{ - return NULL; -} - -ListItem *list_insert(List *list, Data data) -{ - return NULL; -} - -ListItem *list_insert_after(List *list, ListItem *item, Data data) -{ - return NULL; -} - -ListItem *list_erase_first(List *list) -{ - return NULL; -} - -ListItem *list_erase_next(List *list, ListItem *item) -{ - return NULL; -} +#include +#include "list.h" + +struct ListItem +{ +}; + +struct List +{ +}; + +List *list_create() +{ + return new List; +} + +void list_delete(List *list) +{ + // TODO: free items + delete list; +} + +ListItem *list_first(List *list) +{ + return NULL; +} + +Data list_item_data(const ListItem *item) +{ + return (Data)0; +} + +ListItem *list_item_next(ListItem *item) +{ + return NULL; +} + +ListItem *list_item_prev(ListItem *item) +{ + return NULL; +} + +ListItem *list_insert(List *list, Data data) +{ + return NULL; +} + +ListItem *list_insert_after(List *list, ListItem *item, Data data) +{ + return NULL; +} + +ListItem *list_erase_first(List *list) +{ + return NULL; +} + +ListItem *list_erase_next(List *list, ListItem *item) +{ + return NULL; +} diff --git a/LibraryCPP/list.h b/LibraryCPP/list.h index f1fa0f14b8..59a9d193c0 100644 --- a/LibraryCPP/list.h +++ b/LibraryCPP/list.h @@ -1,45 +1,45 @@ -#ifndef LIST_H -#define LIST_H - -// List -// Stores integer values inside -typedef int Data; - -struct List; -struct ListItem; - -// Creates new list -List *list_create(); - -// Destroys the list and frees the memory -void list_delete(List *list); - -// Retrieves the first item from the list -ListItem *list_first(List *list); - -// Extracts data from the list item -Data list_item_data(const ListItem *item); - -// Returns list item following after the specified one -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); - -// Inserts new list item into the beginning -ListItem *list_insert(List *list, Data data); - -// Inserts new list item after the specified item -ListItem *list_insert_after(List *list, ListItem *item, Data data); - -// Deletes the first list item. -// Returns pointer to the item next to the deleted one. -ListItem *list_erase_first(List *list); - -// Deletes the list item following the specified one. -// Returns pointer to the item next to the deleted one. -// Should be O(1) -ListItem *list_erase_next(List *list, ListItem *item); - -#endif +#ifndef LIST_H +#define LIST_H + +// List +// Stores integer values inside +typedef int Data; + +struct List; +struct ListItem; + +// Creates new list +List *list_create(); + +// Destroys the list and frees the memory +void list_delete(List *list); + +// Retrieves the first item from the list +ListItem *list_first(List *list); + +// Extracts data from the list item +Data list_item_data(const ListItem *item); + +// Returns list item following after the specified one +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); + +// Inserts new list item into the beginning +ListItem *list_insert(List *list, Data data); + +// Inserts new list item after the specified item +ListItem *list_insert_after(List *list, ListItem *item, Data data); + +// Deletes the first list item. +// Returns pointer to the item next to the deleted one. +ListItem *list_erase_first(List *list); + +// Deletes the list item following the specified one. +// Returns pointer to the item next to the deleted one. +// Should be O(1) +ListItem *list_erase_next(List *list, ListItem *item); + +#endif diff --git a/LibraryCPP/queue.cpp b/LibraryCPP/queue.cpp index a9b4730488..d6b4511d78 100644 --- a/LibraryCPP/queue.cpp +++ b/LibraryCPP/queue.cpp @@ -1,34 +1,34 @@ -#include "queue.h" - -struct Queue -{ -}; - -Queue *queue_create() -{ - return new Queue; -} - -void queue_delete(Queue *queue) -{ - // TODO: free queue items - delete queue; -} - -void queue_insert(Queue *queue, Data data) -{ -} - -Data queue_get(const Queue *queue) -{ - return (Data)0; -} - -void queue_remove(Queue *queue) -{ -} - -bool queue_empty(const Queue *queue) -{ - return true; -} +#include "queue.h" + +struct Queue +{ +}; + +Queue *queue_create() +{ + return new Queue; +} + +void queue_delete(Queue *queue) +{ + // TODO: free queue items + delete queue; +} + +void queue_insert(Queue *queue, Data data) +{ +} + +Data queue_get(const Queue *queue) +{ + return (Data)0; +} + +void queue_remove(Queue *queue) +{ +} + +bool queue_empty(const Queue *queue) +{ + return true; +} diff --git a/LibraryCPP/queue.h b/LibraryCPP/queue.h index 54704d962c..0ab9ae173b 100644 --- a/LibraryCPP/queue.h +++ b/LibraryCPP/queue.h @@ -1,30 +1,30 @@ -#ifndef QUEUE_H -#define QUEUE_H - -// Queue -// Stores integer values inside -typedef int Data; - -struct Queue; - -// Create empty queue -Queue *queue_create(); - -// Deletes queue -void queue_delete(Queue *queue); - -// Includes new element into the queue -// Should be O(1) on average -void queue_insert(Queue *queue, Data data); - -// Retrieves first element from the queue -Data queue_get(const Queue *queue); - -// Removes first element from the queue -// Should be O(1) on average -void queue_remove(Queue *queue); - -// Returns true if the queue is empty -bool queue_empty(const Queue *queue); - -#endif +#ifndef QUEUE_H +#define QUEUE_H + +// Queue +// Stores integer values inside +typedef int Data; + +struct Queue; + +// Create empty queue +Queue *queue_create(); + +// Deletes queue +void queue_delete(Queue *queue); + +// Includes new element into the queue +// Should be O(1) on average +void queue_insert(Queue *queue, Data data); + +// Retrieves first element from the queue +Data queue_get(const Queue *queue); + +// Removes first element from the queue +// Should be O(1) on average +void queue_remove(Queue *queue); + +// Returns true if the queue is empty +bool queue_empty(const Queue *queue); + +#endif diff --git a/LibraryCPP/stack.cpp b/LibraryCPP/stack.cpp index c090abba1c..419b16243d 100644 --- a/LibraryCPP/stack.cpp +++ b/LibraryCPP/stack.cpp @@ -1,34 +1,34 @@ -#include "stack.h" - -struct Stack -{ -}; - -Stack *stack_create() -{ - return new Stack; -} - -void stack_delete(Stack *stack) -{ - // TODO: free stack elements - delete stack; -} - -void stack_push(Stack *stack, Data data) -{ -} - -Data stack_get(const Stack *stack) -{ - return (Data)0; -} - -void stack_pop(Stack *stack) -{ -} - -bool stack_empty(const Stack *stack) -{ - return true; -} +#include "stack.h" + +struct Stack +{ +}; + +Stack *stack_create() +{ + return new Stack; +} + +void stack_delete(Stack *stack) +{ + // TODO: free stack elements + delete stack; +} + +void stack_push(Stack *stack, Data data) +{ +} + +Data stack_get(const Stack *stack) +{ + return (Data)0; +} + +void stack_pop(Stack *stack) +{ +} + +bool stack_empty(const Stack *stack) +{ + return true; +} diff --git a/LibraryCPP/stack.h b/LibraryCPP/stack.h index 7c5dd97af2..b782d04b8d 100644 --- a/LibraryCPP/stack.h +++ b/LibraryCPP/stack.h @@ -1,30 +1,30 @@ -#ifndef STACK_H -#define STACK_H - -// Stack -// Stores integer values inside -typedef int Data; - -struct Stack; - -// Creates empty stack -Stack *stack_create(); - -// Deletes the stack -void stack_delete(Stack *stack); - -// Pushes data on top of the stack -// Should be O(1) on average -void stack_push(Stack *stack, Data data); - -// Retrives the last element from the stack -Data stack_get(const Stack *stack); - -// Removes the last element from the stack -// Should be O(1) -void stack_pop(Stack *stack); - -// Returns true if the stack is empty -bool stack_empty(const Stack *stack); - -#endif +#ifndef STACK_H +#define STACK_H + +// Stack +// Stores integer values inside +typedef int Data; + +struct Stack; + +// Creates empty stack +Stack *stack_create(); + +// Deletes the stack +void stack_delete(Stack *stack); + +// Pushes data on top of the stack +// Should be O(1) on average +void stack_push(Stack *stack, Data data); + +// Retrives the last element from the stack +Data stack_get(const Stack *stack); + +// Removes the last element from the stack +// Should be O(1) +void stack_pop(Stack *stack); + +// Returns true if the stack is empty +bool stack_empty(const Stack *stack); + +#endif diff --git a/LibraryCPP/vector.cpp b/LibraryCPP/vector.cpp index aee7157b54..ae2d822b80 100644 --- a/LibraryCPP/vector.cpp +++ b/LibraryCPP/vector.cpp @@ -1,34 +1,34 @@ -#include "vector.h" - -struct Vector -{ -}; - -Vector *vector_create() -{ - return new Vector; -} - -void vector_delete(Vector *vector) -{ - // TODO: free vector internals - delete vector; -} - -Data vector_get(const Vector *vector, size_t index) -{ - return (Data)0; -} - -void vector_set(Vector *vector, size_t index, Data value) -{ -} - -size_t vector_size(const Vector *vector) -{ - return 0; -} - -void vector_resize(Vector *vector, size_t size) -{ -} +#include "vector.h" + +struct Vector +{ +}; + +Vector *vector_create() +{ + return new Vector; +} + +void vector_delete(Vector *vector) +{ + // TODO: free vector internals + delete vector; +} + +Data vector_get(const Vector *vector, size_t index) +{ + return (Data)0; +} + +void vector_set(Vector *vector, size_t index, Data value) +{ +} + +size_t vector_size(const Vector *vector) +{ + return 0; +} + +void vector_resize(Vector *vector, size_t size) +{ +} diff --git a/LibraryCPP/vector.h b/LibraryCPP/vector.h index b15b97737a..e54028d3e6 100644 --- a/LibraryCPP/vector.h +++ b/LibraryCPP/vector.h @@ -1,31 +1,31 @@ -#ifndef VECTOR_H -#define VECTOR_H - -#include - -// Vector (dynamic array) -// Stores integer values inside -typedef int Data; - -struct Vector; - -// Creates vector -Vector *vector_create(); - -// Deletes vector structure and internal data -void vector_delete(Vector *vector); - -// Retrieves vector element with the specified index -Data vector_get(const Vector *vector, size_t index); - -// Sets vector element with the specified index -void vector_set(Vector *vector, size_t index, Data value); - -// Retrieves current vector size -size_t vector_size(const Vector *vector); - -// Changes the vector size (may increase or decrease) -// Should be O(1) on average -void vector_resize(Vector *vector, size_t size); - -#endif +#ifndef VECTOR_H +#define VECTOR_H + +#include + +// Vector (dynamic array) +// Stores integer values inside +typedef int Data; + +struct Vector; + +// Creates vector +Vector *vector_create(); + +// Deletes vector structure and internal data +void vector_delete(Vector *vector); + +// Retrieves vector element with the specified index +Data vector_get(const Vector *vector, size_t index); + +// Sets vector element with the specified index +void vector_set(Vector *vector, size_t index, Data value); + +// Retrieves current vector size +size_t vector_size(const Vector *vector); + +// Changes the vector size (may increase or decrease) +// Should be O(1) on average +void vector_resize(Vector *vector, size_t size); + +#endif From 40fd7220ba1f65ff774cfd8f17cdafaae5435391 Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Thu, 21 Nov 2024 04:14:46 +0300 Subject: [PATCH 3/9] Update lab1.cpp --- Lab1CPP/lab1.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index a62bd3217a..2a18d43e54 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -37,7 +37,7 @@ void task1(std::ifstream& input) Array* arr = array_create_and_read(input); size_t c5 = 0, c4 = 0, c3= 0, c2 = 0; - for(int i =0 ; i < array_size(arr); i++) { + for(size_t i =0 ; i < array_size(arr); i++) { std::cout << array_get(arr, i) << " "; if(array_get(arr, i) == 5) {c5++;} else if(array_get(arr, i) == 4) {c4++;} @@ -63,7 +63,7 @@ void task2(std::ifstream& input) return; } - for(int i =0 ; i < array_size(arr); i++) { + for(size_t i =0 ; i < array_size(arr); i++) { std::cout << array_get(arr, i) << " "; int temp = array_get(arr, i); array_set(arr, i, 0); @@ -73,7 +73,7 @@ void task2(std::ifstream& input) } } std::cout << "\n\n"; - for(int i =0 ; i < array_size(arr); i++) { + for(size_t i =0 ; i < array_size(arr); i++) { std::cout << array_get(arr, i) << " "; } } @@ -93,4 +93,4 @@ int main() } input.close(); return 0; -} \ No newline at end of file +} From 3a3834caa747a85406abb449a16bfd24cc6c1eb1 Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Fri, 22 Nov 2024 02:53:07 +0300 Subject: [PATCH 4/9] Update lab1.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit теперь имя файла берется из аргументов. А по поводу заполнения нулями хвоста массива, я это делаю на строчке 69. --- Lab1CPP/lab1.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index 2a18d43e54..9fb2761cf6 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -65,24 +65,25 @@ void task2(std::ifstream& input) for(size_t i =0 ; i < array_size(arr); i++) { std::cout << array_get(arr, i) << " "; - int temp = array_get(arr, i); - array_set(arr, i, 0); + int temp = array_get(arr, i); //запоминаю значение + array_set(arr, i, 0); //а в массиве ячейке присаиваю значение 0. Так все ячейки которые не будут изменены дальше будут 0 как раз таки заполняя нулями хвост if( temp > end || temp < start) { - array_set(arr, counter, temp); + array_set(arr, counter, temp); counter++; } } std::cout << "\n\n"; - for(size_t i =0 ; i < array_size(arr); i++) { + for(size_t i =0 ; i < array_size(arr); i++) { //а тут я уже не меняю массив просто вывожу std::cout << array_get(arr, i) << " "; } } -int main() +int main(int argc, char **argv) { srand((unsigned int)time(NULL)); - //std::cout << "Hello world"; - std::ifstream input("input.txt"); + //std::ifstream input("input.txt"); + if(argc <= 1) return 0; + std::ifstream input(argv[1]); if (input.is_open()) { std::cout << " task 1:\n"; From 102c1234ca994f1e2cb3d813c40e3e7cb12d15b9 Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Fri, 22 Nov 2024 03:05:48 +0300 Subject: [PATCH 5/9] Update lab1.cpp --- Lab1CPP/lab1.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index 9fb2761cf6..69b8dfda00 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -80,18 +80,17 @@ void task2(std::ifstream& input) int main(int argc, char **argv) { - srand((unsigned int)time(NULL)); - //std::ifstream input("input.txt"); - if(argc <= 1) return 0; - std::ifstream input(argv[1]); + srand((unsigned int)time(NULL)); + //std::ifstream input("input.txt"); + std::ifstream input(argv[1]); if (input.is_open()) { - std::cout << " task 1:\n"; - task1(input); + std::cout << " task 1:\n"; + task1(input); - std::cout << "\n---------\n\n task 2:\n"; - task2(input); - } + std::cout << "\n---------\n\n task 2:\n"; + task2(input); + } input.close(); - return 0; + return 0; } From 7b08675cad30c7da4c656cec6c1467f7dccf0794 Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Fri, 22 Nov 2024 03:10:19 +0300 Subject: [PATCH 6/9] Update lab1.cpp --- Lab1CPP/lab1.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index 69b8dfda00..b240deaf3e 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -81,8 +81,8 @@ void task2(std::ifstream& input) int main(int argc, char **argv) { srand((unsigned int)time(NULL)); - //std::ifstream input("input.txt"); - std::ifstream input(argv[1]); + std::ifstream input("input.txt"); + //std::ifstream input(argv[1]); if (input.is_open()) { std::cout << " task 1:\n"; From 7a6489459e07d7fd3f06c39c106a05b512c39a73 Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Fri, 22 Nov 2024 03:31:10 +0300 Subject: [PATCH 7/9] Update lab1.cpp --- Lab1CPP/lab1.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lab1CPP/lab1.cpp b/Lab1CPP/lab1.cpp index b240deaf3e..69b8dfda00 100644 --- a/Lab1CPP/lab1.cpp +++ b/Lab1CPP/lab1.cpp @@ -81,8 +81,8 @@ void task2(std::ifstream& input) int main(int argc, char **argv) { srand((unsigned int)time(NULL)); - std::ifstream input("input.txt"); - //std::ifstream input(argv[1]); + //std::ifstream input("input.txt"); + std::ifstream input(argv[1]); if (input.is_open()) { std::cout << " task 1:\n"; From a9687027090d46bf021665d17c0f307ddf6f693f Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Fri, 22 Nov 2024 03:31:33 +0300 Subject: [PATCH 8/9] Update CMakeLists.txt --- Lab1CPP/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab1CPP/CMakeLists.txt b/Lab1CPP/CMakeLists.txt index 5522d3c408..32f54bfd58 100644 --- a/Lab1CPP/CMakeLists.txt +++ b/Lab1CPP/CMakeLists.txt @@ -2,4 +2,4 @@ add_executable(Lab1CPP lab1.cpp) target_include_directories(Lab1CPP PUBLIC ../LibraryCPP) target_link_libraries(Lab1CPP LibraryCPP) -add_test(NAME TestLab1CPP COMMAND Lab1CPP ${CMAKE_CURRENT_SOURCE_DIR}/input.txt) +add_test(NAME TestLab1CPP COMMAND Lab1CPP input.txt) From db7a4628b72a70ebeb7a6dda8a68cda6d22ecfa1 Mon Sep 17 00:00:00 2001 From: andreev-s-k <94913476+andreev-s-k@users.noreply.github.com> Date: Fri, 22 Nov 2024 03:35:17 +0300 Subject: [PATCH 9/9] Update lab1.cpp