From b8deb00926c232b6f0a7d16b65de3e9d3f8bfff8 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 15 Dec 2024 20:57:15 +0300 Subject: [PATCH] task2 --- CMakeLists.txt | 6 ++++ main.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..020de24 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.28) +project(task2) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(task2 main.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4b2d548 --- /dev/null +++ b/main.cpp @@ -0,0 +1,77 @@ +#include +#include +using namespace std; + +char nibble_to_hex(uint8_t i) { + static const char digits[] = "0123456789ABCDEF"; + return digits[i]; +} + +void print_byte_hex(uint8_t byte) { + cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0x0F); +} + +void print_hex(uint16_t value) { + uint8_t* bytes = (uint8_t*)&value; + print_byte_hex(bytes[0]); + cout << " "; + print_byte_hex(bytes[1]); +} + +void print_byte_binary(uint8_t byte) { + for (int i = 7; i >= 0; i--) { + cout << ((byte >> i) & 1); + } +} + +void print_binary(uint16_t value) { + uint8_t* bytes = (uint8_t*)&value; + print_byte_binary(bytes[0]); + print_byte_binary(bytes[1]); +} + +int main() { + uint16_t operand1, operand2; + char operation; + + // Ввод данных + cin >> operand1 >> operation >> operand2; + + // Вычисление результата + uint16_t result; + switch (operation) { + case '&': + result = operand1 & operand2; + break; + case '|': + result = operand1 | operand2; + break; + case '^': + result = operand1 ^ operand2; + break; + default: + cout << "Ошибка: поддерживаются только операторы &, | и ^" << endl; + return 1; + } + + // Вывод исходного выражения + cout << operand1 << " " << operation << " " << operand2 << endl; + + // Вывод в шестнадцатеричном виде + print_hex(operand1); + cout << " " << operation << " "; + print_hex(operand2); + cout << " = "; + print_hex(result); + cout << endl; + + // Вывод в двоичном виде + print_binary(operand1); + cout << " " << operation << endl; + print_binary(operand2); + cout << " =" << endl; + print_binary(result); + cout << endl; + + return 0; +} \ No newline at end of file