Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions 01_week/tasks/addition/addition.cpp
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 Addition(int a, int b)
{
// throw std::runtime_error{"Not implemented"};
return static_cast<int64_t>(a) + b;
}
108 changes: 106 additions & 2 deletions 01_week/tasks/char_changer/char_changer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,111 @@
#include <cstddef>
#include <stdexcept>
#include <cctype>

size_t CharChanger(char array[], size_t size, char delimiter = ' ')
{
// throw std::runtime_error{"Not implemented"};
size_t j = 0;
size_t i = 0;

size_t CharChanger(char array[], size_t size, char delimiter = ' ') {
throw std::runtime_error{"Not implemented"};
while (i < size && array[i] != '\0')
{
if (array[i] == ' ')
{
// Пропускаем все пробелы
size_t spaces = 0;
while (i < size && array[i] == ' ')
{
i++;
spaces++;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

префиксные инкрименты лучше использовать

}

// Записываем разделитель если есть пробелы
if (spaces > 0)
{
if (j < size - 1)
{
array[j] = delimiter;
j++;
}
else
{
break;
}
}
}
else
{
// Определяем длину последовательности одинаковых символов
char original = array[i];
size_t count = 0;
while (i + count < size && array[i + count] == original && array[i + count] != '\0')
{
count++;
}

// Преобразуем символ
char current = original;
if (std::isdigit(static_cast<unsigned char>(original)))
{
current = '*';
}
else if (std::islower(static_cast<unsigned char>(original)))
{
current = std::toupper(static_cast<unsigned char>(original));
}
else if (!std::isupper(static_cast<unsigned char>(original)))
{
current = '_';
}

// Записываем результат с учетом правила повторений
if (count == 1)
{
if (j < size - 1)
{
array[j] = current;
j++;
}
else
{
break;
}
}
else
{
if (j + 1 < size - 1)
{
array[j] = current;
if (count >= 10)
{
array[j + 1] = '0';
}
else
{
array[j + 1] = '0' + count;
}
j += 2;
}
else
{
break;
}
}

i += count;
}
}

// Завершаем строку
if (j < size)
{
array[j] = '\0';
}
else if (size > 0)
{
array[size - 1] = '\0';
}

return j;
}
58 changes: 54 additions & 4 deletions 01_week/tasks/check_flags/check_flags.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <cstdint>
#include <stdexcept>
#include <iostream>
#include <vector>


enum class CheckFlags : uint8_t {
enum class CheckFlags : uint8_t
{
NONE = 0,
TIME = (1 << 0),
DATE = (1 << 1),
Expand All @@ -13,6 +15,54 @@ enum class CheckFlags : uint8_t {
ALL = TIME | DATE | USER | CERT | KEYS | DEST
};

void PrintCheckFlags(CheckFlags flags) {
throw std::runtime_error{"Not implemented"};
void PrintCheckFlags(CheckFlags flags)
{
// throw std::runtime_error{"Not implemented"};
uint8_t value = static_cast<uint8_t>(flags);

// Проверка на выход за диапазон
if (value > static_cast<uint8_t>(CheckFlags::ALL))
{
return;
}

std::vector<std::string> active_flags;

// Проверяем каждый флаг в правильном порядке
if (value & static_cast<uint8_t>(CheckFlags::TIME))
{
active_flags.push_back("TIME");
}
if (value & static_cast<uint8_t>(CheckFlags::DATE))
{
active_flags.push_back("DATE");
}
if (value & static_cast<uint8_t>(CheckFlags::USER))
{
active_flags.push_back("USER");
}
if (value & static_cast<uint8_t>(CheckFlags::CERT))
{
active_flags.push_back("CERT");
}
if (value & static_cast<uint8_t>(CheckFlags::KEYS))
{
active_flags.push_back("KEYS");
}
if (value & static_cast<uint8_t>(CheckFlags::DEST))
{
active_flags.push_back("DEST");
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не оптимальное решение, создаем зачем-то вектор строк, с реалокациями памяти

// Формируем вывод
std::cout << "[";
for (size_t i = 0; i < active_flags.size(); ++i)
{
if (i > 0)
{
std::cout << ",";
}
std::cout << active_flags[i];
}
std::cout << "]";
}
136 changes: 136 additions & 0 deletions 01_week/tasks/length_lit/length_lit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include <cmath>

// Константы преобразования
constexpr double FEET_TO_METERS = 0.3048;
constexpr double INCHES_TO_METERS = 0.0254;
constexpr double METERS_TO_FEET = 1.0 / FEET_TO_METERS;
constexpr double METERS_TO_INCHES = 1.0 / INCHES_TO_METERS;
constexpr double FEET_TO_INCHES = 12.0;
constexpr double INCHES_TO_FEET = 1.0 / FEET_TO_INCHES;
constexpr double METERS_TO_CM = 100.0;
constexpr double CM_TO_METERS = 0.01;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не уверен что нужно столько констант, достаточно бы было 3, где-то можно было бы делить прям в выражениях. также желательно добавить их в безымянный namespace или пометить static


// Футы в другие единицы
constexpr double operator"" _ft_to_m(long double feet)
{
return feet * FEET_TO_METERS;
}

constexpr double operator"" _ft_to_cm(long double feet)
{
return feet * FEET_TO_METERS * METERS_TO_CM;
}

constexpr double operator"" _ft_to_in(long double feet)
{
return feet * FEET_TO_INCHES;
}

// Дюймы в другие единицы
constexpr double operator"" _in_to_m(long double inches)
{
return inches * INCHES_TO_METERS;
}

constexpr double operator"" _in_to_cm(long double inches)
{
return inches * INCHES_TO_METERS * METERS_TO_CM;
}

constexpr double operator"" _in_to_ft(long double inches)
{
return inches * INCHES_TO_FEET;
}

// Метры в другие единицы
constexpr double operator"" _m_to_ft(long double meters)
{
return meters * METERS_TO_FEET;
}

constexpr double operator"" _m_to_in(long double meters)
{
return meters * METERS_TO_INCHES;
}

constexpr double operator"" _m_to_cm(long double meters)
{
return meters * METERS_TO_CM;
}

// Сантиметры в другие единицы
constexpr double operator"" _cm_to_m(long double centimeters)
{
return centimeters * CM_TO_METERS;
}

constexpr double operator"" _cm_to_ft(long double centimeters)
{
return centimeters * CM_TO_METERS * METERS_TO_FEET;
}

constexpr double operator"" _cm_to_in(long double centimeters)
{
return centimeters * CM_TO_METERS * METERS_TO_INCHES;
}

// Перегрузки для целочисленных литералов
constexpr double operator"" _ft_to_m(unsigned long long feet)
{
return static_cast<double>(feet) * FEET_TO_METERS;
}

constexpr double operator"" _ft_to_cm(unsigned long long feet)
{
return static_cast<double>(feet) * FEET_TO_METERS * METERS_TO_CM;
}

constexpr double operator"" _ft_to_in(unsigned long long feet)
{
return static_cast<double>(feet) * FEET_TO_INCHES;
}

constexpr double operator"" _in_to_m(unsigned long long inches)
{
return static_cast<double>(inches) * INCHES_TO_METERS;
}

constexpr double operator"" _in_to_cm(unsigned long long inches)
{
return static_cast<double>(inches) * INCHES_TO_METERS * METERS_TO_CM;
}

constexpr double operator"" _in_to_ft(unsigned long long inches)
{
return static_cast<double>(inches) * INCHES_TO_FEET;
}

constexpr double operator"" _m_to_ft(unsigned long long meters)
{
return static_cast<double>(meters) * METERS_TO_FEET;
}

constexpr double operator"" _m_to_in(unsigned long long meters)
{
return static_cast<double>(meters) * METERS_TO_INCHES;
}

constexpr double operator"" _m_to_cm(unsigned long long meters)
{
return static_cast<double>(meters) * METERS_TO_CM;
}

constexpr double operator"" _cm_to_m(unsigned long long centimeters)
{
return static_cast<double>(centimeters) * CM_TO_METERS;
}

constexpr double operator"" _cm_to_ft(unsigned long long centimeters)
{
return static_cast<double>(centimeters) * CM_TO_METERS * METERS_TO_FEET;
}

constexpr double operator"" _cm_to_in(unsigned long long centimeters)
{
return static_cast<double>(centimeters) * CM_TO_METERS * METERS_TO_INCHES;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

много лишнего кода, если у нас возвращается double то зачем целочисленный вариант

48 changes: 46 additions & 2 deletions 01_week/tasks/print_bits/print_bits.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
#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;
}

void PrintBits(long long value, size_t bytes) {
throw std::runtime_error{"Not implemented"};
// Вычисляем общее количество битов
size_t total_bits = bytes * 8;

// Создаем маску для извлечения нужного количества байтов
unsigned long long mask = 0;
if (bytes == 8)
{
mask = ~0ULL; // Все биты установлены в 1
}
else
{
mask = (1ULL << total_bits) - 1;
}

// Приводим значение к беззнаковому типу и применяем маску
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

буквально это же написано строкой ниже, это излишний комментарий

unsigned long long unsigned_value = static_cast<unsigned long long>(value);
unsigned_value &= mask;

// Выводим префикс
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

много лишних комментариев в коде

std::cout << "0b";

// Выводим биты группами по 4
for (int i = static_cast<int>(total_bits) - 1; i >= 0; i--)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

префиксный декремент должен быть

{
// Получаем текущий бит
unsigned long long bit = (unsigned_value >> i) & 1;
std::cout << (bit ? '1' : '0');

// Добавляем апостроф после каждой группы из 4 битов (кроме последней)
if (i > 0 && i % 4 == 0)
{
std::cout << "'";
}
}

// Завершаем вывод переводом строки
std::cout << "\n";
}
Loading
Loading