From 0bf7a1136f512bf3080fdc76569848a4e63d7ea2 Mon Sep 17 00:00:00 2001 From: SpelnikovNS <–spelnikovns@mpei.ru> Date: Thu, 26 Dec 2024 01:14:11 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B8=D1=82=D0=BE=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=B0=D0=BB=D1=8C=D0=BA=D1=83=D0=BB=D1=8F=D1=82=D0=BE?= =?UTF-8?q?=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 2.cpp diff --git a/2.cpp b/2.cpp new file mode 100644 index 0000000..071f1fa --- /dev/null +++ b/2.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +using namespace std; + +// Функции для вывода значений в шестнадцатеричном и двоичном форматах +void print_hex_byte(uint8_t byte) { + static const char hex_chars[] = "0123456789abcdef"; + cout << hex_chars[byte >> 4] << hex_chars[byte & 0x0F]; +} + +void print_hex_value(uint16_t value) { + uint8_t* bytes = reinterpret_cast(&value); + print_hex_byte(bytes[0]); + cout << " "; + print_hex_byte(bytes[1]); +} + +void print_binary_byte(uint8_t byte) { + for (int i = 7; i >= 0; --i) { + cout << ((byte >> i) & 1); + } +} + +void print_binary_value(uint16_t value) { + uint8_t* bytes = reinterpret_cast(&value); + print_binary_byte(bytes[0]); + cout << " "; + print_binary_byte(bytes[1]); +} + +int main() { + // Установка русской локали + setlocale(LC_ALL, "Russian"); + + uint16_t operand1, operand2; + char operation; + + // Ввод первого операнда + cout << "Введите первый операнд (uint16_t): "; + cin >> operand1; + + // Ввод оператора + cout << "Введите оператор (&, |, ^): "; + cin >> operation; + + // Проверка корректности оператора + if (operation != '&' && operation != '|' && operation != '^') { + cout << "Ошибка: недопустимый оператор. Используйте &, |, ^." << endl; + return 1; + } + + // Ввод второго операнда + cout << "Введите второй операнд (uint16_t): "; + cin >> operand2; + + uint16_t result; + + // Выполнение операции + switch (operation) { + case '&': + result = operand1 & operand2; + break; + case '|': + result = operand1 | operand2; + break; + case '^': + result = operand1 ^ operand2; + break; + } + + // Вывод результатов + cout << "Результат в шестнадцатеричном формате:" << endl; + print_hex_value(operand1); + cout << " " << operation << " "; + print_hex_value(operand2); + cout << " = "; + print_hex_value(result); + cout << endl; + + cout << "Результат в двоичном формате:" << endl; + print_binary_value(operand1); + cout << " " << operation << " "; + print_binary_value(operand2); + cout << " = "; + print_binary_value(result); + cout << endl; + + return 0; +}