-
Notifications
You must be signed in to change notification settings - Fork 33
Бобровских Сергей #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
786c587
e47e779
2be84bb
73ae725
3c312a5
97ebf04
5eb2252
b53ca2a
3e5a80a
9159eeb
b20c4ff
097fc7d
9f39dfa
4f48105
b6f99dc
78363af
e8920e7
56befd7
27abbaa
a5f6e06
dca960a
81a216b
99c2021
0ecaae7
07c7889
8ecb3cb
7658216
17ee479
5fda3fb
bd5dc5a
c9f0966
6f192e0
bddfbd5
34aec08
77b1c24
45c2b50
159ee20
956c669
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| enum class CheckFlags : uint8_t { | ||
|
|
@@ -14,5 +15,40 @@ enum class CheckFlags : uint8_t { | |
| }; | ||
|
|
||
| void PrintCheckFlags(CheckFlags flags) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| // Значение находится в допустимом диапазоне | ||
| uint8_t value = static_cast<uint8_t>(flags); | ||
| if (value == 0) { | ||
| std::cout << "[]"; | ||
| return; | ||
| } | ||
| if (value > static_cast<uint8_t>(CheckFlags::ALL)) { | ||
| return; | ||
| } | ||
| // Последнее значение | ||
| if (value == static_cast<uint8_t>(CheckFlags::ALL)) { | ||
| std::cout << "[TIME,DATE,USER,CERT,KEYS,DEST]"; | ||
| return; | ||
| } | ||
|
|
||
| std::cout << "["; | ||
| bool first = true; | ||
|
|
||
| const std::pair<CheckFlags, const char*> flag_names[] = { | ||
| {CheckFlags::TIME, "TIME"}, | ||
| {CheckFlags::DATE, "DATE"}, | ||
| {CheckFlags::USER, "USER"}, | ||
| {CheckFlags::CERT, "CERT"}, | ||
| {CheckFlags::KEYS, "KEYS"}, | ||
| {CheckFlags::DEST, "DEST"} | ||
| }; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. единственное не очень хорошо каждый раз выделять память в функции под данный массив. |
||
|
|
||
| for (const auto& [flag, name] : flag_names) { | ||
| if (value & static_cast<uint8_t>(flag)) { | ||
| if (!first) std::cout << ","; | ||
| std::cout << name; | ||
| first = false; | ||
| } | ||
| } | ||
|
|
||
| std::cout << "]"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| double FT_IN_M = 1.0 / 0.3048; | ||
| double FT_IN_INCHES = 12.0; | ||
| double INCH_IN_CM = 2.54; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше будет вынести в безымянный или именной namespace |
||
|
|
||
| // Футы в другие величины | ||
| double operator""_ft_to_m(long double ft) { | ||
| return ft / FT_IN_M; | ||
| } | ||
|
|
||
| double operator""_ft_to_cm(long double ft) { | ||
| return ft / FT_IN_M * 100.0; | ||
| } | ||
|
|
||
| double operator""_ft_to_in(long double ft) { | ||
| return ft * FT_IN_INCHES; | ||
| } | ||
|
|
||
| // Дюймы в другие величины | ||
| double operator""_in_to_ft(long double in) { | ||
| return in / FT_IN_INCHES; | ||
| } | ||
|
|
||
| double operator""_in_to_cm(long double in) { | ||
| return in * INCH_IN_CM; | ||
| } | ||
|
|
||
| double operator""_in_to_m(long double inches) { | ||
| return inches * INCH_IN_CM / 100; | ||
| } | ||
|
|
||
| // См в другие величины | ||
| double operator""_cm_to_m(long double centimeters) { | ||
| return centimeters / 100; | ||
| } | ||
|
|
||
| double operator""_cm_to_in(long double centimeters) { | ||
| return centimeters / INCH_IN_CM; | ||
| } | ||
|
|
||
| double operator""_cm_to_ft(long double centimeters) { | ||
| return centimeters / (FT_IN_INCHES * INCH_IN_CM); | ||
| } | ||
|
|
||
| // Метры в другие величины | ||
| double operator""_m_to_ft(long double meters) { | ||
| return meters * 100 / (INCH_IN_CM * FT_IN_INCHES); | ||
| } | ||
|
|
||
| double operator""_m_to_in(long double meters) { | ||
| return meters * 100 / INCH_IN_CM; | ||
| } | ||
|
|
||
| double operator""_m_to_cm(long double meters) { | ||
| return meters * 100; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,27 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| void PrintBits(long long value, size_t bytes) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| using std::cout; | ||
| // Проверка корректности размера | ||
| if (bytes == 0 || bytes > 8) { | ||
| return; | ||
| } | ||
| std::cout << "0b"; | ||
|
|
||
| // Проходим по всем битам, начиная со старшего | ||
| for (size_t pos = bytes * 8; pos > 0; --pos) { | ||
| // Получаем значение бита на текущей позиции | ||
| int bit = (value >> (pos - 1)) & 1; | ||
| std::cout << bit; | ||
|
|
||
| // Добавляем апостроф между байтами (после каждых 4 бит, кроме последней группы) | ||
| if ((pos - 1) > 0 && (pos - 1) % 4 == 0) { | ||
| std::cout << "'"; | ||
| } | ||
| } | ||
|
|
||
| std::cout << "\n"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,56 @@ | ||
| #include <stdexcept> | ||
| #include <iomanip> | ||
| #include <iostream> | ||
| #include <cmath> | ||
|
|
||
| void PrintRoot(double x) { | ||
| // Для избавления от заведомо небольших значений | ||
| if (std::abs(x) < 1e-10) { | ||
| x = 0.0; | ||
| } | ||
| std::cout << std::setprecision(6) << x; | ||
| } | ||
|
|
||
| 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"; | ||
| return; | ||
| } | ||
|
|
||
| double a_ = static_cast<double>(a); | ||
| double b_ = static_cast<double>(b); | ||
| double c_ = static_cast<double>(c); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это лишнее, вариант выше был лучше, дальше также |
||
|
|
||
| if(a_ == 0) { | ||
| if(b_ == 0) { | ||
| // Нет решений | ||
| std::cout << "no solutions"; | ||
| } else { | ||
| // Один корень линейного уравнения | ||
| PrintRoot(-c_ / b_); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше static_cast одной переменной, без создания |
||
| } | ||
| return; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше не плодить вложенные ветвления, короткие условия как в начале лучше, немного больше может получится проверок на 0, но это не страшно |
||
| } | ||
|
|
||
| double D = b_*b_ - 4*a_*c_; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в данном случае лучше здесь сделать static_cast для одного b, а вместо 4 использовать 4.0 |
||
| if (D > 0) { | ||
| double sqrt_d = std::sqrt(D); | ||
| double x1 = (-b_ - sqrt_d) / (2.0 * a_); | ||
| double x2 = (-b_ + sqrt_d) / (2.0 * a_); | ||
|
|
||
| // Гарантируем, что x1 < x2 | ||
| if (x1 > x2) { | ||
| std::swap(x1, x2); | ||
| } | ||
| PrintRoot(x1); | ||
| std::cout << " "; | ||
| PrintRoot(x2); | ||
| } | ||
| else if (D == 0) { | ||
| double x = -b_ / (2.0 * a_); | ||
| PrintRoot(x); | ||
| } | ||
| else { | ||
| std::cout << "no solutions"; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше начать с коротких ветвей (D < 0) с return а основной случай D > 0 вынести из под условия, чтобы не было лишней вложенность |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| #include <cstdef> | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <cmath> | ||
|
|
||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if(size == 0 || values == nullptr) { // Проверка на отсутствие элементов в массиве и на нулевой указатель | ||
| return 0.0; | ||
| } | ||
| double square_sum = 0.0; | ||
| for(size_t i = 0; i < size; ++i) { | ||
| square_sum += values[i] * values[i]; | ||
| } | ||
| return sqrt(square_sum/size); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| double ApplyOperations(double a, double b, double (*ptr[])(double, double), size_t size) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ptr не удачно название здесь, лучше func_arr например, более соответствует действительности |
||
| double sum = 0.0; | ||
|
|
||
| if(size == 0 || ptr == nullptr) { | ||
| return sum; | ||
| } | ||
|
|
||
| for (size_t i = 0; i < size; ++i) { | ||
| if(ptr[i] == nullptr) continue; | ||
| sum += ptr[i](a, b); | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| /* 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)(const int)) { | ||
| if((begin > end) || (begin == nullptr) || (end == nullptr)) return end; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. для указателей часто принято не использовать явное сравнение с nullptr, а использовать неявное приведение к bool |
||
|
|
||
| for(const int* ptr = end - 1; ptr >= begin; --ptr) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (predicate(*ptr)) {return ptr;} | ||
| } | ||
| return end; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,45 @@ | ||
| #include <stdexcept> | ||
| #include <cstring> | ||
| #include <iomanip> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| void PrintMemory(int /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(int value, bool reverse = false) { | ||
| unsigned char* bytes = reinterpret_cast<unsigned char*>(&value); | ||
|
|
||
| std::cout << "0x"; | ||
|
|
||
| if (reverse) { | ||
| // Вывод в обратном порядке (big-endian) | ||
| for (int i = sizeof(int) - 1; i >= 0; --i) { | ||
| std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]); | ||
| } | ||
| } else { | ||
| // Вывод в естественном порядке (little-endian) | ||
| for (size_t i = 0; i < sizeof(int); ++i) { | ||
| std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]); | ||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно было попробовать избавиться от дублирование, тело одинаковое |
||
|
|
||
| std::cout << std::endl; | ||
| } | ||
|
|
||
| void PrintMemory(double /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(double value, bool reverseBytes = false) { | ||
| unsigned char* bytes = reinterpret_cast<unsigned char*>(&value); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это полное дублирование кода, лучше было из данных функций после получения указателя и зная sizeof от типа вызыватьтретью функцию которая как раз выполняет работу |
||
|
|
||
| std::cout << "0x"; | ||
|
|
||
| if (reverseBytes) { | ||
| // Вывод в обратном порядке (big-endian) | ||
| for (int i = sizeof(double) - 1; i >= 0; --i) { | ||
| std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]); | ||
| } | ||
| } else { | ||
| // Вывод в естественном порядке (little-endian) | ||
| for (size_t i = 0; i < sizeof(double); ++i) { | ||
| std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]); | ||
| } | ||
| } | ||
|
|
||
| std::cout << std::endl; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,34 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| /* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) { | ||
| if (begin == nullptr || end == nullptr || begin >= end) { | ||
| count = 0; | ||
| return nullptr; | ||
| } | ||
|
|
||
| const char* longest_ptr = begin; | ||
| const char* current_ptr = begin; | ||
| size_t max_count = 1; | ||
| size_t current_count = 1; | ||
|
|
||
| for (const char* ptr = begin + 1; ptr < end; ++ptr) { | ||
| if (*ptr == *(ptr - 1)) { | ||
| current_count++; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это должен быть префиксный инкременет |
||
| } else { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше выше написать continue, а тут избавится от вложенности лишнего else |
||
| if(current_count > max_count) { | ||
| max_count = current_count; | ||
| longest_ptr = current_ptr; | ||
| } | ||
| current_ptr = ptr; | ||
| current_count = 1; | ||
| } | ||
| } | ||
|
|
||
| if(current_count > max_count) { | ||
| max_count = current_count; | ||
| longest_ptr = current_ptr; | ||
| } | ||
| count = max_count; | ||
| return const_cast<char*>(longest_ptr); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это UB, так как мы можем принять изначально указатель на константный объект, нужно было написать две функции у одной принимаемые и возвращаемые указатели на константу, а другого простые, и вот уже из функции без константости корректно было бы вызывать константную версию и снимать константность с результата const_cast, так как была бы увереность, что объект изначально не константный |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,38 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| void PrintArray(/* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintArray(const int* begin, const int* end, int constraint = 0) { | ||
| if (begin == end || begin == nullptr || end == nullptr) | ||
| { | ||
| std::cout << "[]\n"; | ||
| return; | ||
| } | ||
|
|
||
| int count = 0; | ||
| std::cout << "["; | ||
| if(begin < end) { | ||
| for(const int* ptr = begin; ptr < end; ++ptr) { | ||
| if((count == constraint) && (constraint != 0)) { | ||
| count = 0; | ||
| std::cout << "...\n "; | ||
| } | ||
| std::cout << *ptr; | ||
| if(ptr != end - 1) { std::cout << ", "; } | ||
| ++count; | ||
| } | ||
| std::cout << "]\n"; | ||
| } else { | ||
| ++end; | ||
| for(const int* ptr = begin; ptr >= end; --ptr) { | ||
| if((count == constraint) && (constraint != 0)) { | ||
| count = 0; | ||
| std::cout << "...\n "; | ||
| } | ||
| std::cout << *ptr; | ||
| if(ptr != end) { std::cout << ", "; } | ||
| if(ptr == end) { break; } | ||
| ++count; | ||
| } | ||
| std::cout << "]\n"; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В данном случае это тоже практически полное дублирование кода, можно и нужно от него избавиться |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лишний каст, второй операнд не следует приводить явно, так как он бы преобразовался с помощью неявного каста. Принято использовать такую возможность и не писать явный каст самостоятельно второй раз