main
IvanchenkoIS 4 месяцев назад
Родитель 046f3be8f9
Сommit 58af48b285

180
3.cpp

@ -1,141 +1,73 @@
#include <cstdint>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <cstddef>
#include <cstdint>
#include <cstring>
using namespace std; using namespace std;
struct Student { char to_hex_char(uint8_t value) {
char name[17]; static const char hex_table[] = "0123456789ABCDEF";
uint16_t year; return hex_table[value];
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<const uint8_t*>(data);
for (size_t i = 0; i < size; i++) {
cout << hex << uppercase << setw(2) << setfill('0')
<< static_cast<int>(bytes[i]) << " ";
}
cout << dec;
} }
void print_binary(const void* data, size_t size) { void print_hex_byte(uint8_t byte) {
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); cout << to_hex_char(byte >> 4) << to_hex_char(byte & 0x0F);
for (size_t i = 0; i < size; i++) {
for (int j = 7; j >= 0; j--) {
cout << ((bytes[i] >> j) & 1);
}
cout << " ";
}
} }
int main() { void print_hex_number(uint16_t value) {
Student students[3]; uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
print_hex_byte(bytes[0]);
strcpy(students[0].name, "Ivanov Ivan"); cout << " ";
students[0].year = 2023; print_hex_byte(bytes[1]);
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;
cout << "2. Информация об элементах массива:" << endl; void print_binary_byte(uint8_t byte) {
for (int i = 0; i < 3; i++) { for (int i = 7; i >= 0; --i) {
cout << "Студент " << i << ":" << endl; cout << ((byte >> i) & 1);
cout << "Адрес: " << &students[i] << endl;
cout << "Размер: " << sizeof(Student) << " байт" << endl << endl;
} }
}
cout << "3. Детальная информация о полях второго студента:" << endl; void print_binary_number(uint16_t value) {
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
cout << "name:" << endl; print_binary_byte(bytes[0]);
cout << " Адрес: " << static_cast<void*>(students[1].name) << endl; print_binary_byte(bytes[1]);
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;
cout << "head_student:" << endl; int main() {
cout << " Адрес: " << &students[1].head_student << endl; uint16_t input1, input2;
cout << " Смещение: " << offsetof(Student, head_student) << endl; char operation;
cout << " Размер: " << sizeof(students[1].head_student) << " байт" << endl;
cout << " Hex: "; cin >> input1 >> operation >> input2;
print_hex(&students[1].head_student, sizeof(students[1].head_student));
cout << endl << " Bin: "; uint16_t outcome;
print_binary(&students[1].head_student, sizeof(students[1].head_student)); switch (operation) {
cout << endl << endl; case '&':
outcome = input1 & input2;
break;
case '|':
outcome = input1 | input2;
break;
case '^':
outcome = input1 ^ input2;
break;
default:
cout << "Ошибка: доступны только &, | и ^" << endl;
return 1;
}
cout << "4. Шестнадцатеричное представление структур:" << endl; cout << input1 << " " << operation << " " << input2 << 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;
// Получаем значение байта, содержащего битовые поля print_hex_number(input1);
uint8_t bit_fields = (students[i].gender << 7) | students[i].courses; cout << " " << operation << " ";
cout << " gender и courses (как байт): "; print_hex_number(input2);
print_hex(&bit_fields, sizeof(bit_fields)); cout << " = ";
cout << endl; print_hex_number(outcome);
cout << endl;
cout << " head_student: "; print_binary_number(input1);
print_hex(&students[i].head_student, sizeof(students[i].head_student)); cout << " " << operation << endl;
cout << endl << endl; print_binary_number(input2);
} cout << " =" << endl;
print_binary_number(outcome);
cout << endl;
return 0; return 0;
} }

Загрузка…
Отмена
Сохранить