-
Notifications
You must be signed in to change notification settings - Fork 33
Титаев Андрей #19
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?
Титаев Андрей #19
Changes from all commits
9f9fa2f
156abcf
2c78ec6
ad02168
2c633b8
dd1a498
96c980f
12d3bc2
2e93014
3d65f3a
cea1c41
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,60 @@ | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
|
|
||
| size_t CharChanger(char* array, size_t size, char delimiter) { | ||
|
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 (size == 0) return 0; | ||
|
|
||
| size_t CharChanger(char array[], size_t size, char delimiter = ' ') { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| size_t writePos = 0; // куда пишем результат | ||
| size_t readPos = 0; // откуда читаем исходные символы | ||
|
|
||
| while (readPos < size - 1) { // последний символ — '\0' | ||
| char currentChar = array[readPos]; | ||
|
|
||
| // Приведения текущего символа к определенным типам: пробел, число или ASCII символ | ||
| bool isSpace = std::isspace(static_cast<unsigned char>(currentChar)) != 0; | ||
| bool isDigit = std::isdigit(static_cast<unsigned char>(currentChar)) != 0; | ||
| bool isAlpha = std::isalpha(static_cast<unsigned char>(currentChar)) != 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. Зачем создавать переменные если переменная:
|
||
|
|
||
| // Подсчёт длины последовательности одинаковых символов | ||
| size_t nextPos = readPos + 1; | ||
| while (nextPos < size - 1 && array[nextPos] == currentChar) { | ||
| nextPos++; | ||
|
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. Постфиксный инкремент создает ненужную копию |
||
| } | ||
| size_t runLength = nextPos - readPos; | ||
|
|
||
| // Обработка пробелов | ||
| if (isSpace) { | ||
| array[writePos++] = delimiter; | ||
|
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. более выразительно переписать с использованием continue и следовательно убрать вложенность и непосредственно следом идущий else, так как иначе приходится листать весь блок кода else чтобы понять, будет ли ещё что-то изменяться |
||
| } else { | ||
| char outChar; | ||
|
|
||
| if (isDigit) { | ||
| outChar = '*'; | ||
| } else if (isAlpha) { | ||
| if (std::islower(static_cast<unsigned char>(currentChar))) { | ||
| outChar = std::toupper(static_cast<unsigned char>(currentChar)); | ||
| } else { | ||
| outChar = currentChar; // буква и так прописная | ||
| } | ||
| } else { | ||
| outChar = '_'; | ||
| } | ||
|
|
||
| array[writePos++] = outChar; | ||
|
|
||
| // Добавление количества повторений | ||
| if (runLength > 1) { | ||
| if (runLength >= 10) { | ||
| array[writePos++] = '0'; | ||
| } else { | ||
| array[writePos++] = char('0' + runLength); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| readPos = nextPos; | ||
| } | ||
|
|
||
| array[writePos] = '\0'; | ||
| return writePos; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,5 +14,39 @@ 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_bits = static_cast<uint8_t>(CheckFlags::ALL); | ||
|
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 & ~all_bits) { | ||
| return; | ||
| } | ||
|
|
||
| // не установлено никаких флагов | ||
| if (value == 0) { | ||
| std::cout << "[]"; | ||
| return; | ||
| } | ||
|
|
||
| const std::pair<CheckFlags, const char*> flag_names[] = { | ||
|
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. Это могло бы быть уместно в случае статической переменной, но в данном случае при каждом вызове функции мы создаем массив из шести пар, которые можем даже не использовать, очень неэффективный поход. |
||
| {CheckFlags::TIME, "TIME"}, | ||
| {CheckFlags::DATE, "DATE"}, | ||
| {CheckFlags::USER, "USER"}, | ||
| {CheckFlags::CERT, "CERT"}, | ||
| {CheckFlags::KEYS, "KEYS"}, | ||
| {CheckFlags::DEST, "DEST"} | ||
| }; | ||
|
|
||
| bool first = true; | ||
| std::cout << "["; | ||
| for (const auto& flag : flag_names) { | ||
| if (value & static_cast<uint8_t>(flag.first)) { | ||
| if (!first) { | ||
| std::cout << ","; | ||
| } | ||
| std::cout << flag.second; | ||
| first = false; | ||
| } | ||
| } | ||
| std::cout << "]"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| constexpr long double FT_TO_M = 0.3048L; | ||
| constexpr long double M_TO_FT = 1.0L / FT_TO_M; | ||
| constexpr long double FT_TO_IN = 12.0L; | ||
| constexpr long double IN_TO_FT = 1.0L / FT_TO_IN; | ||
| constexpr long double FT_TO_CM = FT_TO_M * 100.0L; | ||
| constexpr long double CM_TO_FT = 1.0L / FT_TO_CM; | ||
|
|
||
| constexpr long double IN_TO_M = 0.0254L; | ||
| constexpr long double M_TO_IN = 1.0L / IN_TO_M; | ||
| constexpr long double IN_TO_CM = IN_TO_M * 100.0L; | ||
| constexpr long double CM_TO_IN = 1.0L / IN_TO_CM; | ||
|
|
||
|
|
||
|
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. лишняя строка |
||
| constexpr long double CM_TO_M = 0.01L; | ||
| constexpr long double M_TO_CM = 1.0L / CM_TO_M; | ||
|
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 -> | ||
| constexpr double operator"" _ft_to_m(long double value) { | ||
| return static_cast<double>(value * FT_TO_M); | ||
| } | ||
|
|
||
| constexpr double operator"" _ft_to_in(long double value) { | ||
| return static_cast<double>(value * FT_TO_IN); | ||
| } | ||
|
|
||
| constexpr double operator"" _ft_to_cm(long double value) { | ||
| return static_cast<double>(value * FT_TO_CM); | ||
| } | ||
|
|
||
| // in -> | ||
| constexpr double operator"" _in_to_m(long double value) { | ||
| return static_cast<double>(value * IN_TO_M); | ||
| } | ||
|
|
||
| constexpr double operator"" _in_to_ft(long double value) { | ||
| return static_cast<double>(value * IN_TO_FT); | ||
| } | ||
|
|
||
| constexpr double operator"" _in_to_cm(long double value) { | ||
| return static_cast<double>(value * IN_TO_CM); | ||
| } | ||
|
|
||
| // cm -> | ||
| constexpr double operator"" _cm_to_m(long double value) { | ||
| return static_cast<double>(value * CM_TO_M); | ||
| } | ||
|
|
||
| constexpr double operator"" _cm_to_ft(long double value) { | ||
| return static_cast<double>(value * CM_TO_FT); | ||
| } | ||
|
|
||
| constexpr double operator"" _cm_to_in(long double value) { | ||
| return static_cast<double>(value * CM_TO_IN); | ||
| } | ||
|
|
||
| // m -> | ||
| constexpr double operator"" _m_to_ft(long double value) { | ||
| return static_cast<double>(value * M_TO_FT); | ||
| } | ||
|
|
||
| constexpr double operator"" _m_to_in(long double value) { | ||
| return static_cast<double>(value * M_TO_IN); | ||
| } | ||
|
|
||
| constexpr double operator"" _m_to_cm(long double value) { | ||
| return static_cast<double>(value * M_TO_CM); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,31 @@ | |
| #include <stdexcept> | ||
|
|
||
|
|
||
| void PrintBits(long long value, size_t bytes) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| void PrintBits(long long value, size_t bytes) { | ||
|
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. лишний пробел перед void |
||
| using std::cout; | ||
| using ULL = unsigned long long; | ||
| constexpr char BITS_IN_BYTE = 8; | ||
| constexpr char BYTES_IN_LONG_LONG = sizeof(long long); | ||
|
|
||
| size_t bits = bytes * BITS_IN_BYTE; | ||
|
|
||
| // Сдвиг, чтобы напечатать младшие байты | ||
| ULL mask = (bytes == BYTES_IN_LONG_LONG) | ||
| ? ~0ULL | ||
|
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. Как-то неожиданно отступ стал 2 пробела вместо принятых 4. Обычно пишут в одном стиле, когда сами пишут |
||
| : ((1ULL << (bits)) - 1); | ||
|
|
||
| ULL valueWithMask = static_cast<ULL>(value) & mask; | ||
| cout << "0b"; | ||
|
|
||
| for(size_t i = 0; i < bits; ++i) { | ||
| size_t bitIndex = bits - i - 1; | ||
| bool bit = (valueWithMask >> bitIndex) & 1; // определение бита на позиции bitIndex | ||
| cout << (bit ? '1' : '0'); | ||
|
|
||
| if(bitIndex % 4 == 0 && bitIndex != 0) { | ||
| cout << '\''; | ||
| } | ||
| } | ||
|
|
||
| cout << '\n'; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,36 @@ | ||
| #include <stdexcept> | ||
|
|
||
| #include <iostream> | ||
| #include <iomanip> | ||
| #include <cmath> | ||
|
|
||
| void SolveQuadratic(int a, int b, int c) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| using std::cout, std::setprecision; | ||
| if(a == 0 && b == 0 && c == 0) { | ||
| cout << "infinite solutions"; | ||
| return; | ||
| } | ||
| if(a == 0 && b == 0 && c != 0) { | ||
| cout << "no solutions"; | ||
| return; | ||
| } | ||
|
|
||
| if(a != 0) { // two roots | ||
| int discriminant = b * b - 4 * 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. то 2, то 4 пробела |
||
|
|
||
| cout << setprecision(6); | ||
| if(discriminant > 0) { | ||
| double discriminantRoot = sqrt(discriminant); | ||
|
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 x1 = (-b - discriminantRoot) / (2.0*a); | ||
|
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 x2 = (-b + discriminantRoot) / (2.0*a); | ||
| cout << x1 << ' ' << x2; | ||
| } else if(discriminant == 0){ | ||
| double x1 = (-b) / (2.0*a); | ||
| cout << x1; | ||
| } else { | ||
| cout << "no solutions"; | ||
| } | ||
| } else { // only one root | ||
| double x1 = static_cast<double>(-c) / b; | ||
| cout << x1; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| #include <cstdef> | ||
| #include <stdexcept> | ||
| #include <cmath> | ||
|
|
||
|
|
||
| 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; | ||
|
|
||
| long double sumOfSquares = 0; | ||
|
|
||
| for(size_t i = 0; i < size; ++i) { | ||
| double &value = 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. const double& |
||
| sumOfSquares += value * value; | ||
| } | ||
|
|
||
| return sqrt(sumOfSquares / size); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| #include <stdexcept> | ||
|
|
||
|
|
||
| double ApplyOperations(double a, double b /* other arguments */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| double ApplyOperations(double a, double b, double (**ops)(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. Для наглядности хотелось, чтобы это был массив укзателей на функцию |
||
| if (ops == nullptr || size == 0) return 0.0; | ||
|
|
||
| double sum = 0.0; | ||
| for (size_t i = 0; i < size; ++i) { | ||
| if (ops[i] == nullptr) { | ||
| continue; // �� ��������� ��������, ���� ��������� ������� ����� 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. Вероятно нужно поправить кодировку на UTF-8 |
||
| } | ||
| sum += ops[i](a, b); | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| #include <stdexcept> | ||
| template <typename Predicate> | ||
| const int* FindLastElement(const int* begin, const int* end, Predicate pred) { | ||
| if (begin == nullptr || end == nullptr || begin > end) return end; | ||
|
|
||
|
|
||
| /* return_type */ FindLastElement(/* ptr_type */ begin, /* ptr_type */ end, /* func_type */ predicate) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| for (const int* ptr = end - 1; ptr >= begin; --ptr) { | ||
| if (pred(*ptr)) { | ||
| return ptr; | ||
| } | ||
| } | ||
| return end; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,48 @@ | ||
| #include <stdexcept> | ||
| #include <iomanip> | ||
|
|
||
| void PrintMemory(int value, bool reverse = false) { | ||
| using std::cout, std::hex, std::uppercase, std::setw, std::setfill; | ||
| // Представляем как массив байт | ||
| const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&value); | ||
|
|
||
| void PrintMemory(int /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| cout << "0x"; | ||
| cout << uppercase << hex; | ||
| if (reverse) { | ||
| // Печатаем от старших к младшим | ||
| for (int i = sizeof(int) - 1; i >= 0; --i) { | ||
| cout << setw(2) | ||
| << setfill('0') << static_cast<int>(bytes[i]); | ||
| } | ||
| } | ||
| else { | ||
| // Печатаем от младших к старшим | ||
| for (size_t i = 0; i < sizeof(int); ++i) { | ||
| cout << setw(2) | ||
| << setfill('0') << static_cast<int>(bytes[i]); | ||
| } | ||
| } | ||
| cout << "\n"; | ||
| } | ||
|
|
||
| void PrintMemory(double /* write arguments here */) { | ||
| throw std::runtime_error{"Not implemented"}; | ||
| } | ||
| void PrintMemory(double value, bool reverse = false) { | ||
| using std::cout, std::hex, std::uppercase, std::setw, std::setfill; | ||
| const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&value); | ||
|
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(value) далее можно было вызывать одну общую функцию, и переиспользовать код |
||
|
|
||
| cout << "0x"; | ||
| cout << uppercase << hex; | ||
| if (reverse) { | ||
| // Печатаем от старших к младшим | ||
| for (int i = sizeof(double) - 1; i >= 0; --i) { | ||
| cout << setw(2) | ||
| << setfill('0') << static_cast<int>(bytes[i]); | ||
| } | ||
| } | ||
| else { | ||
| // Печатаем от младших к старшим | ||
| for (size_t i = 0; i < sizeof(double); ++i) { | ||
| cout << setw(2) | ||
| << setfill('0') << static_cast<int>(bytes[i]); | ||
| } | ||
| } | ||
| cout << "\n"; | ||
| } | ||
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.
Лишний каст, после приведения одной переменной, вторая приведется неявно, необходимо понимать и использовать данные возможности языка для лаконичности