-
Notifications
You must be signed in to change notification settings - Fork 33
Никонов Сергей #8
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
19015b8
c07a84f
5a48cac
6d62fdf
4f6aa17
9305fef
0b2043c
e573edc
982008f
50cb31e
bd91bd9
c654826
598856c
145ca6c
bbfb636
9315e8f
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,72 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| #include <cctype> | ||
|
|
||
| size_t CharChanger(char array[], size_t size, char delimiter = ' ') { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (size == 0) return 0; | ||
|
|
||
| size_t pos_read = 0; // позиция чтения | ||
| size_t pos_write = 0; // позиция записи | ||
|
|
||
| while (array[pos_read] != '\0') { // проверка на последний символ | ||
| char current_char = array[pos_read]; // изменяемый символ | ||
| size_t count = 1; | ||
|
|
||
| while (pos_read + count < size && array[pos_read + count] == current_char) { // подсчет повторающихся символов | ||
| ++count; | ||
| } | ||
|
|
||
| if (isdigit(current_char)) {// проверка на цифры | ||
| current_char = '*'; | ||
| } | ||
| else if (islower(current_char)) { // проверка на строчные буквы | ||
| current_char = toupper(current_char); | ||
| } | ||
| else if (!isupper(current_char) && current_char != ' ') { //остальные символы, кроме пробелов и прописных букв | ||
| current_char = '_'; | ||
| } | ||
|
|
||
| if (current_char == ' ') { // оработка пробелов | ||
| current_char = delimiter; | ||
| if (pos_write < size) { | ||
| array[pos_write] = current_char; | ||
| ++pos_write; | ||
| } | ||
| } | ||
|
|
||
| else { // обработка остальных символов | ||
| if (pos_write < size) { //записываем символ | ||
| array[pos_write] = current_char; | ||
| ++pos_write; | ||
| } | ||
| if ( count > 1) { ///обрабатываем повторения | ||
| if (count >= 10) { | ||
| if (pos_write < size) { | ||
| array[pos_write] = '0'; | ||
| ++pos_write; | ||
| } | ||
| } | ||
| else { | ||
| if (pos_write < size) { | ||
| array[pos_write] = '0' + count; | ||
| ++pos_write; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| pos_read += count; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // Добавляем завершающий нуль-символ | ||
| if (pos_write < size) { | ||
| array[pos_write] = '\0'; | ||
| } | ||
| else if (size > 0) { | ||
| array[size - 1] = '\0'; | ||
| pos_write = size - 1; | ||
| } | ||
|
|
||
| return pos_write; | ||
| } |
| 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,44 @@ 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; | ||
| } | ||
|
|
||
| // Обработка специального случая NONE | ||
| if (flags == CheckFlags::NONE) { | ||
| std::cout << "[]"; | ||
| return; | ||
| } | ||
|
|
||
| std::cout<< "["; | ||
| const char* comma = ""; | ||
| if ((value & static_cast<uint8_t>(CheckFlags::TIME)) !=0) { | ||
| std::cout<< comma << "TIME"; | ||
| comma = ","; | ||
| } | ||
| if ((value & static_cast<uint8_t>(CheckFlags::DATE)) !=0) { | ||
| std::cout<< comma << "DATE"; | ||
| comma = ","; | ||
| } | ||
| if ((value & static_cast<uint8_t>(CheckFlags::USER)) !=0) { | ||
| std::cout<< comma << "USER"; | ||
| comma = ","; | ||
| } | ||
| if ((value & static_cast<uint8_t>(CheckFlags::CERT)) !=0) { | ||
| std::cout<< comma << "CERT"; | ||
| comma = ","; | ||
| } | ||
| if ((value & static_cast<uint8_t>(CheckFlags::KEYS)) !=0) { | ||
| std::cout<< comma << "KEYS"; | ||
| comma = ","; | ||
| } | ||
| if ((value & static_cast<uint8_t>(CheckFlags::DEST)) !=0) { | ||
| std::cout<< comma << "DEST"; | ||
| comma = ","; | ||
| } | ||
|
Contributor
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,44 @@ | ||
|
|
||
| namespace length_const { | ||
| constexpr double METER = 1.0; | ||
| constexpr double CENTIMETER = 0.01; | ||
| constexpr double FOOT = 0.3048; | ||
| constexpr double INCH = 0.0254; | ||
| } | ||
|
Contributor
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. это хорошо, METER только лишний в данном случае |
||
|
|
||
| constexpr double operator""_ft_to_in(long double value) { | ||
| return value * length_const::FOOT / length_const::INCH; | ||
| } | ||
| constexpr double operator""_ft_to_cm(long double value) { | ||
| return value * length_const::FOOT / length_const::CENTIMETER; | ||
| } | ||
| constexpr double operator""_ft_to_m(long double value) { | ||
| return value * length_const::FOOT / length_const::METER; | ||
| } | ||
| constexpr double operator""_in_to_ft(long double value) { | ||
| return value * length_const::INCH / length_const::FOOT; | ||
| } | ||
| constexpr double operator""_in_to_cm(long double value) { | ||
| return value * length_const::INCH / length_const::CENTIMETER; | ||
| } | ||
| constexpr double operator""_in_to_m(long double value) { | ||
| return value * length_const::INCH / length_const::METER; | ||
| } | ||
| constexpr double operator""_cm_to_ft(long double value) { | ||
| return value * length_const::CENTIMETER / length_const::FOOT; | ||
| } | ||
| constexpr double operator""_cm_to_in(long double value) { | ||
| return value * length_const::CENTIMETER / length_const::INCH; | ||
| } | ||
| constexpr double operator""_cm_to_m(long double value) { | ||
| return value * length_const::CENTIMETER / length_const::METER; | ||
| } | ||
| constexpr double operator""_m_to_ft(long double value) { | ||
| return value * length_const::METER / length_const::FOOT; | ||
| } | ||
| constexpr double operator""_m_to_in(long double value) { | ||
| return value * length_const::METER / length_const::INCH; | ||
| } | ||
| constexpr double operator""_m_to_cm(long double value) { | ||
| return value * length_const::METER / length_const:: CENTIMETER; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| 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 = static_cast<int>(bytes * 8 - 1); i >= 0; i--) { | ||
|
Contributor
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. префиксный декремент должен быть |
||
| unsigned long long bit = (static_cast<unsigned long long>(value) >> i) & 1; | ||
| std::cout << bit; | ||
|
|
||
| if (i > 0 && i % 4 == 0) { | ||
| std::cout << "'"; | ||
| } | ||
| } | ||
| std::cout << std::endl; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,50 @@ | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <cmath> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| std::cout << std::defaultfloat << std::setprecision(6); | ||
|
|
||
| if (a == 0 && b == 0 && c == 0) { | ||
| std::cout << "infinite solutions"; | ||
| return; | ||
| } | ||
|
|
||
| if (a == 0) { | ||
| if (b == 0) { | ||
| std::cout << "no solutions"; | ||
| } | ||
| else { | ||
|
Contributor
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 и ещё оного уровня вложенности |
||
| double x = -static_cast<double>(c) / b; | ||
| if (x == -0) { | ||
| x = 0; | ||
| } | ||
| std::cout << x; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| double discriminant = static_cast<double>(b) * b - 4.0 * a * c; | ||
| if (discriminant < 0) { | ||
| std::cout << "no solutions"; | ||
|
Contributor
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, у последнего не было бы вложенности и не нужно было бы пролистать оставшийся код, чтоббы увидеть, что больше ничего в std::cout не добавляется общего для случаев |
||
| } | ||
| else if (discriminant == 0) { | ||
| double x = -static_cast<double>(b) / (2.0 * a); | ||
| if (x == -0) { | ||
| x = 0; | ||
| } | ||
| std::cout << x; | ||
| } | ||
| else { | ||
| double sqrt_d = std::sqrt(discriminant); | ||
| double x1 = (-static_cast<double>(b) - sqrt_d) / (2.0 * a); | ||
| double x2 = (-static_cast<double>(b) + sqrt_d) / (2.0 * a); | ||
|
|
||
| if (x1 > x2) { | ||
| std::swap(x1, x2); | ||
| } | ||
|
|
||
| std::cout << x1 << " " << x2; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,18 @@ | ||
| #include <cstdef> | ||
| #include <stdexcept> | ||
|
|
||
| #include <cmath> | ||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| if (size == 0 || values == nullptr) { | ||
|
Contributor
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, !values |
||
| return 0.0; | ||
| } | ||
|
|
||
| double sum_of_squares = 0.0; | ||
| for (size_t i = 0; i < size; ++i) { | ||
| sum_of_squares += values[i] * values[i]; | ||
| } | ||
|
|
||
| double mean_of_squares = sum_of_squares / size; | ||
| double rms = std::sqrt(mean_of_squares); | ||
|
|
||
| return rms; | ||
|
Contributor
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 (*operations[])(double, double), size_t size ) { | ||
| double sum = 0.0; | ||
|
Contributor
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 (operations == nullptr || size == 0) { | ||
| return 0.0; | ||
| } | ||
|
|
||
| for (size_t i = 0; i < size; ++i) { | ||
| if (operations[i] != nullptr) { | ||
| sum += operations[i](a, b); | ||
| } | ||
| } | ||
| return sum; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,14 @@ | ||
| #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 || begin == nullptr || begin > end || begin == end) { | ||
|
Contributor
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; | ||
| } | ||
| for (const int* ptr = end - 1; ptr >= begin; --ptr) { | ||
| if (predicate(*ptr)) { | ||
| return ptr; | ||
| } | ||
| } | ||
| return end; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,39 @@ | ||
| #include <stdexcept> | ||
| #include <cstring> | ||
| #include <iostream> | ||
| #include <cstdint> | ||
| #include <iomanip> | ||
|
|
||
|
|
||
| void PrintMemory(int /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(int value, bool reverse = false) { | ||
| uint32_t bytes; | ||
| std::memcpy(&bytes, &value, sizeof(value)); | ||
|
|
||
| if (reverse) { | ||
| std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8) | ||
| << bytes << "\n"; | ||
| } else { | ||
| uint32_t reversed = 0; | ||
| for (size_t i = 0; i < sizeof(value); ++i) { | ||
| reversed = (reversed << 8) | ((bytes >> (i * 8)) & 0xFF); | ||
| } | ||
| std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8) | ||
| << reversed << "\n"; | ||
| } | ||
| } | ||
|
|
||
| void PrintMemory(double /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| void PrintMemory(double value, bool reverse = false) { | ||
| uint64_t bytes; | ||
| std::memcpy(&bytes, &value, sizeof(value)); | ||
|
Contributor
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 и int третью функцию, куда передавать указатель, размер и флаг |
||
|
|
||
| if (reverse) { | ||
| std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16) | ||
| << bytes << "\n"; | ||
| } else { | ||
| uint64_t reversed = 0; | ||
| for (size_t i = 0; i < sizeof(value); ++i) { | ||
| reversed = (reversed << 8) | ((bytes >> (i * 8)) & 0xFF); | ||
| } | ||
| std::cout << "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16) | ||
| << reversed << "\n"; | ||
| } | ||
| } | ||
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.
две новые переменные создаватьизлишне, достаточно в return написать выражение, используя для одного из операндов static_cast