-
Notifications
You must be signed in to change notification settings - Fork 33
Михайлов Илья #27
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?
Михайлов Илья #27
Changes from all commits
520e4ac
01cb3a4
d453f8b
6bf278a
7030e8b
a4bfdbf
1a22f2b
5aa3581
7505ec7
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,7 +1,6 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| int64_t Addition(int a, int b) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| return static_cast<int64_t>(a) + static_cast<int64_t>(b); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,49 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <cctype> | ||
|
|
||
|
|
||
| size_t CharChanger(char array[], size_t size, char delimiter = ' ') { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| size_t CharChanger(char array[], size_t, char delimiter = ' ') { | ||
| int counter = 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. Лучше, чтобы название отражало то, что написанно в комментариях |
||
| int write = 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. В данном случае это позиция, не указатель, лучше не вводить в замешательство, а еще может лучше добавть префикс |
||
| int read = 0; // Указатель для чтения следующего элемента из массива | ||
| char repeating_symbol = array[read]; // В процессе выполнения - редыдущий символ после read(отслеживаем повторения) | ||
|
|
||
| while (repeating_symbol != '\0'){ | ||
| if (repeating_symbol == array[read]){ | ||
| counter++; | ||
|
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. лучше для
|
||
| if (isalpha(repeating_symbol)){ | ||
| array[write] = toupper(repeating_symbol); | ||
| } else if (isdigit(repeating_symbol)){ | ||
| array[write] = '*'; | ||
| } else if (repeating_symbol == ' '){ | ||
| array[write] = delimiter; | ||
| } else { | ||
| array[write] = '_'; | ||
| } | ||
|
|
||
| write++; | ||
|
|
||
| if (repeating_symbol == ' ') { | ||
| counter = 1; | ||
| } | ||
|
|
||
| if (counter >= 10){ | ||
| counter = 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 (counter != 1){ | ||
| array[write] = static_cast<char>(counter + '0'); // Преобразуем число в символ | ||
| counter = 1; | ||
| write++; | ||
| } | ||
|
|
||
| repeating_symbol = array[read]; | ||
| } | ||
|
|
||
| read++; | ||
| } | ||
|
|
||
| array[write] = '\0'; | ||
| return write; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <string> | ||
|
|
||
|
|
||
| enum class CheckFlags : uint8_t { | ||
|
|
@@ -14,5 +16,42 @@ enum class CheckFlags : uint8_t { | |
| }; | ||
|
|
||
| void PrintCheckFlags(CheckFlags flags) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| if (flags == CheckFlags::NONE) { | ||
| std::cout << "[]"; | ||
| return; | ||
| } | ||
|
|
||
| if ((static_cast<uint8_t>(flags) & ~static_cast<uint8_t>(CheckFlags::ALL)) != 0) { | ||
| std::cout << ""; | ||
| return; | ||
| } | ||
|
|
||
| std::vector<std::string> set_flags; | ||
|
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. Выглядит очень не оптимально, во первых вектор будет вызывать реалокацию при push_back. |
||
| if ((static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::TIME)) != 0) { | ||
| set_flags.push_back("TIME"); | ||
| } | ||
| if ((static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::DATE)) != 0) { | ||
| set_flags.push_back("DATE"); | ||
| } | ||
| if ((static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::USER)) != 0) { | ||
| set_flags.push_back("USER"); | ||
| } | ||
| if ((static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::CERT)) != 0) { | ||
| set_flags.push_back("CERT"); | ||
| } | ||
| if ((static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::KEYS)) != 0) { | ||
| set_flags.push_back("KEYS"); | ||
| } | ||
| if ((static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::DEST)) != 0) { | ||
| set_flags.push_back("DEST"); | ||
| } | ||
|
|
||
| std::cout << "["; | ||
| for (size_t i = 0; i < set_flags.size(); i++) { | ||
| std::cout << set_flags[i]; | ||
| if (i < set_flags.size() - 1) { | ||
| std::cout << ","; | ||
| } | ||
| } | ||
| std::cout << "]"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| #include <string> | ||
|
|
||
|
|
||
| // Константы для преобразования | ||
| constexpr long double IN_TO_CM = 2.54L; | ||
| constexpr long double FT_TO_IN = 12.0L; | ||
| constexpr long double M_TO_CM = 100.0L; | ||
|
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. Это правильный подход, добавить константы |
||
|
|
||
| // ft -> in | ||
| double operator"" _ft_to_in(long double ft_value) { | ||
| return static_cast<double>(ft_value * FT_TO_IN); | ||
| } | ||
|
|
||
| // ft -> cm | ||
| double operator"" _ft_to_cm(long double ft_value) { | ||
| return static_cast<double>(ft_value * FT_TO_IN * IN_TO_CM); | ||
| } | ||
|
|
||
| // ft -> m | ||
| double operator"" _ft_to_m(long double ft_value) { | ||
| return static_cast<double>(ft_value * FT_TO_IN * IN_TO_CM / M_TO_CM); | ||
| } | ||
|
|
||
| // in -> ft | ||
| double operator"" _in_to_ft(long double in_value) { | ||
| return static_cast<double>(in_value / FT_TO_IN); | ||
| } | ||
|
|
||
| // in -> cm | ||
| double operator"" _in_to_cm(long double in_value) { | ||
| return static_cast<double>(in_value * IN_TO_CM); | ||
| } | ||
|
|
||
| // in -> m | ||
| double operator"" _in_to_m(long double in_value) { | ||
| return static_cast<double>(in_value * IN_TO_CM / M_TO_CM); | ||
| } | ||
|
|
||
| // cm -> ft | ||
| double operator"" _cm_to_ft(long double cm_value) { | ||
| return static_cast<double>(cm_value / IN_TO_CM / FT_TO_IN); | ||
| } | ||
|
|
||
| // cm -> in | ||
| double operator"" _cm_to_in(long double cm_value) { | ||
| return static_cast<double>(cm_value / IN_TO_CM); | ||
| } | ||
|
|
||
| // cm -> m | ||
| double operator"" _cm_to_m(long double cm_value) { | ||
| return static_cast<double>(cm_value / M_TO_CM); | ||
| } | ||
|
|
||
| // m -> ft | ||
| double operator"" _m_to_ft(long double m_value) { | ||
| return static_cast<double>(m_value * M_TO_CM / IN_TO_CM / FT_TO_IN); | ||
| } | ||
|
|
||
| // m -> in | ||
| double operator"" _m_to_in(long double m_value) { | ||
| return static_cast<double>(m_value * M_TO_CM / IN_TO_CM); | ||
| } | ||
|
|
||
| // m -> cm | ||
| double operator"" _m_to_cm(long double m_value) { | ||
| return static_cast<double>(m_value * M_TO_CM); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,27 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
|
|
||
| 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 = bytes * 8 - 1; i >= 0; --i) { | ||
| // "Выталкиваем" все биты, кроме i-ого, так чтобы i-ый был последним и выводим этот бит | ||
| if ((value >> i) & 1) { | ||
| 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. Можно 5 строк заменитьтернарным оператор выводить в поток по условию 1 или 0, будет компактнее |
||
|
|
||
| if (i != 0 && i % 4 == 0) { | ||
| std::cout << "'"; | ||
| } | ||
| } | ||
| std::cout << '\n'; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,35 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <cmath> | ||
| #include <iomanip> | ||
| #include <algorithm> // Для std::min и std::max | ||
|
|
||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (a == 0) { | ||
| if (b == 0) { | ||
| if (c == 0) { | ||
| std::cout << "infinite solutions"; | ||
| } else { | ||
| std::cout << "no solutions"; | ||
| } | ||
| } else { | ||
| double x = static_cast<double>(-c) / b; | ||
| std::cout << std::defaultfloat << std::setprecision(6) << x; | ||
| } | ||
|
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 { | ||
| long long discriminant = static_cast<long long>(b) * b - 4 * static_cast<long long>(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. Понятно для чего так сделано, но не лучше хранить дискриминант в |
||
|
|
||
| if (discriminant > 0) { | ||
| double x1 = (-b - std::sqrt(static_cast<double>(discriminant))) / (2 * a); | ||
| double x2 = (-b + std::sqrt(static_cast<double>(discriminant))) / (2 * a); | ||
| std::cout << std::defaultfloat << std::setprecision(6) << std::min(x1, x2); | ||
| std::cout << " "; | ||
| std::cout << std::defaultfloat << std::setprecision(6) << std::max(x1, x2); | ||
| } else if (discriminant == 0) { | ||
| double x = static_cast<double>(-b) / (2 * a); | ||
| std::cout << std::defaultfloat << std::setprecision(6) << x; | ||
| } else { | ||
| std::cout << "no solutions"; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,14 @@ | ||
| #include <cstdef> | ||
| #include <stdexcept> | ||
| #include <cstddef> | ||
| #include <cmath> | ||
|
|
||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (size <= 0 || values == nullptr){ return 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. отсутствует пробел перед |
||
|
|
||
| double sum = 0; | ||
| for (size_t i=0; i < size; 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. Нужно использовать префиксный инкремент |
||
| sum+=std::pow(values[i], 2); | ||
|
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::sqrt(sum / size); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,19 @@ | ||
| #include <stdexcept> | ||
| #include <cstddef> | ||
|
|
||
| using funcPtr = double (*)(double, double); | ||
|
|
||
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| double ApplyOperations(const double a, const double b, funcPtr* arr, size_t funcArraySize) { | ||
|
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 (arr == nullptr || funcArraySize <= 0){ | ||
| return 0.0; | ||
| } | ||
|
|
||
| double cumulative_result = 0; | ||
| for (unsigned int i=0; i < funcArraySize; ++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. пробелы вокруг |
||
| if (arr[i] == nullptr){ | ||
| continue; | ||
| } | ||
| cumulative_result+=arr[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 cumulative_result; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| элемент, удовлетворяющий условию функции предиката. Функция принимает два | ||
| указателя на начало и конец целочисленного массива, а также указатель на | ||
| функцию предикат, и возвращает указатель на найденный элемент. Если элемент | ||
| не найден, то возвращается указатель за последний элемент. | ||
| не найден, то возвращается указатель на последний элемент. | ||
|
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. Это некорректное исправление так как, end не указывает на элемент и его нельзя разыменовывать |
||
|
|
||
| Указатели соответствуют диапазону $[begin, end)$. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,30 @@ | ||
| #include <stdexcept> | ||
| #include <cstddef> | ||
|
|
||
| using funcPtr = bool (*)(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, funcPtr predicate) { | ||
| if (begin == nullptr || end == nullptr || predicate == nullptr){ | ||
| return end; | ||
| } | ||
|
|
||
| if (begin >= end){ | ||
| return end; | ||
| } | ||
|
|
||
| const int* last_element = nullptr; | ||
|
|
||
| while (begin < end) | ||
| { | ||
| if (predicate(*begin)){ | ||
| last_element = begin; | ||
| } | ||
|
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. можно упростить алгоритм поскольку нам нужен последний, быстрее идти с конца и при первом срабатывании сразу возвращать из функции нужный указатель |
||
|
|
||
| ++begin; | ||
| } | ||
|
|
||
| if (last_element == 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. вероятно это лишняя проверка, так как можно |
||
|
|
||
| return last_element; | ||
| } | ||
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.
Лишний каст, второй операнд не следует приводить явно, так как он бы преобразовался с помощью неявного каста. Принято использовать такую возможность и не писать явный каст самостоятельно второй раз