From 318cf97ed3713e41d30cbb0ec69b1ead1eea6122 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Fri, 28 Nov 2025 13:12:03 +0700 Subject: [PATCH 01/25] add (solution): add addition, check_flags, lenght_lit, print_bits, quadratic, rms tasks --- 01_week/tasks/addition/addition.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 92872802..471be4ce 100644 --- a/01_week/tasks/addition/addition.cpp +++ b/01_week/tasks/addition/addition.cpp @@ -1,7 +1,6 @@ #include #include - int64_t Addition(int a, int b) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file + return (int64_t)a + (int64_t)b; +} From 190ea2054d5fd1fcde162e6869d5e7238803bc48 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Fri, 28 Nov 2025 13:29:54 +0700 Subject: [PATCH 02/25] add (solution): add check_flags task --- 01_week/tasks/check_flags/check_flags.cpp | 32 +++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/01_week/tasks/check_flags/check_flags.cpp b/01_week/tasks/check_flags/check_flags.cpp index 75e7c652..68ddf3ac 100644 --- a/01_week/tasks/check_flags/check_flags.cpp +++ b/01_week/tasks/check_flags/check_flags.cpp @@ -1,6 +1,7 @@ #include #include - +#include +#include enum class CheckFlags : uint8_t { NONE = 0, @@ -14,5 +15,32 @@ enum class CheckFlags : uint8_t { }; void PrintCheckFlags(CheckFlags flags) { - throw std::runtime_error{"Not implemented"}; + uint8_t value = static_cast(flags); + + if (value > static_cast(CheckFlags::ALL)) { + return; + } + + std::string result = "["; + bool first = true; + + const std::pair flagNames[] = { + {CheckFlags::TIME, "TIME"}, + {CheckFlags::DATE, "DATE"}, + {CheckFlags::USER, "USER"}, + {CheckFlags::CERT, "CERT"}, + {CheckFlags::KEYS, "KEYS"}, + {CheckFlags::DEST, "DEST"} + }; + + for (const auto& [flag, name] : flagNames) { + if (value & static_cast(flag)) { + if (!first) result += ","; + result += name; + first = false; + } + } + + result += "]"; + std::cout << result; } From 58fbe23a230a83a93797d707ebdca1878fc1a404 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Sat, 29 Nov 2025 00:49:38 +0700 Subject: [PATCH 03/25] add (solution): add length_lit task --- 01_week/tasks/length_lit/length_lit.cpp | 104 ++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/01_week/tasks/length_lit/length_lit.cpp b/01_week/tasks/length_lit/length_lit.cpp index e69de29b..3aba3e20 100644 --- a/01_week/tasks/length_lit/length_lit.cpp +++ b/01_week/tasks/length_lit/length_lit.cpp @@ -0,0 +1,104 @@ +#include + +constexpr double FOOT_TO_METER = 0.3048; +constexpr double INCH_TO_METER = 0.0254; +constexpr double METER_TO_FOOT = 1.0 / FOOT_TO_METER; +constexpr double METER_TO_INCH = 1.0 / INCH_TO_METER; +constexpr double CM_TO_METER = 0.01; +constexpr double METER_TO_CM = 100.0; + +constexpr double operator"" _ft_to_m(long double feet) { + return static_cast(feet) * FOOT_TO_METER; +} + +constexpr double operator"" _ft_to_m(unsigned long long feet) { + return static_cast(feet) * FOOT_TO_METER; +} + +constexpr double operator"" _ft_to_cm(long double feet) { + return static_cast(feet) * FOOT_TO_METER * METER_TO_CM; +} + +constexpr double operator"" _ft_to_cm(unsigned long long feet) { + return static_cast(feet) * FOOT_TO_METER * METER_TO_CM; +} + +constexpr double operator"" _ft_to_in(long double feet) { + return static_cast(feet) * 12.0; +} + +constexpr double operator"" _ft_to_in(unsigned long long feet) { + return static_cast(feet) * 12.0; +} + +constexpr double operator"" _in_to_m(long double inches) { + return static_cast(inches) * INCH_TO_METER; +} + +constexpr double operator"" _in_to_m(unsigned long long inches) { + return static_cast(inches) * INCH_TO_METER; +} + +constexpr double operator"" _in_to_cm(long double inches) { + return static_cast(inches) * INCH_TO_METER * METER_TO_CM; +} + +constexpr double operator"" _in_to_cm(unsigned long long inches) { + return static_cast(inches) * INCH_TO_METER * METER_TO_CM; +} + +constexpr double operator"" _in_to_ft(long double inches) { + return static_cast(inches) / 12.0; +} + +constexpr double operator"" _in_to_ft(unsigned long long inches) { + return static_cast(inches) / 12.0; +} + +constexpr double operator"" _m_to_ft(long double meters) { + return static_cast(meters) * METER_TO_FOOT; +} + +constexpr double operator"" _m_to_ft(unsigned long long meters) { + return static_cast(meters) * METER_TO_FOOT; +} + +constexpr double operator"" _m_to_in(long double meters) { + return static_cast(meters) * METER_TO_INCH; +} + +constexpr double operator"" _m_to_in(unsigned long long meters) { + return static_cast(meters) * METER_TO_INCH; +} + +constexpr double operator"" _m_to_cm(long double meters) { + return static_cast(meters) * METER_TO_CM; +} + +constexpr double operator"" _m_to_cm(unsigned long long meters) { + return static_cast(meters) * METER_TO_CM; +} + +constexpr double operator"" _cm_to_m(long double centimeters) { + return static_cast(centimeters) * CM_TO_METER; +} + +constexpr double operator"" _cm_to_m(unsigned long long centimeters) { + return static_cast(centimeters) * CM_TO_METER; +} + +constexpr double operator"" _cm_to_ft(long double centimeters) { + return static_cast(centimeters) * CM_TO_METER * METER_TO_FOOT; +} + +constexpr double operator"" _cm_to_ft(unsigned long long centimeters) { + return static_cast(centimeters) * CM_TO_METER * METER_TO_FOOT; +} + +constexpr double operator"" _cm_to_in(long double centimeters) { + return static_cast(centimeters) * CM_TO_METER * METER_TO_INCH; +} + +constexpr double operator"" _cm_to_in(unsigned long long centimeters) { + return static_cast(centimeters) * CM_TO_METER * METER_TO_INCH; +} From 3573ba11df514f764e0a86babca76f5b62c1b613 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Sat, 29 Nov 2025 00:52:01 +0700 Subject: [PATCH 04/25] add (solution): add print_bits task --- 01_week/tasks/print_bits/print_bits.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/01_week/tasks/print_bits/print_bits.cpp b/01_week/tasks/print_bits/print_bits.cpp index a48a43c1..1a3c286c 100644 --- a/01_week/tasks/print_bits/print_bits.cpp +++ b/01_week/tasks/print_bits/print_bits.cpp @@ -1,7 +1,22 @@ #include -#include - +#include void PrintBits(long long value, size_t bytes) { - throw std::runtime_error{"Not implemented"}; + if (bytes == 0 || bytes > 8) { + return; + } + + std::cout << "0b"; + + int total_bits = static_cast(bytes) * 8; + for (int i = total_bits - 1; i >= 0; i--) { + unsigned long long bit = (static_cast(value) >> i) & 1; + std::cout << bit; + + if (i > 0 && i % 4 == 0) { + std::cout << "'"; + } + } + + std::cout << std::endl; } From 15ec4a42ba2712d5105fb5fb60851850c62500ff Mon Sep 17 00:00:00 2001 From: VaryVA Date: Sat, 29 Nov 2025 00:53:10 +0700 Subject: [PATCH 05/25] add (solution): add quadratic task --- 01_week/tasks/quadratic/quadratic.cpp | 49 +++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/01_week/tasks/quadratic/quadratic.cpp b/01_week/tasks/quadratic/quadratic.cpp index abf7d632..0e13c46e 100644 --- a/01_week/tasks/quadratic/quadratic.cpp +++ b/01_week/tasks/quadratic/quadratic.cpp @@ -1,6 +1,49 @@ +#include +#include +#include +#include #include - void SolveQuadratic(int a, int b, int c) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file + if (a == 0) { + if (b == 0) { + if (c == 0) { + std::cout << "infinite solutions"; + } + else { + std::cout << "no solutions"; + } + } + else { + double x = -static_cast(c) / b; + if (x == 0.0) x = 0.0; + std::cout << std::setprecision(6) << x; + } + return; + } + + double discriminant = static_cast(b) * b - 4.0 * a * c; + + if (discriminant < 0) { + std::cout << "no solutions"; + } + else if (discriminant == 0) { + double x = -static_cast(b) / (2.0 * a); + if (x == 0.0) x = 0.0; + std::cout << std::setprecision(6) << x; + } + else { + double sqrt_d = std::sqrt(discriminant); + double x1 = (-static_cast(b) - sqrt_d) / (2.0 * a); + double x2 = (-static_cast(b) + sqrt_d) / (2.0 * a); + + if (x1 == 0.0) x1 = 0.0; + if (x2 == 0.0) x2 = 0.0; + + if (x1 > x2) { + std::swap(x1, x2); + } + + std::cout << std::setprecision(6) << x1 << " " << x2; + } +} From 3fcc0bcd010215cdea95b1ed738a7fc52d2b49af Mon Sep 17 00:00:00 2001 From: VaryVA Date: Sat, 29 Nov 2025 00:54:15 +0700 Subject: [PATCH 06/25] add (solution): add rms task --- 01_week/tasks/rms/rms.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/01_week/tasks/rms/rms.cpp b/01_week/tasks/rms/rms.cpp index 6882f0a9..d228bbfa 100644 --- a/01_week/tasks/rms/rms.cpp +++ b/01_week/tasks/rms/rms.cpp @@ -1,7 +1,17 @@ -#include -#include - +#include +#include double CalculateRMS(double values[], size_t size) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file + if (size == 0) { + return 0.0; + } + + double sum_of_squares = 0.0; + for (size_t i = 0; i < size; ++i) { + sum_of_squares += values[i] * values[i]; + } + + double mean_of_squares = sum_of_squares / size; + + return std::sqrt(mean_of_squares); +} From f174bae82050762e5fcf457673f96b76ede8c494 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 8 Dec 2025 22:24:36 +0700 Subject: [PATCH 07/25] add (solution): add func_array task --- 02_week/tasks/func_array/func_array.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/02_week/tasks/func_array/func_array.cpp b/02_week/tasks/func_array/func_array.cpp index b327e68d..cd6990d4 100644 --- a/02_week/tasks/func_array/func_array.cpp +++ b/02_week/tasks/func_array/func_array.cpp @@ -1,6 +1,12 @@ #include +typedef double (*MathOperation)(double, double); -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, MathOperation operations[], size_t size) { + if (operations == nullptr || size == 0) return 0.0; + + double sum = 0.0; + for (size_t i = 0; i < size; ++i) sum += operations[i] ? operations[i](a, b) : 0.0; + + return sum; +} From fc312f942fcd6b4330904c2c6eafa49e8e835846 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 8 Dec 2025 22:42:16 +0700 Subject: [PATCH 08/25] =?UTF-8?q?add=20(solution):=20add=20last=5Fof=5Fus?= =?UTF-8?q?=20task=1B[C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02_week/tasks/last_of_us/last_of_us.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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..26b0ff00 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,17 @@ #include +typedef bool (*Predicate)(int); -/* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file +const int* FindLastElement(const int* begin, const int* end, Predicate predicate) { + if (begin == nullptr || end == nullptr || begin > end || predicate == nullptr) { + return end; + } + + for (const int* current = end - 1; current >= begin; --current) { + if (predicate(*current)) { + return current; + } + } + + return end; +} From 837fe51dc9c4af46a6342c1b6af27a54e8419907 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 8 Dec 2025 22:44:09 +0700 Subject: [PATCH 09/25] add (solution): add little_big task --- 02_week/tasks/little_big/little_big.cpp | 34 +++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/02_week/tasks/little_big/little_big.cpp b/02_week/tasks/little_big/little_big.cpp index abe24379..4e5c4b2f 100644 --- a/02_week/tasks/little_big/little_big.cpp +++ b/02_week/tasks/little_big/little_big.cpp @@ -1,10 +1,34 @@ +#include +#include +#include #include +template +void PrintMemoryImpl(T value, bool reverseBytes) { + unsigned char bytes[sizeof(T)]; + std::memcpy(bytes, &value, sizeof(T)); + + std::cout << "0x"; + + if (reverseBytes) { + for (int i = sizeof(T) - 1; i >= 0; --i) { + std::cout << std::hex << std::setfill('0') << std::setw(2) + << std::uppercase << static_cast(bytes[i]); + } + } else { + for (size_t i = 0; i < sizeof(T); ++i) { + std::cout << std::hex << std::setfill('0') << std::setw(2) + << std::uppercase << static_cast(bytes[i]); + } + } + + std::cout << std::dec << std::endl; +} -void PrintMemory(int /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void PrintMemory(int value, bool reverseBytes = false) { + PrintMemoryImpl(value, reverseBytes); } -void PrintMemory(double /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file +void PrintMemory(double value, bool reverseBytes = false) { + PrintMemoryImpl(value, reverseBytes); +} From 6ea5c9fc5786691cdfbe80ac9f688aad94bebb3e Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 8 Dec 2025 22:45:05 +0700 Subject: [PATCH 10/25] add (solution): add longest task --- 02_week/tasks/longest/longest.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/02_week/tasks/longest/longest.cpp b/02_week/tasks/longest/longest.cpp index 04b3c354..dc3d6aff 100644 --- a/02_week/tasks/longest/longest.cpp +++ b/02_week/tasks/longest/longest.cpp @@ -1,6 +1,31 @@ #include +template +CharPtr FindLongestSubsequenceImpl(CharPtr begin, CharPtr end, size_t& count) { + if (!begin || !end || begin >= end) { + count = 0; + return nullptr; + } + + CharPtr best = begin; + CharPtr current = begin; + size_t bestLen = 1; + size_t currentLen = 1; + + for (CharPtr p = begin + 1; p < end; ++p) { + (*p == *(p - 1)) ? + (currentLen++, (currentLen > bestLen) ? (void)(bestLen = currentLen, best = current) : (void)0) : + (void)(current = p, currentLen = 1); + } + + count = bestLen; + return best; +} + +char* FindLongestSubsequence(char* begin, char* end, size_t& count) { + return FindLongestSubsequenceImpl(begin, end, count); +} -/* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { - throw std::runtime_error{"Not implemented"}; +const char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) { + return FindLongestSubsequenceImpl(begin, end, count); } From 98e5d59bc9a98d12e75eda2b89b873cc2d444488 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 8 Dec 2025 22:46:05 +0700 Subject: [PATCH 11/25] add (solution): add pretty_array task --- 02_week/tasks/pretty_array/pretty_array.cpp | 61 +++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/02_week/tasks/pretty_array/pretty_array.cpp b/02_week/tasks/pretty_array/pretty_array.cpp index 48eab341..af0322f8 100644 --- a/02_week/tasks/pretty_array/pretty_array.cpp +++ b/02_week/tasks/pretty_array/pretty_array.cpp @@ -1,6 +1,59 @@ +#include #include - -void PrintArray(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file +void PrintArray(const int* begin, const int* end, int limit = 0) { + if (begin == nullptr || end == nullptr) { + std::cout << "[]\n"; + return; + } + + if (begin == end) { + std::cout << "[]\n"; + return; + } + + bool reverse = (end < begin); + + int count = 0; + if (!reverse) { + count = static_cast(end - begin); + } else { + count = static_cast(begin - end); + } + + if (count == 0) { + std::cout << "[]\n"; + return; + } + + std::cout << "["; + + int elements_in_current_line = 0; + bool first_element = true; + + for (int i = 0; i < count; i++) { + const int* current; + if (!reverse) { + current = begin + i; + } else { + current = begin - i; + } + + if (!first_element) { + std::cout << ", "; + } + + std::cout << *current; + elements_in_current_line++; + + if (limit > 0 && elements_in_current_line >= limit && i < count - 1) { + std::cout << ", ...\n "; + elements_in_current_line = 0; + first_element = true; + } else { + first_element = false; + } + } + + std::cout << "]\n"; +} From 3a9a4193fa6b06d3badb156663423b17328a2c07 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 8 Dec 2025 22:48:10 +0700 Subject: [PATCH 12/25] add (solution): add swap_ptr task --- 02_week/tasks/swap_ptr/swap_ptr.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/02_week/tasks/swap_ptr/swap_ptr.cpp b/02_week/tasks/swap_ptr/swap_ptr.cpp index 93db625d..07bbbc01 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"}; -} \ No newline at end of file +template +void SwapPtr(T& ptr1, T& ptr2) { + T temp = ptr1; + ptr1 = ptr2; + ptr2 = temp; +} From e66f289e07cbac4df0fef5b1fb2697d377bee407 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Tue, 17 Feb 2026 01:06:50 +0700 Subject: [PATCH 13/25] add (solution): add cow string task --- 05_week/tasks/cow_string/cow_string.cpp | 200 ++++++++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/05_week/tasks/cow_string/cow_string.cpp b/05_week/tasks/cow_string/cow_string.cpp index 34d59738..b5be9058 100644 --- a/05_week/tasks/cow_string/cow_string.cpp +++ b/05_week/tasks/cow_string/cow_string.cpp @@ -2,5 +2,205 @@ #include class CowString { +private: + struct StringData { + char* data; + size_t size; + size_t ref_count; + StringData(const char* str = "") { + size = std::strlen(str); + data = new char[size + 1]; + std::memcpy(data, str, size + 1); + ref_count = 1; + } + + ~StringData() { + delete[] data; + } + }; + + StringData* m_data; + + void Release() { + if (m_data && --m_data->ref_count == 0) { + delete m_data; + } + } + + void Detach() { + if (m_data->ref_count > 1) { + StringData* newData = new StringData(m_data->data); + --m_data->ref_count; + m_data = newData; + } + } + +public: + static inline const size_t npos = static_cast(-1); + + // ---- Constructors ---- + + CowString() + : m_data(new StringData("")) {} + + CowString(const char* str) + : m_data(new StringData(str ? str : "")) {} + + CowString(const std::string& str) + : m_data(new StringData(str.c_str())) {} + + CowString(const CowString& other) + : m_data(other.m_data) { + ++m_data->ref_count; + } + + CowString(CowString&& other) noexcept + : m_data(other.m_data) { + other.m_data = nullptr; + } + + // ---- Assignment ---- + + CowString& operator=(const CowString& other) { + if (this != &other) { + Release(); + m_data = other.m_data; + ++m_data->ref_count; + } + return *this; + } + + CowString& operator=(CowString&& other) noexcept { + if (this != &other) { + Release(); + m_data = other.m_data; + other.m_data = nullptr; + } + return *this; + } + + // ---- Destructor ---- + + ~CowString() { + Release(); + } + + // ---- Non-modifying ---- + + size_t Size() const { + return m_data ? m_data->size : 0; + } + + bool Empty() const { + return Size() == 0; + } + + const char* ToCstr() const { + return m_data ? m_data->data : ""; + } + + std::string ToString() const { + return std::string(ToCstr()); + } + + operator const char*() const { + return ToCstr(); + } + + const char& operator[](size_t index) const { + return m_data->data[index]; + } + + // ---- Modifying (COW) ---- + + char& operator[](size_t index) { + Detach(); + return m_data->data[index]; + } + + CowString& Append(const char* str) { + if (!str) return *this; + + size_t addSize = std::strlen(str); + if (addSize == 0) { + return *this; // ВАЖНО: без Detach() + } + + Detach(); + + size_t newSize = m_data->size + addSize; + char* newBuffer = new char[newSize + 1]; + + std::memcpy(newBuffer, m_data->data, m_data->size); + std::memcpy(newBuffer + m_data->size, str, addSize + 1); + + delete[] m_data->data; + + m_data->data = newBuffer; + m_data->size = newSize; + + return *this; + } + + CowString& Append(const std::string& str) { + return Append(str.c_str()); + } + + CowString Substr(size_t pos = 0, size_t count = npos) const { + if (pos >= Size()) { + return CowString(""); + } + + if (count == npos || pos + count > Size()) { + count = Size() - pos; + } + + char* buffer = new char[count + 1]; + std::memcpy(buffer, m_data->data + pos, count); + buffer[count] = '\0'; + + CowString result(buffer); + delete[] buffer; + + return result; + } + + void Clear() { + Detach(); + + delete[] m_data->data; + m_data->data = new char[1]; + m_data->data[0] = '\0'; + m_data->size = 0; + } + + // ---- Find ---- + + size_t Find(const char* str) const { + if (!str) return npos; + + size_t len = std::strlen(str); + if (len == 0) return 0; + if (len > Size()) return npos; + + for (size_t i = 0; i <= Size() - len; ++i) { + if (std::memcmp(m_data->data + i, str, len) == 0) { + return i; + } + } + + return npos; + } + + size_t Find(char ch) const { + for (size_t i = 0; i < Size(); ++i) { + if (m_data->data[i] == ch) { + return i; + } + } + return npos; + } }; + + From ab6a48ad0b46972bece812f428225d4c755d3e40 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Tue, 17 Feb 2026 01:34:09 +0700 Subject: [PATCH 14/25] add (solution): add simple_vector task --- 05_week/tasks/simple_vector/simple_vector.cpp | 249 +++++++++++++++++- 1 file changed, 247 insertions(+), 2 deletions(-) diff --git a/05_week/tasks/simple_vector/simple_vector.cpp b/05_week/tasks/simple_vector/simple_vector.cpp index 9b2ea971..8821b3a0 100644 --- a/05_week/tasks/simple_vector/simple_vector.cpp +++ b/05_week/tasks/simple_vector/simple_vector.cpp @@ -1,5 +1,250 @@ - +#include +#include +#include class SimpleVector { +public: + // Конструкторы + + SimpleVector() + : data_(nullptr), size_(0), capacity_(0) {} + + explicit SimpleVector(size_t size) + : data_(new int[size]()), size_(size), capacity_(size) {} + + SimpleVector(size_t size, int value) + : data_(new int[size]), size_(size), capacity_(size) { + for (size_t i = 0; i < size_; ++i) { + data_[i] = value; + } + } + + SimpleVector(std::initializer_list init) + : data_(new int[init.size()]), + size_(init.size()), + capacity_(init.size()) { + std::copy(init.begin(), init.end(), data_); + } + + SimpleVector(const SimpleVector& other) + : data_(new int[other.capacity_]), + size_(other.size_), + capacity_(other.capacity_) { + std::copy(other.data_, other.data_ + size_, data_); + } + + SimpleVector(SimpleVector&& other) noexcept + : data_(other.data_), + size_(other.size_), + capacity_(other.capacity_) { + other.data_ = nullptr; + other.size_ = 0; + other.capacity_ = 0; + } + + // Присваивание + SimpleVector& operator=(const SimpleVector& other) { + if (this != &other) { + SimpleVector tmp(other); + Swap(tmp); + } + return *this; + } + + SimpleVector& operator=(SimpleVector&& other) noexcept { + if (this != &other) { + delete[] data_; + + data_ = other.data_; + size_ = other.size_; + capacity_ = other.capacity_; + + other.data_ = nullptr; + other.size_ = 0; + other.capacity_ = 0; + } + return *this; + } + + + ~SimpleVector() { + delete[] data_; + } + + // Основная часть + + void Swap(SimpleVector& other) noexcept { + std::swap(data_, other.data_); + std::swap(size_, other.size_); + std::swap(capacity_, other.capacity_); + } + + int& operator[](size_t index) { + return data_[index]; + } + + const int& operator[](size_t index) const { + return data_[index]; + } + + size_t Size() const { + return size_; + } + + size_t Capacity() const { + return capacity_; + } + + bool Empty() const { + return size_ == 0; + } + + const int* Data() const { + return data_; + } + + // Push / Pop + + void PushBack(int value) { + if (size_ == capacity_) { + size_t new_capacity = (capacity_ == 0) ? 1 : capacity_ * 2; + Reserve(new_capacity); + } + data_[size_++] = value; + } + + void PopBack() { + if (size_ > 0) { + --size_; + } + } + + // Insert + + int* Insert(const int* pos, int value) { + if (pos < data_ || pos > data_ + size_) { + return data_ + size_; + } + + size_t index = pos - data_; + + if (size_ == capacity_) { + size_t new_capacity = (capacity_ == 0) ? 1 : capacity_ * 2; + Reserve(new_capacity); + } + + for (size_t i = size_; i > index; --i) { + data_[i] = data_[i - 1]; + } + + data_[index] = value; + ++size_; + + return data_ + index; + } + + // Erase + + int* Erase(const int* pos) { + if (pos < data_ || pos >= data_ + size_) { + // некорректная позиция → вернуть конец вектора + return data_ + size_; + } + + size_t index = pos - data_; + + for (size_t i = index; i + 1 < size_; ++i) { + data_[i] = data_[i + 1]; + } + + --size_; + + return data_ + index; + } + + // Clear + + void Clear() { + size_ = 0; + } + + // Resize + + void Resize(size_t new_size, int value = 0) { + if (new_size < size_) { + size_ = new_size; + } else if (new_size > size_) { + if (new_size > capacity_) { + Reserve(new_size); + } + for (size_t i = size_; i < new_size; ++i) { + data_[i] = value; + } + size_ = new_size; + } + } + + // Reserve + + void Reserve(size_t new_capacity) { + if (new_capacity <= capacity_) { + return; + } + + int* new_data = new int[new_capacity]; + + for (size_t i = 0; i < size_; ++i) { + new_data[i] = data_[i]; + } + + delete[] data_; + data_ = new_data; + capacity_ = new_capacity; + } + + + int* begin() { + return data_; + } + + int* end() { + return data_ + size_; + } + + const int* begin() const { + return data_; + } + + const int* end() const { + return data_ + size_; + } + + // Сравнение + + bool operator==(const SimpleVector& other) const { + if (size_ != other.size_) { + return false; + } + for (size_t i = 0; i < size_; ++i) { + if (data_[i] != other.data_[i]) { + return false; + } + } + return true; + } + + bool operator!=(const SimpleVector& other) const { + return !(*this == other); + } + +private: + int* data_; + size_t size_; + size_t capacity_; +}; + +inline void swap(SimpleVector& lhs, SimpleVector& rhs) noexcept { + lhs.Swap(rhs); +} + -}; \ No newline at end of file From c51ee71a66f23d1d8c60b2d3489a99edec2b5e22 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Tue, 17 Feb 2026 01:44:05 +0700 Subject: [PATCH 15/25] add (solution): add string_view task --- 05_week/tasks/string_view/string_view.cpp | 104 +++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/05_week/tasks/string_view/string_view.cpp b/05_week/tasks/string_view/string_view.cpp index 438c4536..8e8f8d75 100644 --- a/05_week/tasks/string_view/string_view.cpp +++ b/05_week/tasks/string_view/string_view.cpp @@ -1,7 +1,107 @@ #include #include - +#include +#include class StringView { +public: + StringView() : data_(nullptr), size_(0) {} + + StringView(const std::string& str, size_t pos = 0, size_t len = std::string::npos) { + if (pos > str.size()) { + data_ = nullptr; + size_ = 0; + } else { + data_ = str.data() + pos; + size_ = std::min(len, str.size() - pos); + } + } + + StringView(const char* str) { + data_ = str; + size_ = str ? std::strlen(str) : 0; + } + + StringView(const char* str, size_t len) { + data_ = str; + size_ = str ? len : 0; + } + + StringView(const StringView& other) = default; + + char operator[](size_t index) const { + if (index >= size_) throw std::out_of_range("Index out of range"); + return data_[index]; + } + + const char* Data() const { return data_; } + + char Front() const { + if (Empty()) throw std::out_of_range("StringView is empty"); + return data_[0]; + } + + char Back() const { + if (Empty()) throw std::out_of_range("StringView is empty"); + return data_[size_ - 1]; + } + + size_t Size() const { return size_; } + size_t Length() const { return size_; } + + bool Empty() const { return size_ == 0; } + + void RemovePrefix(size_t n) { + if (n >= size_) { + data_ = nullptr; + size_ = 0; + } else { + data_ += n; + size_ -= n; + } + } + + void RemoveSuffix(size_t n) { + if (n >= size_) { + data_ = nullptr; + size_ = 0; + } else { + size_ -= n; + } + } + + StringView Substr(size_t pos = 0, size_t len = std::string::npos) const { + if (pos > size_) return StringView(nullptr, 0); + return StringView(data_ + pos, std::min(len, size_ - pos)); + } + + size_t Find(char ch, size_t pos = 0) const { + if (pos >= size_) return npos; + for (size_t i = pos; i < size_; ++i) { + if (data_[i] == ch) return i; + } + return npos; + } + + size_t Find(const StringView& sv, size_t pos = 0) const { + if (sv.size_ == 0) return pos <= size_ ? pos : npos; + if (sv.size_ > size_ || pos > size_ - sv.size_) return npos; + + for (size_t i = pos; i <= size_ - sv.size_; ++i) { + if (std::memcmp(data_ + i, sv.data_, sv.size_) == 0) + return i; + } + return npos; + } + + std::string ToString() const { + return data_ ? std::string(data_, size_) : std::string(); + } + + inline static const size_t npos = static_cast(-1); + +private: + const char* data_ = nullptr; + size_t size_ = 0; +}; -}; \ No newline at end of file From ad92d81e80fbcfdd76ab978e9f5c61f4293e9f75 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Tue, 17 Feb 2026 01:50:37 +0700 Subject: [PATCH 16/25] add (solution): add tracer task --- 05_week/tasks/tracer/tracer.cpp | 97 ++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) diff --git a/05_week/tasks/tracer/tracer.cpp b/05_week/tasks/tracer/tracer.cpp index 2ccfb417..4223dd0f 100644 --- a/05_week/tasks/tracer/tracer.cpp +++ b/05_week/tasks/tracer/tracer.cpp @@ -1,6 +1,99 @@ #include - +#include class Tracer { +private: + int id_; + std::string name_; +public: + // Статические счетчики + inline static int count = 0; + inline static int default_ctor = 0; + inline static int str_ctor = 0; + inline static int copy_ctor = 0; + inline static int move_ctor = 0; + inline static int copy_assign = 0; + inline static int move_assign = 0; + inline static int dtor = 0; + inline static int alive = 0; + + // Конструктор по умолчанию + Tracer() : id_(++count), name_("obj_" + std::to_string(id_)) { + ++default_ctor; + ++alive; + } + + // Конструктор от строки + Tracer(const std::string& name) : id_(++count), name_(name + "_" + std::to_string(id_)) { + ++str_ctor; + ++alive; + } + + // Конструктор копирования + Tracer(const Tracer& other) : id_(++count), name_(other.name_) { + ++copy_ctor; + ++alive; + } + + // Конструктор перемещения + Tracer(Tracer&& other) noexcept : id_(++count), name_(std::move(other.name_)) { + ++move_ctor; + ++alive; + } + + // Оператор копирующего присваивания + Tracer& operator=(const Tracer& other) { + if (this != &other) { + name_ = other.name_; + ++copy_assign; + } + return *this; + } + + // Оператор перемещающего присваивания + Tracer& operator=(Tracer&& other) noexcept { + if (this != &other) { + name_ = std::move(other.name_); + ++move_assign; + } + return *this; + } + + // Деструктор + ~Tracer() { + ++dtor; + --alive; + } + + // Методы доступа + int Id() const { return id_; } + const std::string& Name() const { return name_; } + const char* Data() const { return name_.data(); } + + // Сброс статистики + static void ResetStats() { + count = 0; + default_ctor = 0; + str_ctor = 0; + copy_ctor = 0; + move_ctor = 0; + copy_assign = 0; + move_assign = 0; + dtor = 0; + alive = 0; + } + + // Для отладки — вывод статистики + static void PrintStats() { + std::cout << "count: " << count + << ", default_ctor: " << default_ctor + << ", str_ctor: " << str_ctor + << ", copy_ctor: " << copy_ctor + << ", move_ctor: " << move_ctor + << ", copy_assign: " << copy_assign + << ", move_assign: " << move_assign + << ", dtor: " << dtor + << ", alive: " << alive << "\n"; + } +}; -}; \ No newline at end of file From c0a7a6d242f5707630b02f102e90a4d6e418cee2 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 23 Feb 2026 22:29:19 +0700 Subject: [PATCH 17/25] add (solution): add simple_list task --- 06_week/tasks/simple_list/simple_list.cpp | 175 +++++++++++++++++++++- 1 file changed, 174 insertions(+), 1 deletion(-) diff --git a/06_week/tasks/simple_list/simple_list.cpp b/06_week/tasks/simple_list/simple_list.cpp index e97b2a81..8569f660 100644 --- a/06_week/tasks/simple_list/simple_list.cpp +++ b/06_week/tasks/simple_list/simple_list.cpp @@ -1,5 +1,178 @@ #include +#include class SimpleList { +private: + // Структура узла списка + struct Node { + std::string value; + Node* next; + Node* prev; + + explicit Node(const std::string& val) : value(val), next(nullptr), prev(nullptr) {} + explicit Node(std::string&& val) : value(std::move(val)), next(nullptr), prev(nullptr) {} + }; + + Node* head; + Node* tail; + size_t count; + + void Unlink(Node* node) { + if (!node) return; + + if (node->prev) { + node->prev->next = node->next; + } else { + head = node->next; + } + + if (node->next) { + node->next->prev = node->prev; + } else { + tail = node->prev; + } + + delete node; + --count; + } + + void LinkBefore(Node* newNode, Node* beforeNode) { + if (!beforeNode) { + if (!head) { + head = tail = newNode; + } else { + tail->next = newNode; + newNode->prev = tail; + tail = newNode; + } + } else { + newNode->prev = beforeNode->prev; + newNode->next = beforeNode; + + if (beforeNode->prev) { + beforeNode->prev->next = newNode; + } else { + head = newNode; + } + + beforeNode->prev = newNode; + } + + ++count; + } -}; \ No newline at end of file +public: + SimpleList() : head(nullptr), tail(nullptr), count(0) {} + + SimpleList(const SimpleList& other) : head(nullptr), tail(nullptr), count(0) { + Node* current = other.head; + while (current) { + PushBack(current->value); + current = current->next; + } + } + + SimpleList(SimpleList&& other) noexcept + : head(other.head), tail(other.tail), count(other.count) { + other.head = nullptr; + other.tail = nullptr; + other.count = 0; + } + + ~SimpleList() { + Clear(); + } + + SimpleList& operator=(const SimpleList& other) { + if (this != &other) { + SimpleList temp(other); + Swap(temp); + } + return *this; + } + + SimpleList& operator=(SimpleList&& other) noexcept { + if (this != &other) { + Clear(); + head = other.head; + tail = other.tail; + count = other.count; + + other.head = nullptr; + other.tail = nullptr; + other.count = 0; + } + return *this; + } + + void Swap(SimpleList& other) noexcept { + std::swap(head, other.head); + std::swap(tail, other.tail); + std::swap(count, other.count); + } + + size_t Size() const { + return count; + } + + bool Empty() const { + return count == 0; + } + + void PushBack(const std::string& value) { + LinkBefore(new Node(value), nullptr); + } + + void PushBack(std::string&& value) { + LinkBefore(new Node(std::move(value)), nullptr); + } + + void PopBack() { + if (tail) { + Unlink(tail); + } + } + + void PushFront(const std::string& value) { + LinkBefore(new Node(value), head); + } + + void PushFront(std::string&& value) { + LinkBefore(new Node(std::move(value)), head); + } + + void PopFront() { + if (head) { + Unlink(head); + } + } + + std::string& Back() { + return tail->value; + } + + const std::string& Back() const { + return tail->value; + } + + std::string& Front() { + return head->value; + } + + const std::string& Front() const { + return head->value; + } + void Clear() { + while (head) { + Node* temp = head; + head = head->next; + delete temp; + } + tail = nullptr; + count = 0; + } +}; + +inline void Swap(SimpleList& lhs, SimpleList& rhs) noexcept { + lhs.Swap(rhs); +} From 7a02fa885e899a0959962f9f14952f55bbb5a77d Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 23 Feb 2026 22:35:50 +0700 Subject: [PATCH 18/25] add (solution): add smart_ptr task --- 06_week/tasks/smart_ptr/smart_ptr.cpp | 248 +++++++++++++++++++++++++- 1 file changed, 247 insertions(+), 1 deletion(-) diff --git a/06_week/tasks/smart_ptr/smart_ptr.cpp b/06_week/tasks/smart_ptr/smart_ptr.cpp index 05de4dc8..822fc730 100644 --- a/06_week/tasks/smart_ptr/smart_ptr.cpp +++ b/06_week/tasks/smart_ptr/smart_ptr.cpp @@ -1,10 +1,256 @@ #include +#include +class SharedPtr; +class WeakPtr; + +struct ControlBlock { + std::string* data; + size_t strong_count; + size_t weak_count; + + ControlBlock(std::string* ptr) : data(ptr), strong_count(1), weak_count(0) {} + + void add_strong() { ++strong_count; } + void release_strong() { + if (--strong_count == 0) { + delete data; + data = nullptr; + if (weak_count == 0) { + delete this; + } + } + } + + void add_weak() { ++weak_count; } + void release_weak() { + if (--weak_count == 0 && strong_count == 0) { + delete this; + } + } +}; class SharedPtr { +private: + std::string* ptr_; + ControlBlock* control_; + + friend class WeakPtr; + +public: + SharedPtr() : ptr_(nullptr), control_(nullptr) {} + + explicit SharedPtr(std::string* ptr) { + if (ptr) { + control_ = new ControlBlock(ptr); + ptr_ = ptr; + } else { + ptr_ = nullptr; + control_ = nullptr; + } + } + + SharedPtr(const SharedPtr& other) : ptr_(other.ptr_), control_(other.control_) { + if (control_) { + control_->add_strong(); + } + } + + SharedPtr(SharedPtr&& other) noexcept + : ptr_(other.ptr_), control_(other.control_) { + other.ptr_ = nullptr; + other.control_ = nullptr; + } + + explicit SharedPtr(const WeakPtr& weak); + + ~SharedPtr() { + if (control_) { + control_->release_strong(); + } + } + SharedPtr& operator=(const SharedPtr& other) { + if (this != &other) { + if (other.control_) { + other.control_->add_strong(); + } + if (control_) { + control_->release_strong(); + } + ptr_ = other.ptr_; + control_ = other.control_; + } + return *this; + } + + SharedPtr& operator=(SharedPtr&& other) noexcept { + if (this != &other) { + if (control_) { + control_->release_strong(); + } + ptr_ = other.ptr_; + control_ = other.control_; + other.ptr_ = nullptr; + other.control_ = nullptr; + } + return *this; + } + + std::string& operator*() const { + return *ptr_; + } + + std::string* operator->() const { + return ptr_; + } + + std::string* Get() const { + return ptr_; + } + + void Reset(std::string* ptr = nullptr) { + SharedPtr(ptr).Swap(*this); + } + + void Swap(SharedPtr& other) noexcept { + std::swap(ptr_, other.ptr_); + std::swap(control_, other.control_); + } + + size_t UseCount() const { + return control_ ? control_->strong_count : 0; + } + + explicit operator bool() const { + return ptr_ != nullptr; + } }; class WeakPtr { +private: + std::string* ptr_; + ControlBlock* control_; + + friend class SharedPtr; + +public: + WeakPtr() : ptr_(nullptr), control_(nullptr) {} + + WeakPtr(const SharedPtr& shared) : ptr_(shared.ptr_), control_(shared.control_) { + if (control_) { + control_->add_weak(); + } + } + + WeakPtr(const WeakPtr& other) : ptr_(other.ptr_), control_(other.control_) { + if (control_) { + control_->add_weak(); + } + } + + WeakPtr(WeakPtr&& other) noexcept + : ptr_(other.ptr_), control_(other.control_) { + other.ptr_ = nullptr; + other.control_ = nullptr; + } + + ~WeakPtr() { + if (control_) { + control_->release_weak(); + } + } + + WeakPtr& operator=(const WeakPtr& other) { + if (this != &other) { + if (other.control_) { + other.control_->add_weak(); + } + if (control_) { + control_->release_weak(); + } + ptr_ = other.ptr_; + control_ = other.control_; + } + return *this; + } + + WeakPtr& operator=(WeakPtr&& other) noexcept { + if (this != &other) { + if (control_) { + control_->release_weak(); + } + ptr_ = other.ptr_; + control_ = other.control_; + other.ptr_ = nullptr; + other.control_ = nullptr; + } + return *this; + } + + WeakPtr& operator=(const SharedPtr& shared) { + if (shared.control_) { + shared.control_->add_weak(); + } + if (control_) { + control_->release_weak(); + } + ptr_ = shared.ptr_; + control_ = shared.control_; + return *this; + } + + void Reset() { + WeakPtr().Swap(*this); + } + + void Swap(WeakPtr& other) noexcept { + std::swap(ptr_, other.ptr_); + std::swap(control_, other.control_); + } + + size_t UseCount() const { + return control_ ? control_->strong_count : 0; + } + + bool Expired() const { + return !control_ || control_->strong_count == 0; + } + + SharedPtr Lock() const; +}; + +inline SharedPtr::SharedPtr(const WeakPtr& weak) : ptr_(weak.ptr_), control_(weak.control_) { + if (control_) { + if (control_->strong_count > 0) { + control_->add_strong(); + } else { + ptr_ = nullptr; + control_ = nullptr; + } + } +} + +// Определение Lock для WeakPtr +inline SharedPtr WeakPtr::Lock() const { + if (control_ && control_->strong_count > 0) { + return SharedPtr(*this); + } + return SharedPtr(); +} + +inline SharedPtr MakeShared(const std::string& str) { + return SharedPtr(new std::string(str)); +} + +inline SharedPtr MakeShared(std::string&& str) { + return SharedPtr(new std::string(std::move(str))); +} + +inline void Swap(SharedPtr& lhs, SharedPtr& rhs) noexcept { + lhs.Swap(rhs); +} -}; \ No newline at end of file +inline void Swap(WeakPtr& lhs, WeakPtr& rhs) noexcept { + lhs.Swap(rhs); +} From 802320f3b3ab08cbb9aadc4edcdcee2e78d95164 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 23 Feb 2026 22:36:57 +0700 Subject: [PATCH 19/25] add (solution): add unique_ptr task --- 06_week/tasks/unique_ptr/unique_ptr.cpp | 42 +++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/06_week/tasks/unique_ptr/unique_ptr.cpp b/06_week/tasks/unique_ptr/unique_ptr.cpp index db8729ad..8c836127 100644 --- a/06_week/tasks/unique_ptr/unique_ptr.cpp +++ b/06_week/tasks/unique_ptr/unique_ptr.cpp @@ -1,6 +1,44 @@ #include - +#include class UniquePtr { +private: + std::string* ptr; + +public: + UniquePtr() noexcept : ptr(nullptr) {} + explicit UniquePtr(std::string* p) noexcept : ptr(p) {} + UniquePtr(const UniquePtr&) = delete; + UniquePtr(UniquePtr&& other) noexcept : ptr(other.ptr) { other.ptr = nullptr; } + ~UniquePtr() { delete ptr; } + + UniquePtr& operator=(const UniquePtr&) = delete; + UniquePtr& operator=(UniquePtr&& other) noexcept { + if (this != &other) { + delete ptr; + ptr = other.ptr; + other.ptr = nullptr; + } + return *this; + } + + std::string& operator*() const { return *ptr; } + std::string* operator->() const noexcept { return ptr; } + + std::string* Get() const noexcept { return ptr; } + std::string* Release() noexcept { std::string* tmp = ptr; ptr = nullptr; return tmp; } + void Reset(std::string* p = nullptr) noexcept { delete ptr; ptr = p; } + void Swap(UniquePtr& other) noexcept { std::swap(ptr, other.ptr); } + explicit operator bool() const noexcept { return ptr != nullptr; } +}; + +inline UniquePtr MakeUnique(const std::string& str) { + return UniquePtr(new std::string(str)); +} +inline UniquePtr MakeUnique(std::string&& str) { + return UniquePtr(new std::string(std::move(str))); +} -}; \ No newline at end of file +inline void Swap(UniquePtr& a, UniquePtr& b) noexcept { + a.Swap(b); +} From c7536f6c0cfb90976c1636281ea964fbd14a8891 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Tue, 10 Mar 2026 17:55:40 +0700 Subject: [PATCH 20/25] add (solution): add array task --- 07_week/tasks/array/array.cpp | 188 +++++++++++++++++++++++++++++++++- 1 file changed, 186 insertions(+), 2 deletions(-) diff --git a/07_week/tasks/array/array.cpp b/07_week/tasks/array/array.cpp index b1b8feab..ca4542ef 100644 --- a/07_week/tasks/array/array.cpp +++ b/07_week/tasks/array/array.cpp @@ -1,6 +1,190 @@ #include +#include +#include - +template class Array { +private: + T data_[N]; + +public: + // Конструктор по умолчанию + Array() = default; + + // Конструктор, принимающий список инициализации + Array(std::initializer_list list) { + size_t i = 0; + for (const auto& item : list) { + if (i < N) { + data_[i++] = item; + } + } + // Если элементов в списке меньше N, остальные будут value-инициализированы + } + + // Конструктор копирования + Array(const Array& other) = default; + + // Конструктор перемещения + Array(Array&& other) noexcept = default; + + // Деструктор + ~Array() = default; + + // Оператор присваивания копированием + Array& operator=(const Array& other) = default; + + // Оператор присваивания перемещением + Array& operator=(Array&& other) noexcept = default; + + // Операторы индексирования + T& operator[](size_t index) { + return data_[index]; + } + + const T& operator[](size_t index) const { + return data_[index]; + } + + // Метод Front + T& Front() { + return data_[0]; + } + + const T& Front() const { + return data_[0]; + } + + // Метод Back + T& Back() { + return data_[N - 1]; + } + + const T& Back() const { + return data_[N - 1]; + } + + // Метод Data + T* Data() { + return data_; + } + + const T* Data() const { + return data_; + } + + // Метод Empty + constexpr bool Empty() const { + return N == 0; + } + + // Метод Size + constexpr size_t Size() const { + return N; + } + + // Метод Fill + void Fill(const T& value) { + for (size_t i = 0; i < N; ++i) { + data_[i] = value; + } + } + + // Метод Swap + void Swap(Array& other) noexcept { + for (size_t i = 0; i < N; ++i) { + std::swap(data_[i], other.data_[i]); + } + } + + // Методы для range-based for + T* begin() { + return data_; + } + + const T* begin() const { + return data_; + } + + T* end() { + return data_ + N; + } + + const T* end() const { + return data_ + N; + } + + // Константные итераторы + const T* cbegin() const { + return data_; + } + + const T* cend() const { + return data_ + N; + } +}; + +// Внешние функции + +// Операторы сравнения +template +bool operator==(const Array& lhs, const Array& rhs) { + for (size_t i = 0; i < N; ++i) { + if (lhs[i] != rhs[i]) return false; + } + return true; +} + +template +bool operator!=(const Array& lhs, const Array& rhs) { + return !(lhs == rhs); +} + +template +bool operator<(const Array& lhs, const Array& rhs) { + for (size_t i = 0; i < N; ++i) { + if (lhs[i] < rhs[i]) return true; + if (rhs[i] < lhs[i]) return false; + } + return false; +} + +template +bool operator>(const Array& lhs, const Array& rhs) { + return rhs < lhs; +} + +template +bool operator<=(const Array& lhs, const Array& rhs) { + return !(rhs < lhs); +} + +template +bool operator>=(const Array& lhs, const Array& rhs) { + return !(lhs < rhs); +} + +// Функция swap +template +void swap(Array& lhs, Array& rhs) noexcept { + lhs.Swap(rhs); +} + +// Функция get для доступа по индексу (аналогично кортежу) +template +T& get(Array& arr) { + static_assert(I < N, "Index out of bounds"); + return arr[I]; +} + +template +const T& get(const Array& arr) { + static_assert(I < N, "Index out of bounds"); + return arr[I]; +} -}; \ No newline at end of file +template +T&& get(Array&& arr) { + static_assert(I < N, "Index out of bounds"); + return std::move(arr[I]); +} From c8dfdd66d4736f7cd4d62129bf71592e0497bcda Mon Sep 17 00:00:00 2001 From: VaryVA Date: Tue, 10 Mar 2026 18:08:27 +0700 Subject: [PATCH 21/25] add (solution): add make_unique task --- 07_week/tasks/make_unique/make_unique.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/07_week/tasks/make_unique/make_unique.cpp b/07_week/tasks/make_unique/make_unique.cpp index e0fdcbd7..628852b0 100644 --- a/07_week/tasks/make_unique/make_unique.cpp +++ b/07_week/tasks/make_unique/make_unique.cpp @@ -1,4 +1,7 @@ #include +#include - -/* return_type */ MakeUnique( /* args */ ); +template +std::unique_ptr MakeUnique(Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} From 5f90660e649aee2881662b71da664e8c59c81bae Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 16 Mar 2026 11:14:18 +0700 Subject: [PATCH 22/25] add (solution): add stack task --- 04_week/tasks/stack/stack.cpp | 49 ++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/04_week/tasks/stack/stack.cpp b/04_week/tasks/stack/stack.cpp index 222e4ffc..70b3035e 100644 --- a/04_week/tasks/stack/stack.cpp +++ b/04_week/tasks/stack/stack.cpp @@ -1,6 +1,53 @@ #include - class Stack { +private: + std::vector data; +public: + Stack() = default; + + void Push(int value) { + data.push_back(value); + } + + bool Pop() { + if (Empty()) { + return false; + } + data.pop_back(); + return true; + } + + int& Top() { + return data.back(); + } + + const int& Top() const { + return data.back(); + } + + bool Empty() const { + return data.empty(); + } + + size_t Size() const { + return data.size(); + } + + void Clear() { + data.clear(); + } + + void Swap(Stack& other) { + data.swap(other.data); + } + + bool operator==(const Stack& other) const { + return data == other.data; + } + + bool operator!=(const Stack& other) const { + return data != other.data; + } }; From 97875dca8521eef00c985a911d30304d296ecce2 Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 16 Mar 2026 11:15:25 +0700 Subject: [PATCH 23/25] add (solution): add phasor task --- 04_week/tasks/phasor/phasor.cpp | 287 +++++++++++++++++++++++++++++++- 1 file changed, 284 insertions(+), 3 deletions(-) diff --git a/04_week/tasks/phasor/phasor.cpp b/04_week/tasks/phasor/phasor.cpp index 3ec1b9ad..32712b6f 100644 --- a/04_week/tasks/phasor/phasor.cpp +++ b/04_week/tasks/phasor/phasor.cpp @@ -1,10 +1,291 @@ - +#include +#include +#include +#include struct ExpTag {}; struct DegTag {}; struct AlgTag {}; - class Phasor { - +private: + double real_; + double imag_ + + static double NormalizePhase(double phase) { + const double TWO_PI = 2.0 * M_PI; + phase = std::fmod(phase, TWO_PI); + if (phase > M_PI) { + phase -= TWO_PI; + } else if (phase <= -M_PI) { + phase += TWO_PI; + } + if (std::abs(phase) < std::numeric_limits::epsilon() * 100) { + phase = 0.0; + } + return phase; + } + + static double NormalizePhaseDeg(double phaseDeg) { + phaseDeg = std::fmod(phaseDeg, 360.0); + if (phaseDeg > 180.0) { + phaseDeg -= 360.0; + } else if (phaseDeg <= -180.0) { + phaseDeg += 360.0; + } + if (std::abs(phaseDeg) < std::numeric_limits::epsilon() * 100) { + phaseDeg = 0.0; + } + return phaseDeg; + } + +public: + Phasor() : real_(0.0), imag_(0.0) {} + + Phasor(double mag, double phaseRad) { + if (mag < 0) { + mag = -mag; + phaseRad += M_PI; + } + real_ = mag * std::cos(phaseRad); + imag_ = mag * std::sin(phaseRad); + + if (std::abs(real_) < std::numeric_limits::epsilon() * 100) { + real_ = 0.0; + } + if (std::abs(imag_) < std::numeric_limits::epsilon() * 100) { + imag_ = 0.0; + } + } + + Phasor(double mag, double phaseRad, ExpTag) : Phasor(mag, phaseRad) {} + + Phasor(double mag, double phaseDeg, DegTag) + : Phasor(mag, phaseDeg * M_PI / 180.0) {} + + Phasor(double real, double imag, AlgTag) : real_(real), imag_(imag) {} + + void SetPolar(double mag, double phaseRad) { + if (mag < 0) { + mag = -mag; + phaseRad += M_PI; + } + real_ = mag * std::cos(phaseRad); + imag_ = mag * std::sin(phaseRad); + + if (std::abs(real_) < std::numeric_limits::epsilon() * 100) { + real_ = 0.0; + } + if (std::abs(imag_) < std::numeric_limits::epsilon() * 100) { + imag_ = 0.0; + } + } + + void SetCartesian(double real, double imag) { + real_ = real; + imag_ = imag; + } + + double Magnitude() const { + double mag = std::sqrt(real_ * real_ + imag_ * imag_); + if (mag < std::numeric_limits::epsilon() * 100) { + return 0.0; + } + return mag; + } + + double Phase() const { + if (Magnitude() < std::numeric_limits::epsilon() * 100) { + return 0.0; + } + return NormalizePhase(std::atan2(imag_, real_)); + } + + double PhaseDeg() const { + return NormalizePhaseDeg(Phase() * 180.0 / M_PI); + } + + double Real() const { return real_; } + double Imag() const { return imag_; } + + double Abs() const { return Magnitude(); } + double Angle() const { return Phase(); } + double AngleDeg() const { return PhaseDeg(); } + + Phasor operator-() const { + return Phasor(-real_, -imag_, AlgTag{}); + } + + Phasor& operator+=(const Phasor& other) { + real_ += other.real_; + imag_ += other.imag_; + + if (std::abs(real_) < std::numeric_limits::epsilon() * 100) { + real_ = 0.0; + } + if (std::abs(imag_) < std::numeric_limits::epsilon() * 100) { + imag_ = 0.0; + } + return *this; + } + + Phasor& operator-=(const Phasor& other) { + real_ -= other.real_; + imag_ -= other.imag_; + + if (std::abs(real_) < std::numeric_limits::epsilon() * 100) { + real_ = 0.0; + } + if (std::abs(imag_) < std::numeric_limits::epsilon() * 100) { + imag_ = 0.0; + } + return *this; + } + + Phasor& operator*=(const Phasor& other) { + double mag = Magnitude() * other.Magnitude(); + double phase = Phase() + other.Phase(); + + phase = NormalizePhase(phase); + SetPolar(mag, phase); + return *this; + } + + Phasor& operator/=(const Phasor& other) { + double magOther = other.Magnitude(); + if (magOther < std::numeric_limits::epsilon()) { + throw std::runtime_error("Division by zero phasor"); + } + double mag = Magnitude() / magOther; + double phase = Phase() - other.Phase(); + + phase = NormalizePhase(phase); + SetPolar(mag, phase); + return *this; + } + + Phasor& operator*=(double scalar) { + real_ *= scalar; + imag_ *= scalar; + return *this; + } + + Phasor& operator/=(double scalar) { + if (std::abs(scalar) < std::numeric_limits::epsilon()) { + throw std::runtime_error("Division by zero"); + } + real_ /= scalar; + imag_ /= scalar; + return *this; + } + + Phasor Conj() const { + return Phasor(real_, -imag_, AlgTag{}); + } + + Phasor Inv() const { + double mag = Magnitude(); + if (mag < std::numeric_limits::epsilon()) { + throw std::runtime_error("Inverse of zero phasor"); + } + double phase = -Phase(); + return Phasor(1.0 / mag, phase, ExpTag{}); + } + + bool operator==(const Phasor& other) const { + const double eps = std::numeric_limits::epsilon() * 1000; + return std::abs(real_ - other.real_) < eps && + std::abs(imag_ - other.imag_) < eps; + } + + bool operator!=(const Phasor& other) const { + return !(*this == other); + } }; + +inline Phasor operator+(const Phasor& lhs, const Phasor& rhs) { + Phasor result = lhs; + result += rhs; + return result; +} + +inline Phasor operator-(const Phasor& lhs, const Phasor& rhs) { + Phasor result = lhs; + result -= rhs; + return result; +} + +inline Phasor operator*(const Phasor& lhs, const Phasor& rhs) { + Phasor result = lhs; + result *= rhs; + return result; +} + +inline Phasor operator/(const Phasor& lhs, const Phasor& rhs) { + Phasor result = lhs; + result /= rhs; + return result; +} + +inline Phasor operator+(const Phasor& lhs, double rhs) { + return lhs + Phasor(rhs, 0.0, AlgTag{}); +} + +inline Phasor operator-(const Phasor& lhs, double rhs) { + return lhs - Phasor(rhs, 0.0, AlgTag{}); +} + +inline Phasor operator*(const Phasor& lhs, double rhs) { + Phasor result = lhs; + result *= rhs; + return result; +} + +inline Phasor operator/(const Phasor& lhs, double rhs) { + Phasor result = lhs; + result /= rhs; + return result; +} + +inline Phasor operator+(double lhs, const Phasor& rhs) { + return Phasor(lhs, 0.0, AlgTag{}) + rhs; +} + +inline Phasor operator-(double lhs, const Phasor& rhs) { + return Phasor(lhs, 0.0, AlgTag{}) - rhs; +} + +inline Phasor operator*(double lhs, const Phasor& rhs) { + return rhs * lhs; +} + +inline Phasor operator/(double lhs, const Phasor& rhs) { + return Phasor(lhs, 0.0, AlgTag{}) / rhs; +} + +inline Phasor MakePhasorCartesian(double real, double imag) { + return Phasor(real, imag, AlgTag{}); +} + +inline Phasor MakePhasorPolar(double mag, double phaseRad) { + return Phasor(mag, phaseRad, ExpTag{}); +} + +inline Phasor MakePhasorPolarDeg(double mag, double phaseDeg) { + return Phasor(mag, phaseDeg, DegTag{}); +} + +inline std::ostream& operator<<(std::ostream& os, const Phasor& p) { + std::ios_base::fmtflags old_flags = os.flags(); + std::streamsize old_precision = os.precision(); + + os << std::fixed << std::setprecision(3); + + os << p.Magnitude() << "*e(j*" << p.PhaseDeg() << "°) [" + << p.Real() << " + j*" << p.Imag() << "]"; + + os.flags(old_flags); + os.precision(old_precision); + + return os; +} From 395310d2009980df6a0be0a15bba73e6c1ed02fd Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 16 Mar 2026 11:16:19 +0700 Subject: [PATCH 24/25] add (solution): add queue task --- 04_week/tasks/queue/queue.cpp | 131 +++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 1 deletion(-) diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index 2a9f8493..93b2b7a9 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -1,6 +1,135 @@ #include - +#include +#include +#include class Queue { +private: + std::vector input_stack; + std::vector output_stack; + + void transfer_elements() { + if (output_stack.empty()) { + while (!input_stack.empty()) { + output_stack.push_back(input_stack.back()); + input_stack.pop_back(); + } + } + } +public: + Queue() = default; + + Queue(std::stack s) { + while (!s.empty()) { + input_stack.push_back(s.top()); + s.pop(); + } + std::reverse(input_stack.begin(), input_stack.end()); + } + + Queue(const std::vector& vec) : input_stack(vec) {} + + Queue(std::initializer_list init) : input_stack(init) {} + + explicit Queue(size_t size) { + input_stack.reserve(size); + output_stack.reserve(size); + } + + Queue(const Queue& other) + : input_stack(other.input_stack), output_stack(other.output_stack) {} + + Queue& operator=(const Queue& other) { + if (this != &other) { + input_stack = other.input_stack; + output_stack = other.output_stack; + } + return *this; + } + + ~Queue() = default; + + void Push(int value) { + input_stack.push_back(value); + } + + bool Pop() { + if (Empty()) { + return false; + } + + transfer_elements(); + if (!output_stack.empty()) { + output_stack.pop_back(); + return true; + } + return false; + } + + int& Front() { + transfer_elements(); + return output_stack.back(); + } + + const int& Front() const { + if (!output_stack.empty()) { + return output_stack.back(); + } + return input_stack.front(); + } + + int& Back() { + if (!input_stack.empty()) { + return input_stack.back(); + } + return output_stack.front(); + } + + const int& Back() const { + if (!input_stack.empty()) { + return input_stack.back(); + } + return output_stack.front(); + } + + bool Empty() const { + return input_stack.empty() && output_stack.empty(); + } + + size_t Size() const { + return input_stack.size() + output_stack.size(); + } + + void Clear() { + input_stack.clear(); + output_stack.clear(); + } + + void Swap(Queue& other) { + input_stack.swap(other.input_stack); + output_stack.swap(other.output_stack); + } + + bool operator==(const Queue& other) const { + if (Size() != other.Size()) { + return false; + } + + Queue temp1 = *this; + Queue temp2 = other; + + while (!temp1.Empty()) { + if (temp1.Front() != temp2.Front()) { + return false; + } + temp1.Pop(); + temp2.Pop(); + } + return true; + } + + bool operator!=(const Queue& other) const { + return !(*this == other); + } }; From 7eaa4851bcb87b5f92396d07299034964b8aba4a Mon Sep 17 00:00:00 2001 From: VaryVA Date: Mon, 16 Mar 2026 11:17:06 +0700 Subject: [PATCH 25/25] add (solution): add ring_buffer task --- 04_week/tasks/ring_buffer/ring_buffer.cpp | 151 +++++++++++++++++++++- 1 file changed, 150 insertions(+), 1 deletion(-) diff --git a/04_week/tasks/ring_buffer/ring_buffer.cpp b/04_week/tasks/ring_buffer/ring_buffer.cpp index e2b57ba2..9e87a59f 100644 --- a/04_week/tasks/ring_buffer/ring_buffer.cpp +++ b/04_week/tasks/ring_buffer/ring_buffer.cpp @@ -1,6 +1,155 @@ #include - +#include +#include class RingBuffer { +public: + explicit RingBuffer(size_t capacity) + : buffer_(capacity > 0 ? capacity : 1), + head_(0), + tail_(0), + size_(0) {} + + RingBuffer(size_t capacity, int value) + : buffer_(capacity > 0 ? capacity : 1, value), + head_(0), + tail_(0), + size_(capacity > 0 ? capacity : 1) { + tail_ = size_ % buffer_.size(); + } + + RingBuffer(std::initializer_list init) + : buffer_(init.size() > 0 ? init.size() : 1), + head_(0), + tail_(init.size() % buffer_.size()), + size_(init.size()) { + std::copy(init.begin(), init.end(), buffer_.begin()); + } + + void Push(int value) { + buffer_[tail_] = value; + if (size_ < buffer_.size()) { + ++size_; + } else { + head_ = (head_ + 1) % buffer_.size(); + } + tail_ = (tail_ + 1) % buffer_.size(); + } + + bool TryPush(int value) { + if (size_ == buffer_.size()) { + return false; + } + Push(value); + return true; + } + + void Pop() { + if (size_ > 0) { + head_ = (head_ + 1) % buffer_.size(); + --size_; + } + } + + bool TryPop(int& value) { + if (size_ == 0) { + return false; + } + value = buffer_[head_]; + Pop(); + return true; + } + + int& operator[](size_t index) { + assert(index < size_ && "Index out of bounds"); + return buffer_[(head_ + index) % buffer_.size()]; + } + + const int& operator[](size_t index) const { + assert(index < size_ && "Index out of bounds"); + return buffer_[(head_ + index) % buffer_.size()]; + } + + int& Front() { + assert(size_ > 0 && "Buffer is empty"); + return buffer_[(tail_ == 0 ? buffer_.size() - 1 : tail_ - 1)]; + } + + const int& Front() const { + assert(size_ > 0 && "Buffer is empty"); + return buffer_[(tail_ == 0 ? buffer_.size() - 1 : tail_ - 1)]; + } + + int& Back() { + assert(size_ > 0 && "Buffer is empty"); + return buffer_[head_]; + } + + const int& Back() const { + assert(size_ > 0 && "Buffer is empty"); + return buffer_[head_]; + } + + bool Empty() const { + return size_ == 0; + } + + bool Full() const { + return size_ == buffer_.size(); + } + + size_t Size() const { + return size_; + } + + size_t Capacity() const { + return buffer_.size(); + } + + void Clear() { + head_ = 0; + tail_ = 0; + size_ = 0; + } + + void Resize(size_t new_capacity) { + if (new_capacity == 0) { + new_capacity = 1; + } + + std::vector new_buffer(new_capacity); + size_t new_size = std::min(size_, new_capacity); + + if (new_size < size_) { + size_t start_index = size_ - new_size; + for (size_t i = 0; i < new_size; ++i) { + new_buffer[i] = (*this)[start_index + i]; + } + head_ = 0; + tail_ = new_size % new_capacity; + } else { + for (size_t i = 0; i < new_size; ++i) { + new_buffer[i] = (*this)[i]; + } + head_ = 0; + tail_ = new_size % new_capacity; + } + + buffer_.swap(new_buffer); + size_ = new_size; + } + + std::vector Vector() const { + std::vector result(size_); + for (size_t i = 0; i < size_; ++i) { + result[i] = (*this)[i]; + } + return result; + } +private: + std::vector buffer_; + size_t head_; + size_t tail_; + size_t size_; };