From 046f3be8f97c8845d482a5db56785287139d461a Mon Sep 17 00:00:00 2001 From: IvanchenkoIS Date: Mon, 23 Dec 2024 18:44:30 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'2.cpp'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2.cpp | 81 ++++++++++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/2.cpp b/2.cpp index 4b2d548..c709ae4 100644 --- a/2.cpp +++ b/2.cpp @@ -2,76 +2,71 @@ #include using namespace std; -char nibble_to_hex(uint8_t i) { - static const char digits[] = "0123456789ABCDEF"; - return digits[i]; +char hex_digit(uint8_t value) { + static const char hex_chars[] = "0123456789ABCDEF"; + return hex_chars[value]; } -void print_byte_hex(uint8_t byte) { - cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0x0F); +void display_hex_byte(uint8_t byte) { + cout << hex_digit(byte >> 4) << hex_digit(byte & 0x0F); } -void print_hex(uint16_t value) { - uint8_t* bytes = (uint8_t*)&value; - print_byte_hex(bytes[0]); +void display_hex_value(uint16_t data) { + uint8_t* bytes = reinterpret_cast(&data); + display_hex_byte(bytes[0]); cout << " "; - print_byte_hex(bytes[1]); + display_hex_byte(bytes[1]); } -void print_byte_binary(uint8_t byte) { - for (int i = 7; i >= 0; i--) { - cout << ((byte >> i) & 1); +void display_binary_byte(uint8_t byte) { + for (int pos = 7; pos >= 0; --pos) { + cout << ((byte >> pos) & 1); } } -void print_binary(uint16_t value) { - uint8_t* bytes = (uint8_t*)&value; - print_byte_binary(bytes[0]); - print_byte_binary(bytes[1]); +void display_binary_value(uint16_t data) { + uint8_t* bytes = reinterpret_cast(&data); + display_binary_byte(bytes[0]); + display_binary_byte(bytes[1]); } int main() { - uint16_t operand1, operand2; - char operation; + uint16_t num1, num2; + char op; - // Ввод данных - cin >> operand1 >> operation >> operand2; + cin >> num1 >> op >> num2; - // Вычисление результата uint16_t result; - switch (operation) { + switch (op) { case '&': - result = operand1 & operand2; - break; + result = num1 & num2; + break; case '|': - result = operand1 | operand2; - break; + result = num1 | num2; + break; case '^': - result = operand1 ^ operand2; - break; + result = num1 ^ num2; + break; default: - cout << "Ошибка: поддерживаются только операторы &, | и ^" << endl; - return 1; + cout << "Ошибка: доступны только &, | и ^" << endl; + return 1; } - // Вывод исходного выражения - cout << operand1 << " " << operation << " " << operand2 << endl; + cout << num1 << " " << op << " " << num2 << endl; - // Вывод в шестнадцатеричном виде - print_hex(operand1); - cout << " " << operation << " "; - print_hex(operand2); + display_hex_value(num1); + cout << " " << op << " "; + display_hex_value(num2); cout << " = "; - print_hex(result); + display_hex_value(result); cout << endl; - // Вывод в двоичном виде - print_binary(operand1); - cout << " " << operation << endl; - print_binary(operand2); + display_binary_value(num1); + cout << " " << op << endl; + display_binary_value(num2); cout << " =" << endl; - print_binary(result); + display_binary_value(result); cout << endl; return 0; -} \ No newline at end of file +}