Изменил(а) на '2.cpp'
Этот коммит содержится в:
81
2.cpp
81
2.cpp
@@ -2,76 +2,71 @@
|
||||
#include <iostream>
|
||||
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<uint8_t*>(&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<uint8_t*>(&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;
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user