-
Notifications
You must be signed in to change notification settings - Fork 33
Петров Николай #37
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?
Петров Николай #37
Changes from all commits
83ca1d7
a133a8d
f593781
8c8b1df
ae14a5a
4c4bbdf
5815706
6201fcf
4620677
b73b9b8
34a9a4e
9930417
53e1f09
1877c2b
df4918e
72c0db7
0ea827e
fdb1b70
f04d35e
2caaf16
728bdb1
6693bc7
a91e20f
e4133b8
0adb815
891f816
df82b9c
1c9157c
66f8597
33c8359
f591253
be5fe75
413666d
1b507a1
6dda849
baaa204
0529a82
73054a8
6b185e6
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 |
|---|---|---|
|
|
@@ -3,5 +3,55 @@ | |
|
|
||
|
|
||
| size_t CharChanger(char array[], size_t size, char delimiter = ' ') { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (size == 0) return 0; | ||
|
|
||
| char* pread = array; | ||
| char* pwrite = array; | ||
|
|
||
| while (*pread != '\0' && (pread - array) < static_cast<ptrdiff_t>(size - 1)) { | ||
| char tek_char = *pread; | ||
| int repeat_count = 1; | ||
|
|
||
| // Ограничиваем подсчет повторений границами массива | ||
| while (repeat_count < static_cast<int>(size - (pread - array)) && | ||
| pread[repeat_count] == tek_char) { | ||
| repeat_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. в таких случаях должен быть префиксный инкремент, везде, где не нужны свойства постфиксного, который возвращает копию |
||
| } | ||
|
|
||
| if (tek_char == ' ') { | ||
| *pwrite = delimiter; | ||
| pwrite++; | ||
| pread += repeat_count; | ||
| continue; | ||
| } | ||
|
|
||
| char temp; | ||
| if (std::isdigit(tek_char)) { | ||
| temp = '*'; | ||
| } else if (std::isupper(tek_char)) { | ||
| temp = tek_char; | ||
| } else if (std::islower(tek_char)) { | ||
| temp = std::toupper(tek_char); | ||
| } else { | ||
| temp = '_'; | ||
| } | ||
|
|
||
| *pwrite = temp; | ||
| pwrite++; | ||
|
|
||
| if (repeat_count > 1) { | ||
| if (repeat_count >= 10) { | ||
| *pwrite = '0'; | ||
| pwrite++; | ||
| } else { | ||
| *pwrite = '0' + repeat_count; | ||
| pwrite++; | ||
| } | ||
| } | ||
|
|
||
| pread += repeat_count; | ||
| } | ||
|
|
||
| *pwrite = '\0'; | ||
| return pwrite - array; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
|
|
||
| #include <iostream> | ||
|
|
||
| enum class CheckFlags : uint8_t { | ||
| NONE = 0, | ||
|
|
@@ -14,5 +14,26 @@ 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 > static_cast<uint8_t>(CheckFlags::ALL)) { | ||
| return; // значение вне диапазона - ничего не выводим | ||
| } | ||
|
|
||
| if (value == 0) { | ||
| std::cout << "[]"; // нет флагов - выводим [] | ||
| return; | ||
| } | ||
|
|
||
| std::cout << "["; | ||
|
|
||
| if (value & 1) std::cout << "TIME"; | ||
| if (value & 2) std::cout << (value & 1 ? ",DATE" : "DATE"); | ||
|
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 (value & 4) std::cout << (value & 3 ? ",USER" : "USER"); | ||
| if (value & 8) std::cout << (value & 7 ? ",CERT" : "CERT"); | ||
| if (value & 16) std::cout << (value & 15 ? ",KEYS" : "KEYS"); | ||
| if (value & 32) std::cout << (value & 31 ? ",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. интересное решение |
||
|
|
||
| std::cout << "]"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| constexpr double operator""_ft_to_m(long double feet) { | ||
| return feet * 0.3048; | ||
|
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 values не принято использовать в коде, корректнее было определить константы 0.3048 0.0254 0.01 например и выполнять преобразования с их использованием, можно даже разместить их в безымянном namespace, чтобы область видимости была только в данной единице трансляции |
||
| } | ||
|
|
||
| constexpr double operator""_in_to_m(long double inches) { | ||
| return inches * 0.0254; | ||
| } | ||
|
|
||
| constexpr double operator""_cm_to_m(long double centimeters) { | ||
| return centimeters * 0.01; | ||
| } | ||
|
|
||
| constexpr double operator""_m_to_ft(long double meters) { | ||
| return meters / 0.3048; | ||
| } | ||
|
|
||
| constexpr double operator""_m_to_in(long double meters) { | ||
| return meters / 0.0254; | ||
| } | ||
|
|
||
| constexpr double operator""_m_to_cm(long double meters) { | ||
| return meters / 0.01; | ||
| } | ||
|
|
||
| constexpr double operator""_ft_to_cm(long double feet) { | ||
| return operator""_m_to_cm(operator""_ft_to_m(feet)); | ||
| } | ||
|
|
||
| constexpr double operator""_ft_to_in(long double feet) { | ||
| return operator""_m_to_in(operator""_ft_to_m(feet)); | ||
| } | ||
|
|
||
| constexpr double operator""_in_to_ft(long double inches) { | ||
| return operator""_m_to_ft(operator""_in_to_m(inches)); | ||
| } | ||
|
|
||
| constexpr double operator""_in_to_cm(long double inches) { | ||
| return operator""_m_to_cm(operator""_in_to_m(inches)); | ||
| } | ||
|
|
||
| constexpr double operator""_cm_to_ft(long double centimeters) { | ||
| return operator""_m_to_ft(operator""_cm_to_m(centimeters)); | ||
| } | ||
|
|
||
| constexpr double operator""_cm_to_in(long double centimeters) { | ||
| return operator""_m_to_in(operator""_cm_to_m(centimeters)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,30 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| #include <cstddef> | ||
| #include <iostream> | ||
|
|
||
| void PrintBits(long long value, size_t bytes) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (bytes < 1 || bytes > 8) { | ||
| return; | ||
| } | ||
|
|
||
| std::cout << "0b"; | ||
|
|
||
| size_t total_bits = bytes * 8; | ||
| size_t bits_printed = 0; | ||
|
|
||
| // Проходим по всем битам от старшего к младшему | ||
| for (int byte = bytes - 1; byte >= 0; --byte) { | ||
| for (int bit = 7; bit >= 0; --bit) { | ||
| // Получаем текущий бит (сдвигаем и маскируем) | ||
| long long current_bit = (value >> (byte * 8 + bit)) & 1; | ||
| std::cout << current_bit; | ||
| bits_printed++; | ||
|
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 (bits_printed % 4 == 0 && bits_printed < total_bits) { | ||
| std::cout << "'"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| std::cout << std::endl; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,60 @@ | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <cmath> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| // a,b,c=0 | ||
| if (a == 0 && b == 0 && c == 0) { | ||
| std::cout << "infinite solutions"; | ||
| return; | ||
| } | ||
|
|
||
| // a=0 | ||
| if (a == 0) { | ||
| if (b == 0) { | ||
| // c=0 | ||
| if (c == 0) { | ||
| std::cout << "infinite 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. лучше преобразовать в короткие ветви по условиям, с выходом из функции, так намного удобнее читать, чем вложенность, каждый особый случай обрабатывать отдельно, будет немного больше сравнений с 0, но они достаточно дешевые, а читаемость значительно повысится: if (a == 0 && b == 0 && c == 0) {
std::cout << "infinite solutions";
return;
}в принципе можно оптимизировать немного, но отдельные проверки лучше читаются if (a == 0 && b == 0) {
std::cout << (c == 0 ? "infinite solutions" : "no solutions");
return;
}
if (a == 0) {
// b*x+c=0
...
} |
||
| } else { | ||
| std::cout << "no solutions"; | ||
| } | ||
| } else { | ||
| // b*x+c=0 | ||
| double x = -static_cast<double>(c) / b; | ||
| // Обработка случая -0.0 | ||
| if (std::abs(x) < 1e-10) x = 0.0; | ||
| std::cout << std::setprecision(6) << x; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| // Вычисление D | ||
| double discriminant = static_cast<double>(b) * b - 4.0 * a * c; | ||
|
|
||
| if (discriminant < 0) { | ||
| // D<0 | ||
| 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. лучше в коротких ветвях ставить return, а дальше убирать else из условий, это лучше тем, что не нужно листать дальше код, чтобы узнать, что больше к выводу ничего не добавляется |
||
| } else if (discriminant == 0) { | ||
| // D=0 | ||
| double x = -static_cast<double>(b) / (2.0 * a); | ||
| // Обработка случая -0.0 | ||
| if (std::abs(x) < 1e-10) x = 0.0; | ||
| std::cout << std::setprecision(6) << x; | ||
| } else { | ||
| // D>0 | ||
| double sqrt_d = std::sqrt(discriminant); | ||
| double x1 = (-b - sqrt_d) / (2.0 * a); | ||
| double x2 = (-b + sqrt_d) / (2.0 * a); | ||
|
|
||
| // Обработка случая -0.0 для обоих корней | ||
| if (std::abs(x1) < 1e-10) x1 = 0.0; | ||
| if (std::abs(x2) < 1e-10) x2 = 0.0; | ||
|
|
||
| if (x1 > x2) { | ||
| std::swap(x1, x2); | ||
| } | ||
|
|
||
| std::cout << std::setprecision(6) << x1 << " " << x2; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| #include <cstdef> | ||
| #include <stdexcept> | ||
|
|
||
| #include <cmath> | ||
| #include <cstddef> | ||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (size == 0 || values == nullptr) { | ||
| return 0.0; | ||
| } | ||
|
|
||
| // Вычисление суммы квадратов элементов | ||
| double sum1 = 0.0; | ||
| for (size_t i = 0; i < size; ++i) { | ||
| sum1 += values[i] * values[i]; | ||
| } | ||
|
|
||
| // Вычисление среднеквадратического значения | ||
| double squares = sum1 / 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. имена переменных странные всё-таки sum - это уже сумма квадратов, а здесь мы просто делим на количество. зачем нужна 1 в имени также не понятно |
||
| double rms = std::sqrt(squares); | ||
|
|
||
| return rms; | ||
|
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 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,17 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| double ApplyOperations(double a, double b, double (**ops)(double, double), size_t size) { | ||
| if (ops == nullptr || size == 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. указатели часто бывает принято проверять без явного сравнения, а используя неявное приведение к типу bool |
||
| return 0.0; | ||
| } | ||
|
|
||
| double sum = 0.0; | ||
| for (size_t i = 0; i < size; ++i) { | ||
| if (ops[i] != nullptr) { | ||
| sum += ops[i](a, b); | ||
| } | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,17 @@ | ||
| #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 > end || begin == end || end == nullptr || begin == nullptr) { | ||
| return end; | ||
| } | ||
| const int* p_pred = end; | ||
|
|
||
| for (size_t i = 1; (end-i) != (begin-1); ++i) { | ||
| if (predicate(*(end-i))) { | ||
| p_pred = end-i; | ||
| return p_pred; | ||
| } | ||
| } | ||
| return p_pred; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,46 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <cstring> | ||
|
|
||
|
|
||
| void PrintMemory(int /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(int value, bool invert = false) { | ||
| //memcpy копирует байты из числа в массив | ||
| unsigned char bytes[sizeof(int)]; | ||
| std::memcpy(bytes, &value, sizeof(int)); | ||
|
|
||
| std::cout << "0x" << std::uppercase << std::hex; | ||
|
|
||
| if (!invert) { | ||
| for (size_t i = 0; i < sizeof(int); ++i) { | ||
| unsigned int byte_value = bytes[i]; | ||
| std::cout << std::setw(2) << std::setfill('0') << byte_value; | ||
| } | ||
| } else { | ||
| for (int i = sizeof(int) - 1; i >= 0; --i) { | ||
| unsigned int byte_value = bytes[i]; | ||
| std::cout << std::setw(2) << std::setfill('0') << byte_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. дублируется тело функции, можно было подумать как этого избежать |
||
| } | ||
|
|
||
| std::cout << std::endl; | ||
| } | ||
|
|
||
| void PrintMemory(double /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(double value, bool invert = false) { | ||
| unsigned char bytes[sizeof(double)]; | ||
| std::memcpy(bytes, &value, sizeof(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. после это строки идет полное дублирование, можно было вынести код в третью функцию принимающую флаг, указатель на массив и размер типа sizeof и тогда избавится от дублирования вызывая эту функцию как из версии для int и double |
||
| std::cout << "0x" << std::uppercase << std::hex; | ||
|
|
||
| if (!invert) { | ||
| for (size_t i = 0; i < sizeof(double); ++i) { | ||
| unsigned int byte_value = bytes[i]; | ||
| std::cout << std::setw(2) << std::setfill('0') << byte_value; | ||
| } | ||
| } else { | ||
| for (int i = sizeof(double) - 1; i >= 0; --i) { | ||
| unsigned int byte_value = bytes[i]; | ||
| std::cout << std::setw(2) << std::setfill('0') << byte_value; | ||
| } | ||
| } | ||
|
|
||
| std::cout << std::endl; | ||
| } | ||
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.
Лишний каст, второй операнд не следует приводить явно, так как он бы преобразовался с помощью неявного каста. Принято использовать такую возможность и не писать явный каст самостоятельно второй раз