diff --git a/3.cpp b/3.cpp index 092c242..34b10dc 100644 --- a/3.cpp +++ b/3.cpp @@ -1,141 +1,73 @@ +#include #include #include -#include -#include -#include using namespace std; -struct Student { - char name[17]; - uint16_t year; - float average_score; - uint8_t gender : 1; - uint8_t courses : 7; - Student* head_student; -}; - -void print_hex(const void* data, size_t size) { - const uint8_t* bytes = reinterpret_cast(data); - for (size_t i = 0; i < size; i++) { - cout << hex << uppercase << setw(2) << setfill('0') - << static_cast(bytes[i]) << " "; - } - cout << dec; +char to_hex_char(uint8_t value) { + static const char hex_table[] = "0123456789ABCDEF"; + return hex_table[value]; } -void print_binary(const void* data, size_t size) { - const uint8_t* bytes = reinterpret_cast(data); - for (size_t i = 0; i < size; i++) { - for (int j = 7; j >= 0; j--) { - cout << ((bytes[i] >> j) & 1); - } - cout << " "; - } +void print_hex_byte(uint8_t byte) { + cout << to_hex_char(byte >> 4) << to_hex_char(byte & 0x0F); } -int main() { - Student students[3]; - - strcpy(students[0].name, "Ivanov Ivan"); - students[0].year = 2023; - students[0].average_score = 4.5f; - students[0].gender = 1; - students[0].courses = 2; - students[0].head_student = nullptr; - - strcpy(students[1].name, "Petrova Anna"); - students[1].year = 2023; - students[1].average_score = 4.8f; - students[1].gender = 0; - students[1].courses = 2; - students[1].head_student = &students[0]; - - strcpy(students[2].name, "Sidorov Petr"); - students[2].year = 2023; - students[2].average_score = 4.2f; - students[2].gender = 1; - students[2].courses = 2; - students[2].head_student = &students[0]; - - cout << "1. Информация о массиве:" << endl; - cout << "Адрес массива: " << &students << endl; - cout << "Размер массива: " << sizeof(students) << " байт" << endl << endl; +void print_hex_number(uint16_t value) { + uint8_t* bytes = reinterpret_cast(&value); + print_hex_byte(bytes[0]); + cout << " "; + print_hex_byte(bytes[1]); +} - cout << "2. Информация об элементах массива:" << endl; - for (int i = 0; i < 3; i++) { - cout << "Студент " << i << ":" << endl; - cout << "Адрес: " << &students[i] << endl; - cout << "Размер: " << sizeof(Student) << " байт" << endl << endl; +void print_binary_byte(uint8_t byte) { + for (int i = 7; i >= 0; --i) { + cout << ((byte >> i) & 1); } +} - cout << "3. Детальная информация о полях второго студента:" << endl; - - cout << "name:" << endl; - cout << " Адрес: " << static_cast(students[1].name) << endl; - cout << " Смещение: " << offsetof(Student, name) << endl; - cout << " Размер: " << sizeof(students[1].name) << " байт" << endl; - cout << " Hex: "; - print_hex(students[1].name, sizeof(students[1].name)); - cout << endl << " Bin: "; - print_binary(students[1].name, sizeof(students[1].name)); - cout << endl << endl; - - cout << "year:" << endl; - cout << " Адрес: " << &students[1].year << endl; - cout << " Смещение: " << offsetof(Student, year) << endl; - cout << " Размер: " << sizeof(students[1].year) << " байт" << endl; - cout << " Hex: "; - print_hex(&students[1].year, sizeof(students[1].year)); - cout << endl << " Bin: "; - print_binary(&students[1].year, sizeof(students[1].year)); - cout << endl << endl; - - cout << "average_score:" << endl; - cout << " Адрес: " << &students[1].average_score << endl; - cout << " Смещение: " << offsetof(Student, average_score) << endl; - cout << " Размер: " << sizeof(students[1].average_score) << " байт" << endl; - cout << " Hex: "; - print_hex(&students[1].average_score, sizeof(students[1].average_score)); - cout << endl << " Bin: "; - print_binary(&students[1].average_score, sizeof(students[1].average_score)); - cout << endl << endl; +void print_binary_number(uint16_t value) { + uint8_t* bytes = reinterpret_cast(&value); + print_binary_byte(bytes[0]); + print_binary_byte(bytes[1]); +} - cout << "head_student:" << endl; - cout << " Адрес: " << &students[1].head_student << endl; - cout << " Смещение: " << offsetof(Student, head_student) << endl; - cout << " Размер: " << sizeof(students[1].head_student) << " байт" << endl; - cout << " Hex: "; - print_hex(&students[1].head_student, sizeof(students[1].head_student)); - cout << endl << " Bin: "; - print_binary(&students[1].head_student, sizeof(students[1].head_student)); - cout << endl << endl; +int main() { + uint16_t input1, input2; + char operation; + + cin >> input1 >> operation >> input2; + + uint16_t outcome; + switch (operation) { + case '&': + outcome = input1 & input2; + break; + case '|': + outcome = input1 | input2; + break; + case '^': + outcome = input1 ^ input2; + break; + default: + cout << "Ошибка: доступны только &, | и ^" << endl; + return 1; + } - cout << "4. Шестнадцатеричное представление структур:" << endl; - for (int i = 0; i < 3; i++) { - cout << "Студент " << i << " (староста: " << (i == 0 ? "да" : "нет") << "):" << endl; - cout << " Все поля: "; - print_hex(&students[i], sizeof(Student)); - cout << endl; - cout << " name: "; - print_hex(&students[i].name, sizeof(students[i].name)); - cout << endl; - cout << " year: "; - print_hex(&students[i].year, sizeof(students[i].year)); - cout << endl; - cout << " average_score: "; - print_hex(&students[i].average_score, sizeof(students[i].average_score)); - cout << endl; + cout << input1 << " " << operation << " " << input2 << endl; - // Получаем значение байта, содержащего битовые поля - uint8_t bit_fields = (students[i].gender << 7) | students[i].courses; - cout << " gender и courses (как байт): "; - print_hex(&bit_fields, sizeof(bit_fields)); - cout << endl; + print_hex_number(input1); + cout << " " << operation << " "; + print_hex_number(input2); + cout << " = "; + print_hex_number(outcome); + cout << endl; - cout << " head_student: "; - print_hex(&students[i].head_student, sizeof(students[i].head_student)); - cout << endl << endl; - } + print_binary_number(input1); + cout << " " << operation << endl; + print_binary_number(input2); + cout << " =" << endl; + print_binary_number(outcome); + cout << endl; return 0; -} \ No newline at end of file +}