Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

65 строки
1.8 KiB
Plaintext

#include <iostream>
#include <iomanip>
#include <bitset>
#include <sstream>
// Функция для печати uint16_t в шестнадцатеричном представлении
void print_in_hex(uint16_t value) {
std::cout << std::hex << std::setw(4) << std::setfill('0') << value;
}
// Функция для печати uint16_t в двоичном представлении
void print_in_binary(uint16_t value) {
std::cout << std::bitset<16>(value);
}
int main() {
setlocale(LC_ALL, "RUS");
uint16_t operand1, operand2;
char operation;
// Ввод данных пользователем
std::cout << "Введите первый операнд: ";
std::cin >> operand1;
std::cout << "Введите оператор (&, |, ^): ";
std::cin >> operation;
std::cout << "Введите второй операнд: ";
std::cin >> operand2;
// Выполнение операции
uint16_t result;
switch (operation) {
case '&':
result = operand1 & operand2;
break;
case '|':
result = operand1 | operand2;
break;
case '^':
result = operand1 ^ operand2;
break;
default:
std::cerr << "Неверный оператор!" << std::endl;
return 1;
}
// Вывод результатов
std::cout << "Результат в шестнадцатеричном виде:\n";
print_in_hex(operand1);
std::cout << " " << operation << " ";
print_in_hex(operand2);
std::cout << " = ";
print_in_hex(result);
std::cout << std::endl;
std::cout << "Результат в двоичном виде:\n";
print_in_binary(operand1);
std::cout << " " << operation << " ";
print_in_binary(operand2);
std::cout << " = ";
print_in_binary(result);
std::cout << std::endl;
return 0;
}