Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
89 строки
2.9 KiB
C++
89 строки
2.9 KiB
C++
#include <stdio.h>
|
|
#include <stddef.h>
|
|
#include <locale.h>
|
|
|
|
// Øàã 1: Îïðåäåëåíèå ñòðóêòóðû Student
|
|
struct Student {
|
|
char name[17];
|
|
unsigned short year;
|
|
float average_score;
|
|
unsigned int gender : 1;
|
|
unsigned int courses_completed;
|
|
struct Student* group_leader;
|
|
};
|
|
|
|
// Ôóíêöèÿ äëÿ âûâîäà èíôîðìàöèè î ñòóäåíòå
|
|
void print_student_info(struct Student* student, int index) {
|
|
printf("Name: %s\n", student->name);
|
|
printf("Year of reception: %d\n", student->year);
|
|
printf("Average score: %.1f\n", student->average_score);
|
|
printf("Gender (0-1): %d\n", student->gender);
|
|
printf("Courses: %d\n", student->courses_completed);
|
|
printf("Group leader: %p\n", student->group_leader);
|
|
printf("\n");
|
|
}
|
|
|
|
// Ôóíêöèÿ äëÿ âûâîäà àäðåñîâ è ðàçìåðîâ ïîëåé
|
|
void print_addresses_and_sizes(struct Student* student, int index) {
|
|
printf("Address of element %d: %p\n", index, (void*)student);
|
|
printf("Size of element: %lu\n", sizeof(*student));
|
|
printf("Address of element %d:\n", index);
|
|
printf(" name: %p\n", (void*)student->name);
|
|
printf(" year: %p\n", (void*)&student->year);
|
|
printf(" average_score: %p\n", (void*)&student->average_score);
|
|
printf(" courses_completed: %p\n", (void*)&student->courses_completed);
|
|
printf(" group_leader: %p\n", (void*)&student->group_leader);
|
|
printf("\n");
|
|
}
|
|
|
|
// Ôóíêöèÿ äëÿ âûâîäà øåñòíàäöàòåðè÷íîãî ïðåäñòàâëåíèÿ äàííûõ
|
|
void print_hex_representation(struct Student* student) {
|
|
unsigned char* ptr = (unsigned char*)student;
|
|
printf("Array size: %lu\n", sizeof(struct Student));
|
|
for (size_t j = 0; j < sizeof(struct Student); j++) {
|
|
printf("%02X ", ptr[j]);
|
|
if ((j + 1) % 16 == 0) printf("\n");
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
// Ôóíêöèÿ äëÿ âûâîäà äâîè÷íîãî ïðåäñòàâëåíèÿ äàííûõ
|
|
void print_binary_representation(struct Student* student) {
|
|
unsigned char* ptr = (unsigned char*)student;
|
|
for (size_t j = 0; j < sizeof(struct Student); j++) {
|
|
for (int i = 7; i >= 0; i--) {
|
|
printf("%d", (ptr[j] >> i) & 1);
|
|
}
|
|
printf(" ");
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int main() {
|
|
setlocale(LC_ALL, "Russian");
|
|
|
|
// Øàã 3: Îáúÿâëåíèå è çàïîëíåíèå ìàññèâà ñòðóêòóð Student
|
|
struct Student students[3] = {
|
|
{"Alex", 2020, 4.5, 0, 2, NULL},
|
|
{"Julia", 2021, 3.8, 1, 1, NULL},
|
|
{"Sofia", 2022, 4.2, 1, 3, NULL}
|
|
};
|
|
|
|
// Óñòàíîâèì ñòàðîñòó äëÿ Alex è Julia
|
|
students[0].group_leader = &students[2];
|
|
students[1].group_leader = &students[2];
|
|
|
|
// Øàã 4: Âûâîä èíôîðìàöèè
|
|
printf("Array size: %lu\n", sizeof(students));
|
|
printf("Address of array: %p\n", (void*)students);
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
print_student_info(&students[i], i);
|
|
print_addresses_and_sizes(&students[i], i);
|
|
print_hex_representation(&students[i]);
|
|
print_binary_representation(&students[i]);
|
|
}
|
|
|
|
return 0;
|
|
}
|