-
Notifications
You must be signed in to change notification settings - Fork 33
Кайгородов Александр #28
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
dec7e67
06c5afb
a3f7150
9d3ad6a
717cd0f
0a4e403
c0bd5f4
4eefb34
cb16913
137ed90
6736536
6b59a4a
8d44aa3
a1d5e06
4f34559
7d78143
26cca11
71022cd
0a48c21
3b7f11e
d3c7972
d113d10
f138cee
f971b6d
4c2320e
0a562d0
53346cf
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,18 +1,66 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| enum class CheckFlags : uint8_t { | ||
| NONE = 0, | ||
| TIME = (1 << 0), | ||
| DATE = (1 << 1), | ||
| USER = (1 << 2), | ||
| CERT = (1 << 3), | ||
| KEYS = (1 << 4), | ||
| DEST = (1 << 5), | ||
| NONE = 0, //0 | ||
| TIME = (1 << 0), //1 | ||
| DATE = (1 << 1), //2 | ||
| USER = (1 << 2), //4 | ||
| CERT = (1 << 3), //8 | ||
| KEYS = (1 << 4), //16 | ||
| DEST = (1 << 5), //32 | ||
| ALL = TIME | DATE | USER | CERT | KEYS | DEST | ||
| }; | ||
|
|
||
| void PrintCheckFlags(CheckFlags flags) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (flags <= CheckFlags::ALL && flags >= CheckFlags::NONE){ | ||
|
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 << "["; | ||
| bool prev_print = 0; | ||
| if (static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::TIME)){ | ||
|
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 (prev_print == 1){ | ||
| std::cout << ","; | ||
| } | ||
| std::cout << "TIME"; | ||
| prev_print = 1; | ||
| } | ||
| if (static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::DATE)){ | ||
| if (prev_print == 1){ | ||
| std::cout << ","; | ||
| } | ||
| std::cout << "DATE"; | ||
| prev_print = 1; | ||
| } | ||
| if (static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::USER)){ | ||
| if (prev_print == 1){ | ||
| std::cout << ","; | ||
| } | ||
| std::cout << "USER"; | ||
| prev_print = 1; | ||
| } | ||
| if (static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::CERT)){ | ||
| if (prev_print == 1){ | ||
| std::cout << ","; | ||
| } | ||
| std::cout << "CERT"; | ||
| prev_print = 1; | ||
| } | ||
| if (static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::KEYS)){ | ||
| if (prev_print == 1){ | ||
| std::cout << ","; | ||
| } | ||
| std::cout << "KEYS"; | ||
| prev_print = 1; | ||
| } | ||
| if (static_cast<uint8_t>(flags) & static_cast<uint8_t>(CheckFlags::DEST)){ | ||
| if (prev_print == 1){ | ||
| std::cout << ","; | ||
| } | ||
| std::cout << "DEST"; | ||
| prev_print = 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. Много дублирования, можно было вынести общие части придумав другую логику |
||
| std::cout << "]"; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| double operator""_cm_to_m(long double x) { | ||
| return x / 100.0; | ||
| } | ||
| double operator""_m_to_cm(long double x) { | ||
| return x* 100.0; | ||
| } | ||
|
|
||
| double operator""_in_to_ft(long double x) { | ||
| return x / 12.0; | ||
| } | ||
| double operator""_ft_to_in(long double x) { | ||
| return x * 12.0; | ||
| } | ||
|
|
||
| double operator""_m_to_ft(long double x) { | ||
| return x / 0.3048; | ||
| } | ||
| double operator""_ft_to_m(long double x) { | ||
| return x * 0.3048; | ||
| } | ||
|
|
||
| double operator""_cm_to_ft(long double x) { | ||
| return x / 30.48; | ||
| } | ||
| double operator""_ft_to_cm(long double x) { | ||
| return x * 30.48; | ||
| } | ||
|
|
||
| double operator""_cm_to_in(long double x) { | ||
| return x / 30.48 * 12.0; | ||
| } | ||
|
|
||
| double operator""_m_to_in(long double x) { | ||
| return x* 100.0 / 30.48 * 12.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 operator""_in_to_cm(long double x) { | ||
| return x / 12.0 * 30.48; | ||
| } | ||
| double operator""_in_to_m(long double x) { | ||
| return x / 12.0 * 30.48 / 100; | ||
| } | ||
|
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 и других подобных). В данном случае лучше объявить константы для 12, 30.48, например, чтобы затем использовать данные константы с понятными именами |
||
| 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){ | ||
|
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. нет пробела перед
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 << "0b"; | ||
| for(size_t i = 0; i < bytes * 8; ++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 << ((value >> (bytes * 8 - i - 1)) & 1); | ||
| if (i != (bytes * 8 - 1) && (i % 4 == 3) && i != 0) | ||
| std::cout << '\''; | ||
| else if (i == bytes * 8 - 1) | ||
| std::cout << '\n'; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| #include <cstdef> | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <cmath> | ||
|
|
||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (size == 0 || values == nullptr){ | ||
| return 0.0; | ||
| } | ||
| double rms = 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. пробелы перед |
||
| rms += 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. по факту это сумма квадратов нежели rms, поскольку в конце мы возвращаем не rms, а выражение, которое вычисляет rms, не совсем корректное название |
||
| } | ||
| return sqrt(rms/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.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,15 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| double ApplyOperations(double a, double b, double (**arr_func)(double, double), size_t 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. для наглядности лучше всё такие в сигнатуре указывать массив указателей, чем указатель на указатель, это будет более наглядно |
||
| double result = 0; | ||
| if (size == 0 or arr_func == nullptr){ | ||
|
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 0.0; | ||
| } | ||
| for (size_t i = 0; i < size; ++i){ | ||
| if (arr_func[i] != nullptr){ | ||
| result += arr_func[i](a, b); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| 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"}; | ||
| int* FindLastElement(const int* begin, const int* end, bool (*predicate) (int)) { | ||
| if (begin == nullptr or end == nullptr or begin >= end){ | ||
| return const_cast<int*>(end); | ||
| } | ||
| int* result = const_cast<int*>(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. В данном случае это некорректное решение задачи приводящие к UB при передачи действительно константного объекта, нельзя снимать константность, если в исходном варианте это был указатель на константу. Корректно было сделать две версии функции, одна принимает и возвращает указатели на константу, другая просто указатели. И вот в таком случае, чтобы избежать дублирования в версии неконстантной можно было вызвать константную и с результата снять константность. это было бы корректно, так как исходная версия функция была не константная. |
||
| int* p = const_cast<int*>(begin); | ||
| while (p < 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. пробел перед |
||
| if(predicate(*p)){ | ||
|
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. пробелы после |
||
| result = p; | ||
| } | ||
| p++; | ||
|
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 result; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,30 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| /* return_type */ FindLongestSubsequence(/* ptr_type */ begin, /* ptr_type */ end, /* type */ count) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| char* FindLongestSubsequence(const char* begin, const char* end, size_t& count) { | ||
| count = 0; | ||
| if ((begin >= end) or (begin == nullptr) or (end == nullptr)){ | ||
|
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 nullptr; | ||
| } | ||
| char* long_begin = const_cast<char*>(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. Ситуация аналогична задаче last_of_us - это некорректное решение, так как содержит UB |
||
| //char long_char = *begin; | ||
| size_t len = 0; | ||
| char* char0 = const_cast<char*>(begin); | ||
| while (begin <= end){ | ||
| if(*char0 == *begin){ | ||
| len++; | ||
|
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 дополнить сдвигом указателя и continue и избавится от else вовсе |
||
| if (len > count){ | ||
| count = len; | ||
| //long_char = char0; | ||
| long_begin = const_cast<char*>(char0); | ||
| } | ||
| char0 = const_cast<char*>(begin); | ||
| len = 1; | ||
| } | ||
|
|
||
| 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. должен быть префиксный инкремент |
||
| } | ||
| return long_begin; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,50 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
|
|
||
|
|
||
| void PrintArray(/* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
|
|
||
| void PrintArray(const int* begin, const int* end, size_t ogr = 0) { | ||
| /*if (begin == nullptr or end == nullptr){ | ||
| return; | ||
| }*/ | ||
|
|
||
| size_t i_ogr = 1; | ||
| std::cout <<"["; | ||
|
|
||
| if (end >= begin){ | ||
| int* p = const_cast<int*>(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. Это некорректно, снова UB, мы не должны снимать константность, так как не меняем значение под указателем. Нужно работать с константными указателями, просто выводим значения на экран |
||
| while (p < end){ | ||
| std::cout << *p; | ||
| if (p + 1 != end){ | ||
| std::cout <<", "; | ||
| if (ogr != 0 and i_ogr == ogr){ | ||
| std::cout << "..." << 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. не нужно на каждом цикле сбрасывать буфер, следует заменить std::endl на |
||
| i_ogr = 0; | ||
| } | ||
| } | ||
|
|
||
| p++; | ||
| i_ogr++; | ||
| } | ||
| } | ||
| else{ | ||
| int* p = const_cast<int*>(end); | ||
| p--; | ||
| while (p >= begin){ | ||
| std::cout << *p; | ||
| if (p != begin){ | ||
| std::cout <<", "; | ||
| if (ogr != 0 and i_ogr == ogr){ | ||
| std::cout << "..." << std::endl << " "; | ||
| i_ogr = 0; | ||
| } | ||
| } | ||
|
|
||
| p--; | ||
| i_ogr++; | ||
| } | ||
|
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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,19 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| void SwapPtr(/* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| void SwapPtr(int*& ptr1, int*& ptr2) { | ||
|
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. обыычно принято называть lhs, rhs (left/right hand side) что говорит левый и правый аргумент и короче |
||
| int* ptr0 = ptr1; | ||
|
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. вместо ptr0, комфортнее было бы temp, что явно говорит, что это временная переменная и нет необходимости искать глазами цифру среди ptr |
||
| ptr1 = ptr2; | ||
| ptr2 = ptr0; | ||
| } | ||
| void SwapPtr(const int*& ptr1, const int*& ptr2) { | ||
| const int* ptr0 = ptr1; | ||
| ptr1 = ptr2; | ||
| ptr2 = ptr0; | ||
| } | ||
| void SwapPtr(int**& ptr1, int**& ptr2) { | ||
| int** ptr0 = ptr1; | ||
| ptr1 = ptr2; | ||
| ptr2 = ptr0; | ||
| } | ||
|
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,11 +1,49 @@ | ||
| #include <cmath> | ||
| #include <stdexcept> | ||
| #include <vector> | ||
|
|
||
|
|
||
| struct DataStats { | ||
| double avg = 0.0; | ||
| double sd = 0.0; | ||
| }; | ||
|
|
||
| /* return_type */ CalculateDataStats(/* args */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| DataStats CalculateDataStats(std::vector<int>& vect) { | ||
|
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 n = 0; | ||
|
|
||
| DataStats rezult = {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. result - не нужно русифицировать английский |
||
| if (vect.begin() == vect.end()){ | ||
| return rezult; | ||
| } | ||
| for (auto it = vect.begin(); it != vect.end(); ++it) { | ||
| ++n; | ||
| rezult.avg += *it; | ||
| } | ||
| rezult.avg /= n; | ||
| for (auto it = vect.begin(); it != vect.end(); ++it) { | ||
| rezult.sd += pow((*it - rezult.avg), 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. Два цикла, есть возможность считать это в одном цикле, при большом количестве элементов два прохода может быть заметно накладнее одного |
||
| rezult.sd /= n; | ||
| rezult.sd = sqrt(rezult.sd); | ||
|
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 rezult; | ||
| } | ||
|
|
||
| DataStats CalculateDataStats(const std::vector<int>& vect) { | ||
|
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 n = 0; | ||
|
|
||
| DataStats rezult = {0, 0}; | ||
| if (vect.begin() == vect.end()){ | ||
| return rezult; | ||
| } | ||
| for (auto it = vect.begin(); it != vect.end(); ++it) { | ||
| ++n; | ||
| rezult.avg += *it; | ||
| } | ||
| rezult.avg /= n; | ||
| for (auto it = vect.begin(); it != vect.end(); ++it) { | ||
| rezult.sd += pow((*it - rezult.avg), 2); | ||
| } | ||
| rezult.sd /= n; | ||
| rezult.sd = sqrt(rezult.sd); | ||
| return rezult; | ||
| } | ||
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. Причем будет достаточно одного каста, поскольку второй аргумент будет приведен неявно.