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 -#include -#include // Для uint8_t, uint16_t -#include // Для работы с C-style строками -#include // Для вывода в формате hex +#include +#include +#include +#include +#include +#include -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(byte) << ' '; } -// Печать массива в шестнадцатеричном виде 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++) { + const uint8_t* bytes = static_cast(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(data); - for (size_t i = 0; i < size; i++) { + const uint8_t* bytes = static_cast(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(4.5), 0, 3, nullptr}, - {"Bob", 2021, static_cast(4.0), 1, 2, nullptr}, - {"Charlie", 2020, static_cast(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(student.gender) << '\n'; - cout << "Completed Courses: " << static_cast(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(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(&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!