diff --git a/01_week/tasks/addition/addition.cpp b/01_week/tasks/addition/addition.cpp index 92872802..78f77b24 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) { - throw std::runtime_error{"Not implemented"}; + int64_t x1 = a; + int64_t x2 = b; + return x1 + x2; } \ 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..4c0963d8 100644 --- a/01_week/tasks/char_changer/char_changer.cpp +++ b/01_week/tasks/char_changer/char_changer.cpp @@ -1,7 +1,72 @@ #include #include - +#include size_t CharChanger(char array[], size_t size, char delimiter = ' ') { - throw std::runtime_error{"Not implemented"}; + if (size == 0) return 0; + + size_t pos_read = 0; // позиция чтения + size_t pos_write = 0; // позиция записи + + while (array[pos_read] != '\0') { // проверка на последний символ + char current_char = array[pos_read]; // изменяемый символ + size_t count = 1; + + while (pos_read + count < size && array[pos_read + count] == current_char) { // подсчет повторающихся символов + ++count; + } + + if (isdigit(current_char)) {// проверка на цифры + current_char = '*'; + } + else if (islower(current_char)) { // проверка на строчные буквы + current_char = toupper(current_char); + } + else if (!isupper(current_char) && current_char != ' ') { //остальные символы, кроме пробелов и прописных букв + current_char = '_'; + } + + if (current_char == ' ') { // оработка пробелов + current_char = delimiter; + if (pos_write < size) { + array[pos_write] = current_char; + ++pos_write; + } + } + + else { // обработка остальных символов + if (pos_write < size) { //записываем символ + array[pos_write] = current_char; + ++pos_write; + } + if ( count > 1) { ///обрабатываем повторения + if (count >= 10) { + if (pos_write < size) { + array[pos_write] = '0'; + ++pos_write; + } + } + else { + if (pos_write < size) { + array[pos_write] = '0' + count; + ++pos_write; + } + } + } + } + pos_read += count; + } + + + + // Добавляем завершающий нуль-символ + if (pos_write < size) { + array[pos_write] = '\0'; + } + else if (size > 0) { + array[size - 1] = '\0'; + pos_write = size - 1; + } + + return pos_write; } diff --git a/01_week/tasks/check_flags/check_flags.cpp b/01_week/tasks/check_flags/check_flags.cpp index 75e7c652..6545624a 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,44 @@ enum class CheckFlags : uint8_t { }; void PrintCheckFlags(CheckFlags flags) { - throw std::runtime_error{"Not implemented"}; + uint8_t value = static_cast(flags); + + if (value > static_cast(CheckFlags::ALL)) { + return; + } + + // Обработка специального случая NONE + if (flags == CheckFlags::NONE) { + std::cout << "[]"; + return; + } + + std::cout<< "["; + const char* comma = ""; + if ((value & static_cast(CheckFlags::TIME)) !=0) { + std::cout<< comma << "TIME"; + comma = ","; + } + if ((value & static_cast(CheckFlags::DATE)) !=0) { + std::cout<< comma << "DATE"; + comma = ","; + } + if ((value & static_cast(CheckFlags::USER)) !=0) { + std::cout<< comma << "USER"; + comma = ","; + } + if ((value & static_cast(CheckFlags::CERT)) !=0) { + std::cout<< comma << "CERT"; + comma = ","; + } + if ((value & static_cast(CheckFlags::KEYS)) !=0) { + std::cout<< comma << "KEYS"; + comma = ","; + } + if ((value & static_cast(CheckFlags::DEST)) !=0) { + std::cout<< comma << "DEST"; + comma = ","; + } + + std::cout<<"]"; } diff --git a/01_week/tasks/length_lit/length_lit.cpp b/01_week/tasks/length_lit/length_lit.cpp index e69de29b..861c90a8 100644 --- a/01_week/tasks/length_lit/length_lit.cpp +++ b/01_week/tasks/length_lit/length_lit.cpp @@ -0,0 +1,44 @@ + +namespace length_const { + constexpr double METER = 1.0; + constexpr double CENTIMETER = 0.01; + constexpr double FOOT = 0.3048; + constexpr double INCH = 0.0254; +} + +constexpr double operator""_ft_to_in(long double value) { + return value * length_const::FOOT / length_const::INCH; +} +constexpr double operator""_ft_to_cm(long double value) { + return value * length_const::FOOT / length_const::CENTIMETER; +} +constexpr double operator""_ft_to_m(long double value) { + return value * length_const::FOOT / length_const::METER; +} +constexpr double operator""_in_to_ft(long double value) { + return value * length_const::INCH / length_const::FOOT; +} +constexpr double operator""_in_to_cm(long double value) { + return value * length_const::INCH / length_const::CENTIMETER; +} +constexpr double operator""_in_to_m(long double value) { + return value * length_const::INCH / length_const::METER; +} +constexpr double operator""_cm_to_ft(long double value) { + return value * length_const::CENTIMETER / length_const::FOOT; +} +constexpr double operator""_cm_to_in(long double value) { + return value * length_const::CENTIMETER / length_const::INCH; +} +constexpr double operator""_cm_to_m(long double value) { + return value * length_const::CENTIMETER / length_const::METER; +} +constexpr double operator""_m_to_ft(long double value) { + return value * length_const::METER / length_const::FOOT; +} +constexpr double operator""_m_to_in(long double value) { + return value * length_const::METER / length_const::INCH; +} +constexpr double operator""_m_to_cm(long double value) { + return value * length_const::METER / length_const:: CENTIMETER; +} diff --git a/01_week/tasks/print_bits/print_bits.cpp b/01_week/tasks/print_bits/print_bits.cpp index a48a43c1..938a362d 100644 --- a/01_week/tasks/print_bits/print_bits.cpp +++ b/01_week/tasks/print_bits/print_bits.cpp @@ -1,7 +1,21 @@ #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 = static_cast(bytes * 8 - 1); i >= 0; i--) { + unsigned long long bit = (static_cast(value) >> i) & 1; + std::cout << bit; + + if (i > 0 && i % 4 == 0) { + std::cout << "'"; + } + } + std::cout << std::endl; } diff --git a/01_week/tasks/quadratic/quadratic.cpp b/01_week/tasks/quadratic/quadratic.cpp index abf7d632..8c02b919 100644 --- a/01_week/tasks/quadratic/quadratic.cpp +++ b/01_week/tasks/quadratic/quadratic.cpp @@ -1,6 +1,50 @@ +#include +#include +#include #include - void SolveQuadratic(int a, int b, int c) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file + std::cout << std::defaultfloat << std::setprecision(6); + + if (a == 0 && b == 0 && c == 0) { + std::cout << "infinite solutions"; + return; + } + + if (a == 0) { + if (b == 0) { + std::cout << "no solutions"; + } + else { + double x = -static_cast(c) / b; + if (x == -0) { + x = 0; + } + std::cout << x; + } + return; + } + + double discriminant = static_cast(b) * b - 4.0 * a * c; + if (discriminant < 0) { + std::cout << "no solutions"; + } + else if (discriminant == 0) { + double x = -static_cast(b) / (2.0 * a); + if (x == -0) { + x = 0; + } + std::cout << x; + } + else { + double sqrt_d = std::sqrt(discriminant); + double x1 = (-static_cast(b) - sqrt_d) / (2.0 * a); + double x2 = (-static_cast(b) + sqrt_d) / (2.0 * a); + + if (x1 > x2) { + std::swap(x1, x2); + } + + std::cout << x1 << " " << x2; + } +} diff --git a/01_week/tasks/rms/rms.cpp b/01_week/tasks/rms/rms.cpp index 6882f0a9..d88b7ec5 100644 --- a/01_week/tasks/rms/rms.cpp +++ b/01_week/tasks/rms/rms.cpp @@ -1,7 +1,18 @@ -#include #include - +#include double CalculateRMS(double values[], size_t size) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file + if (size == 0 || values == nullptr) { + return 0.0; + } + + double sum_of_squares = 0.0; + for (size_t i = 0; i < size; ++i) { + sum_of_squares += values[i] * values[i]; + } + + double mean_of_squares = sum_of_squares / size; + double rms = std::sqrt(mean_of_squares); + + return rms; +} diff --git a/02_week/tasks/func_array/func_array.cpp b/02_week/tasks/func_array/func_array.cpp index b327e68d..b74ae517 100644 --- a/02_week/tasks/func_array/func_array.cpp +++ b/02_week/tasks/func_array/func_array.cpp @@ -1,6 +1,17 @@ #include -double ApplyOperations(double a, double b /* other arguments */) { - throw std::runtime_error{"Not implemented"}; +double ApplyOperations(double a, double b, double (*operations[])(double, double), size_t size ) { + double sum = 0.0; + + if (operations == nullptr || size == 0) { + return 0.0; + } + + for (size_t i = 0; i < size; ++i) { + if (operations[i] != nullptr) { + sum += operations[i](a, b); + } + } + return sum; } \ No newline at end of file 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..8ab15bad 100644 --- a/02_week/tasks/last_of_us/last_of_us.cpp +++ b/02_week/tasks/last_of_us/last_of_us.cpp @@ -1,6 +1,14 @@ #include -/* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { - throw std::runtime_error{"Not implemented"}; +const int* FindLastElement(const int* begin, const int* end, bool (*predicate)(int)) { + if (begin == nullptr || begin == nullptr || begin > end || begin == end) { + return end; + } + for (const int* ptr = end - 1; ptr >= begin; --ptr) { + if (predicate(*ptr)) { + return ptr; + } + } + return end; } \ 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..5150eb97 100644 --- a/02_week/tasks/little_big/little_big.cpp +++ b/02_week/tasks/little_big/little_big.cpp @@ -1,10 +1,39 @@ #include +#include +#include +#include +#include - -void PrintMemory(int /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; +void PrintMemory(int value, bool reverse = false) { + uint32_t bytes; + std::memcpy(&bytes, &value, sizeof(value)); + + if (reverse) { + std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8) + << bytes << "\n"; + } else { + uint32_t reversed = 0; + for (size_t i = 0; i < sizeof(value); ++i) { + reversed = (reversed << 8) | ((bytes >> (i * 8)) & 0xFF); + } + std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8) + << reversed << "\n"; + } } -void PrintMemory(double /* write arguments here */) { - throw std::runtime_error{"Not implemented"}; -} \ No newline at end of file +void PrintMemory(double value, bool reverse = false) { + uint64_t bytes; + std::memcpy(&bytes, &value, sizeof(value)); + + if (reverse) { + std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16) + << bytes << "\n"; + } else { + uint64_t reversed = 0; + for (size_t i = 0; i < sizeof(value); ++i) { + reversed = (reversed << 8) | ((bytes >> (i * 8)) & 0xFF); + } + std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16) + << reversed << "\n"; + } +} diff --git a/02_week/tasks/longest/longest.cpp b/02_week/tasks/longest/longest.cpp index 04b3c354..a7c6e9f7 100644 --- a/02_week/tasks/longest/longest.cpp +++ b/02_week/tasks/longest/longest.cpp @@ -1,6 +1,40 @@ #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 == nullptr || end == nullptr || begin > end ||begin == end) { + count = 0; + return nullptr; + } + + char* current_start = begin; // Начало текущей последовательности + char* longest_start = begin; // Начало самой длинной последовательности + size_t current_length = 1; // Длина текущей последовательности + size_t max_length = 1; // Максимальная длина найденной последовательности + + // Проходим по всем элементам, начиная со второго + for (char* it = begin + 1; it != end; ++it) { + if (*it == *(it - 1)) { + current_length++; + } else { + if (current_length > max_length) { + max_length = current_length; + longest_start = current_start; + } + current_start = it; + current_length = 1; + } + } + + if (current_length > max_length) { + max_length = current_length; + longest_start = current_start; + } + + count = max_length; + return longest_start; } + +const char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) { + char* result = FindLongestSubsequence(const_cast(begin), const_cast(end), count); + return result; +} \ No newline at end of file