-
Notifications
You must be signed in to change notification settings - Fork 0
Задание 01_week #1
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
52d6984
d1f9351
2f614d2
9e23d9c
ac60374
32b370b
56b2de9
f9c2325
76a91bb
6377cfa
fd55daa
2a914a2
14d2b04
966fe7b
ffe522c
b8514da
013b80d
fd67092
69d0d85
43ece52
fe7df69
9533584
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,89 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| size_t CharChanger(char array[], size_t size, char delimiter = ' ') | ||
| { | ||
| char array_fin[size]={}; | ||
|
Collaborator
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 temp_ptr = 0; // переменная для хранения текущего положения данных в массиве | ||
|
|
||
| for (size_t i = 0; i < size-1; ++i) | ||
|
Collaborator
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. пробелы |
||
| { | ||
|
|
||
|
Collaborator
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 (isalnum(array[i])) | ||
| { // если это буква-цифра | ||
| if (isalpha(array[i])) | ||
| { // если это буква | ||
| size_t temp = 1; | ||
| if (i + 1 < size) | ||
| while (temp + i < size && array[i] == array[i + temp]) | ||
| ++temp; | ||
| array_fin[temp_ptr] = array[i]; | ||
| if (islower(array[i])) | ||
| { // если она низкая, делаем ее высокой | ||
| array_fin[temp_ptr] = toupper(array[i]); | ||
| } | ||
| ++temp_ptr; // счетчик на конец преобразованного массива +1 | ||
| i += temp - 1; // смещаем счетчик рабочий | ||
|
|
||
| if (temp > 1) | ||
| { // если у нас много повторяющихся букоф | ||
| temp >= 10 ? (array_fin[temp_ptr] = '0') : (array_fin[temp_ptr] = '0' + temp); // ставим число повторений, либо 0, если повторений больше 10 | ||
| ++temp_ptr; // увеличиваем рабочий счетчик | ||
| } | ||
| } | ||
| else | ||
| { // обрабатываем цифры | ||
| size_t temp = 1; | ||
| while (temp + i < size && array[i] == array[i + temp]) | ||
| ++temp; | ||
| array_fin[temp_ptr] = '*'; | ||
| ++temp_ptr; | ||
| i += temp - 1; | ||
| if (temp > 1) | ||
| { // если у нас много повторяющихся цифр | ||
| temp >= 10 ? (array_fin[temp_ptr] = '0') : (array_fin[temp_ptr] = '0' + temp); // ставим число повторений, либо 0, если повторений больше 10 | ||
| ++temp_ptr; // увеличиваем рабочий счетчик | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (array[i] != ' ') | ||
| { | ||
| // обрабатываем символы.. | ||
| size_t temp = 1; | ||
| while (temp + i < size && array[i] == array[i + temp]) | ||
| ++temp; | ||
| array_fin[temp_ptr] = '_'; | ||
| ++temp_ptr; | ||
| i += temp - 1; | ||
| if (temp > 1) | ||
| { // если у нас много повторяющихся цифр | ||
| temp >= 10 ? (array_fin[temp_ptr] = '0') : (array_fin[temp_ptr] = '0' + temp); // ставим число повторений, либо 0, если повторений больше 10 | ||
| ++temp_ptr; // увеличиваем рабочий счетчик | ||
| } | ||
| } | ||
| else | ||
| { //...если это не пробел. если пробел, то все ок, меняем: | ||
| size_t temp = 1; | ||
| while (temp + i < size && array[i] == array[i + temp]) | ||
| ++temp; | ||
| i += temp-1; | ||
| array_fin[temp_ptr] = delimiter; | ||
| ++temp_ptr; | ||
| } | ||
| } | ||
| } | ||
|
Collaborator
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. очень много лишнего кода и дублирование |
||
| for (size_t i = temp_ptr; i < size; ++i) | ||
| { | ||
| array[i] = '\0'; // зануляем все оставшееся место в памяти | ||
| } | ||
|
|
||
| strncpy(array, array_fin, size); | ||
|
|
||
| return temp_ptr; | ||
| } | ||
|
|
||
| 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 { | ||
|
|
@@ -13,6 +14,67 @@ enum class CheckFlags : uint8_t { | |
| ALL = TIME | DATE | USER | CERT | KEYS | DEST | ||
| }; | ||
|
|
||
|
|
||
| // побитовое сравнение каждого флага и переданного числа. сравниваем,, приводя все к 8-ми битному инту | ||
| // пробовал сделать через switch, не получилось. удивился, что к CheckFlags обращаемся как к пространству имен | ||
|
|
||
|
|
||
| void PrintCheckFlags(CheckFlags flags) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| uint8_t value = static_cast<uint8_t>(flags); | ||
|
|
||
| if (value > 0b00111111) { // Если установлены биты вне диапазона 0-5 | ||
|
Collaborator
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 для этого есть ALL |
||
| std::cout << ""; | ||
| return; | ||
| } | ||
|
|
||
| bool first=true; | ||
|
|
||
| std::cout<<'['; | ||
|
|
||
| if (static_cast<uint8_t>(flags)&static_cast<u_int8_t>(CheckFlags::TIME)) | ||
| { | ||
| std::cout<<"TIME"; | ||
| first=false; | ||
| } | ||
|
|
||
| if (static_cast<uint8_t>(flags)&static_cast<u_int8_t>(CheckFlags::DATE)) | ||
| { | ||
| if (!first) std::cout<<","; | ||
| first = false; | ||
| std::cout<<"DATE"; | ||
| } | ||
|
|
||
| if (static_cast<uint8_t>(flags)&static_cast<u_int8_t>(CheckFlags::USER)) | ||
| { | ||
| if (!first) std::cout<<","; | ||
| first = false; | ||
| std::cout<<"USER"; | ||
| } | ||
|
|
||
| if (static_cast<uint8_t>(flags)&static_cast<u_int8_t>(CheckFlags::CERT)) | ||
| { | ||
| if (!first) std::cout<<","; | ||
| first = false; | ||
| std::cout<<"CERT"; | ||
| } | ||
|
|
||
| if (static_cast<uint8_t>(flags)&static_cast<u_int8_t>(CheckFlags::KEYS)) | ||
| { | ||
| if (!first) std::cout<<","; | ||
| first = false; | ||
| std::cout<<"KEYS"; | ||
| } | ||
|
|
||
| if (static_cast<uint8_t>(flags)&static_cast<u_int8_t>(CheckFlags::DEST)) | ||
| { | ||
| if (!first) std::cout<<","; | ||
| first = false; | ||
| std::cout<<"DEST"; | ||
| } | ||
|
|
||
| std::cout<<']'; | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
Collaborator
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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| constexpr long double operator""_m_to_cm(long double value){ | ||
| return value *100.00L; | ||
| }; | ||
| constexpr long double operator""_cm_to_m(long double value){ | ||
| return value * 0.01L; | ||
| }; | ||
|
|
||
| constexpr long double operator""_m_to_ft(long double value) { | ||
| return value *3.280839895013123L; | ||
| }; | ||
|
|
||
| constexpr long double operator""_m_to_in(long double value) { | ||
| return value *39.37007874015748031496062992L; | ||
| }; | ||
| constexpr long double operator""_in_to_m(long double value) { | ||
| return value*0.0254L; | ||
| }; | ||
| constexpr long double operator""_ft_to_m(long double value) { | ||
| return value *0.3048L; | ||
| }; | ||
| constexpr long double operator""_ft_to_in(long double value) { | ||
| return value *12.0L; | ||
| }; | ||
|
|
||
| constexpr long double operator""_in_to_ft(long double value) { | ||
| return value /12.0L; | ||
| }; | ||
|
|
||
| constexpr long double operator""_cm_to_ft(long double value) { | ||
| return value *0.03280839895013123359580052493L; | ||
| }; | ||
|
|
||
| constexpr long double operator""_cm_to_in(long double value) { | ||
| return value *0.39370078740157480314L; | ||
| }; | ||
| constexpr long double operator""_in_to_cm(long double value) { | ||
| return value* 2.54L; | ||
| }; | ||
| constexpr long double operator""_ft_to_cm(long double value) { | ||
| return value *30.48L; | ||
|
Collaborator
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 и лишних литералов, все нужно выразить через константы которым дать осмысленные имена, например для 12, 2.54 и 30.48, достаточно для преобразований |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,26 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
|
|
||
| void PrintBits(long long value, size_t bytes) | ||
| { | ||
| if (bytes <= 8 && bytes > 0){ | ||
| char counter = 0; | ||
|
|
||
| void PrintBits(long long value, size_t bytes) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| std::cout << "0b"; | ||
|
|
||
| for (int i = 8*bytes - 1; i >= 0; --i) | ||
|
Collaborator
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 (counter % 4 == 0 && counter) | ||
| { | ||
| std::cout << '\''; | ||
| counter = 0; | ||
| } | ||
| std::cout << ((value >> i) & 1); | ||
| ++counter; | ||
| } | ||
|
|
||
| std::cout << '\n'; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,50 @@ | ||
| #include <stdexcept> | ||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <cmath> | ||
|
|
||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void SolveQuadratic(int a, int b, int c) | ||
| { | ||
| if (a == 0) { | ||
| if (b == 0) { | ||
| if (c == 0) { | ||
| std::cout << "infinite solutions"; | ||
|
Collaborator
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 { | ||
| std::cout << "no solutions"; | ||
| } | ||
| } else { | ||
| std::cout << static_cast<double>(-c) / static_cast<double>(b); | ||
|
Collaborator
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; | ||
| } | ||
|
|
||
| double discr = b * b - 4 * a * c; | ||
|
|
||
| if (discr) | ||
| { | ||
| if (discr > 0) | ||
| { | ||
| double solution1 = 0; | ||
| double solution2 = 0; | ||
|
|
||
| solution1 = (-b + std::sqrt(discr)) / (a * 2.); | ||
| solution2 = (-b - std::sqrt(discr)) / (a * 2.); | ||
|
|
||
| if (solution1 > solution2) | ||
| std::swap(solution1, solution2); | ||
|
|
||
| std::cout << std::setprecision(6) << solution1 << ' ' << solution2; | ||
| } | ||
| else | ||
| { | ||
| std::cout << "no solutions"; | ||
|
|
||
| } | ||
| } | ||
| else | ||
| { | ||
| std::cout << std::setprecision(6) << (-b - std::sqrt(discr)) / (2.0 * a); | ||
|
|
||
| } | ||
|
Collaborator
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,17 @@ | ||
| #include <cstdef> | ||
| #include <cstdint> | ||
| #include <stdexcept> | ||
| #include <cmath> | ||
|
|
||
|
|
||
| double CalculateRMS(double values[], size_t size) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| double CalculateRMS(double values[], size_t size) | ||
| { | ||
| if (!size || values == nullptr){ | ||
| return 0.; | ||
| } | ||
| long double sum = 0; | ||
| for (size_t i = 0; i < size; ++i) | ||
| { | ||
| sum += values[i]*values[i]; | ||
| } | ||
|
|
||
| return static_cast<double>(std::sqrt(sum/static_cast<double> (size))); | ||
|
Collaborator
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #include <stdexcept> | ||
|
|
||
| double ApplyOperations(double a, double b, double (*func[])(double x, double y), size_t size) | ||
|
Collaborator
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. Задачи второй и третьей недели прокоментированы через ПР в основной репозиторий |
||
| { | ||
| long double sum = 0.; | ||
| if (func) | ||
| { | ||
| for (size_t i = 0; i < size; ++i) | ||
| { | ||
| if (*(func+i)) | ||
| sum += func[i](a, b); | ||
| } | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include <stdexcept> | ||
|
|
||
| const int* FindLastElement(const int* begin, const int* end, bool predicate(int x)) | ||
| { | ||
| if (predicate && begin && end) | ||
| { | ||
| size_t size = end - begin; | ||
| for (long i = size-1; i>=0; --i) | ||
| { | ||
| if (predicate(begin[i])) | ||
| { | ||
| return (begin + i); | ||
| } | ||
| } | ||
| } | ||
| return 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.
лишняя переменная, отсутствуют пробелы вокруг операторов, лишний каст. второй операнд кастовать не нужно, поскольку сработает неявное приведение к нужному типу, нужно использовать такую возможность и не писать лишний код