Изменил(а) на 'task_2'

main
VashchishinMV 3 недель назад
Родитель 90a2d6ba44
Сommit 7745a023df

116
task_2

@ -1,100 +1,98 @@
#include #include <clocale>
#include #include <cstdint>
#include #include <iostream>
#include
using namespace std; using namespace std;
char nibble_to_hex(uint8_t i) { char nibble_to_hex(uint8_t i) {
static const char digits[] = "0123456789ABCDEF"; static const char digits[] = "0123456789ABCDEF";
return digits[i]; return digits[i];
} }
void print_byte_hex(uint8_t byte) { void print_byte_hex(uint8_t byte) {
cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0x0F); cout << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0x0F);
} }
void print_hex(uint16_t value) { void print_hex(uint16_t value) {
uint8_t* bytes = (uint8_t*)&value; uint8_t* bytes = (uint8_t*)&value;
print_byte_hex(bytes[0]); print_byte_hex(bytes[0]);
cout << " "; cout << " ";
print_byte_hex(bytes[1]); print_byte_hex(bytes[1]);
} }
void print_byte_binary(uint8_t byte) { void print_byte_binary(uint8_t byte) {
for (int i = 7; i >= 0; i--) { for (int i = 7; i >= 0; i--) {
cout << ((byte >> i) & 1); cout << ((byte >> i) & 1);
if (i % 4 == 0 && i != 0) { if (i % 4 == 0 && i != 0) {
cout << " "; cout << " ";
} }
} }
} }
void print_binary(uint16_t value) { void print_binary(uint16_t value) {
uint8_t* bytes = (uint8_t*)&value; uint8_t* bytes = (uint8_t*)&value;
print_byte_binary(bytes[0]); print_byte_binary(bytes[0]);
print_byte_binary(bytes[1]); print_byte_binary(bytes[1]);
} }
int main() { int main() {
setlocale(LC_ALL, "Rus"); setlocale(LC_ALL, "Rus");
uint16_t operand1, operand2; uint16_t operand1, operand2;
char operation; char operation;
cout << "Введите первое число: "; cout << "Введите первое число: ";
cin >> operand1; cin >> operand1;
if (cin.fail()) { if (cin.fail()) {
cerr << "Ошибка: неверный ввод для первого числа." << endl; cerr << "Ошибка: неверный ввод для первого числа." << endl;
return 1; return 1;
} }
cout << "Введите знак (&, |, или ^): "; cout << "Введите знак (&, |, или ^): ";
cin >> operation; cin >> operation;
if (cin.fail()) { if (cin.fail()) {
cerr << "Ошибка: неверный ввод для знака." << endl; cerr << "Ошибка: неверный ввод для знака." << endl;
return 1; return 1;
} }
cout << "Введите второе число: "; cout << "Введите второе число: ";
cin >> operand2; cin >> operand2;
if (cin.fail()) { if (cin.fail()) {
cerr << "Ошибка: неверный ввод для второго числа." << endl; cerr << "Ошибка: неверный ввод для второго числа." << endl;
return 1; return 1;
} }
uint16_t result; uint16_t result;
switch (operation) { switch (operation) {
case '&': case '&':
result = operand1 & operand2; result = operand1 & operand2;
break; break;
case '|': case '|':
result = operand1 | operand2; result = operand1 | operand2;
break; break;
case '^': case '^':
result = operand1 ^ operand2; result = operand1 ^ operand2;
break; break;
default: default:
cerr << "Ошибка: неверная операция." << endl; cerr << "Ошибка: неверная операция." << endl;
return 1; return 1;
} }
cout << "Ваше выражение:" << operand1 << " " << operation << " " << operand2 << endl; cout << "Ваше выражение:" << operand1 << " " << operation << " " << operand2 << endl;
cout << "Шестнадцатеричный вывод:" << endl; cout << "Шестнадцатеричный вывод:" << endl;
print_hex(operand1); print_hex(operand1);
cout << " " << operation << " "; cout << " " << operation << " ";
print_hex(operand2); print_hex(operand2);
cout << " = "; cout << " = ";
print_hex(result); print_hex(result);
cout << endl; cout << endl;
cout << "Двоичный вывод:" << endl; cout << "Двоичный вывод:" << endl;
print_binary(operand1); print_binary(operand1);
cout << " " << operation << " " << endl; cout << " " << operation << " " << endl;
print_binary(operand2); print_binary(operand2);
cout << " = " << endl; cout << " = " << endl;
print_binary(result); print_binary(result);
cout << endl; cout << endl;
return 0; return 0;
} }
Загрузка…
Отмена
Сохранить