Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
136 строки
4.0 KiB
C++
136 строки
4.0 KiB
C++
#include <iostream>
|
|
using namespace std;
|
|
|
|
struct Student {
|
|
char Name[17];
|
|
uint16_t Year_of_receipt;
|
|
float Average_score;
|
|
uint16_t Sex : 1;
|
|
uint16_t Finished_courses;
|
|
Student *Team_lead;
|
|
};
|
|
|
|
char digits[] = "0123456789ABCDEF";
|
|
const uint8_t*
|
|
as_bytes(const void* data) {
|
|
return reinterpret_cast<const uint8_t*>(data);
|
|
}
|
|
|
|
char nibble_to_hex(uint8_t i) {
|
|
return digits[i & 0xf];
|
|
}
|
|
void print_in_hex(uint8_t byte) {
|
|
cout << nibble_to_hex(byte >> 4)
|
|
<< nibble_to_hex(byte & 0xf);
|
|
}
|
|
void print_in_hex(const void* data, size_t size) {
|
|
const uint8_t *bytes = as_bytes(data);
|
|
for (size_t i = 0; i < size; i++) {
|
|
print_in_hex(bytes[i]);
|
|
if ((i + 1) % 16 == 0) {
|
|
cout << '\n';
|
|
}
|
|
else {
|
|
cout << ' ';
|
|
}
|
|
}
|
|
}
|
|
char bit_digit(uint8_t byte, uint8_t bit) {
|
|
if (byte &(0x1 << bit)) {
|
|
return '1';
|
|
}
|
|
return '0';
|
|
}
|
|
|
|
void print_in_binary(uint8_t byte) {
|
|
for (int bit = 7; bit >= 0; bit--) {
|
|
cout << bit_digit(byte, bit);
|
|
}
|
|
}
|
|
void print_in_binary(const void* data, size_t size) {
|
|
const uint8_t *bytes = as_bytes(data);
|
|
for (size_t i = 0; i < size; i++) {
|
|
print_in_binary(bytes[i]);
|
|
if ((i + 1) % 4 == 0) {
|
|
cout << '\n';
|
|
}
|
|
else {
|
|
cout << ' ';
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
Student students[3];
|
|
uint16_t gender;
|
|
for (int i = 0; i < 3; i++) {
|
|
if (i != 0) {
|
|
cout << "Student " << i << ":\n";
|
|
students[i].Team_lead = &students[0];
|
|
}
|
|
else {
|
|
students[i].Team_lead = NULL;
|
|
cout << "Team lead:\n";
|
|
}
|
|
cout << "Name: ";
|
|
cin >> students[i].Name;
|
|
cout << "Year of receiption: ";
|
|
cin >> students[i].Year_of_receipt;
|
|
cout << "Average score: ";
|
|
cin >> students[i].Average_score;
|
|
cout << "Gender (0-1): ";
|
|
cin >> gender;
|
|
students[i].Sex = gender;
|
|
cout << "Courses: ";
|
|
cin >> students[i].Finished_courses;
|
|
}
|
|
|
|
cout << "Array address: " << (void*)students << "\n";
|
|
cout << "Array size: " << sizeof(students) << "\n";
|
|
for (int i = 0; i < 3; i++) {
|
|
cout << "Address of element " << i << ": " << (void*)&students[i] << "\n";
|
|
cout << "Size of element: " << i << ": " << sizeof(Student) << "\n";
|
|
}
|
|
cout << "students[1]:\n";
|
|
cout << "Name: address = " << (void*)&students[1].Name
|
|
<< " offset = " << offsetof(Student, Name)
|
|
<< " size = " << sizeof(students[1].Name) << "\nhex:\n";
|
|
print_in_hex(&students[1].Name, sizeof(students[1].Name));
|
|
cout << "\nIn binary:\n";
|
|
print_in_binary(&students[1].Name, sizeof(students[1].Name));
|
|
|
|
cout << "\n\nYear_of_receipt: address = " << (void*)&students[1].Year_of_receipt
|
|
<< " offset = " << offsetof(Student, Year_of_receipt)
|
|
<< " size = " << sizeof(students[1].Year_of_receipt) << "\nhex:\n";
|
|
print_in_hex(&students[1].Year_of_receipt, sizeof(students[1].Year_of_receipt));
|
|
cout << "\nIn binary:\n";
|
|
print_in_binary(&students[1].Year_of_receipt, sizeof(students[1].Year_of_receipt));
|
|
|
|
cout << "\n\nAverage_score: address = " << (void*)&students[1].Average_score
|
|
<< " offset = " << offsetof(Student, Average_score)
|
|
<< " size = " << sizeof(students[1].Average_score) << "\nhex:\n";
|
|
print_in_hex(&students[1].Average_score, sizeof(students[1].Average_score));
|
|
cout << "\nIn binary:\n";
|
|
print_in_binary(&students[1].Average_score, sizeof(students[1].Average_score));
|
|
|
|
cout << "\n\nFinished_courses: address = " << (void*)&students[1].Finished_courses
|
|
<< " offset = " << offsetof(Student, Finished_courses)
|
|
<< " size = " << sizeof(students[1].Finished_courses) << "\nhex:\n";
|
|
print_in_hex(&students[1].Finished_courses, sizeof(students[1].Finished_courses));
|
|
cout << "\nIn binary:\n";
|
|
print_in_binary(&students[1].Finished_courses, sizeof(students[1].Finished_courses));
|
|
|
|
cout << "\n\nTeam_lead: address = " << (void*)&students[1].Team_lead
|
|
<< " offset = " << offsetof(Student, Team_lead)
|
|
<< " size = " << sizeof(students[1].Team_lead) << "\nhex:\n";
|
|
print_in_hex(&students[1].Team_lead, sizeof(students[1].Team_lead));
|
|
cout << "\nIn binary:\n";
|
|
print_in_binary(&students[1].Team_lead, sizeof(students[1].Team_lead));
|
|
cout << "\nFull array:\n";
|
|
print_in_hex(students, sizeof(students));
|
|
cout << "\nIn binary:\n";
|
|
print_in_binary(students, sizeof(students));
|
|
cin >> gender;
|
|
return 0;
|
|
}
|