From 0927d1b42a48541649554735bf23301ee6d6a61b Mon Sep 17 00:00:00 2001
From: GorokhovDE <GorokhovDE@mpei.ru>
Date: Tue, 24 Dec 2024 09:58:29 +0300
Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?=
 =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?=
 =?UTF-8?q?=D1=80=D0=B8=D0=B8,=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8?=
 =?UTF-8?q?=D1=8F=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D1=8B?=
 =?UTF-8?q?=20=D0=B4=D0=BE=20=D0=BA=D0=BE=D0=BD=D1=86=D0=B0=20=D0=B8=20?=
 =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D1=82=D0=B5?=
 =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9=D0=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lab04/lab04-pdf.cpp | 186 ++++++++++++++++++++++++++++++--------------
 lab04/text.txt      |   3 +
 2 files changed, 131 insertions(+), 58 deletions(-)
 create mode 100644 lab04/text.txt

diff --git a/lab04/lab04-pdf.cpp b/lab04/lab04-pdf.cpp
index 1723855..aeff0d4 100644
--- a/lab04/lab04-pdf.cpp
+++ b/lab04/lab04-pdf.cpp
@@ -1,107 +1,177 @@
 #include <iostream>
-#include <cassert>
-#include <cstdint> // ��� uint8_t, uint16_t
-#include <cstring> // ��� ������ � C-style ��������
-#include <iomanip>  // ��� ������ � ������� hex
+#include <fstream>
+#include <iomanip>
+#include <cstring>
+#include <cstdint>
+#include <cstddef>
+#include <string>
 
-using namespace std;
-
-// ������ ����� � ����������������� ����
+// 1. ������� ��� ������ ������
 void print_in_hex(uint8_t byte) {
-    cout << hex << uppercase << (byte >> 4) << (byte & 0x0F) << ' ';
+    std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << ' ';
 }
 
-// ������ ������� � ����������������� ����
 void print_in_hex(const void* data, size_t size) {
-    const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
-    for (size_t i = 0; i < size; i++) {
+    const uint8_t* bytes = static_cast<const uint8_t*>(data);
+    for (size_t i = 0; i < size; ++i) {
         print_in_hex(bytes[i]);
-        if ((i + 1) % 16 == 0) cout << '\n';
+        if ((i + 1) % 16 == 0) std::cout << '\n';
     }
-    cout << dec << '\n'; // ���������� ���������� ������
+    std::cout << '\n';
 }
 
-// ������ ����� � �������� ����
 void print_in_binary(uint8_t byte) {
     for (int i = 7; i >= 0; --i) {
-        cout << ((byte >> i) & 1);
+        std::cout << ((byte >> i) & 1);
     }
-    cout << ' ';
+    std::cout << ' ';
 }
 
-// ������ ������� � �������� ����
 void print_in_binary(const void* data, size_t size) {
-    const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
-    for (size_t i = 0; i < size; i++) {
+    const uint8_t* bytes = static_cast<const uint8_t*>(data);
+    for (size_t i = 0; i < size; ++i) {
         print_in_binary(bytes[i]);
-        if ((i + 1) % 4 == 0) cout << '\n';
+        if ((i + 1) % 4 == 0) std::cout << '\n';
     }
-    cout << '\n';
+    std::cout << '\n';
 }
 
+// 2. ����������� ��� ��������� ��������
 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) {
+    char operation;
+
+    std::cout << "Enter the first operand: ";
+    std::cin >> operand1;
+    std::cout << "Enter the operator (&, |, ^): ";
+    std::cin >> operation;
+    std::cout << "Enter the second operand: ";
+    std::cin >> operand2;
+
+    uint16_t result;
+    switch (operation) {
     case '&': result = operand1 & operand2; break;
     case '|': result = operand1 | operand2; break;
     case '^': result = operand1 ^ operand2; break;
     default:
-        cerr << "Invalid operator!\n";
+        std::cerr << "Invalid operator!\n";
         return;
     }
 
-    cout << "Result in hex: ";
+    std::cout << "Result in hexadecimal format: ";
     print_in_hex(&result, sizeof(result));
 
-    cout << "Result in binary: ";
+    std::cout << "Result in binary format: ";
     print_in_binary(&result, sizeof(result));
 }
 
+// 3. ������ �� �����������
 struct Student {
-    char name[17];           // ��� ��������
-    uint16_t year;           // ��� �����������
-    float average_score;     // ������� ����
-    uint8_t gender : 1;      // ���: 0 - �������, 1 - �������
-    uint8_t completed_courses; // ���������� ������
-    Student* group_leader;   // ��������� �� ��������
+    char name[17];
+    uint16_t year_of_admission;
+    float gpa;
+    unsigned gender : 1;
+    uint8_t completed_courses;
+    Student* group_leader;
 };
 
-void test_students() {
+void analyze_students() {
     Student students[3] = {
-        {"Alice", 2020, static_cast<float>(4.5), 0, 3, nullptr},
-        {"Bob", 2021, static_cast<float>(4.0), 1, 2, nullptr},
-        {"Charlie", 2020, static_cast<float>(4.2), 1, 5, nullptr}
+        {"Alice", 2020, 3.8, 0, 5, nullptr},
+        {"Bob", 2020, 3.5, 1, 5, nullptr},
+        {"Charlie", 2019, 4.0, 1, 6, nullptr}
     };
 
-    // ������ ������� � ��������
-    cout << "Array address: " << &students << ", size: " << sizeof(students) << " bytes\n";
+    students[0].group_leader = &students[2];
+    students[1].group_leader = &students[2];
+
+    std::cout << "Array address: " << &students << "\n";
+    std::cout << "Array size: " << sizeof(students) << " bytes\n\n";
+
     for (size_t i = 0; i < 3; ++i) {
-        cout << "Student " << i + 1 << " address: " << &students[i] << ", size: " << sizeof(students[i]) << " bytes\n";
+        std::cout << "Student " << i + 1 << ":\n";
+        std::cout << "  Address: " << &students[i] << "\n";
+        std::cout << "  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<int>(student.gender) << '\n';
-        cout << "Completed Courses: " << static_cast<int>(student.completed_courses) << '\n';
-        cout << "Group Leader Address: " << student.group_leader << "\n\n";
+    Student& student = students[0];
+    std::cout << "\nAnalysis of Alice's fields:\n";
+    std::cout << "  Name: Address=" << static_cast<void*>(student.name)
+        << ", Offset=" << offsetof(Student, name)
+        << ", Size=" << sizeof(student.name) << '\n';
+    std::cout << "  Year of admission: Address=" << &student.year_of_admission
+        << ", Offset=" << offsetof(Student, year_of_admission)
+        << ", Size=" << sizeof(student.year_of_admission) << '\n';
+    std::cout << "  GPA: Address=" << &student.gpa
+        << ", Offset=" << offsetof(Student, gpa)
+        << ", Size=" << sizeof(student.gpa) << '\n';
+    std::cout << "  Number of courses: Address=" << static_cast<void*>(&student.completed_courses)
+        << ", Offset=" << offsetof(Student, completed_courses)
+        << ", Size=" << sizeof(student.completed_courses) << '\n';
+}
+
+// 4. ��������� ���������� �����
+void process_text_file() {
+    const char* filename = "text.txt";
+
+    std::ifstream file(filename, std::ios::binary);
+    if (!file) {
+        std::cerr << "File open error!\n";
+        return;
     }
+
+    file.seekg(0, std::ios::end);
+    size_t filesize = file.tellg();
+    file.seekg(0, std::ios::beg);
+
+    char* buffer = new char[filesize];
+    file.read(buffer, filesize);
+
+    std::cout << "Enter the string to search for: ";
+    char search_str[256];
+    std::cin.ignore();
+    std::cin.getline(search_str, 256);
+
+    size_t count = 0;
+    char* pos = buffer;
+    while ((pos = strstr(pos, search_str)) != nullptr) {
+        ++count;
+        pos += strlen(search_str);
+    }
+
+    std::cout << "Number of occurrences: " << count << "\n";
+
+    delete[] buffer;
 }
 
 int main() {
-    // ����� �������
-    bitwise_calculator();
-    test_students();
+    std::cout << "Choose a task: 1 - Print, 2 - Calculator, 3 - Students, 4 - File\n";
+    int choice;
+    std::cin >> choice;
+
+    switch (choice) {
+    case 1:
+        // ������������ ������� ������
+    {
+        uint16_t data = 0xABCD;
+        std::cout << "Hexadecimal output:\n";
+        print_in_hex(&data, sizeof(data));
+        std::cout << "Binary output:\n";
+        print_in_binary(&data, sizeof(data));
+    }
+    break;
+    case 2:
+        bitwise_calculator();
+        break;
+    case 3:
+        analyze_students();
+        break;
+    case 4:
+        process_text_file();
+        break;
+    default:
+        std::cerr << "Invalid choice!\n";
+    }
+
     return 0;
 }
diff --git a/lab04/text.txt b/lab04/text.txt
new file mode 100644
index 0000000..717d947
--- /dev/null
+++ b/lab04/text.txt
@@ -0,0 +1,3 @@
+Hello, world!
+This is a test file.
+Hello again!