-
Notifications
You must be signed in to change notification settings - Fork 33
Жамбакиев Радий #34
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?
Жамбакиев Радий #34
Changes from all commits
7154dec
abb00b8
4141f23
16fe4e3
e6233b8
520f1f2
057cea0
5af2950
f2b2c19
bff7344
7345a15
706b609
6d51ad2
6a45716
3febd06
cc12364
1aaa0c9
635fcdf
e26d95b
aabca70
12fe9b9
45ed16c
44d8762
8504d0e
6b566ab
d455bf1
7a87f24
5afec6c
0c21b29
7bc19a1
2adfae0
f128b9c
42b81c6
23e327e
e81fc9c
6095c22
9efba7f
83f3bce
55ccf15
5b5b649
a9ddb14
bd18c29
857c48f
0ebcff7
0a479eb
270198a
8f37dc1
d05dff0
1da7fd8
666f3ee
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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /build | ||
| /build-asan |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,83 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| // Есть ощущение, что это можно решить как-то более лаконично и просто | ||
|
|
||
| /** | ||
| * @brief Получить длину последовательности из одинаковых знаков. | ||
| * @param array указатель на элемент массива, с которого начнется проверка. | ||
| * @return Длина последовательности. | ||
| * @note Все символы будут сравниваться с первым из переданного массива. | ||
| */ | ||
| size_t GetIdenticalCharSequenceLen(char *array) { | ||
| size_t sequenceLen = 0; | ||
|
|
||
| if (array[0] == '\0') { | ||
| return 0; | ||
| } | ||
|
|
||
| while (array[0] == array[sequenceLen]) { | ||
| ++sequenceLen; | ||
| } | ||
|
|
||
| return sequenceLen; | ||
|
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. много лишних пустых строк |
||
| } | ||
|
|
||
| /** | ||
| * @brief Сконвертировать одинаковые символы. | ||
| * @param array указатель на начало обрабатываемого массива. | ||
| * @param convertedSymIdx ссылка на индекс последнего сконвертированного символа. | ||
| * @param currentSymIdx ссылка на индекс текущего обрабатываемого символа. | ||
| * @param swapChar знак, на который будет произведена замена array[convertedSymIdx]. | ||
|
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 none | ||
| */ | ||
| void ConvertIdenticalCharacters(char array[], size_t& convertedSymIdx, size_t& currentSymIdx, char swapChar) { | ||
| size_t sequenceLen = GetIdenticalCharSequenceLen(&array[currentSymIdx]); | ||
|
|
||
| array[convertedSymIdx++] = swapChar; | ||
|
|
||
| if (sequenceLen == 1) { | ||
| currentSymIdx += sequenceLen; | ||
| return; | ||
| } | ||
|
|
||
| if (sequenceLen >= 10) { | ||
| array[convertedSymIdx++] = '0'; | ||
| } else { | ||
| array[convertedSymIdx++] = sequenceLen + '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. в данном случае тернарный оператор был бы в одну строку |
||
|
|
||
| currentSymIdx += sequenceLen; | ||
| } | ||
|
|
||
| size_t CharChanger(char array[], size_t size, char delimiter = ' ') { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| size_t convertedSymbolIdx = 0, currentSymbolIdx = 0; | ||
|
|
||
| while(currentSymbolIdx < size && convertedSymbolIdx < size) { | ||
| if (array[currentSymbolIdx] == '\0') { | ||
| break; | ||
| } | ||
|
|
||
| if (isspace(array[currentSymbolIdx])) { | ||
| currentSymbolIdx += GetIdenticalCharSequenceLen(&array[currentSymbolIdx]); | ||
|
|
||
| array[convertedSymbolIdx++] = delimiter; | ||
| continue; | ||
| } | ||
|
|
||
| if (isalnum(array[currentSymbolIdx])) { | ||
| if (isdigit(array[currentSymbolIdx])) { | ||
| ConvertIdenticalCharacters(array, convertedSymbolIdx, currentSymbolIdx, '*'); | ||
| continue; | ||
| } | ||
|
|
||
| ConvertIdenticalCharacters(array, convertedSymbolIdx, currentSymbolIdx, toupper(array[currentSymbolIdx])); | ||
| continue; | ||
| } | ||
|
|
||
| ConvertIdenticalCharacters(array, convertedSymbolIdx, currentSymbolIdx, '_'); | ||
| } | ||
|
|
||
| array[convertedSymbolIdx] = '\0'; | ||
| return convertedSymbolIdx; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
|
|
||
| #include <string> | ||
|
|
||
| enum class CheckFlags : uint8_t { | ||
| NONE = 0, | ||
|
|
@@ -13,6 +13,47 @@ enum class CheckFlags : uint8_t { | |
| ALL = TIME | DATE | USER | CERT | KEYS | DEST | ||
| }; | ||
|
|
||
| // В данном случае можно было использовать map, но с ним придется использовать | ||
| // static_cast<int>. Так же map отсортирует значения по возрасанию, соответственно | ||
| // использовать enum написанный иначе будет невозможно. | ||
| const std::vector<std::pair<CheckFlags, std::string_view>> checkNames = { | ||
| {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. Лучше поместить в безымянный |
||
|
|
||
| void PrintCheckFlags(CheckFlags flags) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (flags > CheckFlags::ALL) { | ||
| return; | ||
| } | ||
|
|
||
| std::string needed_checks = "["; | ||
|
|
||
| if (flags == CheckFlags::NONE) { | ||
| needed_checks += "]"; // Такая модификация происходит inplace, не | ||
|
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 << needed_checks; | ||
| return; | ||
| } | ||
|
|
||
| bool first = true; | ||
| for (const auto& [key, val] : checkNames) { | ||
| if (static_cast<uint8_t>(flags) & static_cast<uint8_t>(key)) { | ||
| if (!first) { | ||
| needed_checks += ","; // добавляем запятую перед элементом, а не после, | ||
| // проверяя на первый элемент, это гарантирует, что лишних | ||
| // запятых не будет | ||
|
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. комментарий тут излишний, тем более он трехстрочный, это и так понятно по коду |
||
| } | ||
|
|
||
| needed_checks += val; | ||
| first = false; | ||
| } | ||
| } | ||
|
|
||
| needed_checks += "]"; | ||
|
|
||
| std::cout << needed_checks; | ||
|
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,51 @@ | ||
| constexpr long double METER_IN_FOOT = 0.3048; | ||
| constexpr long double INCH_IN_FOOT = 12; | ||
| constexpr long double METER_IN_INCH = 0.0254; | ||
|
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. это хорошо |
||
|
|
||
| constexpr long double operator""_ft_to_m(long double foot) { | ||
| return foot * METER_IN_FOOT; | ||
| } | ||
|
|
||
| constexpr long double operator""_m_to_ft(long double meter) { | ||
| return meter / METER_IN_FOOT; | ||
| } | ||
|
|
||
| constexpr long double operator""_ft_to_cm(long double foot) { | ||
| return foot * METER_IN_FOOT * 100; | ||
| } | ||
|
|
||
| constexpr long double operator""_cm_to_ft(long double cm) { | ||
| return cm / (METER_IN_FOOT * 100); | ||
| } | ||
|
|
||
| constexpr long double operator""_ft_to_in(long double foot) { | ||
| return foot * INCH_IN_FOOT; | ||
| } | ||
|
|
||
| constexpr long double operator""_in_to_ft(long double inch) { | ||
| return inch / INCH_IN_FOOT; | ||
| } | ||
|
|
||
| constexpr long double operator""_in_to_m(long double inch) { | ||
| return inch * METER_IN_INCH; | ||
| } | ||
|
|
||
| constexpr long double operator""_m_to_in(long double meter) { | ||
| return meter / METER_IN_INCH; | ||
| } | ||
|
|
||
| constexpr long double operator""_in_to_cm(long double inch) { | ||
| return inch * METER_IN_INCH * 100; | ||
| } | ||
|
|
||
| constexpr long double operator""_cm_to_in(long double inch) { | ||
| return inch / (METER_IN_INCH * 100); | ||
| } | ||
|
|
||
| constexpr long double operator""_m_to_cm(long double meter) { | ||
| return meter * 100; | ||
| } | ||
|
|
||
| constexpr long double operator""_cm_to_m(long double cm) { | ||
| return cm / 100; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| #include <string> | ||
|
|
||
| void PrintBits(long long value, size_t bytes) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| std::string byteForm = "0b"; | ||
| // i < bytes * 8 сработает, потому что 0ULL - 1 переполняется в ULL_MAX | ||
| for (size_t i = (bytes * 8 - 1); i < bytes * 8; --i) { | ||
| byteForm += ((value >> i) & 0x01U) + '0'; | ||
|
|
||
| if (i != 0 && i % 4 == 0) { | ||
| byteForm += "'"; | ||
| } | ||
| } | ||
|
|
||
| std::cout << byteForm << std::endl; | ||
|
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 |
|---|---|---|
| @@ -1,6 +1,64 @@ | ||
| #include <stdexcept> | ||
| #include <cmath> | ||
| #include <iomanip> | ||
|
|
||
| // Уверен, что это можно сделать значительно качественнее, но дедлайн близко, | ||
| // поэтому спагетти код >:) | ||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| void SolveQuadratic(int a, int b, int c) { | ||
| if (!a && !b && !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. числовые значения принято явно сравнивать с нулем, а вот указатели приводить так к булеву типу окей |
||
| std::cout << "infinite solutions"; | ||
| return; | ||
| } | ||
|
|
||
| if (!a && !b) { | ||
| std::cout << "no solutions"; | ||
| return; | ||
| } | ||
|
|
||
| std::cout << std::setprecision(6); | ||
|
|
||
| if (!a) { | ||
| std::cout << (static_cast<double>(-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. как будто внешние () лишние |
||
| return; | ||
| } | ||
|
|
||
| if (!b) { | ||
| if (static_cast<double>(-c) / a < 0) { | ||
| std::cout << "no solutions"; | ||
| return; | ||
| } | ||
|
|
||
| double root = std::sqrt(static_cast<double>(-c) / a); | ||
|
|
||
| if (root == 0) { | ||
| std::cout << root; | ||
| } else { | ||
| std::cout << -root << " " << root; | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| double d = static_cast<double>(b) * b - 4.0 * 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. лучше d обозначить полным именем |
||
| double x1 = 0.0, x2 = 0.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. по аналогии с кодом выше root1, root2 было бы лучше |
||
|
|
||
| if (d < 0) { | ||
| std::cout << "no solutions"; | ||
| return; | ||
| } | ||
|
|
||
| if (d > 0) { | ||
| x1 = ((-b - std::sqrt(d)) / (2.0 * a)); | ||
| x2 = ((-b + std::sqrt(d)) / (2.0 * a)); | ||
|
|
||
| if (x1 > x2) { | ||
| std::cout << x2 << " " << x1; | ||
| } else { | ||
| std::cout << x1 << " " << x2; | ||
| } | ||
|
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; | ||
| } | ||
|
|
||
| std::cout << -b / (2.0 * a); | ||
|
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 сделать без вложенности |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| #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 == NULL) { | ||
|
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 0.0; | ||
| } | ||
|
|
||
| double sum = 0.0; | ||
| for (size_t i = 0; i < size; ++i) { | ||
| sum += values[i] * values[i]; | ||
| } | ||
|
|
||
| return std::sqrt(sum / static_cast<double>(size)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,19 @@ | ||
| #include <stdexcept> | ||
|
|
||
| typedef double(*operations_t)(double, double); | ||
|
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++ коде принято использовать using, и выглядело бы понятнее |
||
|
|
||
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| double ApplyOperations(double a, double b, operations_t mathOperations[], size_t size) { | ||
| if (size == 0 || mathOperations == nullptr) { | ||
| return 0.0; | ||
| } | ||
|
|
||
| double sum = 0.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. лишняя пустая строка |
||
| for (size_t i = 0; i < size; ++i) { | ||
| if (mathOperations[i] != nullptr) { | ||
| sum += mathOperations[i](a, b); | ||
| } | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,26 @@ | ||
| #include <stdexcept> | ||
|
|
||
| typedef bool (*predicate_func_t)(int); | ||
|
|
||
| /* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| const int* FindLastElement(const int* begin, const int* end, predicate_func_t predicate) { | ||
| if (begin == nullptr || end == nullptr) { | ||
| return end; | ||
| } | ||
|
|
||
| if (begin >= end) { | ||
| 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. в данном случае можно объединить условия, поскольку возвращается указатель за последний элемент |
||
|
|
||
| const int* result = end; | ||
|
|
||
| // begin и end передаются by copy, так что инкремент указателя | ||
| // не повлияет на переданный указатель | ||
|
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. это лишний комментаарий |
||
| while (begin != end) { | ||
| if (predicate(*begin)) { | ||
| result = begin; | ||
| } | ||
| ++begin; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
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.
Лишний каст, второй операнд не следует приводить явно, так как он бы преобразовался с помощью неявного каста. Принято использовать такую возможность и не писать явный каст самостоятельно второй раз