Сравнить коммиты
3 Коммитов
46719114f3
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| f939cd26f5 | |||
| 662d3ca8ce | |||
| 59dbf76198 |
120
main.cpp
120
main.cpp
@@ -3,10 +3,19 @@
|
||||
#include <cstdio>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
#include <cassert>
|
||||
using namespace std;
|
||||
|
||||
struct Student {
|
||||
char name[17]; // имя + '\0'
|
||||
uint16_t year; // год поступления (2 байта)
|
||||
float gpa; // средний балл
|
||||
unsigned gender : 1; // 1 бит: 0 — жен, 1 — муж
|
||||
unsigned courses; // количество курсов (обычное целое)
|
||||
struct Student* headman; // указатель на старосту
|
||||
};
|
||||
|
||||
char nibble_to_hex(uint8_t i){
|
||||
assert(0x0 <= i && i <= 0xf);
|
||||
switch (i) {
|
||||
@@ -47,11 +56,16 @@ void print_in_hex(const void* data, size_t size) {
|
||||
}
|
||||
|
||||
}
|
||||
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) {
|
||||
printf("%d", (byte >> bit) & 1);
|
||||
}
|
||||
cout<< endl;
|
||||
}
|
||||
|
||||
void print_in_binary(const void* data, size_t size) {
|
||||
@@ -67,37 +81,95 @@ void print_in_binary(const void* data, size_t size) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
uint8_t u8 = 0x42;
|
||||
uint16_t u16 = 0x42;
|
||||
uint32_t u32 = 0x42;
|
||||
print_in_hex(u8);
|
||||
cout<<" ";
|
||||
cout<< endl;
|
||||
cout << "u8 bytes: ";
|
||||
print_in_hex(&u8, sizeof(u8));
|
||||
cout<< endl;
|
||||
cout << "u16 bytes: ";
|
||||
print_in_hex(&u16, sizeof(u16));
|
||||
cout<< endl;
|
||||
cout << "u32 bytes: ";
|
||||
print_in_hex(&u32, sizeof(u32));
|
||||
cout << '\n';
|
||||
print_in_hex(&u8,40);
|
||||
struct Student group[3] = {
|
||||
{ "Ivanov", 2023, 4.5f, 1, 10, NULL }, // [0] староста
|
||||
{ "Petrova", 2023, 4.2f, 0, 8, &group[0] }, // [1]
|
||||
{ "Sidorov", 2023, 3.9f, 1, 6, &group[0] } // [2]
|
||||
};
|
||||
|
||||
cout<< endl;
|
||||
printf("%02X", u8);
|
||||
/* 1) Адрес и размер массива */
|
||||
printf("group address = %p, sizeof(group) = %zu\n",
|
||||
(void*)group, sizeof(group));
|
||||
|
||||
/* 2) Адреса и размеры элементов массива */
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
printf("group[%d]: address = %p, sizeof = %zu\n",
|
||||
i, (void*)&group[i], sizeof group[i]);
|
||||
}
|
||||
|
||||
/* 3) Информация по полям (пример для group[1], не староста) */
|
||||
struct Student *s = &group[1];
|
||||
printf("\nField name: addr=%p, offset=%zu, size=%zu\n",
|
||||
(void*)&s->name, offsetof(struct Student, name),
|
||||
sizeof s->name);
|
||||
|
||||
printf("Field year: addr=%p, offset=%zu, size=%zu\n",
|
||||
(void*)&s->year, offsetof(struct Student, year),
|
||||
sizeof s->year);
|
||||
|
||||
cout<< endl << "Enter number: ";
|
||||
|
||||
printf("Field gpa: addr=%p, offset=%zu, size=%zu\n",
|
||||
(void*)&s->gpa, offsetof(struct Student, gpa),
|
||||
sizeof s->gpa);
|
||||
|
||||
printf("Field courses: addr=%p, offset=%zu, size=%zu\n",
|
||||
(void*)&s->courses, offsetof(struct Student, courses),
|
||||
sizeof s->courses);
|
||||
|
||||
printf("Field headman: addr=%p, offset=%zu, size=%zu\n",
|
||||
(void*)&s->headman, offsetof(struct Student, headman),
|
||||
sizeof s->headman);
|
||||
printf("Field name hex:\n");
|
||||
print_in_hex(&group[1], sizeof group[1]);
|
||||
printf("\nField year hex: ");
|
||||
print_in_hex(&s->year, sizeof s->year);
|
||||
printf("\nField gpa hex: ");
|
||||
print_in_hex(&s->gpa, sizeof s->gpa);
|
||||
printf("Field name bin:\n");
|
||||
print_in_binary(&group[1], sizeof group[1]);
|
||||
printf("Field year bin: ");
|
||||
print_in_binary(&s->year, sizeof s->year);
|
||||
printf("Field gpa bin: ");
|
||||
print_in_binary(&s->gpa, sizeof s->gpa);
|
||||
cout<<endl;
|
||||
uint16_t a, b, result;
|
||||
char op;
|
||||
printf("Enter the first operand (0..65535): ");
|
||||
cin >> a;
|
||||
printf("Enter the operator (&, | or ^):");
|
||||
cin >> op;
|
||||
printf("Enter the second operand (0..65535): ");
|
||||
cin >> b;
|
||||
switch (op) {
|
||||
case '&':
|
||||
result = a & b;
|
||||
break;
|
||||
case '|':
|
||||
result = a | b;
|
||||
break;
|
||||
case '^':
|
||||
result = a ^ b;
|
||||
break;
|
||||
default:
|
||||
cout << "Unknown operator: " << op << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("\nResult of the operation %hu %c %hu:\n", a, op, b);
|
||||
printf("Hex: ");
|
||||
print_in_hex(&a, sizeof(a));
|
||||
printf(" %c ", op);
|
||||
print_in_hex(&b, sizeof(b));
|
||||
printf(" = ");
|
||||
print_in_hex(&result, sizeof(result));
|
||||
printf("\n");
|
||||
printf("Bin: ");
|
||||
print_in_binary(&a, sizeof(a));
|
||||
printf(" %c ", op);
|
||||
print_in_binary(&b, sizeof(b));
|
||||
printf(" = ");
|
||||
print_in_binary(&result, sizeof(result));
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
//test23234
|
||||
Ссылка в новой задаче
Block a user