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

170
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) { 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++) {
cout << hex << uppercase << setw(2) << setfill('0')
<< static_cast<int>(bytes[i]) << " ";
} }
cout << dec;
void print_hex_number(uint16_t value) {
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
print_hex_byte(bytes[0]);
cout << " ";
print_hex_byte(bytes[1]);
} }
void print_binary(const void* data, size_t size) { void print_binary_byte(uint8_t byte) {
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); for (int i = 7; i >= 0; --i) {
for (size_t i = 0; i < size; i++) { cout << ((byte >> i) & 1);
for (int j = 7; j >= 0; j--) {
cout << ((bytes[i] >> j) & 1);
} }
cout << " ";
} }
void print_binary_number(uint16_t value) {
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
print_binary_byte(bytes[0]);
print_binary_byte(bytes[1]);
} }
int main() { int main() {
Student students[3]; uint16_t input1, input2;
char operation;
strcpy(students[0].name, "Ivanov Ivan");
students[0].year = 2023; cin >> input1 >> operation >> input2;
students[0].average_score = 4.5f;
students[0].gender = 1; uint16_t outcome;
students[0].courses = 2; switch (operation) {
students[0].head_student = nullptr; case '&':
outcome = input1 & input2;
strcpy(students[1].name, "Petrova Anna"); break;
students[1].year = 2023; case '|':
students[1].average_score = 4.8f; outcome = input1 | input2;
students[1].gender = 0; break;
students[1].courses = 2; case '^':
students[1].head_student = &students[0]; outcome = input1 ^ input2;
break;
strcpy(students[2].name, "Sidorov Petr"); default:
students[2].year = 2023; cout << "Ошибка: доступны только &, | и ^" << endl;
students[2].average_score = 4.2f; return 1;
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;
for (int i = 0; i < 3; i++) {
cout << "Студент " << i << ":" << endl;
cout << "Адрес: " << &students[i] << endl;
cout << "Размер: " << sizeof(Student) << " байт" << endl << endl;
} }
cout << "3. Детальная информация о полях второго студента:" << endl; cout << input1 << " " << operation << " " << input2 << endl;
cout << "name:" << endl;
cout << " Адрес: " << static_cast<void*>(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;
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;
cout << "4. Шестнадцатеричное представление структур:" << endl; print_hex_number(input1);
for (int i = 0; i < 3; i++) { cout << " " << operation << " ";
cout << "Студент " << i << " (староста: " << (i == 0 ? "да" : "нет") << "):" << endl; print_hex_number(input2);
cout << " Все поля: "; cout << " = ";
print_hex(&students[i], sizeof(Student)); print_hex_number(outcome);
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 << endl;
// Получаем значение байта, содержащего битовые поля print_binary_number(input1);
uint8_t bit_fields = (students[i].gender << 7) | students[i].courses; cout << " " << operation << endl;
cout << " gender и courses (как байт): "; print_binary_number(input2);
print_hex(&bit_fields, sizeof(bit_fields)); cout << " =" << endl;
print_binary_number(outcome);
cout << endl; cout << endl;
cout << " head_student: ";
print_hex(&students[i].head_student, sizeof(students[i].head_student));
cout << endl << endl;
}
return 0; return 0;
} }
Загрузка…
Отмена
Сохранить