-
Notifications
You must be signed in to change notification settings - Fork 33
Рычков Григорий #42
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?
Рычков Григорий #42
Changes from all commits
05f634a
c1917a1
4d776aa
0b5b0cf
9c2e6d1
64105b2
8693a72
92b781c
eb97186
5ebb519
98f7b0b
32d3368
887a093
e9853e4
afdacf7
7acc01e
46c4f81
4df22bb
68129b4
4b035c8
c16f030
aa36ec4
fb2e26d
1b1360b
86efdf4
1f6bca4
0cf4e9c
b14a286
f644ce2
3557ab2
f3c5e4b
7e663dc
e56d485
5acad0a
4f80bcd
34fa6ac
935fd58
eb4892a
9a84272
39d1ff7
fc75705
59d3cd1
ee77fe1
da25729
f2a213f
113ab71
76f3e6e
2ed52ca
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,18 +1,45 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| enum class CheckFlags : uint8_t { | ||
| NONE = 0, | ||
| TIME = (1 << 0), | ||
| DATE = (1 << 1), | ||
| USER = (1 << 2), | ||
| CERT = (1 << 3), | ||
| KEYS = (1 << 4), | ||
| DEST = (1 << 5), | ||
| ALL = TIME | DATE | USER | CERT | KEYS | DEST | ||
| }; | ||
|
|
||
| void PrintCheckFlags(CheckFlags flags) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| enum class CheckFlags : uint8_t { | ||
| NONE = 0, | ||
| TIME = (1 << 0), | ||
| DATE = (1 << 1), | ||
| USER = (1 << 2), | ||
| CERT = (1 << 3), | ||
| KEYS = (1 << 4), | ||
| DEST = (1 << 5), | ||
| ALL = TIME | DATE | USER | CERT | KEYS | DEST | ||
| }; | ||
|
|
||
| void PrintCheckFlags(CheckFlags flags) { | ||
| uint8_t value = static_cast<uint8_t>(flags); | ||
| const uint8_t valid_mask = 0x3F; // 0b00111111 | ||
|
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. лучше задействовать ALL, тогда при изменениях перечисления не нужно будет вносить изменения в код |
||
|
|
||
| if (value & ~valid_mask) { | ||
| std::cout << ""; | ||
| return; | ||
| } | ||
|
|
||
| std::string output = "["; | ||
| const char* separator = ""; | ||
|
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. почему не char ? |
||
|
|
||
| auto add_flag = [&](CheckFlags flag, const char* name) { | ||
| if (value & static_cast<uint8_t>(flag)) { | ||
| output += separator; | ||
| output += name; | ||
| separator = ","; | ||
| } | ||
| }; | ||
|
|
||
| add_flag(CheckFlags::TIME, "TIME"); | ||
|
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. имя не подходящее, не отражает сути |
||
| add_flag(CheckFlags::DATE, "DATE"); | ||
| add_flag(CheckFlags::USER, "USER"); | ||
| add_flag(CheckFlags::CERT, "CERT"); | ||
| add_flag(CheckFlags::KEYS, "KEYS"); | ||
| add_flag(CheckFlags::DEST, "DEST"); | ||
|
|
||
| output += "]"; | ||
| std::cout << output; | ||
|
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. можно было сразу в поток писать |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include <cmath> | ||
|
|
||
| constexpr double operator "" _ft_to_m(long double value) { | ||
| return static_cast<double>(value * 0.3048); | ||
| } | ||
|
|
||
| constexpr double operator "" _ft_to_cm(long double value) { | ||
| return static_cast<double>(value * 30.48); | ||
| } | ||
|
|
||
| constexpr double operator "" _ft_to_in(long double value) { | ||
| return static_cast<double>(value * 12.0); | ||
| } | ||
|
|
||
| constexpr double operator "" _in_to_m(long double value) { | ||
| return static_cast<double>(value * 0.0254); | ||
| } | ||
|
|
||
| constexpr double operator "" _in_to_cm(long double value) { | ||
| return static_cast<double>(value * 2.54); | ||
| } | ||
|
|
||
| constexpr double operator "" _in_to_ft(long double value) { | ||
| return static_cast<double>(value / 12.0); | ||
| } | ||
|
|
||
| constexpr double operator "" _m_to_ft(long double value) { | ||
| return static_cast<double>(value / 0.3048); | ||
| } | ||
|
|
||
| constexpr double operator "" _m_to_in(long double value) { | ||
| return static_cast<double>(value / 0.0254); | ||
| } | ||
|
|
||
| constexpr double operator "" _m_to_cm(long double value) { | ||
| return static_cast<double>(value * 100.0); | ||
| } | ||
|
|
||
| constexpr double operator "" _cm_to_m(long double value) { | ||
| return static_cast<double>(value * 0.01); | ||
| } | ||
|
|
||
| constexpr double operator "" _cm_to_ft(long double value) { | ||
| return static_cast<double>(value / 30.48); | ||
| } | ||
|
|
||
| constexpr double operator "" _cm_to_in(long double value) { | ||
| return static_cast<double>(value / 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. magic value не принято в коде использовать, стоило вынести в константы 12, 0.3048, 0.0254 ... с понятными именами и в выражениях использовать. константы можно поместить в безымянный namespace, чтобы видимость ограничивалась данным файлом |
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,29 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| void PrintBits(long long value, size_t bytes) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| #include <iostream> | ||
|
|
||
| void PrintBits(long long value, size_t bytes) { | ||
| if (bytes == 0 || bytes > 8) { | ||
| return; | ||
| } | ||
|
|
||
| const size_t total_bits = bytes * 8; | ||
| unsigned long long unsigned_value = value; | ||
|
|
||
| std::cout << "0b"; | ||
|
|
||
| for (size_t i = 0; i < total_bits; ++i) { | ||
| size_t bit_pos = total_bits - 1 - i; | ||
| unsigned long long mask = 1ULL << bit_pos; | ||
|
|
||
| if (unsigned_value & mask) { | ||
| std::cout << '1'; | ||
| } else { | ||
| std::cout << '0'; | ||
| } | ||
|
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 ((i + 1) % 4 == 0 && i != total_bits - 1) { | ||
| std::cout << "'"; | ||
| } | ||
| } | ||
|
|
||
| std::cout << '\n'; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,56 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <cmath> | ||
| #include <utility> | ||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| std::cout << std::setprecision(6); | ||
|
|
||
| if (a == 0 && b == 0 && c == 0) { | ||
| std::cout << "infinite solutions"; | ||
| return; | ||
| } | ||
|
|
||
| if (a == 0 && b == 0 && c != 0) { | ||
|
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. третья проверка лишняя, так как после первой проверки уже звестно что c != 0 |
||
| std::cout << "no solutions"; | ||
| return; | ||
| } | ||
|
|
||
| if (a == 0) { | ||
| double root = -static_cast<double>(c) / b; | ||
|
|
||
| if (root == 0.0) root = 0.0; | ||
|
|
||
| std::cout << root; | ||
| return; | ||
| } | ||
|
|
||
| double discr = static_cast<double>(b) * b - 4.0 * a * c; | ||
|
|
||
| if (discr < 0) { | ||
| std::cout << "no solutions"; | ||
| return; | ||
| } | ||
|
|
||
| if (discr == 0) { | ||
| double root = -static_cast<double>(b) / (2.0 * a); | ||
|
|
||
| if (root == 0.0) root = 0.0; | ||
|
|
||
| std::cout << root; | ||
| return; | ||
| } | ||
|
|
||
| double sqrt_d = std::sqrt(discr); | ||
| double root1 = (-static_cast<double>(b) - sqrt_d) / (2.0 * a); | ||
| double root2 = (-static_cast<double>(b) + sqrt_d) / (2.0 * a); | ||
|
|
||
| if (root1 > root2) { | ||
| std::swap(root1, root2); | ||
| } | ||
|
|
||
| if (root1 == 0.0) root1 = 0.0; | ||
| if (root2 == 0.0) root2 = 0.0; | ||
|
|
||
| std::cout << root1 << " " << root2; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,19 @@ | ||
| #include <cstdef> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <cmath> | ||
|
|
||
|
|
||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| if (values == nullptr || size == 0) { | ||
| return 0.0; | ||
| } | ||
|
|
||
| double sum = 0.0; | ||
| for (size_t i = 0; i < size; ++i) { | ||
| sum += values[i] * values[i]; | ||
| } | ||
|
|
||
| double sqavg = sum / size; | ||
| return std::sqrt(sqavg); | ||
|
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. лучше без дополнительной переменной сразу записать в выражение, так как sqavg больше нигде неиспользуется |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,15 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| #include <stdexcept> | ||
|
|
||
| double ApplyOperations(double a, double b, double (**operations)(double, double), size_t n) { | ||
| double total = 0.0; | ||
|
|
||
| if (operations == nullptr || n == 0) { | ||
| return total;} | ||
|
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 (size_t i = 0; i < n; ++i) { | ||
| if (operations[i] != nullptr) { | ||
|
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. можно воспользовать неявным приведением указателя к булеву типо, так часто делают, и не писать |
||
| total += operations[i](a, 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. } на следующей строке |
||
| } | ||
|
|
||
| return total; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,22 @@ | ||
| #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)(int)) { | ||
|
|
||
| if (begin == nullptr || end == nullptr || begin > 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. Для указателей часто принято использовать неявное приведение к bool |
||
| return end; | ||
| } | ||
|
|
||
| const int* last_found = end; | ||
|
|
||
| // Проходим по массиву с конца к началу | ||
| const int* current = end; | ||
| while (current > begin) { | ||
| --current; | ||
| if (predicate(*current)) { | ||
| last_found = current; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
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. в целом хорошо, но можно было обойтись без дополнительного указателя last_found, в конце возвращать end, а в теле возвращать current при совпадении |
||
| return last_found; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,48 @@ | ||
| #include <stdexcept> | ||
|
|
||
| template<typename CharType> | ||
| CharType* FindLongestSubsequenceImpl(CharType* begin, CharType* end, size_t& count) { | ||
| if (begin == nullptr || end == nullptr || begin > end) { | ||
| count = 0; | ||
| return nullptr; | ||
| } | ||
|
|
||
| if (begin == end) { | ||
| count = 0; | ||
| return nullptr; | ||
| } | ||
|
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. можно объединить условия, действия одинаковые и в каждом из случаев считаем входные указатели не валидными |
||
|
|
||
| CharType* longest_start = begin; | ||
| size_t longest_length = 1; | ||
| CharType* current_start = begin; | ||
| size_t current_length = 1; | ||
|
|
||
| for (CharType* ptr = begin; ptr < end - 1; ++ptr) { | ||
| if (*ptr == *(ptr + 1)) { | ||
| current_length++; | ||
| } else { | ||
| if (current_length > longest_length) { | ||
| longest_length = current_length; | ||
| longest_start = current_start; | ||
| } | ||
| current_start = ptr + 1; | ||
| current_length = 1; | ||
| } | ||
| } | ||
|
|
||
| if (current_length > longest_length) { | ||
| longest_length = current_length; | ||
| longest_start = current_start; | ||
| } | ||
|
|
||
| count = longest_length; | ||
| return longest_start; | ||
| } | ||
|
|
||
| const char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) { | ||
| return FindLongestSubsequenceImpl(begin, end, count); | ||
| } | ||
|
|
||
| /* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| char* FindLongestSubsequence(char* begin, char* end, size_t& count) { | ||
| return FindLongestSubsequenceImpl(begin, end, 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. если это шаблонная функция, зачем тогда данные версии? |
||
| } | ||
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.
Лучше написать сразу выражение, не создавая лишних переменных
тогда останется один каст, второй операнд не следует приводить явно, так как он бы преобразовался с помощью неявного каста. Принято использовать такую возможность и не писать явный каст самостоятельно второй раз