-
Notifications
You must be signed in to change notification settings - Fork 33
Карсканова Светлана #29
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
aa06b1f
240b2e7
8374322
73267df
4b96e10
71bc667
b570327
7be7e34
15175f3
392bcb2
8c32ee1
620df85
49b333b
2171651
46982b6
c8ec7eb
a7f167e
2e8f4d9
a67e355
bd129ad
0c30f21
edfd91a
7450931
319b6f3
5e9f0cf
ed641e9
43dcac3
465b6f1
3e84f50
e805cf1
109d2c5
8a5d6b8
2f9c89a
b9c1606
f952ce2
6e2d037
090484b
7132675
08992b7
1056207
3a3eb3f
befa598
6607112
fc2bec1
e32646e
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 @@ | ||
| build/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| #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); | ||
|
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,7 +1,45 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| #include <cctype> | ||
| #include <cstring> | ||
|
|
||
| size_t CharChanger(char array[], size_t size, char delimiter = ' ') { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| size_t w = 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. лучше расположить рядом с |
||
| auto map_char = [&](char ch) -> char { | ||
| unsigned char uc = ch; //в этом месте возникала ошибка, решено использовать unsigner | ||
|
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 (std::isspace(uc)) return delimiter; | ||
| if (std::isdigit(uc)) return '*'; | ||
| if (std::isalpha(uc)) return (char)std::toupper(uc); | ||
|
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-style каст, а использовать C++ касты, в данном случае static_cast |
||
| return '_'; }; | ||
| size_t r = 0; | ||
| while (r < size && array[r] != '\0') { | ||
| char current = array[r]; | ||
| char mapped = map_char(current); | ||
| size_t count = 1; | ||
| size_t next = r + 1; | ||
| while (next < size && array[next] != '\0') { | ||
| char next_char = array[next]; | ||
| char mapped_next = map_char(next_char); | ||
| if (mapped_next != mapped) break; | ||
| if (mapped == '*' || mapped == '_') { | ||
| if (current != next_char) break; | ||
| } else if (mapped >= 'A' && mapped <= 'Z') { | ||
| unsigned char curr_uc = current; | ||
| unsigned char next_uc = next_char; | ||
| if (std::islower(curr_uc) != std::islower(next_uc)) break; } | ||
| count++; | ||
| next++;} | ||
|
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.
|
||
| unsigned char current_uc = current; | ||
| bool is_space_seq = (mapped == delimiter) && std::isspace(current_uc); | ||
| if (is_space_seq) { | ||
| bool is_letter_delimiter = (delimiter >= 'A' && delimiter <= 'Z'); | ||
| if (is_letter_delimiter || w == 0 || array[w - 1] != delimiter) { | ||
| array[w++] = delimiter;} | ||
| } else { | ||
| array[w++] = mapped; | ||
| if (count > 1) { | ||
| char repeat_char = (count < 10) ? ('0' + count) : '0'; | ||
| array[w++] = repeat_char;}} | ||
| r += count; } | ||
| array[w] = '\0'; | ||
| return w; | ||
|
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,5 +1,5 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| enum class CheckFlags : uint8_t { | ||
|
|
@@ -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); | ||
| uint8_t all = static_cast<uint8_t>(CheckFlags::ALL); | ||
| if ((value & ~all) != 0) { | ||
| return; } | ||
|
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 == 0) { | ||
| std::cout << "[]"; | ||
| return;} | ||
| std::cout << "["; | ||
|
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. эта строка должна быть перед вызовами print_flag, так будет удобнее читать код |
||
| bool first = true; | ||
| auto print_flag = [&](CheckFlags f, const char* name) { | ||
| if (value & static_cast<uint8_t>(f)) { | ||
| if (!first) std::cout << ","; | ||
| std::cout << name; | ||
| first = false;} | ||
| }; | ||
| print_flag(CheckFlags::TIME, "TIME"); | ||
| print_flag(CheckFlags::DATE, "DATE"); | ||
| print_flag(CheckFlags::USER, "USER"); | ||
| print_flag(CheckFlags::CERT, "CERT"); | ||
| print_flag(CheckFlags::KEYS, "KEYS"); | ||
| print_flag(CheckFlags::DEST, "DEST"); | ||
| std::cout << "]"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #include <cstdint> | ||
|
|
||
|
|
||
| constexpr long double operator"" _ft_to_m(long double v) { | ||
| return static_cast<double>(v * 0.3048L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _ft_to_in(long double v) { | ||
| return static_cast<double>(v * 12.0L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _ft_to_cm(long double v) { | ||
| return static_cast<double>(v * 30.48L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _in_to_ft(long double v) { | ||
| return static_cast<double>(v / 12.0L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _in_to_m(long double v) { | ||
| return static_cast<double>(v * 0.0254L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _in_to_cm(long double v) { | ||
| return static_cast<double>(v * 2.54L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _cm_to_in(long double v) { | ||
| return static_cast<double>(v / 2.54L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _cm_to_ft(long double v) { | ||
| return static_cast<double>(v / 30.48L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _cm_to_m(long double v) { | ||
| return static_cast<double>(v / 100.0L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _m_to_cm(long double v) { | ||
| return static_cast<double>(v * 100.0L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _m_to_in(long double v) { | ||
| return static_cast<double>(v / 0.0254L); | ||
| } | ||
|
|
||
| constexpr long double operator"" _m_to_ft(long double v) { | ||
| return static_cast<double>(v / 0.3048L); | ||
| } | ||
|
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 value в коде (кроме 100, 1000 и т.п.), правильнее добавить константы, например, 12, 2.54, 30.48, видимые в данном файле с помощью безымянного namespace |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| #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"; | ||
| size_t total_bits = bytes * 8; | ||
| for (size_t i = 0; i < total_bits; ++i) { | ||
| size_t bit_index = total_bits - 1 - i; | ||
| bool bit = (value >> bit_index) & 1; | ||
| std::cout << (bit ? '1' : '0'); | ||
| if (i % 4 == 3 && i + 1 < total_bits) | ||
| std::cout << "'"; } | ||
| std::cout << "\n"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,39 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <cmath> | ||
|
|
||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| auto print = [](double x) { | ||
| if (std::abs(x) < 1e-9) x = 0; | ||
| if (std::abs(x - std::round(x)) < 1e-9) { | ||
| std::cout << static_cast<long long>(std::round(x)); | ||
| } else { | ||
| x = std::round(x * 1e6) / 1e6; //округление (тест) | ||
| std::cout << 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. в данном случае при выводе в поток, всё долджно было быть корректно и манипуляции дополнительные излишни |
||
| if (a == 0) { | ||
| if (b == 0) { | ||
| if (c == 0) std::cout << "infinite solutions"; | ||
| else 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. так компактнее конечно, но появляется несколько уровней вложенности, что не очень хорошо. Обычно принято отдельные особые случаи выделять короткими ветвями, в данной задаче лучше было бы разделить на отдельные условия для infinite solutions, no solutions и одного корня |
||
| } else { | ||
| double x = -static_cast<double>(c) / b; | ||
| print(x);} | ||
| return;} | ||
| double D = static_cast<double>(b) * b - 4.0 * a * c; | ||
| if (D < 0) { | ||
| std::cout << "no solutions"; | ||
| } else if (D == 0) { | ||
| double x = -static_cast<double>(b) / (2.0 * a); | ||
| print(x); | ||
| } else { | ||
| double sqrtD = std::sqrt(D); | ||
| double x1 = (-b - sqrtD) / (2.0 * a); | ||
| double x2 = (-b + sqrtD) / (2.0 * a); | ||
| if (x1 > x2) { | ||
| double temp = x1; | ||
| x1 = x2; | ||
| x2 = temp;} | ||
|
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. нагляднее и правильнее использовать готовую функцию |
||
| print(x1); | ||
| std::cout << " "; | ||
| print(x2);} | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| #include <cstdef> | ||
| #include <cstddef> | ||
| #include <cmath> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| if (size == 0) return 0.0; | ||
| if (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_squares = 0.0; | ||
| for (size_t i = 0; i < size; ++i) { | ||
| sum_squares += values[i] * values[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. закрывающая скобка на новой строке |
||
| return std::sqrt(sum_squares / static_cast<double>(size)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,12 @@ | ||
| #include <stdexcept> | ||
| #include <cstddef> | ||
|
|
||
|
|
||
| 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) { | ||
| if (size == 0 || operations == nullptr) { | ||
| return 0.0;} | ||
| double sum = 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,12 @@ | ||
| #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) { | ||
| return end;} | ||
| const int* result = end; | ||
| for (const int* ptr = begin; ptr < end; ++ptr) { | ||
| if (predicate(*ptr)) { | ||
| result = ptr;} | ||
| } | ||
| return result; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,34 @@ | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <stdexcept> | ||
|
|
||
| void PrintMemory(int value, bool reverse = false) {// Для int | ||
| unsigned char* bytes = reinterpret_cast<unsigned char*>(&value); | ||
| std::cout << "0x" << std::uppercase; | ||
| if (reverse) { | ||
| for (size_t i = sizeof(int); i > 0; --i) { | ||
| std::cout << std::hex << std::setw(2) << std::setfill('0') | ||
| << static_cast<int>(bytes[i - 1]); } | ||
| } else { | ||
| for (size_t i = 0; i < sizeof(int); ++i) { | ||
| std::cout << std::hex << std::setw(2) << std::setfill('0') | ||
| << static_cast<int>(bytes[i]); } | ||
| } | ||
| std::cout << std::dec << std::endl; | ||
| } | ||
|
|
||
| void PrintMemory(int /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintMemory(double value, bool reverse = false) {// Для double | ||
| unsigned char* bytes = reinterpret_cast<unsigned char*>(&value); | ||
| std::cout << "0x" << std::uppercase; | ||
| if (reverse) { | ||
| for (size_t i = sizeof(double); i > 0; --i) { | ||
| std::cout << std::hex << std::setw(2) << std::setfill('0') | ||
| << static_cast<int>(bytes[i - 1]); } | ||
| } else { | ||
| for (size_t i = 0; i < sizeof(double); ++i) { | ||
| std::cout << std::hex << std::setw(2) << std::setfill('0') | ||
| << static_cast<int>(bytes[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. в данном случае тоже дублирование, можно было использовать тернарный оператор в нужных местах, либо вынести настройку std::cout |
||
| } | ||
| 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. В данном случае практически полное дублирование кода. После приведения к указателю на символ и зная sizeof от типа, правильнее было вызвать третью функцию которая уже печатает требуемое представление |
||
|
|
||
| void PrintMemory(double /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,32 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| char* FindLongestSubsequence(char* begin, char* end, size_t& count) { | ||
| if (!begin || !end || begin >= end) { | ||
| count = 0; | ||
| return nullptr; } | ||
| char* max_start = begin; | ||
| size_t max_len = 1; | ||
| char* current_start = begin; | ||
| size_t current_len = 1; | ||
| for (char* ptr = begin + 1; ptr < end; ++ptr) { | ||
| if (*ptr == *(ptr - 1)) { | ||
| ++current_len;} | ||
| else { | ||
| if (current_len > max_len) { | ||
| max_len = current_len; | ||
| max_start = current_start; | ||
| } | ||
| current_start = ptr; | ||
| current_len = 1;} | ||
| } | ||
|
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 (current_len > max_len) { | ||
| max_len = current_len; | ||
| max_start = current_start;} | ||
| count = max_len; | ||
| return max_start; | ||
| } | ||
|
|
||
| /* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| const char* FindLongestSubsequence(const char* begin, const char* end, size_t& 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. немного некорректное описание проблемы |
||
| return FindLongestSubsequence(const_cast<char*>(begin), const_cast<char*>(end), 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. Это некорректно, это UB, должно быть наоборот, снимать const можно, если изначально был объект не конст, то есть из версии без константности, можно вызвать константную (где весь код из текущей неконстантной версии), а затем снять константность, но никак не наоборот, в данном случае это UB |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
лишняя строка