diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 92872802..e5f56142 100644 --- a/01_week/tasks/addition/addition.cpp +++ b/01_week/tasks/addition/addition.cpp @@ -2,6 +2,11 @@ #include -int64_t Addition(int a, int b) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file + +int64_t Addition(int a, int b) +{ + return static_cast(a) + static_cast(b); +} + + + diff --git a/01_week/tasks/quadratic/quadratic.cpp b/01_week/tasks/quadratic/quadratic.cpp index abf7d632..0b8b44e0 100644 --- a/01_week/tasks/quadratic/quadratic.cpp +++ b/01_week/tasks/quadratic/quadratic.cpp @@ -1,6 +1,43 @@ #include +#include +#include void SolveQuadratic(int a, int b, int c) { - throw std::runtime_error{"Not implemented"}; + if (a == 0) + { + if (b == 0) { + if (c == 0) { + std::cout << "infinite solutions"; + } + else { + std::cout << "no solutions"; + } + } + else { + double sol = -static_cast(c) / static_cast(b); + std::cout << std::setprecision(6) << sol; + } + } + else { + // Êâàäðàòíîå óðàâíåíèå + double discriminant = static_cast(b) * static_cast(b) - 4.0 * static_cast(a) * static_cast(c); + if (discriminant < 0) { + std::cout << "no solutions"; + } + else if (discriminant == 0) { + double sol = -b / (2.0 * a); + std::cout << std::setprecision(6) << sol; + } + else { + double sqr = std::sqrt(discriminant); + double x1 = (-b - sqr) / (2.0 * a); + double x2 = (-b + sqr) / (2.0 * a); + // Îáåñïå÷èâàåì ïîðÿäîê x1 < x2 + if (x1 > x2) { + std::swap(x1, x2); + } + std::cout << std::setprecision(6) << x1 << " " << x2; + } + } } \ No newline at end of file diff --git a/01_week/tasks/rms/rms.cpp b/01_week/tasks/rms/rms.cpp index 6882f0a9..f4652aaf 100644 --- a/01_week/tasks/rms/rms.cpp +++ b/01_week/tasks/rms/rms.cpp @@ -1,7 +1,18 @@ -#include +#include #include +#include double CalculateRMS(double values[], size_t size) { - throw std::runtime_error{"Not implemented"}; + if (size == 0 || values == nullptr) { + return 0.0; + } + + double sumofSquares = 0.0; + for (size_t i = 0; i < size; ++i) { + sumofSquares += values[i] * values[i]; + } + + double meanofSquares = sumofSquares / static_cast(size); + return std::sqrt(meanofSquares); } \ No newline at end of file diff --git a/02_week/tasks/func_array/func_array.cpp b/02_week/tasks/func_array/func_array.cpp index b327e68d..62f72aac 100644 --- a/02_week/tasks/func_array/func_array.cpp +++ b/02_week/tasks/func_array/func_array.cpp @@ -1,6 +1,18 @@ #include -double ApplyOperations(double a, double b /* other arguments */) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file +double ApplyOperations(double a, double b, double (**funks)(double, double), size_t size) { + if (funks == nullptr || size == 0) { + return 0.0; + } + + double Sum = 0.0; + + for (size_t i = 0; i < size; ++i) { + if (funks[i] != nullptr) { + Sum += funks[i](a, b); + } + } + + return Sum; +} diff --git a/02_week/tasks/last_of_us/last_of_us.cpp b/02_week/tasks/last_of_us/last_of_us.cpp index c7bf1a25..bbd3590a 100644 --- a/02_week/tasks/last_of_us/last_of_us.cpp +++ b/02_week/tasks/last_of_us/last_of_us.cpp @@ -1,6 +1,14 @@ #include -/* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file +int* FindLastElement(int* begin,int* end, bool (*predicate)(int)) { + if (begin == nullptr || end == nullptr || begin > end) { + return end; + } + for (int* p = end - 1; p >= begin; --p) { + if (predicate(*p)) { + return p; + } + } + return end; +} diff --git a/02_week/tasks/swap_ptr/swap_ptr.cpp b/02_week/tasks/swap_ptr/swap_ptr.cpp index 93db625d..685468b0 100644 --- a/02_week/tasks/swap_ptr/swap_ptr.cpp +++ b/02_week/tasks/swap_ptr/swap_ptr.cpp @@ -1,6 +1,8 @@ #include -void SwapPtr(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void SwapPtr(int* a, int* b) { + int* c = a; + a = b; + b = c; } \ No newline at end of file diff --git a/03_week/tasks/filter/filter.cpp b/03_week/tasks/filter/filter.cpp index 6648cb39..1dffd57f 100644 --- a/03_week/tasks/filter/filter.cpp +++ b/03_week/tasks/filter/filter.cpp @@ -1,6 +1,14 @@ #include +#include -/* return_type */ Filter(/* args */) { - throw std::runtime_error{"Not implemented"}; +void Filter(std::vector& v, bool (*predicate)(int)) { + size_t ind = 0; + for (size_t i = 0; i < v.size(); ++i) { + if (predicate(v[i])) { + v[ind] = v[i]; + ++ind; + } + } + v.resize(ind); } \ No newline at end of file diff --git a/03_week/tasks/find_all/find_all.cpp b/03_week/tasks/find_all/find_all.cpp index 74f393b2..f6d4d4ca 100644 --- a/03_week/tasks/find_all/find_all.cpp +++ b/03_week/tasks/find_all/find_all.cpp @@ -1,6 +1,15 @@ #include +#include -/* return_type */ FindAll(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector FindAll(const std::vector& container, bool (*predicate)(int)) { + std::vector cont2; + + for (size_t i = 0; i < container.size(); ++i) { + if (predicate(container[i])) { + cont2.push_back(i); + } + } + + return cont2; } \ No newline at end of file diff --git a/03_week/tasks/unique/unique.cpp b/03_week/tasks/unique/unique.cpp index 9d2545bb..2a00e882 100644 --- a/03_week/tasks/unique/unique.cpp +++ b/03_week/tasks/unique/unique.cpp @@ -1,6 +1,26 @@ #include #include -/* return_type */ Unique(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector Unique(const std::vector& v) { + if (v.empty()) { + return {}; + } + size_t uniq = 1; + for (size_t i = 1; i < v.size(); ++i) { + if (v[i] != v[i - 1]) { + ++uniq; + } + } + std::vector res(uniq); + res[0] = v[0]; + + size_t index = 1; + for (size_t i = 1; i < v.size(); ++i) { + if (v[i] != v[i - 1]) { + res[index] = v[i]; + ++index; + } + } + + return res; } diff --git a/04_week/CMakeLists.txt b/04_week/CMakeLists.txt index 0531237e..fcfe9625 100644 --- a/04_week/CMakeLists.txt +++ b/04_week/CMakeLists.txt @@ -7,9 +7,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Гарантирует иÑпользов set(EXAMPLES_DIR examples) # Определим переменную Ñ Ð¸Ð¼ÐµÐ½ÐµÐ¼ директории set(TASKS_DIR tasks) -add_subdirectory(tasks) +#add_subdirectory(tasks) # Создать иÑполнÑемый файл Ð´Ð»Ñ ÐºÐ°Ð¶Ð´Ð¾Ð³Ð¾ примера if (BUILD_EXAMPLES_04_WEEK) # add_example(struct_examples ${EXAMPLES_DIR}/struct_examples.cpp) -endif() \ No newline at end of file +endif()