From a478b0879432bbcd28351e5cb4e4e7f757f5df5f Mon Sep 17 00:00:00 2001 From: GorokhovDE Date: Mon, 23 Dec 2024 10:52:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=B8?= =?UTF-8?q?=D0=B7=20.pdf=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab04/lab04-pdf.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 lab04/lab04-pdf.cpp diff --git a/lab04/lab04-pdf.cpp b/lab04/lab04-pdf.cpp new file mode 100644 index 0000000..1723855 --- /dev/null +++ b/lab04/lab04-pdf.cpp @@ -0,0 +1,107 @@ +#include +#include +#include // Для uint8_t, uint16_t +#include // Для работы с C-style строками +#include // Для вывода в формате hex + +using namespace std; + +// Печать байта в шестнадцатеричном виде +void print_in_hex(uint8_t byte) { + cout << hex << uppercase << (byte >> 4) << (byte & 0x0F) << ' '; +} + +// Печать массива в шестнадцатеричном виде +void print_in_hex(const void* data, size_t size) { + const uint8_t* bytes = reinterpret_cast(data); + for (size_t i = 0; i < size; i++) { + print_in_hex(bytes[i]); + if ((i + 1) % 16 == 0) cout << '\n'; + } + cout << dec << '\n'; // Возвращаем десятичный формат +} + +// Печать байта в двоичном виде +void print_in_binary(uint8_t byte) { + for (int i = 7; i >= 0; --i) { + cout << ((byte >> i) & 1); + } + cout << ' '; +} + +// Печать массива в двоичном виде +void print_in_binary(const void* data, size_t size) { + const uint8_t* bytes = reinterpret_cast(data); + for (size_t i = 0; i < size; i++) { + print_in_binary(bytes[i]); + if ((i + 1) % 4 == 0) cout << '\n'; + } + cout << '\n'; +} + +void bitwise_calculator() { + uint16_t operand1, operand2; + char op; + cout << "Enter first operand (uint16_t): "; + cin >> operand1; + cout << "Enter operator (&, |, ^): "; + cin >> op; + cout << "Enter second operand (uint16_t): "; + cin >> operand2; + + uint16_t result = 0; + switch (op) { + case '&': result = operand1 & operand2; break; + case '|': result = operand1 | operand2; break; + case '^': result = operand1 ^ operand2; break; + default: + cerr << "Invalid operator!\n"; + return; + } + + cout << "Result in hex: "; + print_in_hex(&result, sizeof(result)); + + cout << "Result in binary: "; + print_in_binary(&result, sizeof(result)); +} + +struct Student { + char name[17]; // Имя студента + uint16_t year; // Год поступления + float average_score; // Средний балл + uint8_t gender : 1; // Пол: 0 - женский, 1 - мужской + uint8_t completed_courses; // Количество курсов + Student* group_leader; // Указатель на старосту +}; + +void test_students() { + Student students[3] = { + {"Alice", 2020, static_cast(4.5), 0, 3, nullptr}, + {"Bob", 2021, static_cast(4.0), 1, 2, nullptr}, + {"Charlie", 2020, static_cast(4.2), 1, 5, nullptr} + }; + + // Печать адресов и размеров + cout << "Array address: " << &students << ", size: " << sizeof(students) << " bytes\n"; + for (size_t i = 0; i < 3; ++i) { + cout << "Student " << i + 1 << " address: " << &students[i] << ", size: " << sizeof(students[i]) << " bytes\n"; + } + + // Печать всех полей + for (const auto& student : students) { + cout << "Name: " << student.name << ", Address: " << (void*)student.name << '\n'; + cout << "Year: " << student.year << ", Address: " << &student.year << '\n'; + cout << "Average Score: " << student.average_score << ", Address: " << &student.average_score << '\n'; + cout << "Gender: " << static_cast(student.gender) << '\n'; + cout << "Completed Courses: " << static_cast(student.completed_courses) << '\n'; + cout << "Group Leader Address: " << student.group_leader << "\n\n"; + } +} + +int main() { + // Вызов функций + bitwise_calculator(); + test_students(); + return 0; +}