From 8ce78950a8acee6af90500dbd354e37a1c7f9fef Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 27 Nov 2025 00:49:08 +0500 Subject: [PATCH 01/37] add (solution): add addition task --- 01_week/tasks/addition/addition.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 92872802..691087ad 100644 --- a/01_week/tasks/addition/addition.cpp +++ b/01_week/tasks/addition/addition.cpp @@ -3,5 +3,5 @@ int64_t Addition(int a, int b) { - throw std::runtime_error{"Not implemented"}; + return (int64_t)a + (int64_t)b; } \ No newline at end of file From 94026751931503681d648a052a38c53b2b626713 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 27 Nov 2025 00:50:19 +0500 Subject: [PATCH 02/37] add (solution): add char_changer task --- 01_week/tasks/char_changer/char_changer.cpp | 43 ++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/01_week/tasks/char_changer/char_changer.cpp b/01_week/tasks/char_changer/char_changer.cpp index 3a7344d9..5be36eb7 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -1,7 +1,48 @@ #include #include +#include size_t CharChanger(char array[], size_t size, char delimiter = ' ') { - throw std::runtime_error{"Not implemented"}; + size_t cntRepeat = 0; + char* res = (char*)calloc(size, sizeof(char)); + size_t resInd = 0; + + auto parse = [&](size_t i, char cymbol) { + if (array[i] == array[i-1]) { + if (cntRepeat != 1) + --resInd; + ++cntRepeat; + res[resInd] = (cntRepeat > 9) ? '0' : '0' + cntRepeat; + } + else { + res[resInd] = cymbol; + cntRepeat = 1; + } + ++resInd; + }; + + for (size_t i = 0; i < size; ++i) { + if (48 <= array[i] && array[i] <= 57) + parse(i, '*'); + else if (97 <= array[i] && array[i] <= 122) + parse(i, array[i] - ' '); + else if (65 <= array[i] && array[i] <= 90) + parse(i, array[i]); + else if (array[i] == 32) { + res[resInd] = delimiter; + ++resInd; + while (array[i] == ' ') + ++i; + --i; + } + else if (array[i] != '\0') + parse(i, '_'); + } + + for (size_t i = 0; i < size; ++i) + array[i] = res[i]; + + free(res); + return resInd; } From 80dc57e5ab36e22b870a0c267fe64a307ac3e25b Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 27 Nov 2025 00:51:02 +0500 Subject: [PATCH 03/37] add (solution): add check_flags task --- 01_week/tasks/check_flags/check_flags.cpp | 33 ++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/01_week/tasks/check_flags/check_flags.cpp b/01_week/tasks/check_flags/check_flags.cpp index 75e7c652..4cdc735a 100644 --- a/01_week/tasks/check_flags/check_flags.cpp +++ b/01_week/tasks/check_flags/check_flags.cpp @@ -1,5 +1,6 @@ #include #include +#include enum class CheckFlags : uint8_t { @@ -14,5 +15,35 @@ 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::NONE)) { + std::cout << "[]"; + return; + } + else if (value >= 1 << 6) { + std::cout << ""; + return; + } + + std::cout << "["; + + bool first = true; + auto try_print = [&](CheckFlags f, const char* name) { + if (value & static_cast(f)) { + if (!first) + std::cout << ","; + std::cout << name; + first = false; + } + }; + + try_print(CheckFlags::TIME, "TIME"); + try_print(CheckFlags::DATE, "DATE"); + try_print(CheckFlags::USER, "USER"); + try_print(CheckFlags::CERT, "CERT"); + try_print(CheckFlags::KEYS, "KEYS"); + try_print(CheckFlags::DEST, "DEST"); + + std::cout << "]"; } From 3f647a2cc4c3e0f438c3bd1166f964740c2e872a Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 27 Nov 2025 00:51:44 +0500 Subject: [PATCH 04/37] add (solution): add length_lit task --- 01_week/tasks/length_lit/length_lit.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/01_week/tasks/length_lit/length_lit.cpp b/01_week/tasks/length_lit/length_lit.cpp index e69de29b..9e4adb3b 100644 --- a/01_week/tasks/length_lit/length_lit.cpp +++ b/01_week/tasks/length_lit/length_lit.cpp @@ -0,0 +1,17 @@ +#pragma once + +constexpr long double operator"" _ft_to_m(long double x) {return x * 0.3048L;} +constexpr long double operator"" _ft_to_cm(long double x) {return x * 30.48L;} +constexpr long double operator"" _ft_to_in(long double x) {return x * 12.0L;} + +constexpr long double operator"" _in_to_m(long double x) {return x * 0.0254L;} +constexpr long double operator"" _in_to_cm(long double x) {return x * 2.54L;} +constexpr long double operator"" _in_to_ft(long double x) {return x / 12.0L;} + +constexpr long double operator"" _m_to_ft(long double x) {return x * 3.280839895L;} +constexpr long double operator"" _m_to_in(long double x) {return x * 39.37007874L;} +constexpr long double operator"" _m_to_cm(long double x) {return x * 100.0L;} + +constexpr long double operator"" _cm_to_m(long double x) {return x / 100.0L;} +constexpr long double operator"" _cm_to_ft(long double x) {return x * 0.0328084L;} +constexpr long double operator"" _cm_to_in(long double x) {return x * 0.3937007874L;} \ No newline at end of file From b50d9bb2422efd6c4613aea862c30f1f5cbb99e7 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 27 Nov 2025 00:52:08 +0500 Subject: [PATCH 05/37] add (solution): add print_bits task --- 01_week/tasks/print_bits/print_bits.cpp | 28 ++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/01_week/tasks/print_bits/print_bits.cpp b/01_week/tasks/print_bits/print_bits.cpp index a48a43c1..483d683c 100644 --- a/01_week/tasks/print_bits/print_bits.cpp +++ b/01_week/tasks/print_bits/print_bits.cpp @@ -1,7 +1,33 @@ #include #include +#include void PrintBits(long long value, size_t bytes) { - throw std::runtime_error{"Not implemented"}; + size_t bits = bytes*8; + bool reverse = false; + bool* res = (bool*)calloc(bits, sizeof(bool)); + + if (value < 0) { + reverse = true; + value = -value - 1; + } + + for (size_t i = bits; i > 0 && value != 0; --i) { + res[i-1] = value % 2; + value /= 2; + } + + if (reverse) + for (size_t i = 0; i < bits; ++i) + res[i] ^= 1; + + std::cout << "0b"; + for (size_t i = 0; i < bits; ++i) { + if (i % 4 == 0 && i != 0) + std::cout << '\''; + std::cout << res[i]; + } + std::cout << '\n'; + free(res); } From ee50f0981889270d92d6b37578e6a248835fd8f9 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 27 Nov 2025 00:52:36 +0500 Subject: [PATCH 06/37] add (solution): add quadratic task --- 01_week/tasks/quadratic/quadratic.cpp | 45 ++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/01_week/tasks/quadratic/quadratic.cpp b/01_week/tasks/quadratic/quadratic.cpp index abf7d632..de4c4e88 100644 --- a/01_week/tasks/quadratic/quadratic.cpp +++ b/01_week/tasks/quadratic/quadratic.cpp @@ -1,6 +1,49 @@ +#include #include +#include +#include void SolveQuadratic(int a, int b, int c) { - throw std::runtime_error{"Not implemented"}; + if (a == 0 && b == 0 && c == 0) + std::cout << "infinite solutions"; + else if (a == 0 && b == 0 && c != 0) + std::cout << "no solutions"; + else if (a == 0 && b != 0 && c == 0) + std::cout << "0"; + else if (a == 0 && b != 0 && c != 0) + std::cout << std::setprecision(6) << (long double)-c / (long double)b; + else if (a != 0 && b == 0 && c == 0) + std::cout << "0"; + else if (a != 0 && b == 0 && c != 0) { + if (c > 0) + std::cout << "no solutions"; + else { + long double x = sqrt((long double)-c / (long double)a); + std::cout << std::setprecision(6) << -x << ' ' << x; + } + } + else if (a != 0 && b != 0 && c == 0) { + long double x = (long double)-b / (long double)a; + if (x < 0) + std::cout << std::setprecision(6) << x << " 0"; + else + std::cout << std::setprecision(6) << "0 " << x; + } + else { + long long d = b*b - 4*a*c; + if (d < 0) + std::cout << "no solutions"; + else if (d == 0) + std::cout << std::setprecision(6) << (long double)-b / (2.0L*a); + else { + long double sqrtd = sqrt(d); + long double x1 = ((long double)-b + sqrtd) / (2.0L*a); + long double x2 = ((long double)-b - sqrtd) / (2.0L*a); + if (x1 < x2) + std::cout << std::setprecision(6) << x1 << ' ' << x2; + else + std::cout << std::setprecision(6) << x2 << ' ' << x1; + } + } } \ No newline at end of file From 573b181bcf39adebd863b48b7ea773dd520a80cc Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 27 Nov 2025 00:52:54 +0500 Subject: [PATCH 07/37] add (solution): add rms task --- 01_week/tasks/rms/rms.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/01_week/tasks/rms/rms.cpp b/01_week/tasks/rms/rms.cpp index 6882f0a9..f2eb7dc2 100644 --- a/01_week/tasks/rms/rms.cpp +++ b/01_week/tasks/rms/rms.cpp @@ -1,7 +1,13 @@ -#include #include +#include double CalculateRMS(double values[], size_t size) { - throw std::runtime_error{"Not implemented"}; + if (size == 0 || values == nullptr) + return 0.0; + long double sum = 0.0L; + for (size_t i = 0; i < size; ++i) + sum += values[i] * values[i]; + sum /= size; + return sqrt(sum); } \ No newline at end of file From 5aa6519f885b57ce03c0e97d306ba37c49b86f14 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 4 Dec 2025 19:52:58 +0500 Subject: [PATCH 08/37] add (solution): add addition task --- 01_week/tasks/addition/addition.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 691087ad..a91454b8 100644 --- a/01_week/tasks/addition/addition.cpp +++ b/01_week/tasks/addition/addition.cpp @@ -3,5 +3,7 @@ int64_t Addition(int a, int b) { - return (int64_t)a + (int64_t)b; + int64_t first = static_cast(a); + int64_t second = static_cast(b); + return first + second; } \ No newline at end of file From bdadfb5b7dd629a6aadcdd090b50d4b2f8c43281 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Thu, 4 Dec 2025 19:53:34 +0500 Subject: [PATCH 09/37] add (solution): add quadratic task --- 01_week/tasks/quadratic/quadratic.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/01_week/tasks/quadratic/quadratic.cpp b/01_week/tasks/quadratic/quadratic.cpp index de4c4e88..15cf9ec2 100644 --- a/01_week/tasks/quadratic/quadratic.cpp +++ b/01_week/tasks/quadratic/quadratic.cpp @@ -12,34 +12,34 @@ void SolveQuadratic(int a, int b, int c) { else if (a == 0 && b != 0 && c == 0) std::cout << "0"; else if (a == 0 && b != 0 && c != 0) - std::cout << std::setprecision(6) << (long double)-c / (long double)b; + std::cout << std::setprecision(6) << static_cast(-c) / static_cast(b); else if (a != 0 && b == 0 && c == 0) std::cout << "0"; else if (a != 0 && b == 0 && c != 0) { if (c > 0) std::cout << "no solutions"; else { - long double x = sqrt((long double)-c / (long double)a); + long double x = sqrt(static_cast(-c) / static_cast(a)); std::cout << std::setprecision(6) << -x << ' ' << x; } } else if (a != 0 && b != 0 && c == 0) { - long double x = (long double)-b / (long double)a; + long double x = static_cast(-b) / static_cast(a); if (x < 0) std::cout << std::setprecision(6) << x << " 0"; else std::cout << std::setprecision(6) << "0 " << x; } else { - long long d = b*b - 4*a*c; + long long d = (long long)b*b - 4LL*a*c; if (d < 0) std::cout << "no solutions"; else if (d == 0) - std::cout << std::setprecision(6) << (long double)-b / (2.0L*a); + std::cout << std::setprecision(6) << static_cast(-b) / (2.0L*a); else { long double sqrtd = sqrt(d); - long double x1 = ((long double)-b + sqrtd) / (2.0L*a); - long double x2 = ((long double)-b - sqrtd) / (2.0L*a); + long double x1 = (static_cast(-b) + sqrtd) / (2.0L*a); + long double x2 = (static_cast(-b) - sqrtd) / (2.0L*a); if (x1 < x2) std::cout << std::setprecision(6) << x1 << ' ' << x2; else From 086d65f96d3e16c362b93b5275f986dd4f23534d Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 01:21:59 +0500 Subject: [PATCH 10/37] add (solution): add addition task --- 01_week/tasks/addition/addition.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 8a2d05c5..aef47d9c 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) { return static_cast(a) + b; } \ No newline at end of file From 541a260fb599b04908b9a117730a19d1938356aa Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 01:23:00 +0500 Subject: [PATCH 11/37] add (solution): add char_changer task --- 01_week/tasks/char_changer/char_changer.cpp | 13 ++++++------- 1 file 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 5be36eb7..11be8cc1 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -5,11 +5,11 @@ size_t CharChanger(char array[], size_t size, char delimiter = ' ') { size_t cntRepeat = 0; - char* res = (char*)calloc(size, sizeof(char)); + char res[size] = {0}; size_t resInd = 0; auto parse = [&](size_t i, char cymbol) { - if (array[i] == array[i-1]) { + if (i > 0 && array[i] == array[i-1]) { if (cntRepeat != 1) --resInd; ++cntRepeat; @@ -23,13 +23,13 @@ size_t CharChanger(char array[], size_t size, char delimiter = ' ') { }; for (size_t i = 0; i < size; ++i) { - if (48 <= array[i] && array[i] <= 57) + if ('0' <= array[i] && array[i] <= '9') parse(i, '*'); - else if (97 <= array[i] && array[i] <= 122) + else if ('a' <= array[i] && array[i] <= 'z') parse(i, array[i] - ' '); - else if (65 <= array[i] && array[i] <= 90) + else if ('A' <= array[i] && array[i] <= 'Z') parse(i, array[i]); - else if (array[i] == 32) { + else if (array[i] == ' ') { res[resInd] = delimiter; ++resInd; while (array[i] == ' ') @@ -43,6 +43,5 @@ size_t CharChanger(char array[], size_t size, char delimiter = ' ') { for (size_t i = 0; i < size; ++i) array[i] = res[i]; - free(res); return resInd; } From 65add832e345b4d342ffe52535891d0abce2c66f Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 01:23:43 +0500 Subject: [PATCH 12/37] add (solution): add print_bits task --- 01_week/tasks/print_bits/print_bits.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/01_week/tasks/print_bits/print_bits.cpp b/01_week/tasks/print_bits/print_bits.cpp index 483d683c..ffcf5076 100644 --- a/01_week/tasks/print_bits/print_bits.cpp +++ b/01_week/tasks/print_bits/print_bits.cpp @@ -6,7 +6,7 @@ void PrintBits(long long value, size_t bytes) { size_t bits = bytes*8; bool reverse = false; - bool* res = (bool*)calloc(bits, sizeof(bool)); + bool res[bits] = {0}; if (value < 0) { reverse = true; @@ -29,5 +29,4 @@ void PrintBits(long long value, size_t bytes) { std::cout << res[i]; } std::cout << '\n'; - free(res); } From e9531fd4c12a667d88e99136902ab761ccf6f681 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 01:24:12 +0500 Subject: [PATCH 13/37] add (solution): add quadratic task --- 01_week/tasks/quadratic/quadratic.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/01_week/tasks/quadratic/quadratic.cpp b/01_week/tasks/quadratic/quadratic.cpp index 15cf9ec2..0159ecbf 100644 --- a/01_week/tasks/quadratic/quadratic.cpp +++ b/01_week/tasks/quadratic/quadratic.cpp @@ -12,26 +12,26 @@ void SolveQuadratic(int a, int b, int c) { else if (a == 0 && b != 0 && c == 0) std::cout << "0"; else if (a == 0 && b != 0 && c != 0) - std::cout << std::setprecision(6) << static_cast(-c) / static_cast(b); + std::cout << std::setprecision(6) << static_cast(-c) / b; else if (a != 0 && b == 0 && c == 0) std::cout << "0"; else if (a != 0 && b == 0 && c != 0) { if (c > 0) std::cout << "no solutions"; else { - long double x = sqrt(static_cast(-c) / static_cast(a)); + long double x = sqrt(static_cast(-c) / a); std::cout << std::setprecision(6) << -x << ' ' << x; } } else if (a != 0 && b != 0 && c == 0) { - long double x = static_cast(-b) / static_cast(a); + long double x = static_cast(-b) / a; if (x < 0) std::cout << std::setprecision(6) << x << " 0"; else std::cout << std::setprecision(6) << "0 " << x; } else { - long long d = (long long)b*b - 4LL*a*c; + long long d = static_cast(b)*b - 4LL*a*c; if (d < 0) std::cout << "no solutions"; else if (d == 0) From 412fe66fb04f0c51f2b660dd54e5911b2a820373 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 01:24:47 +0500 Subject: [PATCH 14/37] add (solution): add func_array task --- 02_week/tasks/func_array/func_array.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/02_week/tasks/func_array/func_array.cpp b/02_week/tasks/func_array/func_array.cpp index b327e68d..73276d1e 100644 --- a/02_week/tasks/func_array/func_array.cpp +++ b/02_week/tasks/func_array/func_array.cpp @@ -1,6 +1,11 @@ +#include #include -double ApplyOperations(double a, double b /* other arguments */) { - throw std::runtime_error{"Not implemented"}; +double ApplyOperations(double a, double b, double (*funcs[])(double a, double b), size_t size) { + double res = 0.0; + for (size_t i = 0; i < size; ++i) + if (funcs[i]) + res += funcs[i](a, b); + return res; } \ No newline at end of file From b52a152c0b54e2a1d07d26464d909852e63e1a64 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 01:25:12 +0500 Subject: [PATCH 15/37] add (solution): add last_of_us task --- 02_week/tasks/last_of_us/last_of_us.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 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..71f5aae1 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,13 @@ #include -/* 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, bool (*predicate)(int a)) { + if (!(begin && end && predicate && begin < end)) + return end; + const int* first = begin; + const int* last = end-1; + for (; last >= first; --last) + if (predicate(*last)) + return last; + return end; } \ No newline at end of file From 5874a3daae356b7d243f1a45b431499fd88fa6e8 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 01:25:41 +0500 Subject: [PATCH 16/37] add (solution): add little_big task --- 02_week/tasks/little_big/little_big.cpp | 40 ++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/02_week/tasks/little_big/little_big.cpp b/02_week/tasks/little_big/little_big.cpp index abe24379..ab441ba1 100644 --- a/02_week/tasks/little_big/little_big.cpp +++ b/02_week/tasks/little_big/little_big.cpp @@ -1,10 +1,42 @@ #include +#include +#include +#include +void PrintByte(unsigned char byte) { + std::cout << std::setw(2) << static_cast(byte); +} + +void PrintMemory(int value, bool reverse = false) { + const size_t size = sizeof(int); + unsigned char bytes[size]; + + std::memcpy(bytes, &value, size); + std::cout << "0x" << std::hex << std::uppercase << std::setfill('0'); -void PrintMemory(int /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; + if (reverse) + for (size_t i = size; i > 0; --i) + PrintByte(bytes[i-1]); + else + for (size_t i = 0; i < size; ++i) + PrintByte(bytes[i]); + + std::cout << std::endl; } -void PrintMemory(double /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void PrintMemory(double value, bool reverse = false) { + const size_t size = sizeof(double); + unsigned char bytes[size]; + + std::memcpy(bytes, &value, size); + std::cout << "0x" << std::hex << std::uppercase << std::setfill('0'); + + if (reverse) + for (size_t i = size; i > 0; --i) + PrintByte(bytes[i-1]); + else + for (size_t i = 0; i < size; ++i) + PrintByte(bytes[i]); + + std::cout << std::endl; } \ No newline at end of file From 856803dbdc0187752ba691ea81d499080215e64e Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 01:26:11 +0500 Subject: [PATCH 17/37] add (solution): add swap_ptr task --- 02_week/tasks/swap_ptr/swap_ptr.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/02_week/tasks/swap_ptr/swap_ptr.cpp b/02_week/tasks/swap_ptr/swap_ptr.cpp index 93db625d..5515ea45 100644 --- a/02_week/tasks/swap_ptr/swap_ptr.cpp +++ b/02_week/tasks/swap_ptr/swap_ptr.cpp @@ -1,6 +1,20 @@ #include -void SwapPtr(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void SwapPtr(int*& ptr1, int*& ptr2) { + int* temp = ptr1; + ptr1 = ptr2; + ptr2 = temp; +} + +void SwapPtr(const int*& ptr1, const int*& ptr2) { + const int* temp = ptr1; + ptr1 = ptr2; + ptr2 = temp; +} + +void SwapPtr(int**& ptr1, int**& ptr2) { + int** temp = ptr1; + ptr1 = ptr2; + ptr2 = temp; } \ No newline at end of file From a7eefbec3842f84d57c02ac9218a8dc2447fd837 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 22:09:35 +0500 Subject: [PATCH 18/37] add (solution): add pretty_array task --- 02_week/tasks/pretty_array/pretty_array.cpp | 33 +++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/02_week/tasks/pretty_array/pretty_array.cpp b/02_week/tasks/pretty_array/pretty_array.cpp index 48eab341..18ae55bb 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, size_t limit = 0) { + const bool reverse = (begin > end); + const size_t len = std::abs(end - begin); -void PrintArray(/* write arguments here */) { - throw std::runtime_error{"Not implemented"}; + if (len == 0) { + std::cout << "[]\n"; + return; + } + + const int* cur = begin; + size_t cnt = 0; + + std::cout << "["; + + for (size_t i = 0; i < len; ++i) { + std::cout << *cur; + if (i != len - 1) { + if ((limit > 0) && (cnt == limit - 1)) { + std::cout << ", ...\n "; + cnt = 0; + } + else { + std::cout << ", "; + ++cnt; + } + } + + if (i != len - 1) + cur = reverse ? cur - 1 : cur + 1; + } + std::cout << "]\n"; } \ No newline at end of file From 36240fb4e4879e7cab633d100c6965114d06adff Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 9 Dec 2025 22:10:11 +0500 Subject: [PATCH 19/37] add (solution): add longest task --- 02_week/tasks/longest/longest.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/02_week/tasks/longest/longest.cpp b/02_week/tasks/longest/longest.cpp index 04b3c354..1fd3f4d6 100644 --- a/02_week/tasks/longest/longest.cpp +++ b/02_week/tasks/longest/longest.cpp @@ -1,6 +1,30 @@ #include -/* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { - throw std::runtime_error{"Not implemented"}; +char* FindLongestSubsequence(char* begin, char* end, size_t& count) { + if (!begin || !end || end - begin == 0) { + count = 0; + return nullptr; + } + + size_t curLen = 0; + char* startPtr = nullptr; + char curChar = *begin; + for (; begin <= end; ++begin) { + if (curChar != *begin) { + if (curLen > count) { + count = curLen; + startPtr = begin - curLen; + } + curLen = 1; + curChar = *begin; + } + else + ++curLen; + } + return startPtr; } + +const char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) { + return FindLongestSubsequence(const_cast(begin), const_cast(end), count); +} \ No newline at end of file From 10707a74b559e29298e071dc00f8bc8cc7b0d335 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 01:20:47 +0500 Subject: [PATCH 20/37] add (solution): add easy_compare task --- 03_week/tasks/easy_compare/easy_compare.cpp | 60 ++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/03_week/tasks/easy_compare/easy_compare.cpp b/03_week/tasks/easy_compare/easy_compare.cpp index dd5cb7f6..580e3633 100644 --- a/03_week/tasks/easy_compare/easy_compare.cpp +++ b/03_week/tasks/easy_compare/easy_compare.cpp @@ -5,12 +5,70 @@ struct Date { unsigned year; unsigned month; unsigned day; + + Date (unsigned y = 0u, unsigned m = 0u, unsigned d = 0u) : year(y), month(m), day(d) {} }; +bool operator==(const Date& leftDate, const Date& rightDate) { + return std::tie(leftDate.year, leftDate.month, leftDate.day) == std::tie(rightDate.year, rightDate.month, rightDate.day); +} + +bool operator!=(const Date& leftDate, const Date& rightDate) { + return !(leftDate == rightDate); +} + +bool operator<(const Date& leftDate, const Date& rightDate) { + return std::tie(leftDate.year, leftDate.month, leftDate.day) < std::tie(rightDate.year, rightDate.month, rightDate.day); +} + +bool operator>(const Date& leftDate, const Date& rightDate) { + return rightDate < leftDate; +} + +bool operator<=(const Date& leftDate, const Date& rightDate) { + return leftDate < rightDate || leftDate == rightDate; +} + +bool operator>=(const Date& leftDate, const Date& rightDate) { + return leftDate > rightDate || leftDate == rightDate; +} + struct StudentInfo { size_t id; char mark; int score; unsigned course; Date birth_date; -}; \ No newline at end of file +}; + +bool operator==(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return std::tie(leftStudent.mark, leftStudent.score) == std::tie(rightStudent.mark, rightStudent.score); +} + +bool operator!=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return !(leftStudent == rightStudent); +} + +bool operator<(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + if (leftStudent.mark != rightStudent.mark) + return leftStudent.mark > rightStudent.mark; + if (leftStudent.score != rightStudent.score) + return leftStudent.score < rightStudent.score; + if (leftStudent.course != rightStudent.course) + return leftStudent.course > rightStudent.course; + if (leftStudent.birth_date != rightStudent.birth_date) + return leftStudent.birth_date < rightStudent.birth_date; + return false; +} + +bool operator>(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return rightStudent < leftStudent; +} + +bool operator<=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return leftStudent < rightStudent || leftStudent == rightStudent; +} + +bool operator>=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return leftStudent > rightStudent || leftStudent == rightStudent; +} \ No newline at end of file From 70166263b0b48551b9b61ac0215d951534345f53 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 01:21:16 +0500 Subject: [PATCH 21/37] add (solution): add enum_operators task --- .../tasks/enum_operators/enum_operators.cpp | 57 +++++++++++++++---- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/03_week/tasks/enum_operators/enum_operators.cpp b/03_week/tasks/enum_operators/enum_operators.cpp index a539be38..bc3db3fd 100644 --- a/03_week/tasks/enum_operators/enum_operators.cpp +++ b/03_week/tasks/enum_operators/enum_operators.cpp @@ -1,5 +1,7 @@ +#include #include -#include +#include +#include enum class CheckFlags : uint8_t { NONE = 0, @@ -12,22 +14,55 @@ enum class CheckFlags : uint8_t { ALL = TIME | DATE | USER | CERT | KEYS | DEST }; -/* return_type */ operator|(/* args */) { - throw std::runtime_error{"Not implemented"}; +constexpr uint8_t flagAll = static_cast(CheckFlags::ALL); + +constexpr uint8_t toUi(CheckFlags flag) { + return static_cast(flag) & flagAll; +} + +constexpr CheckFlags operator|(CheckFlags lFlag, CheckFlags rFlag) { + return static_cast(toUi(lFlag) | toUi(rFlag)); } -/* return_type */ operator&(/* args */) { - throw std::runtime_error{"Not implemented"}; +constexpr bool operator&(CheckFlags lFlag, CheckFlags rFlag) { + uint8_t lFlagUi = toUi(lFlag); + uint8_t rFlagUi = toUi(rFlag); + + if (lFlagUi == 0 || rFlagUi == 0) + return false; + + return (lFlagUi & rFlagUi) == lFlagUi || (lFlagUi & rFlagUi) == rFlagUi; } -/* return_type */ operator^(/* args */) { - throw std::runtime_error{"Not implemented"}; +constexpr CheckFlags operator^(CheckFlags lFlag, CheckFlags rFlag) { + return static_cast(toUi(lFlag) ^ toUi(rFlag)); } -/* return_type */ operator~(/* args */) { - throw std::runtime_error{"Not implemented"}; +constexpr CheckFlags operator~(CheckFlags flag) { + return static_cast(flagAll & ~toUi(flag)); } -/* return_type */ operator<<(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::ostream& operator<<(std::ostream& os, CheckFlags flag) { + uint8_t num = toUi(flag); + bool first = true; + auto try_print = [&](CheckFlags f, const char* name) { + if (num & toUi(f)) { + if (!first) + os << ", "; + os << name; + first = false; + } + }; + + try_print(CheckFlags::TIME, "TIME"); + try_print(CheckFlags::DATE, "DATE"); + try_print(CheckFlags::USER, "USER"); + try_print(CheckFlags::CERT, "CERT"); + try_print(CheckFlags::KEYS, "KEYS"); + try_print(CheckFlags::DEST, "DEST"); + + if (first) + os << "NONE"; + + return os; } From 1107258ae897253a0439b6b0e1390e16dc5b70ee Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 01:21:43 +0500 Subject: [PATCH 22/37] add (solution): add filter task --- 03_week/tasks/filter/filter.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/03_week/tasks/filter/filter.cpp b/03_week/tasks/filter/filter.cpp index 6648cb39..269c1cf3 100644 --- a/03_week/tasks/filter/filter.cpp +++ b/03_week/tasks/filter/filter.cpp @@ -1,6 +1,19 @@ +#include +#include #include +#include -/* return_type */ Filter(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector& Filter(std::vector& vec, bool (*func)(int)) { + if (func == nullptr) + return vec; + + size_t pos = 0; + size_t len = vec.size(); + for (size_t i = 0; i < len; ++i) + if (func(vec[i])) + vec[pos++] = vec[i]; + + vec.resize(pos); + return vec; } \ No newline at end of file From 0d083b88c2782f96a88a4723af0d507c5808b908 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 01:22:18 +0500 Subject: [PATCH 23/37] add (solution): add filter task --- 03_week/tasks/find_all/find_all.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/03_week/tasks/find_all/find_all.cpp b/03_week/tasks/find_all/find_all.cpp index 74f393b2..4cdd8e6f 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::vector FindAll(const std::vector& vec, bool (*func)(int)) { + if (func == nullptr) + return {}; + + size_t count = 0; + for (size_t i = 0; i < vec.size(); ++i) + if (func(vec[i])) + ++count; + std::vector result(count); -/* return_type */ FindAll(/* args */) { - throw std::runtime_error{"Not implemented"}; + size_t pos = 0; + size_t len = vec.size(); + for (size_t i = 0; i < len; ++i) + if (func(vec[i])) + result[pos++] = i; + + return result; } \ No newline at end of file From b7a67be8018595959e41908117b853424ee60abe Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 01:22:41 +0500 Subject: [PATCH 24/37] add (solution): add minmax task --- 03_week/tasks/minmax/minmax.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/03_week/tasks/minmax/minmax.cpp b/03_week/tasks/minmax/minmax.cpp index c2869799..d4f8ed79 100644 --- a/03_week/tasks/minmax/minmax.cpp +++ b/03_week/tasks/minmax/minmax.cpp @@ -1,6 +1,20 @@ #include +#include -/* return_type */ MinMax(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::pair::const_iterator, std::vector::const_iterator> MinMax(const std::vector& vec) { + if (vec.empty()) + return {vec.end(), vec.end()}; + + auto minEl = vec.begin(); + auto maxEl = vec.begin(); + + for (auto x = vec.begin(); x != vec.end(); ++x) { + if (*x < *minEl) + minEl = x; + if (*x >= *maxEl) + maxEl = x; + } + + return {minEl, maxEl}; } From a236f5c9b0c96198ded1f45c2dd5f28951102dfe Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 01:24:02 +0500 Subject: [PATCH 25/37] change in main.cpp --- main.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 9dad8356..c336f7e9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,99 @@ +#include +#include +#include #include +struct Date { + unsigned year; + unsigned month; + unsigned day; -int main() { - std::cout << "I wait your unbelievable code here" << std::endl; - return 0; + Date (unsigned y = 0u, unsigned m = 0u, unsigned d = 0u) : year(y), month(m), day(d) {} +}; + +bool operator==(const Date& leftDate, const Date& rightDate) { + return std::tie(leftDate.year, leftDate.month, leftDate.day) == std::tie(rightDate.year, rightDate.month, rightDate.day); +} + +bool operator!=(const Date& leftDate, const Date& rightDate) { + return !(leftDate == rightDate); +} + +bool operator<(const Date& leftDate, const Date& rightDate) { + return std::tie(leftDate.year, leftDate.month, leftDate.day) < std::tie(rightDate.year, rightDate.month, rightDate.day); +} + +bool operator>(const Date& leftDate, const Date& rightDate) { + return rightDate < leftDate; +} + +bool operator<=(const Date& leftDate, const Date& rightDate) { + return leftDate < rightDate || leftDate == rightDate; +} + +bool operator>=(const Date& leftDate, const Date& rightDate) { + return leftDate > rightDate || leftDate == rightDate; +} + +struct StudentInfo { + size_t id; + char mark; + int score; + unsigned course; + Date birth_date; +}; + +bool operator==(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return std::tie(leftStudent.mark, leftStudent.score) == std::tie(rightStudent.mark, rightStudent.score); } + +bool operator!=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return !(leftStudent == rightStudent); +} + +bool operator<(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + if (leftStudent.mark != rightStudent.mark) + return leftStudent.mark > rightStudent.mark; + if (leftStudent.score != rightStudent.score) + return leftStudent.score < rightStudent.score; + if (leftStudent.course != rightStudent.course) + return leftStudent.course > rightStudent.course; + if (leftStudent.birth_date != rightStudent.birth_date) + return leftStudent.birth_date < rightStudent.birth_date; + return false; +} + +bool operator>(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return rightStudent < leftStudent; +} + +bool operator<=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return leftStudent < rightStudent || leftStudent == rightStudent; +} + +bool operator>=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { + return leftStudent > rightStudent || leftStudent == rightStudent; +} + +int main(void) { + std::vector students = { + {1, 'A', 2000, 5, Date{2000, 1, 1}}, + {2, 'A', 1999, 1, Date{2004, 2, 1}}, + {3, 'A', 1999, 1, Date{2003, 2, 1}}, + {4, 'B', 150, 5, Date{2017, 1, 1}}, + {5, 'C', 140, 3, Date{2016, 1, 1}}, + {6, 'D', 120, 2, Date{2017, 1, 1}}, + {7, 'Z', 100, 1, Date{2018, 1, 1}}, + {8, 'A', 1999, 2, Date{2003, 1, 1}}, + {9, 'A', 1999, 1, Date{2003, 1, 1}}, + {10, 'A', 2000, 4, Date{2000, 1, 1}}, + {11, 'A', 2000, 4, Date{2000, 1, 2}} + }; + + std::sort(students.begin(), students.end()); + + for (size_t i = 0; i < students.size(); ++i) { + std::cout << students[i].id << ' ' << students[i].mark << ' ' << students[i].score << ' ' << students[i].course << ' ' << students[i].birth_date.year << '\n'; + } + return 0; +} \ No newline at end of file From de774e1bafc3d58656a6a4d7e3ae4b3583953984 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 17:56:54 +0500 Subject: [PATCH 26/37] add (solution): add range task --- 03_week/tasks/range/range.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/03_week/tasks/range/range.cpp b/03_week/tasks/range/range.cpp index d2085495..f4976670 100644 --- a/03_week/tasks/range/range.cpp +++ b/03_week/tasks/range/range.cpp @@ -2,6 +2,19 @@ #include -std::vector Range(int from, int to, int step) { - throw std::runtime_error{"Not implemented"}; +std::vector Range(int from, int to, int step = 1) { + if ((from <= to && step < 0) || (from >= to && step > 0) || step == 0) + return {}; + + int cnt = (to - from) / step; + if ((to - from) % step != 0) + ++cnt; + + std::vector result; + result.reserve(cnt); + + for (int el = from; (step > 0 ? el < to : el > to); el += step) + result.push_back(el); + + return result; } From fa9b7116b5c747bd71380312deb7eb673795a131 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 17:57:22 +0500 Subject: [PATCH 27/37] add (solution): add unique task --- 03_week/tasks/unique/unique.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/03_week/tasks/unique/unique.cpp b/03_week/tasks/unique/unique.cpp index 9d2545bb..ab2d94cb 100644 --- a/03_week/tasks/unique/unique.cpp +++ b/03_week/tasks/unique/unique.cpp @@ -1,6 +1,23 @@ #include #include -/* return_type */ Unique(/* args */) { - throw std::runtime_error{"Not implemented"}; +std::vector Unique(const std::vector& vec) { + if (vec.size() == 0) + return {}; + + size_t len = vec.size(); + size_t cnt = 1; + for (size_t i = 1; i < len; ++i) + if (vec[i] != vec[i - 1]) + ++cnt; + + std::vector result; + result.reserve(cnt); + + result.push_back(vec[0]); + for (size_t i = 1; i < len; ++i) + if (vec[i] != vec[i - 1]) + result.push_back(vec[i]); + + return result; } From 541c88a0aa45f47b94338538436c3dcbd4f6cea1 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 17 Dec 2025 17:59:36 +0500 Subject: [PATCH 28/37] change in main --- main.cpp | 97 +++++++++----------------------------------------------- 1 file changed, 15 insertions(+), 82 deletions(-) diff --git a/main.cpp b/main.cpp index c336f7e9..0274e011 100644 --- a/main.cpp +++ b/main.cpp @@ -3,97 +3,30 @@ #include #include -struct Date { - unsigned year; - unsigned month; - unsigned day; +std::vector Range(int from, int to, int step = 1) { + if ((from <= to && step < 0) || (from >= to && step > 0) || step == 0) + return {}; - Date (unsigned y = 0u, unsigned m = 0u, unsigned d = 0u) : year(y), month(m), day(d) {} -}; + int count = (to - from) / step; + if ((to - from) % step != 0) + ++count; -bool operator==(const Date& leftDate, const Date& rightDate) { - return std::tie(leftDate.year, leftDate.month, leftDate.day) == std::tie(rightDate.year, rightDate.month, rightDate.day); -} - -bool operator!=(const Date& leftDate, const Date& rightDate) { - return !(leftDate == rightDate); -} + std::vector result; + result.reserve(count); -bool operator<(const Date& leftDate, const Date& rightDate) { - return std::tie(leftDate.year, leftDate.month, leftDate.day) < std::tie(rightDate.year, rightDate.month, rightDate.day); -} + for (int el = from; (step > 0 ? el < to : el > to); el += step) + result.push_back(el); -bool operator>(const Date& leftDate, const Date& rightDate) { - return rightDate < leftDate; + return result; } -bool operator<=(const Date& leftDate, const Date& rightDate) { - return leftDate < rightDate || leftDate == rightDate; -} - -bool operator>=(const Date& leftDate, const Date& rightDate) { - return leftDate > rightDate || leftDate == rightDate; -} - -struct StudentInfo { - size_t id; - char mark; - int score; - unsigned course; - Date birth_date; -}; - -bool operator==(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { - return std::tie(leftStudent.mark, leftStudent.score) == std::tie(rightStudent.mark, rightStudent.score); -} - -bool operator!=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { - return !(leftStudent == rightStudent); -} - -bool operator<(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { - if (leftStudent.mark != rightStudent.mark) - return leftStudent.mark > rightStudent.mark; - if (leftStudent.score != rightStudent.score) - return leftStudent.score < rightStudent.score; - if (leftStudent.course != rightStudent.course) - return leftStudent.course > rightStudent.course; - if (leftStudent.birth_date != rightStudent.birth_date) - return leftStudent.birth_date < rightStudent.birth_date; - return false; -} - -bool operator>(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { - return rightStudent < leftStudent; -} - -bool operator<=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { - return leftStudent < rightStudent || leftStudent == rightStudent; -} - -bool operator>=(const StudentInfo& leftStudent, const StudentInfo& rightStudent) { - return leftStudent > rightStudent || leftStudent == rightStudent; -} int main(void) { - std::vector students = { - {1, 'A', 2000, 5, Date{2000, 1, 1}}, - {2, 'A', 1999, 1, Date{2004, 2, 1}}, - {3, 'A', 1999, 1, Date{2003, 2, 1}}, - {4, 'B', 150, 5, Date{2017, 1, 1}}, - {5, 'C', 140, 3, Date{2016, 1, 1}}, - {6, 'D', 120, 2, Date{2017, 1, 1}}, - {7, 'Z', 100, 1, Date{2018, 1, 1}}, - {8, 'A', 1999, 2, Date{2003, 1, 1}}, - {9, 'A', 1999, 1, Date{2003, 1, 1}}, - {10, 'A', 2000, 4, Date{2000, 1, 1}}, - {11, 'A', 2000, 4, Date{2000, 1, 2}} - }; - - std::sort(students.begin(), students.end()); + std::vector res = Range(20, 20, 3); - for (size_t i = 0; i < students.size(); ++i) { - std::cout << students[i].id << ' ' << students[i].mark << ' ' << students[i].score << ' ' << students[i].course << ' ' << students[i].birth_date.year << '\n'; + for (size_t i = 0; i < res.size(); ++i) { + std::cout << res[i] << ' '; } + std::cout << '\n'; return 0; } \ No newline at end of file From eca65d47b08059c831a863215f81311b156f25e3 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 23 Dec 2025 14:54:43 +0500 Subject: [PATCH 29/37] add (solution): add phasor task --- 04_week/tasks/phasor/phasor.cpp | 251 +++++++++++++++++++++++++++++++- 1 file changed, 250 insertions(+), 1 deletion(-) diff --git a/04_week/tasks/phasor/phasor.cpp b/04_week/tasks/phasor/phasor.cpp index 3ec1b9ad..840d00bb 100644 --- a/04_week/tasks/phasor/phasor.cpp +++ b/04_week/tasks/phasor/phasor.cpp @@ -1,4 +1,5 @@ - +#include +#include struct ExpTag {}; struct DegTag {}; @@ -6,5 +7,253 @@ struct AlgTag {}; class Phasor { +public: + // Конструкторы (объявления) + Phasor(); + Phasor(double amplitude, double phase); + Phasor(double amplitude, double phase, ExpTag); + Phasor(double amplitude, double phase, DegTag); + Phasor(double real, double imag, AlgTag); + + // Методы (объявления) + void SetPolar(double amplitude, double phase); + void SetCartesian(double real, double imag); + 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 operator-() const; + Phasor& operator+=(const Phasor& other); + Phasor& operator-=(const Phasor& other); + Phasor& operator*=(const Phasor& other); + Phasor& operator/=(const Phasor& other); + Phasor& operator+=(double val); + Phasor& operator-=(double val); + Phasor& operator*=(double val); + Phasor& operator/=(double val); + + Phasor Conj() const; + Phasor Inv() const; + friend bool operator==(const Phasor& a, const Phasor& b); + friend bool operator!=(const Phasor& a, const Phasor& b); + +private: + double re; + double im; + + static constexpr double PI = 3.14159265358979323846; }; + +// Арифметические операторы (объявления) +Phasor operator+(Phasor a, const Phasor& b); +Phasor operator-(Phasor a, const Phasor& b); +Phasor operator*(Phasor a, const Phasor& b); +Phasor operator/(Phasor a, const Phasor& b); + +Phasor operator+(Phasor a, double b); +Phasor operator+(double a, const Phasor& b); +Phasor operator-(Phasor a, double b); +Phasor operator-(double a, const Phasor& b); +Phasor operator*(Phasor a, double b); +Phasor operator*(double a, const Phasor& b); +Phasor operator/(Phasor a, double b); +Phasor operator/(double a, const Phasor& b); + +// Методы создания (объявления) +Phasor MakePhasorCartesian(double re, double im); +Phasor MakePhasorPolar(double amplitude, double phase); +Phasor MakePhasorPolarDeg(double amplitude, double phase); + +// Вывод (объявление) +std::ostream& operator<<(std::ostream& os, const Phasor& p); + + +// Конструкторы (реализации) + +Phasor::Phasor() : re(0.0), im(0.0) {} + +Phasor::Phasor(double amplitude, double phase) { + SetPolar(amplitude, phase); +} + +Phasor::Phasor(double amplitude, double phase, ExpTag) : Phasor(amplitude, phase) {} + +Phasor::Phasor(double amplitude, double phase, DegTag) { + SetPolar(amplitude, phase * PI / 180.0); +} + +Phasor::Phasor(double real, double imag, AlgTag) : re(real), im(imag) {} + +// Методы (реализации) + +void Phasor::SetPolar(double amplitude, double phase) { + re = amplitude * std::cos(phase); + im = amplitude * std::sin(phase); +} + +void Phasor::SetCartesian(double real, double imag) { + re = real; + im = imag; +} + +double Phasor::Magnitude() const { + return std::hypot(re, im); +} + +double Phasor::Abs() const { + return Magnitude(); +} + +double Phasor::Phase() const { + double p = std::atan2(im, re); + return (p <= -PI + 1e-20) ? PI : p; +} + +double Phasor::Angle() const { + return Phase(); +} + +double Phasor::PhaseDeg() const { + return Phase() * 180.0 / PI; +} + +double Phasor::AngleDeg() const { + return PhaseDeg(); +} + +double Phasor::Real() const { + return re; +} + +double Phasor::Imag() const { + return im; +} + + +// Операторы (реализации) + +Phasor Phasor::operator-() const { + return Phasor(-re, -im, AlgTag()); +} + +Phasor& Phasor::operator+=(const Phasor& other) { + re += other.re; + im += other.im; + return *this; +} + +Phasor& Phasor::operator-=(const Phasor& other) { + re -= other.re; + im -= other.im; + return *this; +} + +Phasor& Phasor::operator*=(const Phasor& other) { + double old_re = re; + re = old_re * other.re - im * other.im; + im = old_re * other.im + im * other.re; + return *this; +} + +Phasor& Phasor::operator/=(const Phasor& other) { + double denom = other.re * other.re + other.im * other.im; + double old_re = re; + re = (old_re * other.re + im * other.im) / denom; + im = (im * other.re - old_re * other.im) / denom; + return *this; +} + +Phasor& Phasor::operator+=(double val) { + re += val; + return *this; +} + +Phasor& Phasor::operator-=(double val) { + re -= val; + return *this; +} + +Phasor& Phasor::operator*=(double val) { + re *= val; + im *= val; + return *this; +} + +Phasor& Phasor::operator/=(double val) { + re /= val; + im /= val; + return *this; +} + +Phasor Phasor::Conj() const { + return Phasor(re, -im, AlgTag()); +} + +Phasor Phasor::Inv() const { + double denom = re * re + im * im; + return Phasor(re / denom, -im / denom, AlgTag()); +} + + +bool operator==(const Phasor& a, const Phasor& b) { + return a.re == b.re && a.im == b.im; +} + +bool operator!=(const Phasor& a, const Phasor& b) { + return !(a == b); +} + +// Арифметические операторы (реализации) + +Phasor operator+(Phasor a, const Phasor& b) { return a += b; } +Phasor operator-(Phasor a, const Phasor& b) { return a -= b; } +Phasor operator*(Phasor a, const Phasor& b) { return a *= b; } +Phasor operator/(Phasor a, const Phasor& b) { return a /= b; } + +Phasor operator+(Phasor a, double b) { return a += b; } +Phasor operator+(double a, const Phasor& b) { return Phasor(a, 0, AlgTag()) + b; } + +Phasor operator-(Phasor a, double b) { return a -= b; } +Phasor operator-(double a, const Phasor& b) { return Phasor(a, 0, AlgTag()) - b; } + +Phasor operator*(Phasor a, double b) { return a *= b; } +Phasor operator*(double a, const Phasor& b) { return Phasor(a, 0, AlgTag()) * b; } + +Phasor operator/(Phasor a, double b) { return a /= b; } +Phasor operator/(double a, const Phasor& b) { return Phasor(a, 0, AlgTag()) / b; } + + +// Методы создания (реализация) + +Phasor MakePhasorCartesian(double re, double im) { + return Phasor(re, im, AlgTag()); +} + +Phasor MakePhasorPolar(double amplitude, double phase) { + return Phasor(amplitude, phase, ExpTag()); +} + +Phasor MakePhasorPolarDeg(double amplitude, double phase) { + return Phasor(amplitude, phase, DegTag()); +} + +// Вывод в поток (реализация) + +std::ostream& operator<<(std::ostream& os, const Phasor& p) { + std::streamsize default_precision = os.precision(); + std::ios_base::fmtflags default_flags = os.flags(); + + os << std::fixed << std::setprecision(3); + os << p.Magnitude() << "*e(j*" << p.PhaseDeg() << ") [" << p.Real() << " + j*" << p.Imag() << "]"; + + os.precision(default_precision); + os.flags(default_flags); + return os; +} \ No newline at end of file From e8966ccf7ccd48d228fdabd05d73573653d78c9e Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 23 Dec 2025 22:26:54 +0500 Subject: [PATCH 30/37] add (solution): add queue task --- 04_week/tasks/queue/queue.cpp | 145 ++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index 2a9f8493..fd31be06 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -1,6 +1,151 @@ +#include +#include #include +#include class Queue { +public: + Queue(); + Queue(std::stack st); + Queue(std::vector vec); + Queue(std::initializer_list list); + Queue(int n); + size_t Size() const; + bool Empty() const; + void Push(int val); + bool Pop(); + int& Front(); + const int& Front() const; + int& Back(); + const int& Back() const; + void Clear(); + void Swap(Queue& other); + + friend bool operator==(const Queue& a, const Queue& b); + friend bool operator!=(const Queue& a, const Queue& b); + +private: + mutable std::vector in; + mutable std::vector out; + + void prepareOut() const { + if (out.empty()) { + while (!in.empty()) { + out.push_back(in.back()); + in.pop_back(); + } + } + } + + void prepareIn() const { + if (in.empty()) { + while (!out.empty()) { + in.push_back(out.back()); + out.pop_back(); + } + } + } }; + +Queue::Queue() = default; + +Queue::Queue(std::stack st) { + while (!st.empty()) { + this->Push(st.top()); + st.pop(); + } + std::reverse(in.begin(), in.end()); +} + +Queue::Queue(std::vector vec) { + in.reserve(vec.size()); + out.reserve(vec.size()); + in = vec; +} + +Queue::Queue(std::initializer_list list) { + in.reserve(list.size()); + out.reserve(list.size()); + in = list; +} + +Queue::Queue(int n) { + in.reserve(n); + out.reserve(n); +} + +size_t Queue::Size() const { + return in.size() + out.size(); +} + +bool Queue::Empty() const { + return in.empty() && out.empty(); +} + +void Queue::Push(int val) { + in.push_back(val); +} + +bool Queue::Pop() { + prepareOut(); + if (out.empty()) + return false; + out.pop_back(); + return true; +} + +int& Queue::Front() { + prepareOut(); + return out.back(); +} + +const int& Queue::Front() const { + prepareOut(); + return out.back(); +} + +int& Queue::Back() { + prepareIn(); + return in.back(); +} + +const int& Queue::Back() const { + prepareIn(); + return in.back(); +} + +void Queue::Clear() { + if (!in.empty()) + in.clear(); + if (!out.empty()) + out.clear(); +} + +void Queue::Swap(Queue& other) { + std::swap(in, other.in); + std::swap(out, other.out); +} + +bool operator==(const Queue& a, const Queue& b) { + if ((a.in.size() + a.out.size()) != (b.in.size() + b.out.size())) + return false; + + auto get_val = [](const Queue& q, size_t index) { + if (index < q.out.size()) + return q.out[q.out.size() - 1 - index]; + return q.in[index - q.out.size()]; + }; + + size_t total_size = a.in.size() + a.out.size(); + for (size_t i = 0; i < total_size; ++i) + if (get_val(a, i) != get_val(b, i)) + return false; + + return true; +} + +bool operator!=(const Queue& a, const Queue& b) { + return !(a == b); +} \ No newline at end of file From 526bb1bc80af6e0ddbbfb44bc086e6b458cab6d3 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Tue, 23 Dec 2025 22:51:50 +0500 Subject: [PATCH 31/37] add (solution): add queue task --- 04_week/tasks/queue/queue.cpp | 46 +++++++++++++++-------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index fd31be06..93f0d6c8 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -6,12 +6,14 @@ class Queue { public: + // Конструкторы (объявления) Queue(); Queue(std::stack st); Queue(std::vector vec); Queue(std::initializer_list list); Queue(int n); + // Методы (объявления) size_t Size() const; bool Empty() const; void Push(int val); @@ -23,6 +25,7 @@ class Queue { void Clear(); void Swap(Queue& other); + // Операторы (объявления) friend bool operator==(const Queue& a, const Queue& b); friend bool operator!=(const Queue& a, const Queue& b); @@ -30,6 +33,7 @@ class Queue { mutable std::vector in; mutable std::vector out; + // Подготовка стека на вывод void prepareOut() const { if (out.empty()) { while (!in.empty()) { @@ -38,19 +42,12 @@ class Queue { } } } - - void prepareIn() const { - if (in.empty()) { - while (!out.empty()) { - in.push_back(out.back()); - out.pop_back(); - } - } - } }; -Queue::Queue() = default; +// Конструкторы (реализации) + +Queue::Queue() = default; Queue::Queue(std::stack st) { while (!st.empty()) { this->Push(st.top()); @@ -58,24 +55,15 @@ Queue::Queue(std::stack st) { } std::reverse(in.begin(), in.end()); } - -Queue::Queue(std::vector vec) { - in.reserve(vec.size()); - out.reserve(vec.size()); - in = vec; -} - -Queue::Queue(std::initializer_list list) { - in.reserve(list.size()); - out.reserve(list.size()); - in = list; -} - +Queue::Queue(std::vector vec) : in(vec) {} +Queue::Queue(std::initializer_list list) : in(list) {} Queue::Queue(int n) { in.reserve(n); out.reserve(n); } +//Методы (реализации) + size_t Queue::Size() const { return in.size() + out.size(); } @@ -107,13 +95,15 @@ const int& Queue::Front() const { } int& Queue::Back() { - prepareIn(); - return in.back(); + if (!in.empty()) + return in.back(); + return out.front(); } const int& Queue::Back() const { - prepareIn(); - return in.back(); + if (!in.empty()) + return in.back(); + return out.front(); } void Queue::Clear() { @@ -128,6 +118,8 @@ void Queue::Swap(Queue& other) { std::swap(out, other.out); } +// Операторы (реализации) + bool operator==(const Queue& a, const Queue& b) { if ((a.in.size() + a.out.size()) != (b.in.size() + b.out.size())) return false; From 39167d8ae421cff03e0179c6e0940de9626a4feb Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 24 Dec 2025 19:41:23 +0500 Subject: [PATCH 32/37] add (solution): add stack task --- 04_week/tasks/stack/stack.cpp | 92 +++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/04_week/tasks/stack/stack.cpp b/04_week/tasks/stack/stack.cpp index 222e4ffc..9687e02c 100644 --- a/04_week/tasks/stack/stack.cpp +++ b/04_week/tasks/stack/stack.cpp @@ -1,6 +1,98 @@ +#include +#include #include +#include class Stack { +public: + // Конструкторы (объявления) + Stack(); + Stack(const Stack& other); + // Методы (объявления) + size_t Size() const; + bool Empty() const; + void Push(int val); + bool Pop(); + int& Top(); + const int& Top() const; + void Clear(); + void Swap(Stack& other); + + // Операторы (объявления) + Stack& operator=(const Stack& other); + bool operator==(const Stack& other) const; + bool operator!=(const Stack& other) const; + +private: + std::vector st; }; + + +// Конструкторы (реализации) + +Stack::Stack() {}; +Stack::Stack(const Stack& other) : st(other.st) {} + +//Методы (реализации) + +size_t Stack::Size() const { + return st.size(); +} + +bool Stack::Empty() const { + return st.empty(); +} + +void Stack::Push(int val) { + st.push_back(val); +} + +bool Stack::Pop() { + if (st.empty()) + return false; + st.pop_back(); + return true; +} + +int& Stack::Top() { + return st.back(); +} + +const int& Stack::Top() const { + return st.back(); +} + +void Stack::Clear() { + if (!st.empty()) + st.clear(); +} + +void Stack::Swap(Stack& other) { + std::swap(st, other.st); +} + +// Операторы (реализации) + +Stack& Stack::operator=(const Stack& other) { + if (this == &other) + return *this; + this->st = other.st; + return *this; +} + +bool Stack::operator==(const Stack& other) const { + if (this->st.size() != other.st.size()) + return false; + + for (size_t i = 0; i < this->Size(); ++i) + if (this->st[i] != other.st[i]) + return false; + + return true; +} + +bool Stack::operator!=(const Stack& other) const { + return !(*this == other); +} \ No newline at end of file From e6a7df0952c06a84976336f8bc74e03029b2285f Mon Sep 17 00:00:00 2001 From: Rikitick Date: Wed, 24 Dec 2025 19:45:27 +0500 Subject: [PATCH 33/37] add (solution): add queue task --- 04_week/tasks/queue/queue.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index 93f0d6c8..77367b40 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -26,8 +26,8 @@ class Queue { void Swap(Queue& other); // Операторы (объявления) - friend bool operator==(const Queue& a, const Queue& b); - friend bool operator!=(const Queue& a, const Queue& b); + bool operator==(const Queue& other) const; + bool operator!=(const Queue& other) const; private: mutable std::vector in; @@ -47,7 +47,7 @@ class Queue { // Конструкторы (реализации) -Queue::Queue() = default; +Queue::Queue() {}; Queue::Queue(std::stack st) { while (!st.empty()) { this->Push(st.top()); @@ -120,8 +120,8 @@ void Queue::Swap(Queue& other) { // Операторы (реализации) -bool operator==(const Queue& a, const Queue& b) { - if ((a.in.size() + a.out.size()) != (b.in.size() + b.out.size())) +bool Queue::operator==(const Queue& other) const { + if ((this->in.size() + this->out.size()) != (other.in.size() + other.out.size())) return false; auto get_val = [](const Queue& q, size_t index) { @@ -130,14 +130,14 @@ bool operator==(const Queue& a, const Queue& b) { return q.in[index - q.out.size()]; }; - size_t total_size = a.in.size() + a.out.size(); + size_t total_size = this->in.size() + this->out.size(); for (size_t i = 0; i < total_size; ++i) - if (get_val(a, i) != get_val(b, i)) + if (get_val(*this, i) != get_val(other, i)) return false; return true; } -bool operator!=(const Queue& a, const Queue& b) { - return !(a == b); +bool Queue::operator!=(const Queue& other) const { + return !(*this == other); } \ No newline at end of file From 0ad1565d3320f35428bf5154da57eb49c132d821 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Fri, 26 Dec 2025 19:58:32 +0500 Subject: [PATCH 34/37] add (solution): add queue task --- 04_week/tasks/queue/queue.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/04_week/tasks/queue/queue.cpp b/04_week/tasks/queue/queue.cpp index 77367b40..8352746c 100644 --- a/04_week/tasks/queue/queue.cpp +++ b/04_week/tasks/queue/queue.cpp @@ -124,7 +124,7 @@ bool Queue::operator==(const Queue& other) const { if ((this->in.size() + this->out.size()) != (other.in.size() + other.out.size())) return false; - auto get_val = [](const Queue& q, size_t index) { + auto getVal = [](const Queue& q, size_t index) { if (index < q.out.size()) return q.out[q.out.size() - 1 - index]; return q.in[index - q.out.size()]; @@ -132,7 +132,7 @@ bool Queue::operator==(const Queue& other) const { size_t total_size = this->in.size() + this->out.size(); for (size_t i = 0; i < total_size; ++i) - if (get_val(*this, i) != get_val(other, i)) + if (getVal(*this, i) != getVal(other, i)) return false; return true; From a3b4cb1e3337aa67371cc23f9ec53eeab33531b8 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Fri, 26 Dec 2025 19:59:11 +0500 Subject: [PATCH 35/37] add (solution): add ring_buffer task --- 04_week/tasks/ring_buffer/ring_buffer.cpp | 148 +++++++++++++++++++++- 1 file changed, 146 insertions(+), 2 deletions(-) diff --git a/04_week/tasks/ring_buffer/ring_buffer.cpp b/04_week/tasks/ring_buffer/ring_buffer.cpp index e2b57ba2..5d82879d 100644 --- a/04_week/tasks/ring_buffer/ring_buffer.cpp +++ b/04_week/tasks/ring_buffer/ring_buffer.cpp @@ -1,6 +1,150 @@ -#include - + #include +#include +#include class RingBuffer { +public: + RingBuffer(size_t n); + RingBuffer(size_t n, int val); + RingBuffer(std::initializer_list list); + + bool Empty() const; + bool Full() const; + size_t Size() const; + size_t Capacity() const; + void Push(int val); + bool TryPush(int val); + void Pop(); + bool TryPop(int& val); + int& Front(); + const int& Front() const; + int& Back(); + const int& Back() const; + void Clear(); + void Resize(size_t n); + std::vector Vector() const; + int& operator[](size_t ind); + const int& operator[](size_t ind) const; + +private: + std::vector buf; + size_t begin = 0; + size_t curSize = 0; }; + +// Конструкторы + +RingBuffer::RingBuffer(size_t n) : buf(std::max(1, n)), begin(0), curSize(0) {} + +RingBuffer::RingBuffer(size_t n, int val) : buf(std::max(1, n), val) { + curSize = buf.size(); + begin = 0; +} + +RingBuffer::RingBuffer(std::initializer_list list) : buf(list), begin(0), curSize(list.size()) { + if (buf.empty()) + buf.resize(1); +} + +// Методы + +size_t RingBuffer::Size() const { + return curSize; +} + +size_t RingBuffer::Capacity() const { + return buf.size(); +} + +bool RingBuffer::Empty() const { + return curSize == 0; +} + +bool RingBuffer::Full() const { + return curSize == buf.size(); +} + +void RingBuffer::Push(int val) { + if (Full()) { + buf[begin] = val; + begin = (begin + 1) % buf.size(); + } else { + buf[(begin + curSize) % buf.size()] = val; + curSize++; + } +} + +bool RingBuffer::TryPush(int val) { + if (Full()) + return false; + Push(val); + return true; +} + +void RingBuffer::Pop() { + if (!Empty()) { + begin = (begin + 1) % buf.size(); + curSize--; + } +} + +bool RingBuffer::TryPop(int& val) { + if (Empty()) + return false; + val = Back(); + Pop(); + return true; +} + +int& RingBuffer::Front() { + return buf[(begin + curSize - 1) % buf.size()]; +} +const int& RingBuffer::Front() const { + return buf[(begin + curSize - 1) % buf.size()]; +} + +int& RingBuffer::Back() { + return buf[begin]; +} +const int& RingBuffer::Back() const { + return buf[begin]; +} + +void RingBuffer::Clear() { + begin = 0; + curSize = 0; +} + +void RingBuffer::Resize(size_t n) { + if (n == 0) + n = 1; + + std::vector newBuf(n); + size_t cntCopy = std::min(curSize, n); + size_t start = curSize > n ? (curSize - n) : 0; + + for (size_t i = 0; i < cntCopy; ++i) + newBuf[i] = (*this)[start + i]; + + buf = std::move(newBuf); + begin = 0; + curSize = cntCopy; +} + +std::vector RingBuffer::Vector() const { + std::vector res(curSize); + + for (size_t i = 0; i < curSize; ++i) + res[i] = (*this)[i]; + + return res; +} + +int& RingBuffer::operator[](size_t ind) { + return buf[(begin + ind) % buf.size()]; +} + +const int& RingBuffer::operator[](size_t ind) const { + return buf[(begin + ind) % buf.size()]; +} \ No newline at end of file From 2079c05d7270fab458e92c35b6df8d71b59466a2 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Fri, 26 Dec 2025 20:36:55 +0500 Subject: [PATCH 36/37] add (solution): add ring_phasor task --- 04_week/tasks/phasor/phasor.cpp | 91 ++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 30 deletions(-) diff --git a/04_week/tasks/phasor/phasor.cpp b/04_week/tasks/phasor/phasor.cpp index 840d00bb..083050b8 100644 --- a/04_week/tasks/phasor/phasor.cpp +++ b/04_week/tasks/phasor/phasor.cpp @@ -26,6 +26,8 @@ class Phasor { double AngleDeg() const; double Real() const; double Imag() const; + Phasor Conj() const; + Phasor Inv() const; // Операторы (объявления) Phasor operator-() const; @@ -37,17 +39,13 @@ class Phasor { Phasor& operator-=(double val); Phasor& operator*=(double val); Phasor& operator/=(double val); - - Phasor Conj() const; - Phasor Inv() const; - - friend bool operator==(const Phasor& a, const Phasor& b); - friend bool operator!=(const Phasor& a, const Phasor& b); + bool operator==(const Phasor& other) const; + bool operator!=(const Phasor& other) const; + private: double re; double im; - static constexpr double PI = 3.14159265358979323846; }; @@ -71,7 +69,7 @@ Phasor MakePhasorCartesian(double re, double im); Phasor MakePhasorPolar(double amplitude, double phase); Phasor MakePhasorPolarDeg(double amplitude, double phase); -// Вывод (объявление) +// // Вывод в поток (объявление) std::ostream& operator<<(std::ostream& os, const Phasor& p); @@ -91,6 +89,7 @@ Phasor::Phasor(double amplitude, double phase, DegTag) { Phasor::Phasor(double real, double imag, AlgTag) : re(real), im(imag) {} + // Методы (реализации) void Phasor::SetPolar(double amplitude, double phase) { @@ -136,6 +135,15 @@ double Phasor::Imag() const { return im; } +Phasor Phasor::Conj() const { + return Phasor(re, -im, AlgTag()); +} + +Phasor Phasor::Inv() const { + double denom = re * re + im * im; + return Phasor(re / denom, -im / denom, AlgTag()); +} + // Операторы (реализации) @@ -192,42 +200,64 @@ Phasor& Phasor::operator/=(double val) { return *this; } -Phasor Phasor::Conj() const { - return Phasor(re, -im, AlgTag()); +bool Phasor::operator==(const Phasor& other) const { + return this->re == other.re && this->im == other.im; } -Phasor Phasor::Inv() const { - double denom = re * re + im * im; - return Phasor(re / denom, -im / denom, AlgTag()); +bool Phasor::operator!=(const Phasor& other) const { + return !(*this == other); } -bool operator==(const Phasor& a, const Phasor& b) { - return a.re == b.re && a.im == b.im; +// Арифметические операторы (реализации) + +Phasor operator+(Phasor a, const Phasor& b) { + return a += b; } -bool operator!=(const Phasor& a, const Phasor& b) { - return !(a == b); +Phasor operator-(Phasor a, const Phasor& b) { + return a -= b; } -// Арифметические операторы (реализации) +Phasor operator*(Phasor a, const Phasor& b) { + return a *= b; +} + +Phasor operator/(Phasor a, const Phasor& b) { + return a /= b; +} + +Phasor operator+(Phasor a, double b) { + return a += b; +} -Phasor operator+(Phasor a, const Phasor& b) { return a += b; } -Phasor operator-(Phasor a, const Phasor& b) { return a -= b; } -Phasor operator*(Phasor a, const Phasor& b) { return a *= b; } -Phasor operator/(Phasor a, const Phasor& b) { return a /= b; } +Phasor operator+(double a, const Phasor& b) { + return Phasor(a, 0, AlgTag()) + b; +} + +Phasor operator-(Phasor a, double b) { + return a -= b; +} + +Phasor operator-(double a, const Phasor& b) { + return Phasor(a, 0, AlgTag()) - b; +} -Phasor operator+(Phasor a, double b) { return a += b; } -Phasor operator+(double a, const Phasor& b) { return Phasor(a, 0, AlgTag()) + b; } +Phasor operator*(Phasor a, double b) { + return a *= b; +} -Phasor operator-(Phasor a, double b) { return a -= b; } -Phasor operator-(double a, const Phasor& b) { return Phasor(a, 0, AlgTag()) - b; } +Phasor operator*(double a, const Phasor& b) { + return Phasor(a, 0, AlgTag()) * b; +} -Phasor operator*(Phasor a, double b) { return a *= b; } -Phasor operator*(double a, const Phasor& b) { return Phasor(a, 0, AlgTag()) * b; } +Phasor operator/(Phasor a, double b) { + return a /= b; +} -Phasor operator/(Phasor a, double b) { return a /= b; } -Phasor operator/(double a, const Phasor& b) { return Phasor(a, 0, AlgTag()) / b; } +Phasor operator/(double a, const Phasor& b) { + return Phasor(a, 0, AlgTag()) / b; +} // Методы создания (реализация) @@ -244,6 +274,7 @@ Phasor MakePhasorPolarDeg(double amplitude, double phase) { return Phasor(amplitude, phase, DegTag()); } + // Вывод в поток (реализация) std::ostream& operator<<(std::ostream& os, const Phasor& p) { From ba80fc5bee38d59fb4f2188d70ce22fa760f2c18 Mon Sep 17 00:00:00 2001 From: Rikitick Date: Fri, 26 Dec 2025 20:37:20 +0500 Subject: [PATCH 37/37] add (solution): add ring_ring_buffer task --- 04_week/tasks/ring_buffer/ring_buffer.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/04_week/tasks/ring_buffer/ring_buffer.cpp b/04_week/tasks/ring_buffer/ring_buffer.cpp index 5d82879d..8a6274aa 100644 --- a/04_week/tasks/ring_buffer/ring_buffer.cpp +++ b/04_week/tasks/ring_buffer/ring_buffer.cpp @@ -4,10 +4,12 @@ class RingBuffer { public: + // Конструкторы (объявления) RingBuffer(size_t n); RingBuffer(size_t n, int val); RingBuffer(std::initializer_list list); + // Методы (объявления) bool Empty() const; bool Full() const; size_t Size() const; @@ -24,6 +26,7 @@ class RingBuffer { void Resize(size_t n); std::vector Vector() const; + // Операторы (объявсления) int& operator[](size_t ind); const int& operator[](size_t ind) const; @@ -33,7 +36,7 @@ class RingBuffer { size_t curSize = 0; }; -// Конструкторы +// Конструкторы (реализации) RingBuffer::RingBuffer(size_t n) : buf(std::max(1, n)), begin(0), curSize(0) {} @@ -47,7 +50,7 @@ RingBuffer::RingBuffer(std::initializer_list list) : buf(list), begin(0), c buf.resize(1); } -// Методы +// Методы (реализации) size_t RingBuffer::Size() const { return curSize; @@ -100,6 +103,7 @@ bool RingBuffer::TryPop(int& val) { int& RingBuffer::Front() { return buf[(begin + curSize - 1) % buf.size()]; } + const int& RingBuffer::Front() const { return buf[(begin + curSize - 1) % buf.size()]; } @@ -107,6 +111,7 @@ const int& RingBuffer::Front() const { int& RingBuffer::Back() { return buf[begin]; } + const int& RingBuffer::Back() const { return buf[begin]; } @@ -141,6 +146,8 @@ std::vector RingBuffer::Vector() const { return res; } +// Операторы (реализации) + int& RingBuffer::operator[](size_t ind) { return buf[(begin + ind) % buf.size()]; }