-
Notifications
You must be signed in to change notification settings - Fork 33
Кузнецов Владимир #14
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
dad85a7
cd838e2
e0756b3
a2df496
77d65d3
13ced12
e1b9360
578ce8e
ca41a8c
39e864a
25212e4
e51b15d
78eedfd
abd3695
bcdbc84
cd158cf
a2130ad
4a8e567
8de38a3
fd46bb9
685908d
099cfcf
b88738d
5c96344
dcf12db
c0bf6d8
4bedb7e
8392551
96da6a7
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,8 @@ | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| int64_t Addition(int a, int b) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| int64_t a_64 = static_cast<int64_t>(a); | ||
| int64_t b_64 = static_cast<int64_t>(b); | ||
| return a_64 + b_64; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,72 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <cctype> | ||
|
|
||
| const char a = 'a'; | ||
| const char A = 'A'; | ||
|
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. не очень уместно создавать глобальные константы |
||
| const char dif_letters = static_cast<int>(a) - static_cast<int>(A); | ||
|
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. Лишний каст, вычислить расстояние можно и без него
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. Мне запомнилось на первом занятии, что Вы говорили везде статиккасты вставлять, где только можно. Либо я неверно понял... |
||
|
|
||
| size_t CharChanger(char array[], size_t size, char delimiter = ' ') { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| size_t count = 1, last_ind = size - 1; | ||
| size_t start_rep_ind, end_rep_ind; | ||
|
|
||
| for (size_t i = 0; i < size; ++i) { | ||
|
|
||
| if (i == size - 1) { | ||
| return last_ind; | ||
| } | ||
|
|
||
| if (array[i] == array[i + 1]) { | ||
| if (count == 1) start_rep_ind = i; | ||
| count++; | ||
|
|
||
| if (array[i] == ' ') continue; | ||
| } | ||
| else if (count != 1) { | ||
| end_rep_ind = i + 1; | ||
| i = start_rep_ind; | ||
|
|
||
| size_t ind_next_simbol = i + 2; | ||
|
|
||
| last_ind -= count - 2; | ||
|
|
||
| if (array[i] != ' ') { | ||
| if (count >= 10) array[++i] = '0'; | ||
| else array[++i] = static_cast<char>(static_cast<size_t>('0') + count); | ||
| } | ||
| else { | ||
| --ind_next_simbol; | ||
| --last_ind; | ||
|
|
||
| array[i] = delimiter; | ||
| } | ||
|
|
||
| for (size_t j = end_rep_ind; j < size; ++j) { | ||
| std::swap(array[ind_next_simbol], array[j]); | ||
| ++ind_next_simbol; | ||
| } | ||
|
|
||
| size_t reduct_size = end_rep_ind - start_rep_ind; | ||
| size -= reduct_size - 2; | ||
|
|
||
| count = 1; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (static_cast<bool>(std::isdigit(array[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. лишний static_cast, где очевидно неявное приведение типов, нужно оставлять неявным |
||
| array[i] = '*'; | ||
| } | ||
| else if (static_cast<bool>(std::islower(array[i]))) { | ||
| array[i] -= dif_letters; | ||
| } | ||
| else if (static_cast<bool>(std::ispunct(array[i]))) { | ||
| array[i] = '_'; | ||
| } | ||
| else if (static_cast<bool>(std::isspace(array[i]))) { | ||
| array[i] = delimiter; | ||
| } | ||
| } | ||
|
|
||
| return last_ind; | ||
| } | ||
| 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,45 @@ enum class CheckFlags : uint8_t { | |
| }; | ||
|
|
||
| void PrintCheckFlags(CheckFlags flags) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| if (flags > CheckFlags::ALL) { | ||
| std::cout << ""; | ||
| return; | ||
| } | ||
|
|
||
| bool the_first = true; | ||
| std::string str_flags = "["; | ||
|
|
||
| if (static_cast<int8_t>(flags) & | ||
| static_cast<int8_t>(CheckFlags::TIME)) { | ||
| str_flags += "TIME"; | ||
| the_first = false; | ||
| } | ||
| if (static_cast<int8_t>(flags) & | ||
| static_cast<int8_t>(CheckFlags::DATE)) { | ||
| the_first ? str_flags += "DATE" : str_flags += ",DATE"; | ||
|
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. дублируется DATE |
||
| the_first = false; | ||
| } | ||
| if (static_cast<int8_t>(flags) & | ||
| static_cast<int8_t>(CheckFlags::USER) ) { | ||
| the_first ? str_flags += "USER" : str_flags += ",USER"; | ||
| the_first = false; | ||
| } | ||
| if (static_cast<int8_t>(flags) & | ||
| static_cast<int8_t>(CheckFlags::CERT)) { | ||
| the_first ? str_flags += "CERT" : str_flags += ",CERT"; | ||
| the_first = false; | ||
| } | ||
| if (static_cast<int8_t>(flags) & | ||
| static_cast<int8_t>(CheckFlags::KEYS)) { | ||
| the_first ? str_flags += "KEYS" : str_flags += ",KEYS"; | ||
| the_first = false; | ||
| } | ||
| if (static_cast<int8_t>(flags) & | ||
| static_cast<int8_t>(CheckFlags::DEST)) { | ||
| the_first ? str_flags += "DEST" : str_flags += ",DEST"; | ||
| the_first = false; | ||
| } | ||
|
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. много дублирования кода, можно код построить более оптимально без дублирвоания |
||
|
|
||
| str_flags += "]"; | ||
| std::cout << str_flags; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| constexpr long double operator""_m_to_in(long double m) { | ||
| return m / 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. в коде не принято использовать magic value. Принято давать имена константам и использовать уже имена констант. константы можно помстить в безымянный namespace |
||
| } | ||
|
|
||
| constexpr long double operator""_m_to_ft(long double m) { | ||
| return m / 0.3048; | ||
| } | ||
|
|
||
| constexpr long double operator""_m_to_m(long double m) { | ||
| return m; | ||
| } | ||
|
|
||
| constexpr long double operator""_m_to_cm(long double m) { | ||
| return m * 100; | ||
| } | ||
|
|
||
| // foots | ||
| constexpr long double operator""_ft_to_in(long double ft) { | ||
| return ft * (0.3048 / 0.0254); | ||
| } | ||
|
|
||
| constexpr long double operator""_ft_to_ft(long double ft) { | ||
| return ft; | ||
| } | ||
|
|
||
| constexpr long double operator""_ft_to_m(long double ft) { | ||
| return ft * 0.3048; | ||
| } | ||
|
|
||
| constexpr long double operator""_ft_to_cm(long double ft) { | ||
| return ft * 30.48; | ||
| } | ||
|
|
||
|
|
||
| // inch | ||
| constexpr long double operator""_in_to_in(long double in) { | ||
| return in; | ||
| } | ||
|
|
||
| constexpr long double operator""_in_to_ft(long double in) { | ||
| return in * (0.0254 / 0.3048); | ||
| } | ||
|
|
||
| constexpr long double operator""_in_to_m(long double in) { | ||
| return in * 0.0254; | ||
| } | ||
|
|
||
| constexpr long double operator""_in_to_cm(long double in) { | ||
| return in * 2.54; | ||
| } | ||
|
|
||
|
|
||
| // cm | ||
| constexpr long double operator""_cm_to_in(long double cm) { | ||
| return cm / 2.54; | ||
| } | ||
|
|
||
| constexpr long double operator""_cm_to_ft(long double cm) { | ||
| return cm / 30.48; | ||
| } | ||
|
|
||
| constexpr long double operator""_cm_to_m(long double cm) { | ||
| return cm / 100; | ||
| } | ||
|
|
||
| constexpr long double operator""_cm_to_cm(long double cm) { | ||
| return cm; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,25 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| #include <iostream> | ||
| #include <algorithm> | ||
|
|
||
| void PrintBits(long long value, size_t bytes) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| std::string str_bset = ""; | ||
|
|
||
| auto bits = bytes * 8; | ||
|
|
||
| for (size_t i = 0; i < bits; ++i) { | ||
| auto b_i = (value >> i) & 1u; | ||
|
|
||
| if (static_cast<bool>(b_i)) str_bset += "1"; | ||
| else str_bset += "0"; | ||
|
|
||
| if (i % 4 == 3 && i != bits - 1) { | ||
| str_bset += '\''; | ||
| } | ||
| } | ||
|
|
||
| str_bset += "b0"; | ||
|
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::reverse(str_bset.begin(), str_bset.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. полагаю можно складывать сразу в инвертированном порядке |
||
|
|
||
| std::cout << str_bset << "\n"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,91 @@ | ||
| #include <stdexcept> | ||
|
|
||
| #include <iomanip> | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <cmath> | ||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| double x_1, x_2; | ||
| bool a_is_Zero = (a == 0 ? true : false); | ||
|
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 b_is_Zero = (b == 0 ? true : false); | ||
| bool c_is_Zero = (c == 0 ? true : false); | ||
|
|
||
| std::cout << std::setprecision(6); | ||
|
|
||
| if (a_is_Zero) { | ||
| if (b_is_Zero) { | ||
| if (c_is_Zero) { | ||
| std::cout << "infinite 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. это неудобная конструкция с несколькими if лучше сделать коротку ветвь и объединить условие через логическое && и выходить непосредственно после печатания ответа |
||
| } | ||
| else { | ||
| std::cout << "no solutions"; | ||
| } | ||
| } | ||
| else { | ||
| if (c_is_Zero){ | ||
| std::cout << c; | ||
| } | ||
| else{ | ||
| x_1 = static_cast<double>(-c) / static_cast<double>(b); | ||
| std::cout << x_1; | ||
|
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 { | ||
| if (b_is_Zero){ | ||
| if (c_is_Zero) { | ||
| std::cout << c; | ||
| } | ||
| else { | ||
| if (a * c < 0) { | ||
| x_1 = std::sqrt(static_cast<double>(-c) / static_cast<double>(a)); | ||
| x_2 = -x_1; | ||
|
|
||
| if (x_1 > x_2) std::swap(x_1, x_2); | ||
|
|
||
| std::cout << x_1 << " " << x_2; | ||
| } | ||
| else std::cout << "no solutions"; | ||
| } | ||
| return; | ||
| } | ||
| else if (c_is_Zero) { | ||
| x_1 = static_cast<double>(0); | ||
| x_2 = static_cast<double>(-b) / static_cast<double>(a); | ||
|
|
||
| if (x_1 > x_2) std::swap(x_1, x_2); | ||
|
|
||
| std::cout << x_1 << " " << x_2; | ||
|
|
||
| return; | ||
| } | ||
| } | ||
|
|
||
| double b_d = static_cast<double>(b); | ||
| double a_d = static_cast<double>(a); | ||
| double c_d = static_cast<double>(c); | ||
|
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. это лишнии переменные, достаточно чтобы дискриминант был типа дабл и восполльзоваться static_cast по месту для одного b и 4 заменить 4.0 |
||
|
|
||
| double D_2 = b_d * b_d - 4 * a_d * c_d; | ||
| double eps = 1e-14; | ||
|
|
||
| if (D_2 < 0) { | ||
| std::cout << "no solutions"; | ||
| return; | ||
| } | ||
| else if (D_2 < eps) { | ||
| x_1 = -b_d / (2 * a_d); | ||
| std::cout << x_1; | ||
| } | ||
| else { | ||
| double D = static_cast<double>(std::sqrt(D_2)); | ||
|
|
||
| x_1 = (-b_d + D) / (2 * a_d); | ||
| x_2 = (-b_d - D) / (2 * a_d); | ||
|
|
||
| if (x_1 > x_2) std::swap(x_1, x_2); | ||
|
|
||
| std::cout << x_1 << " " << x_2; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| #include <cstdef> | ||
| #include <stdexcept> | ||
|
|
||
| #include <cstddef> | ||
| #include <cmath> | ||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
|
|
||
| if (values == NULL) return static_cast<double>(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. в С++ для этого есть nullptr и его нужно использовать или использовать неявное приведение указателей к типу bool
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 (size == 0) return static_cast<double>(0.0); | ||
|
|
||
| double sum = 0.0; | ||
|
|
||
| for (size_t i = 0; i < size; ++i) { | ||
| sum += values[i] * values[i]; | ||
| } | ||
|
|
||
| return sqrt(sum / size); | ||
|
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.
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 к стандартным функциям, не понимаю, с чем это может быть связано. Обычно пишу в VS22, потому не углубулялся в этот вопрос |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,12 @@ | ||
| #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 countOperations) | ||
|
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 res = 0.0; | ||
| for (size_t i = 0; i < countOperations; ++i){ | ||
| if (operations[i] != nullptr) res += operations[i](a, b); | ||
| } | ||
| return res; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| #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)) { | ||
|
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 (predicate == nullptr || | ||
| begin == nullptr || | ||
| end == nullptr || | ||
| std::distance(begin, end) < 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. так не очень хорошо делать, такое выравнивание с пробелами, 4 строки вместо 1 или 2, можно воспользоваться неявным приведением указателей к булеву типу |
||
| return const_cast<int*>(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. Это UB так как здесь может сниматься константность для изначально константного объекта, так делать нельзя |
||
|
|
||
| for(int* it = const_cast<int*>(end - 1); it != begin - 1; --it){ | ||
|
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. аналогично UB |
||
| if (predicate(*it)) return it; | ||
| } | ||
|
|
||
| return const_cast<int*>(end); | ||
| } | ||
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 нужную инструкцию.
Лишний каст, второй операнд не следует приводить явно, так как он бы преобразовался с помощью неявного каста. Принято использовать такую возможность и не писать явный каст самостоятельно второй раз