From 520e4ac0c7fdaf1bdf542e564cfbabe3b4216089 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Thu, 27 Nov 2025 14:24:10 +0500 Subject: [PATCH 01/30] feat: implement tasks --- 01_week/tasks/addition/addition.cpp | 3 +- 01_week/tasks/char_changer/char_changer.cpp | 48 ++++++++++++++- 01_week/tasks/check_flags/README.md | 3 +- 01_week/tasks/check_flags/check_flags.cpp | 45 +++++++++++++- 01_week/tasks/length_lit/length_lit.cpp | 67 +++++++++++++++++++++ 01_week/tasks/print_bits/print_bits.cpp | 26 +++++++- 01_week/tasks/quadratic/quadratic.cpp | 33 +++++++++- 01_week/tasks/rms/rms.cpp | 13 +++- main.cpp | 4 +- 9 files changed, 222 insertions(+), 20 deletions(-) diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 92872802..3529a582 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"}; + return static_cast(a) + static_cast(b); } \ No newline at end of file diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index 3a7344d9..d4ec5de8 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -1,7 +1,49 @@ #include -#include +#include size_t CharChanger(char array[], size_t size, char delimiter = ' ') { - throw std::runtime_error{"Not implemented"}; -} + int counter = 0; // Счётчик повторяющихся символов + int write = 0; // Указатель для записи обработанного символа + int read = 0; // Указатель для чтения следующего элемента из массива + char repeating_symbol = array[read]; // В процессе выполнения - редыдущий символ после read(отслеживаем повторения) + + while (repeating_symbol != '\0'){ + if (repeating_symbol == array[read]){ + counter++; + } else { + if (isalpha(repeating_symbol)){ + array[write] = toupper(repeating_symbol); + } else if (isdigit(repeating_symbol)){ + array[write] = '*'; + } else if (repeating_symbol == ' '){ + array[write] = delimiter; + } else { + array[write] = '_'; + } + + write++; + + if (repeating_symbol == ' ') { + counter = 1; + } + + if (counter >= 10){ + counter = 0; + } + + if (counter != 1){ + array[write] = static_cast(counter + '0'); // Преобразуем число в символ + counter = 1; + write++; + } + + repeating_symbol = array[read]; + } + + read++; + } + + array[write] = '\0'; + return write; +} \ No newline at end of file diff --git a/01_week/tasks/check_flags/README.md b/01_week/tasks/check_flags/README.md index b0940336..a2e605ea 100644 --- a/01_week/tasks/check_flags/README.md +++ b/01_week/tasks/check_flags/README.md @@ -8,5 +8,4 @@ Если передан флаг отсутствия проверок, то необходимо вывести пустые `[]`. -Если передано значение выходит из возможного диапазона значений, то вывод -следует оставить пустым. \ No newline at end of file +Если переданное значение выходит из возможного диапазона значений, то вывод следует оставить пустым. \ No newline at end of file diff --git a/01_week/tasks/check_flags/check_flags.cpp b/01_week/tasks/check_flags/check_flags.cpp index 75e7c652..4fe4d5f0 100644 --- a/01_week/tasks/check_flags/check_flags.cpp +++ b/01_week/tasks/check_flags/check_flags.cpp @@ -1,5 +1,7 @@ #include -#include +#include +#include +#include enum class CheckFlags : uint8_t { @@ -14,5 +16,42 @@ enum class CheckFlags : uint8_t { }; void PrintCheckFlags(CheckFlags flags) { - throw std::runtime_error{"Not implemented"}; -} + if (flags == CheckFlags::NONE) { + std::cout << "[]"; + return; + } + + if ((static_cast(flags) & ~static_cast(CheckFlags::ALL)) != 0) { + std::cout << ""; + return; + } + + std::vector set_flags; + if ((static_cast(flags) & static_cast(CheckFlags::TIME)) != 0) { + set_flags.push_back("TIME"); + } + if ((static_cast(flags) & static_cast(CheckFlags::DATE)) != 0) { + set_flags.push_back("DATE"); + } + if ((static_cast(flags) & static_cast(CheckFlags::USER)) != 0) { + set_flags.push_back("USER"); + } + if ((static_cast(flags) & static_cast(CheckFlags::CERT)) != 0) { + set_flags.push_back("CERT"); + } + if ((static_cast(flags) & static_cast(CheckFlags::KEYS)) != 0) { + set_flags.push_back("KEYS"); + } + if ((static_cast(flags) & static_cast(CheckFlags::DEST)) != 0) { + set_flags.push_back("DEST"); + } + + std::cout << "["; + for (int i = 0; i < set_flags.size(); i++) { + std::cout << set_flags[i]; + if (i < set_flags.size() - 1) { + std::cout << ","; + } + } + std::cout << "]"; +} \ No newline at end of file diff --git a/01_week/tasks/length_lit/length_lit.cpp b/01_week/tasks/length_lit/length_lit.cpp index e69de29b..7f91a3b4 100644 --- a/01_week/tasks/length_lit/length_lit.cpp +++ b/01_week/tasks/length_lit/length_lit.cpp @@ -0,0 +1,67 @@ +#include + + +// Константы для преобразования +constexpr long double IN_TO_CM = 2.54L; +constexpr long double FT_TO_IN = 12.0L; +constexpr long double M_TO_CM = 100.0L; + +// ft -> in +double operator"" _ft_to_in(long double ft_value) { + return static_cast(ft_value * FT_TO_IN); +} + +// ft -> cm +double operator"" _ft_to_cm(long double ft_value) { + return static_cast(ft_value * FT_TO_IN * IN_TO_CM); +} + +// ft -> m +double operator"" _ft_to_m(long double ft_value) { + return static_cast(ft_value * FT_TO_IN * IN_TO_CM / M_TO_CM); +} + +// in -> ft +double operator"" _in_to_ft(long double in_value) { + return static_cast(in_value / FT_TO_IN); +} + +// in -> cm +double operator"" _in_to_cm(long double in_value) { + return static_cast(in_value * IN_TO_CM); +} + +// in -> m +double operator"" _in_to_m(long double in_value) { + return static_cast(in_value * IN_TO_CM / M_TO_CM); +} + +// cm -> ft +double operator"" _cm_to_ft(long double cm_value) { + return static_cast(cm_value / IN_TO_CM / FT_TO_IN); +} + +// cm -> in +double operator"" _cm_to_in(long double cm_value) { + return static_cast(cm_value / IN_TO_CM); +} + +// cm -> m +double operator"" _cm_to_m(long double cm_value) { + return static_cast(cm_value / M_TO_CM); +} + +// m -> ft +double operator"" _m_to_ft(long double m_value) { + return static_cast(m_value * M_TO_CM / IN_TO_CM / FT_TO_IN); +} + +// m -> in +double operator"" _m_to_in(long double m_value) { + return static_cast(m_value * M_TO_CM / IN_TO_CM); +} + +// m -> cm +double operator"" _m_to_cm(long double m_value) { + return static_cast(m_value * M_TO_CM); +} \ No newline at end of file diff --git a/01_week/tasks/print_bits/print_bits.cpp b/01_week/tasks/print_bits/print_bits.cpp index a48a43c1..155a3b45 100644 --- a/01_week/tasks/print_bits/print_bits.cpp +++ b/01_week/tasks/print_bits/print_bits.cpp @@ -1,7 +1,27 @@ #include -#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"; + + // Выводим биты, начиная со старшего + for (int i = bytes * 8 - 1; i >= 0; --i) { + // "Выталкиваем" все биты, кроме i-ого, так чтобы i-ый был последним и выводим этот бит + if ((value >> i) & 1) { + std::cout << 1; + } else { + std::cout << 0; + } + + if (i != 0 && i % 4 == 0) { + std::cout << "'"; + } + } + std::cout << '\n'; +} \ No newline at end of file diff --git a/01_week/tasks/quadratic/quadratic.cpp b/01_week/tasks/quadratic/quadratic.cpp index abf7d632..4fcf562e 100644 --- a/01_week/tasks/quadratic/quadratic.cpp +++ b/01_week/tasks/quadratic/quadratic.cpp @@ -1,6 +1,35 @@ -#include +#include +#include +#include +#include // Для std::min и std::max 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 x = static_cast(-c) / b; + std::cout << std::defaultfloat << std::setprecision(6) << x; + } + } else { + long long discriminant = static_cast(b) * b - 4 * static_cast(a) * c; + + if (discriminant > 0) { + double x1 = (-b - std::sqrt(static_cast(discriminant))) / (2 * a); + double x2 = (-b + std::sqrt(static_cast(discriminant))) / (2 * a); + std::cout << std::defaultfloat << std::setprecision(6) << std::min(x1, x2); + std::cout << " "; + std::cout << std::defaultfloat << std::setprecision(6) << std::max(x1, x2); + } else if (discriminant == 0) { + double x = static_cast(-b) / (2 * a); + std::cout << std::defaultfloat << std::setprecision(6) << x; + } else { + std::cout << "no solutions"; + } + } } \ No newline at end of file diff --git a/01_week/tasks/rms/rms.cpp b/01_week/tasks/rms/rms.cpp index 6882f0a9..f13c54bb 100644 --- a/01_week/tasks/rms/rms.cpp +++ b/01_week/tasks/rms/rms.cpp @@ -1,7 +1,14 @@ -#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 sum = 0; + for (int i=0; i < size; i++){ + sum+=std::pow(values[i], 2); + } + + return std::sqrt(sum / size); } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 9dad8356..b7a6622d 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,6 @@ int main() { - std::cout << "I wait your unbelievable code here" << std::endl; + std::cout << "My unbelievable code here" << std::endl; return 0; -} +} \ No newline at end of file From d453f8b1a59c0466c65d03623f40e89043430672 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Tue, 9 Dec 2025 23:56:05 +0500 Subject: [PATCH 02/30] feat: implement second week tasks --- 02_week/tasks/func_array/func_array.cpp | 19 ++++++++++-- 02_week/tasks/last_of_us/README.md | 2 +- 02_week/tasks/last_of_us/last_of_us.cpp | 30 +++++++++++++++++-- 02_week/tasks/little_big/little_big.cpp | 40 +++++++++++++++++++++---- 02_week/tasks/longest/longest.cpp | 40 ++++++++++++++++++++++--- 5 files changed, 114 insertions(+), 17 deletions(-) diff --git a/02_week/tasks/func_array/func_array.cpp b/02_week/tasks/func_array/func_array.cpp index b327e68d..ef115411 100644 --- a/02_week/tasks/func_array/func_array.cpp +++ b/02_week/tasks/func_array/func_array.cpp @@ -1,6 +1,19 @@ -#include +#include +using funcPtr = double (*)(double, double); -double ApplyOperations(double a, double b /* other arguments */) { - throw std::runtime_error{"Not implemented"}; +double ApplyOperations(const double a, const double b, funcPtr* arr, size_t funcArraySize) { + if (arr == nullptr || funcArraySize <= 0){ + return 0.0; + } + + double cumulative_result = 0; + for (unsigned int i=0; i < funcArraySize; ++i){ + if (arr[i] == nullptr){ + continue; + } + cumulative_result+=arr[i](a, b); + } + + return cumulative_result; } \ No newline at end of file diff --git a/02_week/tasks/last_of_us/README.md b/02_week/tasks/last_of_us/README.md index 56fee4ec..077a84fb 100644 --- a/02_week/tasks/last_of_us/README.md +++ b/02_week/tasks/last_of_us/README.md @@ -4,7 +4,7 @@ элемент, удовлетворяющий условию функции предиката. Функция принимает два указателя на начало и конец целочисленного массива, а также указатель на функцию предикат, и возвращает указатель на найденный элемент. Если элемент -не найден, то возвращается указатель за последний элемент. +не найден, то возвращается указатель на последний элемент. Указатели соответствуют диапазону $[begin, end)$. 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..ef17e289 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,30 @@ -#include +#include +using funcPtr = bool (*)(int); -/* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { - throw std::runtime_error{"Not implemented"}; +const int* FindLastElement(const int* begin, const int* end, funcPtr predicate) { + if (begin == nullptr || end == nullptr || predicate == nullptr){ + return end; + } + + if (begin >= end){ + return end; + } + + const int* last_element = nullptr; + + while (begin < end) + { + if (predicate(*begin)){ + last_element = begin; + } + + ++begin; + } + + if (last_element == nullptr){ + return end; + } + + return last_element; } \ No newline at end of file diff --git a/02_week/tasks/little_big/little_big.cpp b/02_week/tasks/little_big/little_big.cpp index abe24379..1887280e 100644 --- a/02_week/tasks/little_big/little_big.cpp +++ b/02_week/tasks/little_big/little_big.cpp @@ -1,10 +1,38 @@ -#include +#include +#include +#include +#include - -void PrintMemory(int /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void PrintMemory(int number, bool inversion = false) { + unsigned char bytes[sizeof(int)]; + std::memcpy(bytes, &number, sizeof(int)); + + if (inversion) { + std::reverse(bytes, bytes + sizeof(int)); + } + + std::cout << "0x"; + for (size_t i = 0; i < sizeof(int); ++i) { + std::cout << std::hex << std::uppercase + << std::setfill('0') << std::setw(2) + << static_cast(bytes[i]); + } + std::cout << std::dec << std::endl; } -void PrintMemory(double /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void PrintMemory(double number, bool inversion = false) { + unsigned char bytes[sizeof(double)]; + std::memcpy(bytes, &number, sizeof(double)); + + if (inversion) { + std::reverse(bytes, bytes + sizeof(double)); + } + + std::cout << "0x"; + for (size_t i = 0; i < sizeof(double); ++i) { + std::cout << std::hex << std::uppercase + << std::setfill('0') << std::setw(2) + << static_cast(bytes[i]); + } + std::cout << std::dec << std::endl; } \ No newline at end of file diff --git a/02_week/tasks/longest/longest.cpp b/02_week/tasks/longest/longest.cpp index 04b3c354..4940ed99 100644 --- a/02_week/tasks/longest/longest.cpp +++ b/02_week/tasks/longest/longest.cpp @@ -1,6 +1,38 @@ -#include +char* FindLongestSubsequence(const char* begin, const char* end, size_t &count) { + if (begin == nullptr || end == nullptr || begin >= end){ + count = 0; + return nullptr; + } + const char* startOfSubsequence = begin; + const char* left = begin; + const char* right = ++begin; + int max_length = 1; + int length = 1; -/* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { - throw std::runtime_error{"Not implemented"}; -} + while (right != end) + { + if (*left == *right){ + ++right; + ++length; + } else { + if (length > max_length){ + startOfSubsequence = left; + max_length = length; + } + + length = 0; + while (left != right){ + ++left; + } + } + } + + if (length > max_length){ + startOfSubsequence = left; + max_length = length; + } + + count = max_length; + return const_cast(startOfSubsequence); +} \ No newline at end of file From 6bf278adcbf22e3b287c53c3c51d6ffa858bd9ae Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Wed, 10 Dec 2025 11:24:43 +0500 Subject: [PATCH 03/30] feat: implement second week remaining tasks --- 02_week/tasks/pretty_array/pretty_array.cpp | 35 +++++++++++++++++++-- 02_week/tasks/swap_ptr/swap_ptr.cpp | 10 +++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/02_week/tasks/pretty_array/pretty_array.cpp b/02_week/tasks/pretty_array/pretty_array.cpp index 48eab341..f6c872e2 100644 --- a/02_week/tasks/pretty_array/pretty_array.cpp +++ b/02_week/tasks/pretty_array/pretty_array.cpp @@ -1,6 +1,35 @@ -#include +#include +void PrintArray(const int* begin, const int* end, const int count = 0) { + if (begin == nullptr || end == nullptr || count < 0) { + std::cout << "[]\n"; + return; + } -void PrintArray(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; + std::cout << "["; + if (begin < end) { + for (const int* ptr = begin; ptr < end; ++ptr) { + if (count > 0 && static_cast(ptr - begin) % count == 0 && ptr != begin){ + std::cout << "...\n "; + } + + std::cout << *ptr; + if (ptr != end - 1) { + std::cout << ", "; + } + } + } + else if (begin > end) { + for (const int* ptr = begin; ptr > end; --ptr) { + if (count > 0 && static_cast(begin - ptr) % count == 0 && ptr != begin) { + std::cout << "...\n "; + } + + std::cout << *ptr; + if (ptr != end + 1) { + std::cout << ", "; + } + } + } + std::cout << "]\n"; } \ No newline at end of file diff --git a/02_week/tasks/swap_ptr/swap_ptr.cpp b/02_week/tasks/swap_ptr/swap_ptr.cpp index 93db625d..2813af8f 100644 --- a/02_week/tasks/swap_ptr/swap_ptr.cpp +++ b/02_week/tasks/swap_ptr/swap_ptr.cpp @@ -1,6 +1,6 @@ -#include - - -void SwapPtr(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +template +void SwapPtr(T*& ptr1, T*& ptr2) { + T* temp = ptr2; + ptr2 = ptr1; + ptr1 = temp; } \ No newline at end of file From a4bfdbf86aa824f3f5773ca9370f905d32e99aeb Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 21 Dec 2025 23:01:41 +0500 Subject: [PATCH 04/30] feat: implement third week tasks --- 03_week/tasks/data_stats/data_stats.cpp | 31 +++- 03_week/tasks/easy_compare/easy_compare.cpp | 138 +++++++++++++++++- .../tasks/enum_operators/enum_operators.cpp | 92 ++++++++++-- 03_week/tasks/filter/filter.cpp | 24 ++- 03_week/tasks/find_all/find_all.cpp | 22 ++- 03_week/tasks/minmax/minmax.cpp | 22 ++- 03_week/tasks/os_overload/os_overload.cpp | 55 ++++++- 03_week/tasks/range/range.cpp | 20 ++- 03_week/tasks/unique/unique.cpp | 17 ++- 9 files changed, 379 insertions(+), 42 deletions(-) diff --git a/03_week/tasks/data_stats/data_stats.cpp b/03_week/tasks/data_stats/data_stats.cpp index b941c211..cf62e3b4 100644 --- a/03_week/tasks/data_stats/data_stats.cpp +++ b/03_week/tasks/data_stats/data_stats.cpp @@ -1,11 +1,32 @@ -#include - +#include +#include struct DataStats { double avg = 0.0; double sd = 0.0; }; -/* return_type */ CalculateDataStats(/* args */) { - throw std::runtime_error{"Not implemented"}; -} +DataStats CalculateDataStats(std::vector numArray) { + double average = 0.0; + double standardDeviation = 0.0; + + if (!numArray.empty()){ + double delta = 0.0; + double delta2 = 0.0; + int count = 0.0; + + for (double num : numArray){ + ++count; + delta = num - average; + average += delta / count; + delta2 = num - average; + standardDeviation += delta * delta2; + } + + standardDeviation = std::sqrt(standardDeviation / count); + } + + DataStats datastats = {average, standardDeviation}; + + return datastats; +} \ No newline at end of file diff --git a/03_week/tasks/easy_compare/easy_compare.cpp b/03_week/tasks/easy_compare/easy_compare.cpp index dd5cb7f6..cd7971f4 100644 --- a/03_week/tasks/easy_compare/easy_compare.cpp +++ b/03_week/tasks/easy_compare/easy_compare.cpp @@ -1,10 +1,72 @@ -#include - +#include struct Date { unsigned year; unsigned month; unsigned day; + + Date(){this->year = 0u; this->month = 0u; this->day = 0u;} + Date(unsigned year, unsigned month, unsigned day) : year(year), month(month), day(day){} + + bool operator==(Date other) const { + if (this->year == other.year && this->month == other.month && this->day == other.day){ + return true; + } + return false; + } + + bool operator!=(Date other) const { + if (!this->operator==(other)){ + return true; + } + return false; + } + + bool operator<(Date other) const { + if (this->year < other.year){ + return true; + } else if (this->year == other.year){ + if (this->month < other.month){ + return true; + } else if (this->month == other.month){ + if (this->day < other.day){ + return true; + } + } + } + + return false; + } + + bool operator>(Date other) const { + if (this->year > other.year){ + return true; + } else if (this->year == other.year){ + if (this->month > other.month){ + return true; + } else if (this->month == other.month){ + if (this->day > other.day){ + return true; + } + } + } + + return false; + } + + bool operator>=(Date other) const { + if (this->operator>(other) || this->operator==(other)){ + return true; + } + return false; + } + + bool operator<=(Date other) const { + if (this->operator<(other) || this->operator==(other)){ + return true; + } + return false; + } }; struct StudentInfo { @@ -13,4 +75,76 @@ struct StudentInfo { int score; unsigned course; Date birth_date; + + StudentInfo(){} + StudentInfo(size_t id, char mark, int score, unsigned course, Date birth_date) : + id(id), mark(mark), score(score), course(course), birth_date(birth_date){} + + bool operator==(StudentInfo other) const { + if (this->mark == other.mark && this->score == other.score){ + return true; + } + return false; + } + + bool operator!=(StudentInfo other) const { + if (!this->operator==(other)){ + return true; + } + return false; + } + + bool operator<(StudentInfo other) const { + if (this->mark > other.mark){ + return true; + } else if (this->mark == other.mark){ + if (this->score < other.score){ + return true; + } else if (this->score == other.score){ + if (this->course > other.course){ + return true; + } else if (this->course == other.course){ + if (this->birth_date < other.birth_date){ + return true; + } + } + } + } + + return false; + } + + bool operator>(StudentInfo other) const { + if (this->mark < other.mark){ + return true; + } else if (this->mark == other.mark){ + if (this->score > other.score){ + return true; + } else if (this->score == other.score){ + if (this->course < other.course){ + return true; + } else if (this->course == other.course){ + if (this->birth_date > other.birth_date){ + return true; + } + } + } + } + + return false; + } + + bool operator>=(StudentInfo other) const { + if (this->operator>(other) || this->operator==(other)){ + return true; + } + return false; + } + + bool operator<=(StudentInfo other) const { + if (this->operator<(other) || this->operator==(other)){ + return true; + } + return false; + } }; \ No newline at end of file diff --git a/03_week/tasks/enum_operators/enum_operators.cpp b/03_week/tasks/enum_operators/enum_operators.cpp index a539be38..928db352 100644 --- a/03_week/tasks/enum_operators/enum_operators.cpp +++ b/03_week/tasks/enum_operators/enum_operators.cpp @@ -1,5 +1,8 @@ -#include +#include #include +#include +#include +#include enum class CheckFlags : uint8_t { NONE = 0, @@ -12,22 +15,87 @@ enum class CheckFlags : uint8_t { ALL = TIME | DATE | USER | CERT | KEYS | DEST }; -/* return_type */ operator|(/* args */) { - throw std::runtime_error{"Not implemented"}; +CheckFlags operator|(CheckFlags a, CheckFlags b) { + uint8_t a_val = static_cast(a); + uint8_t b_val = static_cast(b); + uint8_t result = a_val | b_val; + + uint8_t valid_result = result & static_cast(CheckFlags::ALL); + return static_cast(valid_result); } -/* return_type */ operator&(/* args */) { - throw std::runtime_error{"Not implemented"}; +bool operator&(CheckFlags a, CheckFlags b) { + uint8_t a_val = static_cast(a); + uint8_t b_val = static_cast(b); + + uint8_t a_valid = a_val & static_cast(CheckFlags::ALL); + uint8_t b_valid = b_val & static_cast(CheckFlags::ALL); + + if (a_valid == 0 || b_valid == 0) { + return false; + } + + return (a_valid & b_valid) == a_valid || (a_valid & b_valid) == b_valid; } -/* return_type */ operator^(/* args */) { - throw std::runtime_error{"Not implemented"}; +CheckFlags operator^(CheckFlags a, CheckFlags b) { + uint8_t a_val = static_cast(a); + uint8_t b_val = static_cast(b); + + uint8_t a_valid = a_val & static_cast(CheckFlags::ALL); + uint8_t b_valid = b_val & static_cast(CheckFlags::ALL); + + uint8_t result = a_valid ^ b_valid; + return static_cast(result); } -/* return_type */ operator~(/* args */) { - throw std::runtime_error{"Not implemented"}; +CheckFlags operator~(CheckFlags a) { + uint8_t a_val = static_cast(a); + uint8_t a_valid = a_val & static_cast(CheckFlags::ALL); + + uint8_t result = ~a_valid & static_cast(CheckFlags::ALL); + + return static_cast(result); } -/* return_type */ operator<<(/* args */) { - throw std::runtime_error{"Not implemented"}; -} +std::ostream& operator<<(std::ostream& os, CheckFlags flags) { + uint8_t NOT_IN_RANGE = (1 << 6) | (1 << 7); + uint8_t flags_val = static_cast(flags); + + uint8_t valid_flags = flags_val & static_cast(CheckFlags::ALL); + + if (valid_flags == 0) { + os << "NONE"; + return os; + } + + std::vector flag_names; + + if ((valid_flags & static_cast(CheckFlags::TIME)) != 0) { + flag_names.push_back("TIME"); + } + if ((valid_flags & static_cast(CheckFlags::DATE)) != 0) { + flag_names.push_back("DATE"); + } + if ((valid_flags & static_cast(CheckFlags::USER)) != 0) { + flag_names.push_back("USER"); + } + if ((valid_flags & static_cast(CheckFlags::CERT)) != 0) { + flag_names.push_back("CERT"); + } + if ((valid_flags & static_cast(CheckFlags::KEYS)) != 0) { + flag_names.push_back("KEYS"); + } + if ((valid_flags & static_cast(CheckFlags::DEST)) != 0) { + flag_names.push_back("DEST"); + } + + for (size_t i = 0; i < flag_names.size(); ++i) { + os << flag_names[i]; + if (i < flag_names.size() - 1) { + os << ", "; + } + } + + return os; +} \ No newline at end of file diff --git a/03_week/tasks/filter/filter.cpp b/03_week/tasks/filter/filter.cpp index 6648cb39..5a12ad06 100644 --- a/03_week/tasks/filter/filter.cpp +++ b/03_week/tasks/filter/filter.cpp @@ -1,6 +1,20 @@ -#include +#include +#include // Для std::is_same_v - -/* return_type */ Filter(/* args */) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file +template +void Filter(std::vector& array, Predicat func) { + if constexpr (!std::is_same_v) { + if (array.empty()){ + return; + } + + int lagging = 0; + for (size_t i=0; i < array.size(); ++i){ + if (func(array[i])){ + array[lagging] = array[i]; + ++lagging; + } + } + array.resize(lagging); + } +} diff --git a/03_week/tasks/find_all/find_all.cpp b/03_week/tasks/find_all/find_all.cpp index 74f393b2..6a0f1036 100644 --- a/03_week/tasks/find_all/find_all.cpp +++ b/03_week/tasks/find_all/find_all.cpp @@ -1,6 +1,22 @@ -#include +#include +#include // Для std::is_same_v +template +std::vector FindAll(const std::vector& array, Predicat func) { + if constexpr (!std::is_same_v) { + if (array.empty()){ + return {}; + } -/* return_type */ FindAll(/* args */) { - throw std::runtime_error{"Not implemented"}; + std::vector result; + for (size_t i = 0; i < array.size(); ++i) { + if (func(array[i])) { + result.push_back(i); + } + } + result.shrink_to_fit(); + return result; + } + + return {}; } \ No newline at end of file diff --git a/03_week/tasks/minmax/minmax.cpp b/03_week/tasks/minmax/minmax.cpp index c2869799..c5cbe99b 100644 --- a/03_week/tasks/minmax/minmax.cpp +++ b/03_week/tasks/minmax/minmax.cpp @@ -1,6 +1,22 @@ -#include +#include +#include // Для std::pair и std::make_pair +std::pair::const_iterator, std::vector::const_iterator> MinMax(const std::vector& vec) { + if (vec.empty()) { + return std::make_pair(vec.end(), vec.end()); + } -/* return_type */ MinMax(/* args */) { - throw std::runtime_error{"Not implemented"}; + auto min_it = vec.begin(); + auto max_it = vec.begin(); + + for (auto it = vec.begin() + 1; it != vec.end(); ++it) { + if (*it < *min_it) { + min_it = it; + } + if (*it >= *max_it) { + max_it = it; + } + } + + return std::make_pair(min_it, max_it); } diff --git a/03_week/tasks/os_overload/os_overload.cpp b/03_week/tasks/os_overload/os_overload.cpp index e473418d..6521b492 100644 --- a/03_week/tasks/os_overload/os_overload.cpp +++ b/03_week/tasks/os_overload/os_overload.cpp @@ -1,21 +1,64 @@ -#include +#include #include #include struct Coord2D { - int x; - int y; + int x = 0; + int y = 0; + + Coord2D(){} + Coord2D(int x_val, int y_val) : x(x_val), y(y_val) {} }; + struct Circle { Coord2D coord; - unsigned radius; + unsigned radius = 1; + + Circle(){} + Circle(Coord2D c_val) : coord(c_val){} + Circle(Coord2D c_val, unsigned r_val) : coord(c_val), radius(r_val) {} }; + using CircleRegion = std::pair; using CircleRegionList = std::vector; -/* return_type */ operator<<(/* args */) { - throw std::runtime_error{"Not implemented"}; + +std::ostream& operator<<(std::ostream& os, const Coord2D& coord) { + os << "(" << coord.x << ", " << coord.y << ")"; + return os; +} + +std::ostream& operator<<(std::ostream& os, const Circle& circle) { + if (circle.radius == 0) { + os << "circle[]"; + } else { + os << "circle[" << circle.coord << ", r = " << circle.radius << "]"; + } + return os; +} + +std::ostream& operator<<(std::ostream& os, const CircleRegion& region) { + os << (region.second ? '+' : '-') << region.first; + return os; +} + +std::ostream& operator<<(std::ostream& os, const CircleRegionList& list) { + if (list.empty()) { + os << "{}"; + } else { + os << "{\n"; + for (size_t i = 0; i < list.size(); ++i) { + os << "\t" << list[i]; + if (i < list.size() - 1) { + os << ",\n"; + } else { + os << "\n"; + } + } + os << "}"; + } + return os; } diff --git a/03_week/tasks/range/range.cpp b/03_week/tasks/range/range.cpp index d2085495..7e0fae07 100644 --- a/03_week/tasks/range/range.cpp +++ b/03_week/tasks/range/range.cpp @@ -1,7 +1,21 @@ -#include #include +std::vector Range(int from, int to, int step = 1) { + if (step == 0) { + return {}; + } -std::vector Range(int from, int to, int step) { - throw std::runtime_error{"Not implemented"}; + if ((step > 0 && from >= to) || (step < 0 && from <= to)) { + return {}; + } + + int count = (to - from + step - (step > 0 ? 1 : -1)) / step; + std::vector result; + result.reserve(count); + + for (int i = 0; i < count; ++i) { + result.push_back(from + i * step); + } + + return result; } diff --git a/03_week/tasks/unique/unique.cpp b/03_week/tasks/unique/unique.cpp index 9d2545bb..7514a09c 100644 --- a/03_week/tasks/unique/unique.cpp +++ b/03_week/tasks/unique/unique.cpp @@ -1,6 +1,17 @@ -#include #include -/* return_type */ Unique(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector Unique(const std::vector& input_vector) { + if (input_vector.empty()) { + return {}; + } + + std::vector result; + result.push_back(input_vector[0]); + for (size_t i = 1; i < input_vector.size(); ++i) { + if (input_vector[i] != input_vector[i-1]) { + result.push_back(input_vector[i]); + } + } + result.shrink_to_fit(); + return result; } From 5aa35817de7cf85bd7eb6ee8a6eca6ae44a05f97 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Fri, 26 Dec 2025 16:15:41 +0500 Subject: [PATCH 05/30] feat: implement 4 week tasks --- 04_week/tasks/phasor/phasor.cpp | 265 +++++++++++++++++++++- 04_week/tasks/queue/queue.cpp | 167 ++++++++++++++ 04_week/tasks/ring_buffer/ring_buffer.cpp | 229 +++++++++++++++++++ 04_week/tasks/stack/stack.cpp | 73 ++++++ 4 files changed, 730 insertions(+), 4 deletions(-) diff --git a/04_week/tasks/phasor/phasor.cpp b/04_week/tasks/phasor/phasor.cpp index 3ec1b9ad..87a9b8a1 100644 --- a/04_week/tasks/phasor/phasor.cpp +++ b/04_week/tasks/phasor/phasor.cpp @@ -1,10 +1,267 @@ +#define _USE_MATH_DEFINES +#include +#include +#include -struct ExpTag {}; -struct DegTag {}; -struct AlgTag {}; +struct ExpTag{}; +struct DegTag{}; +struct AlgTag{}; -class Phasor { +class Phasor +{ + double real; + double imag; +public: + Phasor() : real(0.0), imag(0.0) {} + Phasor(double amplitude, double phase) : real(amplitude * std::cos(phase)), imag(amplitude * std::sin(phase)) {} + Phasor(double amplitude, double phase, ExpTag tag) : Phasor(amplitude, phase) {} + Phasor(double amplitude, double phase, DegTag tag) : Phasor(amplitude, M_PI * phase / 180) {} + Phasor(double real, double imag, AlgTag) : real(real), imag(imag) {} + + void SetPolar(const double &r, const double &angle); + void SetCartesian(const double &x, const double &y); + double Magnitude() const; + double Abs() const; + double Phase() const; + double Angle() const; + double PhaseDeg() const; + double AngleDeg() const; + double Real() const; + double Imag() const; + Phasor Conj() const; + Phasor Inv() const; + + Phasor operator+(const Phasor &other) const; + Phasor operator-(const Phasor &other) const; + Phasor operator*(const Phasor &other) const; + Phasor operator/(const Phasor &other) const; + Phasor operator-() const; + + Phasor& operator+=(const Phasor &other); + Phasor& operator-=(const Phasor &other); + Phasor& operator*=(const Phasor &other); + Phasor& operator/=(const Phasor &other); + + bool operator==(const Phasor &other) const; + bool operator!=(const Phasor &other) const; + + friend std::ostream& operator<<(std::ostream& os, const Phasor& phasor); }; + + +Phasor operator+(const Phasor &p, double scalar); +Phasor operator+(double scalar, const Phasor &p); +Phasor operator-(const Phasor &p, double scalar); +Phasor operator-(double scalar, const Phasor &p); +Phasor operator*(const Phasor &p, double scalar); +Phasor operator*(double scalar, const Phasor &p); +Phasor operator/(const Phasor &p, double scalar); +Phasor operator/(double scalar, const Phasor &p); + +void Phasor::SetPolar(const double &r, const double &angle) +{ + real = r * std::cos(angle); + imag = r * std::sin(angle); +} + +void Phasor::SetCartesian(const double &x, const double &y) +{ + real = x; + imag = y; +} + +double Phasor::Magnitude() const +{ + return std::sqrt(real * real + imag * imag); +} + +double Phasor::Abs() const +{ + return std::sqrt(real * real + imag * imag); +} + +double Phasor::Phase() const +{ + return std::atan2(imag, real); +} + +double Phasor::Angle() const +{ + return std::atan2(imag, real); +} + +double Phasor::PhaseDeg() const +{ + double angle_deg = 180 * Phase() / M_PI; + if (angle_deg > 180.0) { + angle_deg = 180.0; + } else if (angle_deg <= -180.0) { + angle_deg = 180.0; + } + return angle_deg; +} + +double Phasor::AngleDeg() const +{ + double angle_deg = 180 * Phase() / M_PI; + if (angle_deg > 180.0) { + angle_deg = 180.0; + } else if (angle_deg <= -180.0) { + angle_deg = 180.0; + } + return angle_deg; +} + +double Phasor::Real() const +{ + return real; +} + +double Phasor::Imag() const +{ + return imag; +} + +Phasor Phasor::operator+(const Phasor &other) const +{ + return Phasor(real + other.Real(), imag + other.Imag(), AlgTag{}); +} + +Phasor Phasor::operator-(const Phasor &other) const +{ + return Phasor(real - other.Real(), imag - other.Imag(), AlgTag{}); +} + +Phasor Phasor::operator*(const Phasor &other) const +{ + return Phasor(real * other.Real() - imag * other.Imag(), real * other.Imag() + imag * other.Real(), AlgTag{}); +} + +Phasor Phasor::operator/(const Phasor &other) const +{ + double denominator = other.Real() * other.Real() + other.Imag() * other.Imag(); + return Phasor((real * other.Real() + imag * other.Imag()) / denominator, (imag * other.Real() - real * other.Imag()) / denominator, AlgTag{}); +} + +Phasor Phasor::operator-() const +{ + return Phasor(-real, -imag, AlgTag{}); +} + +Phasor& Phasor::operator+=(const Phasor &other) +{ + real += other.Real(); + imag += other.Imag(); + return *this; +} + +Phasor& Phasor::operator-=(const Phasor &other) +{ + real -= other.Real(); + imag -= other.Imag(); + return *this; +} + +Phasor& Phasor::operator*=(const Phasor &other) +{ + SetCartesian(real * other.Real() - imag * other.Imag(), real * other.Imag() + imag * other.Real()); + return *this; +} + +Phasor& Phasor::operator/=(const Phasor &other) +{ + double denominator = other.Real() * other.Real() + other.Imag() * other.Imag(); + SetCartesian((real * other.Real() + imag * other.Imag()) / denominator, (imag * other.Real() - real * other.Imag()) / denominator); + return *this; +} + +bool Phasor::operator==(const Phasor &other) const +{ + const double EPSILON = 1e-9; + return (std::abs(real - other.Real()) < EPSILON && std::abs(imag - other.Imag()) < EPSILON); +} + +bool Phasor::operator!=(const Phasor &other) const +{ + return !(*this == other); +} + +Phasor Phasor::Conj() const +{ + return Phasor(real, -imag, AlgTag{}); +} + +Phasor Phasor::Inv() const +{ + double mag_sq = Magnitude() * Magnitude(); + if (mag_sq == 0.0) { + return Phasor(0.0, 0.0, AlgTag{}); + } else { + return Phasor(real / mag_sq, -imag / mag_sq, AlgTag{}); + } +} + +Phasor MakePhasorCartesian(double real, double imag) +{ + return Phasor(real, imag, AlgTag{}); +} + +Phasor MakePhasorPolar(double amplitude, double phase) +{ + return Phasor(amplitude, phase, ExpTag{}); +} + +Phasor MakePhasorPolarDeg(double amplitude, double phase_deg) +{ + return Phasor(amplitude, phase_deg, DegTag{}); +} + +Phasor operator+(const Phasor &p, double scalar) +{ + return Phasor(p.Real() + scalar, p.Imag(), AlgTag{}); +} + +Phasor operator+(double scalar, const Phasor &p) +{ + return Phasor(p.Real() + scalar, p.Imag(), AlgTag{}); +} + +Phasor operator-(const Phasor &p, double scalar) +{ + return Phasor(p.Real() - scalar, p.Imag(), AlgTag{}); +} + +Phasor operator-(double scalar, const Phasor &p) +{ + return Phasor(scalar - p.Real(), -p.Imag(), AlgTag{}); +} + +Phasor operator*(const Phasor &p, double scalar) +{ + return Phasor(p.Magnitude() * scalar, p.Phase(), ExpTag{}); +} + +Phasor operator*(double scalar, const Phasor &p) +{ + return Phasor(p.Magnitude() * scalar, p.Phase(), ExpTag{}); +} + +Phasor operator/(const Phasor &p, double scalar) +{ + return Phasor(p.Magnitude() / scalar, p.Phase(), ExpTag{}); +} + +Phasor operator/(double scalar, const Phasor &p) +{ + return Phasor(scalar / p.Magnitude(), -p.Phase(), ExpTag{}); +} + +std::ostream& operator<<(std::ostream& os, const Phasor& phasor){ + os << std::fixed << std::setprecision(3) << phasor.Magnitude() << "*e(j*" << phasor.PhaseDeg() << ") "; + os << "[" << phasor.Real() << " + j*" << phasor.Imag() << "]"; + return os; +} + diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index 2a9f8493..e62ee449 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -1,6 +1,173 @@ #include +#include +#include class Queue { + std::vector input; + std::vector output; + bool eqArrays(const std::vector& a, const std::vector& b) const; + std::vector unionArrays(const std::vector& input, const std::vector& output) const; + +public: + Queue(){}; + Queue(std::vector vector); + Queue(std::stack stack); + Queue(const std::initializer_list& list); + Queue(const size_t& size); + + void Push(const int& value); + + bool Pop(); + + int& Front(); + const int& Front() const; + + int& Back(); + const int& Back() const; + + bool Empty() const; + + int Size() const; + + void Clear(); + + void Swap(Queue& other); + + bool operator==(const Queue& other) const; + bool operator!=(const Queue& other) const; }; + + +Queue::Queue(std::vector vector) : output(std::move(vector)) { + std::reverse(output.begin(), output.end()); +} + +Queue::Queue(std::stack stack) { + while (!stack.empty()) { + output.push_back(stack.top()); + stack.pop(); + } +} + +Queue::Queue(const std::initializer_list& list) : output(list) { + std::reverse(output.begin(), output.end()); +} + +Queue::Queue(const size_t& size){ + input.reserve(size); +} + +void Queue::Push(const int& value){ + input.push_back(value); +} + +bool Queue::Pop(){ + if (input.empty() && output.empty()){ + return false; + } + + if (output.empty()){ + output.reserve(input.size()); + while (!input.empty()){ + output.push_back(input.back()); + input.pop_back(); + } + } + + output.pop_back(); + return true; +} + +int& Queue::Front(){ + if (output.empty()){ + return input.front(); + } else { + return output.back(); + } +} + +const int& Queue::Front() const { + if (output.empty()){ + return input.front(); + } else { + return output.back(); + } +} + +int& Queue::Back(){ + if (input.empty()){ + return output.front(); + } else { + return input.back(); + } +} + +const int& Queue::Back() const { + if (input.empty()){ + return output.front(); + } else { + return input.back(); + } +} + +bool Queue::Empty() const { + return (input.empty()) && (output.empty()); +} + +int Queue::Size() const { + return input.size() + output.size(); +} + +void Queue::Clear(){ + input.clear(); + output.clear(); +} + +void Queue::Swap(Queue& other){ + std::vector tmpInput = std::move(input); + std::vector tmpOutput = std::move(output); + input = std::move(other.input); + output = std::move(other.output); + other.input = std::move(tmpInput); + other.output = std::move(tmpOutput); +} + +bool Queue::eqArrays(const std::vector& a, const std::vector& b) const{ + if (a.size() != b.size()){ return false; } + if (a.empty() && b.empty()){ return true; } + + for (int i = 0; i < a.size(); ++i) { + if (a[i] != b[i]) { + return false; + } + } + return true; +} + +std::vector Queue::unionArrays(const std::vector& input, const std::vector& output) const { + std::vector unionArray; + unionArray.reserve(input.size() + output.size()); + + for (auto it = output.rbegin(); it != output.rend(); ++it) { + unionArray.push_back(*it); + } + + for (size_t i=0; i < input.size(); ++i){ + unionArray.push_back(input[i]); + } + + return unionArray; +} + +bool Queue::operator==(const Queue& other) const { + return eqArrays(unionArrays(input, output), unionArrays(other.input, other.output)); +} + +bool Queue::operator!=(const Queue& other) const { + if (*this == other){ + return false; + } + return true; +} \ No newline at end of file diff --git a/04_week/tasks/ring_buffer/ring_buffer.cpp b/04_week/tasks/ring_buffer/ring_buffer.cpp index e2b57ba2..d8559ecd 100644 --- a/04_week/tasks/ring_buffer/ring_buffer.cpp +++ b/04_week/tasks/ring_buffer/ring_buffer.cpp @@ -1,6 +1,235 @@ #include +#include class RingBuffer { + std::vector buffer; + size_t capacity = 0; + size_t size = 0; + +public: + size_t begin = 0; + size_t end = 0; + + RingBuffer(size_t size); + RingBuffer(size_t size, const int& value); + RingBuffer(const std::initializer_list& list); + + void Push(const int& value); + bool TryPush(const int& value); + + void Pop(); + bool TryPop(int& value); + + int& operator[](const size_t& idx); + const int& operator[](const size_t& idx) const; + + int& Front(); + const int& Front() const; + + int& Back(); + const int& Back() const; + + int Size() const; + + int Capacity() const; + + bool Empty() const; + + bool Full() const; + + void Clear(); + + void Resize(size_t new_size); + + std::vector Vector() const; }; + + +RingBuffer::RingBuffer(size_t size){ + if (size == 0){ + size = 1; + } + + buffer.resize(size); + capacity = size; +} + +RingBuffer::RingBuffer(size_t size, const int& value){ + if (size == 0){ + size = 1; + } + + buffer.resize(size, value); + capacity = size; + this->size = size; + end = size-1; +} + +RingBuffer::RingBuffer(const std::initializer_list& list) : RingBuffer(list.size() > 0 ? list.size() : 1) +{ + if (list.size() > 0) { + size = list.size(); + end = list.size() - 1; + begin = 0; + buffer = list; + } +} + +void RingBuffer::Push(const int& value){ + if (size == 0){ + end = 0; + buffer[end] = value; + ++size; + } else { + if (end < capacity-1){ + if (size < capacity){ ++size; } + ++end; + buffer[end] = value; + } else if (end == capacity-1 && size != capacity){ + ++size; + end = 0; + buffer[end] = value; + } else if (end == capacity-1 && size == capacity){ + end = 0; + buffer[end] = value; + } + + if (end == begin && begin != capacity-1){ + ++begin; + } else if (end == begin && begin == capacity-1){ + begin = 0; + } + } +} + +bool RingBuffer::TryPush(const int& value){ + if (size == 0){ + end = 0; + buffer[end] = value; + ++size; + } else { + if (end < capacity-1 && end+1 != begin){ + ++size; + ++end; + buffer[end] = value; + } else if (end == capacity-1 && size != capacity){ // && begin != 0 + ++size; + end = 0; + buffer[end] = value; + } else { + return false; + } + } + + return true; +} + +void RingBuffer::Pop(){ + if (size > 0){ + if (begin+1 <= end){ + ++begin; + } else { + begin = 0; + } + --size; + + } + +} + +bool RingBuffer::TryPop(int& value){ + if (size > 0){ + value = buffer[begin]; + + if (begin+1 <= end){ + ++begin; + } else { + begin = 0; + } + --size; + return true; + } + + return false; +} + +int& RingBuffer::operator[](const size_t& idx) { + return buffer[(begin + idx) % capacity]; +} + +const int& RingBuffer::operator[](const size_t& idx) const { + return buffer[(begin + idx) % capacity]; +} + +int& RingBuffer::Front() { return buffer[end]; } +const int& RingBuffer::Front() const { return buffer[end]; } + +int& RingBuffer::Back() { return buffer[begin]; } +const int& RingBuffer::Back() const { return buffer[begin]; } + +int RingBuffer::Size() const { return size; } + +int RingBuffer::Capacity() const { return capacity; } + +bool RingBuffer::Empty() const { return size == 0; } + +bool RingBuffer::Full() const { return size == capacity; } + +void RingBuffer::Clear() { + end = 0; + begin = 0; + size = 0; +} + +void RingBuffer::Resize(size_t new_size) { + if (new_size == capacity) { + return; + } + + if (new_size == 0){ + new_size = 1; + } + + std::vector new_buffer(new_size); + size_t new_current_size; + + if (new_size >= size){ + for (size_t i = 0; i < size; ++i) { + new_buffer[i] = buffer[i]; + new_current_size = size; + } + } else { + size_t j = begin+(size-new_size); + for (size_t i = 0; i < new_size; ++i) { + if (i+(size-new_size) < capacity){ + new_buffer[i] = buffer[j]; + new_current_size = new_size; + } else { + i = 0; + } + + if (j+1 < capacity){ + ++j; + } else { + j = 0; + } + } + } + + buffer = std::move(new_buffer); + capacity = new_size; + size = new_current_size; + begin = 0; + end = (size == 0) ? 0 : size - 1; +} + +std::vector RingBuffer::Vector() const { + std::vector result_vector; + result_vector.reserve(size); + for (size_t i = 0; i < size; ++i) { + result_vector.push_back(buffer[(begin + i) % capacity]); + } + return result_vector; +} \ No newline at end of file diff --git a/04_week/tasks/stack/stack.cpp b/04_week/tasks/stack/stack.cpp index 222e4ffc..5f0b109a 100644 --- a/04_week/tasks/stack/stack.cpp +++ b/04_week/tasks/stack/stack.cpp @@ -2,5 +2,78 @@ class Stack { + std::vector stack; +public: + void Push(const int& value); + + int& Top(); + const int& Top() const; + + int Pop(); + + bool Empty() const; + + int Size() const; + + void Clear(); + + void Swap(Stack& other); + + bool operator==(const Stack& other) const; + + bool operator!=(const Stack& other) const; }; + +void Stack::Push(const int& value){ + stack.push_back(value); +} + +int& Stack::Top() { + return stack.back(); +} + +const int& Stack::Top() const { + return stack.back(); +} + +int Stack::Pop(){ + if (!Empty()){ + int value = stack.back(); + stack.pop_back(); + return value; + } + return 0; +} + +bool Stack::Empty() const { + return stack.empty(); +} + +int Stack::Size() const { + return stack.size(); +} + +void Stack::Clear(){ + stack.clear(); +} + +void Stack::Swap(Stack& other){ + Stack tmp = std::move(*this); + *this = std::move(other); + other = std::move(tmp); +} + +bool Stack::operator==(const Stack& other) const { + if (Size() != other.Size()){ return false; } + for (int i = 0; i < Size(); ++i) { + if (stack[i] != other.stack[i]) { + return false; + } + } + return true; +} + +bool Stack::operator!=(const Stack& other) const { + return !(*this == other); +} From 7505ec7979d678c46ab2217a755474ed58ac9ecb Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Fri, 26 Dec 2025 18:00:36 +0500 Subject: [PATCH 06/30] fix mistakes --- 01_week/tasks/char_changer/char_changer.cpp | 2 +- 01_week/tasks/check_flags/check_flags.cpp | 2 +- 01_week/tasks/rms/rms.cpp | 2 +- 03_week/tasks/enum_operators/enum_operators.cpp | 1 - 04_week/tasks/phasor/phasor.cpp | 4 ++-- 04_week/tasks/queue/queue.cpp | 2 +- 6 files changed, 6 insertions(+), 7 deletions(-) diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index d4ec5de8..be2df247 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -2,7 +2,7 @@ #include -size_t CharChanger(char array[], size_t size, char delimiter = ' ') { +size_t CharChanger(char array[], size_t, char delimiter = ' ') { int counter = 0; // Счётчик повторяющихся символов int write = 0; // Указатель для записи обработанного символа int read = 0; // Указатель для чтения следующего элемента из массива diff --git a/01_week/tasks/check_flags/check_flags.cpp b/01_week/tasks/check_flags/check_flags.cpp index 4fe4d5f0..22507684 100644 --- a/01_week/tasks/check_flags/check_flags.cpp +++ b/01_week/tasks/check_flags/check_flags.cpp @@ -47,7 +47,7 @@ void PrintCheckFlags(CheckFlags flags) { } std::cout << "["; - for (int i = 0; i < set_flags.size(); i++) { + for (size_t i = 0; i < set_flags.size(); i++) { std::cout << set_flags[i]; if (i < set_flags.size() - 1) { std::cout << ","; diff --git a/01_week/tasks/rms/rms.cpp b/01_week/tasks/rms/rms.cpp index f13c54bb..04c4114f 100644 --- a/01_week/tasks/rms/rms.cpp +++ b/01_week/tasks/rms/rms.cpp @@ -6,7 +6,7 @@ double CalculateRMS(double values[], size_t size) { if (size <= 0 || values == nullptr){ return 0.0; } double sum = 0; - for (int i=0; i < size; i++){ + for (size_t i=0; i < size; i++){ sum+=std::pow(values[i], 2); } diff --git a/03_week/tasks/enum_operators/enum_operators.cpp b/03_week/tasks/enum_operators/enum_operators.cpp index 928db352..75f0e34b 100644 --- a/03_week/tasks/enum_operators/enum_operators.cpp +++ b/03_week/tasks/enum_operators/enum_operators.cpp @@ -59,7 +59,6 @@ CheckFlags operator~(CheckFlags a) { } std::ostream& operator<<(std::ostream& os, CheckFlags flags) { - uint8_t NOT_IN_RANGE = (1 << 6) | (1 << 7); uint8_t flags_val = static_cast(flags); uint8_t valid_flags = flags_val & static_cast(CheckFlags::ALL); diff --git a/04_week/tasks/phasor/phasor.cpp b/04_week/tasks/phasor/phasor.cpp index 87a9b8a1..6055f6a2 100644 --- a/04_week/tasks/phasor/phasor.cpp +++ b/04_week/tasks/phasor/phasor.cpp @@ -17,8 +17,8 @@ class Phasor public: Phasor() : real(0.0), imag(0.0) {} Phasor(double amplitude, double phase) : real(amplitude * std::cos(phase)), imag(amplitude * std::sin(phase)) {} - Phasor(double amplitude, double phase, ExpTag tag) : Phasor(amplitude, phase) {} - Phasor(double amplitude, double phase, DegTag tag) : Phasor(amplitude, M_PI * phase / 180) {} + Phasor(double amplitude, double phase, ExpTag) : Phasor(amplitude, phase) {} + Phasor(double amplitude, double phase, DegTag) : Phasor(amplitude, M_PI * phase / 180) {} Phasor(double real, double imag, AlgTag) : real(real), imag(imag) {} void SetPolar(const double &r, const double &angle); diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index e62ee449..a0e1eeba 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -138,7 +138,7 @@ bool Queue::eqArrays(const std::vector& a, const std::vector& b) const if (a.size() != b.size()){ return false; } if (a.empty() && b.empty()){ return true; } - for (int i = 0; i < a.size(); ++i) { + for (size_t i = 0; i < a.size(); ++i) { if (a[i] != b[i]) { return false; } From 57d12f5302aaf7401b00df9650b4563e653bfa49 Mon Sep 17 00:00:00 2001 From: Ilya Mikhailov Date: Sun, 8 Feb 2026 14:40:01 +0500 Subject: [PATCH 07/30] Update check_flags.cpp --- 01_week/tasks/check_flags/check_flags.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/01_week/tasks/check_flags/check_flags.cpp b/01_week/tasks/check_flags/check_flags.cpp index 22507684..34cecc9f 100644 --- a/01_week/tasks/check_flags/check_flags.cpp +++ b/01_week/tasks/check_flags/check_flags.cpp @@ -47,11 +47,11 @@ void PrintCheckFlags(CheckFlags flags) { } std::cout << "["; - for (size_t i = 0; i < set_flags.size(); i++) { + for (size_t i = 0; i < set_flags.size(); ++i) { std::cout << set_flags[i]; if (i < set_flags.size() - 1) { std::cout << ","; } } std::cout << "]"; -} \ No newline at end of file +} From eea9f87613e9dfdd2eb3d68130b59a6c0e1ebde0 Mon Sep 17 00:00:00 2001 From: Ilya Mikhailov Date: Sun, 8 Feb 2026 14:42:28 +0500 Subject: [PATCH 08/30] Update rms.cpp --- 01_week/tasks/rms/rms.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/01_week/tasks/rms/rms.cpp b/01_week/tasks/rms/rms.cpp index 04c4114f..75bcb8c0 100644 --- a/01_week/tasks/rms/rms.cpp +++ b/01_week/tasks/rms/rms.cpp @@ -6,9 +6,9 @@ double CalculateRMS(double values[], size_t size) { if (size <= 0 || values == nullptr){ return 0.0; } double sum = 0; - for (size_t i=0; i < size; i++){ + for (size_t i=0; i < size; ++i){ sum+=std::pow(values[i], 2); } return std::sqrt(sum / size); -} \ No newline at end of file +} From 939790e86f515cc0af138dca1db496b7cb5f6fde Mon Sep 17 00:00:00 2001 From: Ilya Mikhailov Date: Sun, 8 Feb 2026 16:21:35 +0500 Subject: [PATCH 09/30] Update addition.cpp --- 01_week/tasks/addition/addition.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 3529a582..f3469331 100644 --- a/01_week/tasks/addition/addition.cpp +++ b/01_week/tasks/addition/addition.cpp @@ -2,5 +2,5 @@ int64_t Addition(int a, int b) { - return static_cast(a) + static_cast(b); -} \ No newline at end of file + return static_cast(a) + b; +} From 8fa735a4908ad21e5c3844d879b129ae16bee7e4 Mon Sep 17 00:00:00 2001 From: Ilya Mikhailov Date: Sun, 8 Feb 2026 16:23:15 +0500 Subject: [PATCH 10/30] Rename write/read variables to pos_write/pos_read Refactor variable names for clarity in CharChanger function. --- 01_week/tasks/char_changer/char_changer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index be2df247..a06d43e0 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -4,8 +4,8 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { int counter = 0; // Счётчик повторяющихся символов - int write = 0; // Указатель для записи обработанного символа - int read = 0; // Указатель для чтения следующего элемента из массива + int pos_write = 0; // Индекс для записи обработанного символа + int pos_read = 0; // Индекс для чтения следующего элемента из массива char repeating_symbol = array[read]; // В процессе выполнения - редыдущий символ после read(отслеживаем повторения) while (repeating_symbol != '\0'){ @@ -46,4 +46,4 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { array[write] = '\0'; return write; -} \ No newline at end of file +} From a6df4ebc9787a619ab9111e0104f92e1a0000306 Mon Sep 17 00:00:00 2001 From: Ilya Mikhailov Date: Sun, 8 Feb 2026 16:27:25 +0500 Subject: [PATCH 11/30] Update char_changer.cpp --- 01_week/tasks/char_changer/char_changer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index a06d43e0..d297806c 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -10,7 +10,7 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { while (repeating_symbol != '\0'){ if (repeating_symbol == array[read]){ - counter++; + ++counter; } else { if (isalpha(repeating_symbol)){ array[write] = toupper(repeating_symbol); @@ -22,7 +22,7 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { array[write] = '_'; } - write++; + ++write; if (repeating_symbol == ' ') { counter = 1; @@ -35,13 +35,13 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { if (counter != 1){ array[write] = static_cast(counter + '0'); // Преобразуем число в символ counter = 1; - write++; + ++write; } repeating_symbol = array[read]; } - read++; + ++read; } array[write] = '\0'; From 876338ac231678fa38336738c3e0531a76663f27 Mon Sep 17 00:00:00 2001 From: Ilya Mikhailov Date: Sun, 8 Feb 2026 16:36:33 +0500 Subject: [PATCH 12/30] Update char_changer.cpp --- 01_week/tasks/char_changer/char_changer.cpp | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index d297806c..f5bed071 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -6,23 +6,23 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { int counter = 0; // Счётчик повторяющихся символов int pos_write = 0; // Индекс для записи обработанного символа int pos_read = 0; // Индекс для чтения следующего элемента из массива - char repeating_symbol = array[read]; // В процессе выполнения - редыдущий символ после read(отслеживаем повторения) + char repeating_symbol = array[pos_read]; // В процессе выполнения - предыдущий символ после pos_read(отслеживаем повторения) while (repeating_symbol != '\0'){ - if (repeating_symbol == array[read]){ + if (repeating_symbol == array[pos_read]){ ++counter; } else { if (isalpha(repeating_symbol)){ - array[write] = toupper(repeating_symbol); + array[pos_write] = toupper(repeating_symbol); } else if (isdigit(repeating_symbol)){ - array[write] = '*'; + array[pos_write] = '*'; } else if (repeating_symbol == ' '){ - array[write] = delimiter; + array[pos_write] = delimiter; } else { - array[write] = '_'; + array[pos_write] = '_'; } - ++write; + ++pos_write; if (repeating_symbol == ' ') { counter = 1; @@ -33,15 +33,15 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { } if (counter != 1){ - array[write] = static_cast(counter + '0'); // Преобразуем число в символ + array[pos_write] = static_cast(counter + '0'); // Преобразуем число в символ counter = 1; - ++write; + ++pos_write; } - repeating_symbol = array[read]; + repeating_symbol = array[pos_read]; } - ++read; + ++pos_read; } array[write] = '\0'; From adf1073627245646b932f2b1e179178f620445fd Mon Sep 17 00:00:00 2001 From: Ilya Mikhailov Date: Sun, 8 Feb 2026 16:39:57 +0500 Subject: [PATCH 13/30] Fix null terminator position in char_changer --- 01_week/tasks/char_changer/char_changer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index f5bed071..500be4b2 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -44,6 +44,6 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { ++pos_read; } - array[write] = '\0'; - return write; + array[pos_write] = '\0'; + return pos_write; } From 8f2fe09bf0e0c0a17e9edf2725fafbc5fd88c76d Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 17:37:27 +0500 Subject: [PATCH 14/30] Refactor CharChanger function to improve readability and efficiency - Renamed counter variable for repeated symbols for clarity. - Replaced while loop with a for loop for better iteration over the character array. - Simplified logic for handling character transformations and counting repetitions. --- 01_week/tasks/char_changer/char_changer.cpp | 60 ++++++++++----------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index 500be4b2..5438542a 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -3,45 +3,43 @@ size_t CharChanger(char array[], size_t, char delimiter = ' ') { - int counter = 0; // Счётчик повторяющихся символов + int counterRepeatedSymbols = 1; // Счётчик повторяющихся символов int pos_write = 0; // Индекс для записи обработанного символа - int pos_read = 0; // Индекс для чтения следующего элемента из массива - char repeating_symbol = array[pos_read]; // В процессе выполнения - предыдущий символ после pos_read(отслеживаем повторения) - - while (repeating_symbol != '\0'){ - if (repeating_symbol == array[pos_read]){ - ++counter; + char repeating_symbol = array[0]; // В процессе выполнения - предыдущий символ(отслеживаем повторения) + + for (char* ptr = array + 1; repeating_symbol != '\0'; ++ptr) { + if (repeating_symbol == *ptr){ + ++counterRepeatedSymbols; + continue; + } + + if (isalpha(repeating_symbol)){ + array[pos_write] = toupper(repeating_symbol); + } else if (isdigit(repeating_symbol)){ + array[pos_write] = '*'; + } else if (repeating_symbol == ' '){ + array[pos_write] = delimiter; } else { - if (isalpha(repeating_symbol)){ - array[pos_write] = toupper(repeating_symbol); - } else if (isdigit(repeating_symbol)){ - array[pos_write] = '*'; - } else if (repeating_symbol == ' '){ - array[pos_write] = delimiter; - } else { - array[pos_write] = '_'; - } - - ++pos_write; + array[pos_write] = '_'; + } + ++pos_write; - if (repeating_symbol == ' ') { - counter = 1; - } - if (counter >= 10){ - counter = 0; - } + if (repeating_symbol == ' ') { + counterRepeatedSymbols = 1; + } - if (counter != 1){ - array[pos_write] = static_cast(counter + '0'); // Преобразуем число в символ - counter = 1; - ++pos_write; - } + if (counterRepeatedSymbols >= 10){ + counterRepeatedSymbols = 0; + } - repeating_symbol = array[pos_read]; + if (counterRepeatedSymbols != 1){ + array[pos_write] = static_cast(counterRepeatedSymbols + '0'); // Преобразуем число в символ + counterRepeatedSymbols = 1; + ++pos_write; } - ++pos_read; + repeating_symbol = *ptr; } array[pos_write] = '\0'; From d5f22a7c2f1bc589a68924f82eb1d9fe8185d0f0 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 17:55:36 +0500 Subject: [PATCH 15/30] Refactor PrintCheckFlags function to enhance output formatting - Removed unnecessary vector for storing flag names. - Streamlined output logic to directly print flags in a comma-separated format. - Improved readability by using a boolean flag to manage comma placement. --- 01_week/tasks/check_flags/check_flags.cpp | 37 ++++++++++++----------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/01_week/tasks/check_flags/check_flags.cpp b/01_week/tasks/check_flags/check_flags.cpp index 34cecc9f..9f39250e 100644 --- a/01_week/tasks/check_flags/check_flags.cpp +++ b/01_week/tasks/check_flags/check_flags.cpp @@ -1,7 +1,5 @@ #include #include -#include -#include enum class CheckFlags : uint8_t { @@ -26,32 +24,37 @@ void PrintCheckFlags(CheckFlags flags) { return; } - std::vector set_flags; + std::cout << "["; + bool first = true; if ((static_cast(flags) & static_cast(CheckFlags::TIME)) != 0) { - set_flags.push_back("TIME"); + if (!first) std::cout << ","; + std::cout << "TIME"; + first = false; } if ((static_cast(flags) & static_cast(CheckFlags::DATE)) != 0) { - set_flags.push_back("DATE"); + if (!first) std::cout << ","; + std::cout << "DATE"; + first = false; } if ((static_cast(flags) & static_cast(CheckFlags::USER)) != 0) { - set_flags.push_back("USER"); + if (!first) std::cout << ","; + std::cout << "USER"; + first = false; } if ((static_cast(flags) & static_cast(CheckFlags::CERT)) != 0) { - set_flags.push_back("CERT"); + if (!first) std::cout << ","; + std::cout << "CERT"; + first = false; } if ((static_cast(flags) & static_cast(CheckFlags::KEYS)) != 0) { - set_flags.push_back("KEYS"); + if (!first) std::cout << ","; + std::cout << "KEYS"; + first = false; } if ((static_cast(flags) & static_cast(CheckFlags::DEST)) != 0) { - set_flags.push_back("DEST"); - } - - std::cout << "["; - for (size_t i = 0; i < set_flags.size(); ++i) { - std::cout << set_flags[i]; - if (i < set_flags.size() - 1) { - std::cout << ","; - } + if (!first) std::cout << ","; + std::cout << "DEST"; + first = false; } std::cout << "]"; } From f116bf56a3c0b9d498305b1e990b277c372bd856 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 18:09:07 +0500 Subject: [PATCH 16/30] Update README.md to clarify return pointer behavior in predicate function description --- 02_week/tasks/last_of_us/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02_week/tasks/last_of_us/README.md b/02_week/tasks/last_of_us/README.md index 077a84fb..56fee4ec 100644 --- a/02_week/tasks/last_of_us/README.md +++ b/02_week/tasks/last_of_us/README.md @@ -4,7 +4,7 @@ элемент, удовлетворяющий условию функции предиката. Функция принимает два указателя на начало и конец целочисленного массива, а также указатель на функцию предикат, и возвращает указатель на найденный элемент. Если элемент -не найден, то возвращается указатель на последний элемент. +не найден, то возвращается указатель за последний элемент. Указатели соответствуют диапазону $[begin, end)$. From ea3bb073859b6a9e3e56d92161a30b6e9dbdb79b Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 18:10:39 +0500 Subject: [PATCH 17/30] Fix FindLastElement function to correctly initialize last_element pointer - Changed initialization of last_element from nullptr to end to ensure proper return behavior. - Removed unnecessary null check before returning last_element. --- 02_week/tasks/last_of_us/last_of_us.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 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 ef17e289..229bacf8 100644 --- a/02_week/tasks/last_of_us/last_of_us.cpp +++ b/02_week/tasks/last_of_us/last_of_us.cpp @@ -11,7 +11,7 @@ const int* FindLastElement(const int* begin, const int* end, funcPtr predicate) return end; } - const int* last_element = nullptr; + const int* last_element = end; while (begin < end) { @@ -21,10 +21,6 @@ const int* FindLastElement(const int* begin, const int* end, funcPtr predicate) ++begin; } - - if (last_element == nullptr){ - return end; - } return last_element; } \ No newline at end of file From 160fb3711fc8e1b22d4ebea4978664750276a6c1 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 18:43:30 +0500 Subject: [PATCH 18/30] Refactor FindLastElement function to improve return logic - Changed the iteration to decrement the end pointer and return it directly when the predicate is satisfied. - Removed the last_element pointer initialization and unnecessary checks for clarity and efficiency. --- 02_week/tasks/last_of_us/last_of_us.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 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 229bacf8..b63142c8 100644 --- a/02_week/tasks/last_of_us/last_of_us.cpp +++ b/02_week/tasks/last_of_us/last_of_us.cpp @@ -15,11 +15,10 @@ const int* FindLastElement(const int* begin, const int* end, funcPtr predicate) while (begin < end) { - if (predicate(*begin)){ - last_element = begin; + --end; + if (predicate(*end)){ + return end; } - - ++begin; } return last_element; From b7613d488f08b35fcea56615c2dd5f5a42d672a3 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 19:01:39 +0500 Subject: [PATCH 19/30] Refactor PrintMemory functions to simplify byte handling and improve output logic - Replaced memcpy with direct reinterpretation of the number's bytes for both int and double types. - Streamlined the output logic to handle byte inversion directly during printing, enhancing readability and efficiency. --- 02_week/tasks/little_big/little_big.cpp | 49 +++++++++++++++---------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/02_week/tasks/little_big/little_big.cpp b/02_week/tasks/little_big/little_big.cpp index 1887280e..b8f40a5e 100644 --- a/02_week/tasks/little_big/little_big.cpp +++ b/02_week/tasks/little_big/little_big.cpp @@ -3,36 +3,45 @@ #include #include + void PrintMemory(int number, bool inversion = false) { - unsigned char bytes[sizeof(int)]; - std::memcpy(bytes, &number, sizeof(int)); - - if (inversion) { - std::reverse(bytes, bytes + sizeof(int)); - } + unsigned char* bytes = reinterpret_cast(&number); std::cout << "0x"; - for (size_t i = 0; i < sizeof(int); ++i) { - std::cout << std::hex << std::uppercase - << std::setfill('0') << std::setw(2) - << static_cast(bytes[i]); + if (inversion) { + // Читаем байты в обратном порядке сразу при выводе + for (size_t i = sizeof(int); i > 0; --i) { + std::cout << std::hex << std::uppercase + << std::setfill('0') << std::setw(2) + << static_cast(bytes[i-1]); + } + } else { + for (size_t i = 0; i < sizeof(int); ++i) { + std::cout << std::hex << std::uppercase + << std::setfill('0') << std::setw(2) + << static_cast(bytes[i]); + } } std::cout << std::dec << std::endl; } void PrintMemory(double number, bool inversion = false) { - unsigned char bytes[sizeof(double)]; - std::memcpy(bytes, &number, sizeof(double)); - - if (inversion) { - std::reverse(bytes, bytes + sizeof(double)); - } + unsigned char* bytes = reinterpret_cast(&number); std::cout << "0x"; - for (size_t i = 0; i < sizeof(double); ++i) { - std::cout << std::hex << std::uppercase - << std::setfill('0') << std::setw(2) - << static_cast(bytes[i]); + if (inversion) { + // Читаем байты в обратном порядке сразу при выводе + for (size_t i = sizeof(double); i > 0; --i) { + std::cout << std::hex << std::uppercase + << std::setfill('0') << std::setw(2) + << static_cast(bytes[i-1]); + } + } else { + for (size_t i = 0; i < sizeof(double); ++i) { + std::cout << std::hex << std::uppercase + << std::setfill('0') << std::setw(2) + << static_cast(bytes[i]); + } } std::cout << std::dec << std::endl; } \ No newline at end of file From 7d7117b19e33cf5ba02348ff14b26f875a1c97dd Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 19:10:22 +0500 Subject: [PATCH 20/30] Refactor FindLongestSubsequence function to simplify pointer handling - Updated the logic for advancing the left pointer to improve clarity and efficiency. - Removed unnecessary while loop, directly assigning the right pointer to left for better readability. --- 02_week/tasks/longest/longest.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/02_week/tasks/longest/longest.cpp b/02_week/tasks/longest/longest.cpp index 4940ed99..4d8b2f69 100644 --- a/02_week/tasks/longest/longest.cpp +++ b/02_week/tasks/longest/longest.cpp @@ -22,9 +22,7 @@ char* FindLongestSubsequence(const char* begin, const char* end, size_t &count) } length = 0; - while (left != right){ - ++left; - } + left = right; } } From 450cc1af856260aff0024693eeed348d29e4870b Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 19:17:21 +0500 Subject: [PATCH 21/30] Enhance FindLongestSubsequence function with overload for non-const char pointers - Changed the return type of the existing FindLongestSubsequence function to const char* for better type safety. - Added an overload for FindLongestSubsequence that accepts non-const char pointers, utilizing the const version internally. - Improved pointer handling by removing unnecessary const_cast in the return statement. --- 02_week/tasks/longest/longest.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/02_week/tasks/longest/longest.cpp b/02_week/tasks/longest/longest.cpp index 4d8b2f69..f14d3470 100644 --- a/02_week/tasks/longest/longest.cpp +++ b/02_week/tasks/longest/longest.cpp @@ -1,4 +1,4 @@ -char* FindLongestSubsequence(const char* begin, const char* end, size_t &count) { +const char* FindLongestSubsequence(const char* begin, const char* end, size_t &count) { if (begin == nullptr || end == nullptr || begin >= end){ count = 0; return nullptr; @@ -32,5 +32,16 @@ char* FindLongestSubsequence(const char* begin, const char* end, size_t &count) } count = max_length; - return const_cast(startOfSubsequence); + return startOfSubsequence; +} + +char* FindLongestSubsequence(char* begin, char* end, size_t &count) { + // Вызываем константную версию + const char* result = FindLongestSubsequence( + const_cast(begin), + const_cast(end), + count + ); + // Снимаем константность + return const_cast(result); } \ No newline at end of file From 9a5d1b1465ca167c792c6e5c4bef51318bcb35c0 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 19:24:11 +0500 Subject: [PATCH 22/30] Refactor PrintArray function to enhance clarity and efficiency - Updated input validation to simplify null and boundary checks. - Streamlined element printing logic by calculating total elements and using a single loop for both forward and backward traversal. - Improved output formatting by handling line breaks based on the specified count, enhancing readability. --- 02_week/tasks/pretty_array/pretty_array.cpp | 38 ++++++++++----------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/02_week/tasks/pretty_array/pretty_array.cpp b/02_week/tasks/pretty_array/pretty_array.cpp index f6c872e2..461c929e 100644 --- a/02_week/tasks/pretty_array/pretty_array.cpp +++ b/02_week/tasks/pretty_array/pretty_array.cpp @@ -1,34 +1,32 @@ #include void PrintArray(const int* begin, const int* end, const int count = 0) { - if (begin == nullptr || end == nullptr || count < 0) { + if (!begin || !end || begin == end || count < 0) { std::cout << "[]\n"; return; } + // Определяем направление: 1 для движения вперед, -1 для движения назад + const int step = (begin < end) ? 1 : -1; + + // Вычисляем количество элементов (всегда положительное) + const int total_elements = (step == 1) ? (end - begin) : (begin - end); + std::cout << "["; - if (begin < end) { - for (const int* ptr = begin; ptr < end; ++ptr) { - if (count > 0 && static_cast(ptr - begin) % count == 0 && ptr != begin){ - std::cout << "...\n "; - } + for (int i = 0; i < total_elements; ++i) { + // Вычисляем текущий указатель на основе индекса и шага + const int* ptr = begin + (i * step); - std::cout << *ptr; - if (ptr != end - 1) { - std::cout << ", "; - } + // Логика переноса строки (count) + if (count > 0 && i > 0 && i % count == 0) { + std::cout << "...\n "; } - } - else if (begin > end) { - for (const int* ptr = begin; ptr > end; --ptr) { - if (count > 0 && static_cast(begin - ptr) % count == 0 && ptr != begin) { - std::cout << "...\n "; - } - std::cout << *ptr; - if (ptr != end + 1) { - std::cout << ", "; - } + std::cout << *ptr; + + // Запятая, если это не последний элемент + if (i < total_elements - 1) { + std::cout << ", "; } } std::cout << "]\n"; From 5784369b8092f01a1a7523dd7ae80714bdb067da Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 19:27:05 +0500 Subject: [PATCH 23/30] Refactor CalculateDataStats function to accept a const reference for improved performance - Changed the parameter type of CalculateDataStats from std::vector to const std::vector& to avoid unnecessary copies and enhance efficiency. --- 03_week/tasks/data_stats/data_stats.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/03_week/tasks/data_stats/data_stats.cpp b/03_week/tasks/data_stats/data_stats.cpp index cf62e3b4..70bc78e5 100644 --- a/03_week/tasks/data_stats/data_stats.cpp +++ b/03_week/tasks/data_stats/data_stats.cpp @@ -6,7 +6,7 @@ struct DataStats { double sd = 0.0; }; -DataStats CalculateDataStats(std::vector numArray) { +DataStats CalculateDataStats(const std::vector& numArray) { double average = 0.0; double standardDeviation = 0.0; From 15c75f21d827a5807cf6e79f4a2081496de6d80b Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 19:47:49 +0500 Subject: [PATCH 24/30] Refactor Date and StudentInfo structs to simplify comparison operators - Replaced manual comparison logic with std::tie for equality, inequality, and relational operators in both Date and StudentInfo structs. - Streamlined operator implementations for improved readability and maintainability. --- 03_week/tasks/easy_compare/easy_compare.cpp | 115 +++----------------- 1 file changed, 18 insertions(+), 97 deletions(-) diff --git a/03_week/tasks/easy_compare/easy_compare.cpp b/03_week/tasks/easy_compare/easy_compare.cpp index cd7971f4..60471f49 100644 --- a/03_week/tasks/easy_compare/easy_compare.cpp +++ b/03_week/tasks/easy_compare/easy_compare.cpp @@ -1,71 +1,36 @@ #include +#include struct Date { - unsigned year; - unsigned month; - unsigned day; - - Date(){this->year = 0u; this->month = 0u; this->day = 0u;} + unsigned year = 0u; + unsigned month = 0u; + unsigned day = 0u; + + Date(){} Date(unsigned year, unsigned month, unsigned day) : year(year), month(month), day(day){} bool operator==(Date other) const { - if (this->year == other.year && this->month == other.month && this->day == other.day){ - return true; - } - return false; + return std::tie(year, month, day) == std::tie(other.year, other.month, other.day); } bool operator!=(Date other) const { - if (!this->operator==(other)){ - return true; - } - return false; + return !(*this == other); } bool operator<(Date other) const { - if (this->year < other.year){ - return true; - } else if (this->year == other.year){ - if (this->month < other.month){ - return true; - } else if (this->month == other.month){ - if (this->day < other.day){ - return true; - } - } - } - - return false; + return std::tie(year, month, day) < std::tie(other.year, other.month, other.day); } bool operator>(Date other) const { - if (this->year > other.year){ - return true; - } else if (this->year == other.year){ - if (this->month > other.month){ - return true; - } else if (this->month == other.month){ - if (this->day > other.day){ - return true; - } - } - } - - return false; + return std::tie(year, month, day) > std::tie(other.year, other.month, other.day); } bool operator>=(Date other) const { - if (this->operator>(other) || this->operator==(other)){ - return true; - } - return false; + return *this > other || *this == other; } bool operator<=(Date other) const { - if (this->operator<(other) || this->operator==(other)){ - return true; - } - return false; + return *this < other || *this == other; } }; @@ -81,70 +46,26 @@ struct StudentInfo { id(id), mark(mark), score(score), course(course), birth_date(birth_date){} bool operator==(StudentInfo other) const { - if (this->mark == other.mark && this->score == other.score){ - return true; - } - return false; + return std::tie(mark, score) == std::tie(other.mark, other.score); } bool operator!=(StudentInfo other) const { - if (!this->operator==(other)){ - return true; - } - return false; + return !(*this == other); } bool operator<(StudentInfo other) const { - if (this->mark > other.mark){ - return true; - } else if (this->mark == other.mark){ - if (this->score < other.score){ - return true; - } else if (this->score == other.score){ - if (this->course > other.course){ - return true; - } else if (this->course == other.course){ - if (this->birth_date < other.birth_date){ - return true; - } - } - } - } - - return false; + return std::tie(other.mark, score, other.course, birth_date) < std::tie(mark, other.score, course, other.birth_date); } bool operator>(StudentInfo other) const { - if (this->mark < other.mark){ - return true; - } else if (this->mark == other.mark){ - if (this->score > other.score){ - return true; - } else if (this->score == other.score){ - if (this->course < other.course){ - return true; - } else if (this->course == other.course){ - if (this->birth_date > other.birth_date){ - return true; - } - } - } - } - - return false; + return std::tie(other.mark, score, other.course, birth_date) > std::tie(mark, other.score, course, other.birth_date); } bool operator>=(StudentInfo other) const { - if (this->operator>(other) || this->operator==(other)){ - return true; - } - return false; + return *this > other || *this == other; } bool operator<=(StudentInfo other) const { - if (this->operator<(other) || this->operator==(other)){ - return true; - } - return false; + return *this < other || *this == other; } }; \ No newline at end of file From 700b226e1d4d8c86522fc09c30457a19f0c7ef33 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 20:10:54 +0500 Subject: [PATCH 25/30] Refactor PrintCheckFlags function to simplify output logic - Removed the use of a vector for storing flag names, directly printing flags in a comma-separated format. - Enhanced readability by utilizing a boolean flag to manage comma placement, improving the overall output formatting. --- .../tasks/enum_operators/enum_operators.cpp | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/03_week/tasks/enum_operators/enum_operators.cpp b/03_week/tasks/enum_operators/enum_operators.cpp index 75f0e34b..646b6f3a 100644 --- a/03_week/tasks/enum_operators/enum_operators.cpp +++ b/03_week/tasks/enum_operators/enum_operators.cpp @@ -1,8 +1,5 @@ #include -#include #include -#include -#include enum class CheckFlags : uint8_t { NONE = 0, @@ -60,7 +57,6 @@ CheckFlags operator~(CheckFlags a) { std::ostream& operator<<(std::ostream& os, CheckFlags flags) { uint8_t flags_val = static_cast(flags); - uint8_t valid_flags = flags_val & static_cast(CheckFlags::ALL); if (valid_flags == 0) { @@ -68,32 +64,36 @@ std::ostream& operator<<(std::ostream& os, CheckFlags flags) { return os; } - std::vector flag_names; - + bool first = true; if ((valid_flags & static_cast(CheckFlags::TIME)) != 0) { - flag_names.push_back("TIME"); + if (!first) os << ", "; + os << "TIME"; + first = false; } if ((valid_flags & static_cast(CheckFlags::DATE)) != 0) { - flag_names.push_back("DATE"); + if (!first) os << ", "; + os << "DATE"; + first = false; } if ((valid_flags & static_cast(CheckFlags::USER)) != 0) { - flag_names.push_back("USER"); + if (!first) os << ", "; + os << "USER"; + first = false; } if ((valid_flags & static_cast(CheckFlags::CERT)) != 0) { - flag_names.push_back("CERT"); + if (!first) os << ", "; + os << "CERT"; + first = false; } if ((valid_flags & static_cast(CheckFlags::KEYS)) != 0) { - flag_names.push_back("KEYS"); + if (!first) os << ", "; + os << "KEYS"; + first = false; } if ((valid_flags & static_cast(CheckFlags::DEST)) != 0) { - flag_names.push_back("DEST"); - } - - for (size_t i = 0; i < flag_names.size(); ++i) { - os << flag_names[i]; - if (i < flag_names.size() - 1) { - os << ", "; - } + if (!first) os << ", "; + os << "DEST"; + first = false; } return os; From fed1b0f7478a985e8c5420e40a51006a153baa23 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 21:08:56 +0500 Subject: [PATCH 26/30] Enhance Unique function by reserving space for result vector to improve performance - Added a reserve call for the result vector to optimize memory allocation based on the input vector size, enhancing efficiency during element insertion. --- 03_week/tasks/unique/unique.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/03_week/tasks/unique/unique.cpp b/03_week/tasks/unique/unique.cpp index 7514a09c..82cd2394 100644 --- a/03_week/tasks/unique/unique.cpp +++ b/03_week/tasks/unique/unique.cpp @@ -6,6 +6,7 @@ std::vector Unique(const std::vector& input_vector) { } std::vector result; + result.reserve(input_vector.size()); result.push_back(input_vector[0]); for (size_t i = 1; i < input_vector.size(); ++i) { if (input_vector[i] != input_vector[i-1]) { From 5baf83ca63de0548146b6c136ca4f159229c56ab Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 21:31:59 +0500 Subject: [PATCH 27/30] Refactor Stack class methods for improved efficiency and readability - Simplified the Swap method by utilizing std::swap for stack elements, enhancing performance. - Streamlined the equality operator implementation by directly comparing stack contents, improving code clarity. --- 04_week/tasks/stack/stack.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/04_week/tasks/stack/stack.cpp b/04_week/tasks/stack/stack.cpp index 5f0b109a..59cc49a9 100644 --- a/04_week/tasks/stack/stack.cpp +++ b/04_week/tasks/stack/stack.cpp @@ -59,19 +59,11 @@ void Stack::Clear(){ } void Stack::Swap(Stack& other){ - Stack tmp = std::move(*this); - *this = std::move(other); - other = std::move(tmp); + std::swap((*this).stack, other.stack); } bool Stack::operator==(const Stack& other) const { - if (Size() != other.Size()){ return false; } - for (int i = 0; i < Size(); ++i) { - if (stack[i] != other.stack[i]) { - return false; - } - } - return true; + return ((*this).stack == other.stack); } bool Stack::operator!=(const Stack& other) const { From ddc54ba4688146e4d2fae4a95b80a28852d057cb Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 21:50:14 +0500 Subject: [PATCH 28/30] Refactor RingBuffer methods for improved efficiency and clarity - Simplified Push and TryPush methods by consolidating logic and reducing conditional checks. - Enhanced Pop and TryPop methods for better readability and streamlined operations. - Improved handling of buffer indices using modular arithmetic to simplify wrap-around logic. --- 04_week/tasks/ring_buffer/ring_buffer.cpp | 89 ++++++----------------- 1 file changed, 21 insertions(+), 68 deletions(-) diff --git a/04_week/tasks/ring_buffer/ring_buffer.cpp b/04_week/tasks/ring_buffer/ring_buffer.cpp index d8559ecd..0cf065c6 100644 --- a/04_week/tasks/ring_buffer/ring_buffer.cpp +++ b/04_week/tasks/ring_buffer/ring_buffer.cpp @@ -77,82 +77,35 @@ RingBuffer::RingBuffer(const std::initializer_list& list) : RingBuffer(list } } -void RingBuffer::Push(const int& value){ - if (size == 0){ - end = 0; - buffer[end] = value; - ++size; - } else { - if (end < capacity-1){ - if (size < capacity){ ++size; } - ++end; - buffer[end] = value; - } else if (end == capacity-1 && size != capacity){ - ++size; - end = 0; - buffer[end] = value; - } else if (end == capacity-1 && size == capacity){ - end = 0; - buffer[end] = value; - } - - if (end == begin && begin != capacity-1){ - ++begin; - } else if (end == begin && begin == capacity-1){ - begin = 0; - } - } -} - -bool RingBuffer::TryPush(const int& value){ - if (size == 0){ - end = 0; - buffer[end] = value; - ++size; - } else { - if (end < capacity-1 && end+1 != begin){ - ++size; - ++end; - buffer[end] = value; - } else if (end == capacity-1 && size != capacity){ // && begin != 0 - ++size; - end = 0; - buffer[end] = value; - } else { - return false; - } +bool RingBuffer::TryPush(const int& value) { + if (Full()) return false; + + if (!Empty()) { + end = (end + 1) % capacity; } - + buffer[end] = value; + ++size; return true; } -void RingBuffer::Pop(){ - if (size > 0){ - if (begin+1 <= end){ - ++begin; - } else { - begin = 0; - } - --size; - +void RingBuffer::Push(const int& value) { + if (Full()) { + Pop(); } - + TryPush(value); } -bool RingBuffer::TryPop(int& value){ - if (size > 0){ - value = buffer[begin]; - - if (begin+1 <= end){ - ++begin; - } else { - begin = 0; - } - --size; - return true; - } +bool RingBuffer::TryPop(int& value) { + if (Empty()) return false; + value = buffer[begin]; + begin = (begin + 1) % capacity; + --size; + return true; +} - return false; +void RingBuffer::Pop() { + int trash; + TryPop(trash); } int& RingBuffer::operator[](const size_t& idx) { From f2abe8b8675a69337cdd6b1f623045bfa3fce594 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 22:04:21 +0500 Subject: [PATCH 29/30] Refactor Queue class equality operator for improved efficiency and clarity - Removed redundant array comparison methods and replaced them with a direct comparison of queue elements. - Implemented a helper function to access elements in a virtual vector, enhancing performance and memory usage. - Streamlined the equality operator to compare sizes and elements without additional memory allocation. --- 04_week/tasks/queue/queue.cpp | 47 +++++++++++++++-------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index a0e1eeba..2ec64f22 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -7,9 +7,6 @@ class Queue { std::vector input; std::vector output; - bool eqArrays(const std::vector& a, const std::vector& b) const; - std::vector unionArrays(const std::vector& input, const std::vector& output) const; - public: Queue(){}; Queue(std::vector vector); @@ -134,35 +131,31 @@ void Queue::Swap(Queue& other){ other.output = std::move(tmpOutput); } -bool Queue::eqArrays(const std::vector& a, const std::vector& b) const{ - if (a.size() != b.size()){ return false; } - if (a.empty() && b.empty()){ return true; } +bool Queue::operator==(const Queue& other) const { + // Быстрая проверка на размер + if ((input.size() + output.size()) != (other.input.size() + other.output.size())) { + return false; + } - for (size_t i = 0; i < a.size(); ++i) { - if (a[i] != b[i]) { + // Вспомогательная функция для получения элемента по индексу в "виртуальном" векторе + auto getElement = [](const Queue& q, size_t index) { + // Сначала идут элементы из output в обратном порядке + if (index < q.output.size()) { + return q.output[q.output.size() - 1 - index]; + } + // Затем элементы из input в прямом порядке + return q.input[index - q.output.size()]; + }; + + // Поэлементное сравнение без выделения памяти + size_t totalSize = input.size() + output.size(); + for (size_t i = 0; i < totalSize; ++i) { + if (getElement(*this, i) != getElement(other, i)) { return false; } } - return true; -} - -std::vector Queue::unionArrays(const std::vector& input, const std::vector& output) const { - std::vector unionArray; - unionArray.reserve(input.size() + output.size()); - - for (auto it = output.rbegin(); it != output.rend(); ++it) { - unionArray.push_back(*it); - } - - for (size_t i=0; i < input.size(); ++i){ - unionArray.push_back(input[i]); - } - return unionArray; -} - -bool Queue::operator==(const Queue& other) const { - return eqArrays(unionArrays(input, output), unionArrays(other.input, other.output)); + return true; } bool Queue::operator!=(const Queue& other) const { From 890adb7a7a4070241aed411af5bf3f0118192112 Mon Sep 17 00:00:00 2001 From: ilyamikhailov16 Date: Sun, 8 Feb 2026 22:08:40 +0500 Subject: [PATCH 30/30] Refactor Queue class Swap method for improved efficiency - Simplified the Swap method by replacing manual vector moves with std::swap, enhancing performance and code clarity. --- 04_week/tasks/queue/queue.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index 2ec64f22..4a23c1b4 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -123,12 +123,8 @@ void Queue::Clear(){ } void Queue::Swap(Queue& other){ - std::vector tmpInput = std::move(input); - std::vector tmpOutput = std::move(output); - input = std::move(other.input); - output = std::move(other.output); - other.input = std::move(tmpInput); - other.output = std::move(tmpOutput); + std::swap(input, other.input); + std::swap(output, other.output); } bool Queue::operator==(const Queue& other) const {