-
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
57d12f5
eea9f87
939790e
8fa735a
a6df4eb
876338a
adf1073
8f2fe09
d5f22a7
f116bf5
ea3bb07
160fb37
b7613d4
7d7117b
450cc1a
9a5d1b1
5784369
15c75f2
700b226
fed1b0f
5baf83c
ddc54ba
f2abe8b
890adb7
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) + b; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,47 @@ | ||
| #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 counterRepeatedSymbols = 1; // Счётчик повторяющихся символов | ||
| int pos_write = 0; // Индекс для записи обработанного символа | ||
| char repeating_symbol = array[0]; // В процессе выполнения - предыдущий символ(отслеживаем повторения) | ||
|
|
||
| for (char* ptr = array + 1; repeating_symbol != '\0'; ++ptr) { | ||
| if (repeating_symbol == *ptr){ | ||
| ++counterRepeatedSymbols; | ||
| continue; | ||
| } | ||
|
|
||
| if (isalpha(repeating_symbol)){ | ||
| array[pos_write] = toupper(repeating_symbol); | ||
| } else if (isdigit(repeating_symbol)){ | ||
| array[pos_write] = '*'; | ||
| } else if (repeating_symbol == ' '){ | ||
| array[pos_write] = delimiter; | ||
| } else { | ||
| array[pos_write] = '_'; | ||
| } | ||
| ++pos_write; | ||
|
|
||
|
|
||
| if (repeating_symbol == ' ') { | ||
| counterRepeatedSymbols = 1; | ||
| } | ||
|
|
||
| if (counterRepeatedSymbols >= 10){ | ||
| counterRepeatedSymbols = 0; | ||
| } | ||
|
|
||
| if (counterRepeatedSymbols != 1){ | ||
| array[pos_write] = static_cast<char>(counterRepeatedSymbols + '0'); // Преобразуем число в символ | ||
| counterRepeatedSymbols = 1; | ||
| ++pos_write; | ||
| } | ||
|
|
||
| repeating_symbol = *ptr; | ||
| } | ||
|
|
||
| array[pos_write] = '\0'; | ||
| return pos_write; | ||
| } | ||
| 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){ | ||
| 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 |
|---|---|---|
| @@ -1,6 +1,25 @@ | ||
| #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 = end; | ||
|
|
||
| while (begin < end) | ||
| { | ||
| --end; | ||
| if (predicate(*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. можно упростить алгоритм поскольку нам нужен последний, быстрее идти с конца и при первом срабатывании сразу возвращать из функции нужный указатель |
||
| } | ||
|
|
||
| return last_element; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,47 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <cstring> | ||
| #include <algorithm> | ||
|
|
||
|
|
||
| void PrintMemory(int /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(int number, bool inversion = false) { | ||
| unsigned char* bytes = reinterpret_cast<unsigned char*>(&number); | ||
|
|
||
| std::cout << "0x"; | ||
| if (inversion) { | ||
| // Читаем байты в обратном порядке сразу при выводе | ||
| for (size_t i = sizeof(int); i > 0; --i) { | ||
| std::cout << std::hex << std::uppercase | ||
| << std::setfill('0') << std::setw(2) | ||
| << static_cast<unsigned int>(bytes[i-1]); | ||
| } | ||
| } else { | ||
| for (size_t i = 0; i < sizeof(int); ++i) { | ||
| std::cout << std::hex << std::uppercase | ||
| << std::setfill('0') << std::setw(2) | ||
| << static_cast<unsigned int>(bytes[i]); | ||
| } | ||
| } | ||
| std::cout << std::dec << std::endl; | ||
| } | ||
|
|
||
| void PrintMemory(double /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(double number, bool inversion = false) { | ||
| unsigned char* bytes = reinterpret_cast<unsigned char*>(&number); | ||
|
|
||
| std::cout << "0x"; | ||
| if (inversion) { | ||
| // Читаем байты в обратном порядке сразу при выводе | ||
| for (size_t i = sizeof(double); i > 0; --i) { | ||
| std::cout << std::hex << std::uppercase | ||
| << std::setfill('0') << std::setw(2) | ||
| << static_cast<unsigned int>(bytes[i-1]); | ||
| } | ||
| } else { | ||
| for (size_t i = 0; i < sizeof(double); ++i) { | ||
| std::cout << std::hex << std::uppercase | ||
| << std::setfill('0') << std::setw(2) | ||
| << static_cast<unsigned int>(bytes[i]); | ||
| } | ||
| } | ||
| std::cout << std::dec << 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. Это задание было конечно на |
||
| } | ||
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.
лучше для
ifсделать короткую ветвь сcontinue:forelseи лишний уровень вложенности