diff --git a/Задание 3.cpp b/Задание 3.cpp new file mode 100644 index 0000000..cccf397 --- /dev/null +++ b/Задание 3.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +struct Student { + char name[17]; + uint16_t year; + float gpa; + unsigned gender : 1; + uint8_t courses; + Student* leader; +}; + +void printStudentInfo(const Student &student) { + std::cout << ": " << student.name << std::endl; + std::cout << " : " << student.year << std::endl; + std::cout << " : " << student.gpa << std::endl; + std::cout << ": " << (student.gender ? "" : "") << std::endl; + std::cout << " : " << static_cast(student.courses) << std::endl; + std::cout << " : " << student.leader << std::endl; +} + +void printMemoryLayout(const Student &student) { + std::cout << " : " << &student << std::endl; + std::cout << " : " << offsetof(Student, name) << ", : " << sizeof(student.name) << std::endl; + std::cout << " : " << offsetof(Student, year) << ", : " << sizeof(student.year) << std::endl; + std::cout << " : " << offsetof(Student, gpa) << ", : " << sizeof(student.gpa) << std::endl; + // offsetof gender + // std::cout << " : " << offsetof(Student, gender) << ", : 1 " << std::endl; + std::cout << ": 1 ( offset-of)" << std::endl; + std::cout << " : " << offsetof(Student, courses) << ", : " << sizeof(student.courses) << std::endl; + std::cout << " : " << offsetof(Student, leader) << ", : " << sizeof(student.leader) << std::endl; + + std::cout << " : "; + const unsigned char* bytes = reinterpret_cast(&student); + for (size_t i = 0; i < sizeof(Student); ++i) { + std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast(bytes[i]) << " "; + } + std::cout << std::dec << std::endl; +} + +int main() { + Student student1 = {"Alice", 2021, 3.8, 0, 5, nullptr}; + Student student2 = {"Bob", 2021, 3.5, 1, 6, &student1}; + Student starosta = {"Charlie", 2020, 4.0, 1, 8, nullptr}; + + Student students[] = {student1, student2, starosta}; + + std::cout << " : " << &students << ", : " << sizeof(students) << std::endl; + + for (size_t i = 0; i < 3; ++i) { + std::cout << "\n--- " << i + 1 << " ---" << std::endl; + printStudentInfo(students[i]); + printMemoryLayout(students[i]); + } + + return 0; +} diff --git a/Задание 4.cpp b/Задание 4.cpp new file mode 100644 index 0000000..903f377 --- /dev/null +++ b/Задание 4.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +bool isValidFilename(const char* filename) { + const char* invalidChars = "*\"<>?|"; + if (strpbrk(filename, invalidChars)) return false; + + char* colon = strchr(filename, ':'); + if (colon) { + if (colon != filename + 1 || !isalpha(filename[0]) || colon[1] != '\\') return false; + } + + const char* extension = strrchr(filename, '.'); + if (extension) { + char ext[5]; + strncpy(ext, extension, 4); + ext[4] = '\0'; + for (char& c : ext) c = tolower(c); + if (strncmp(ext, ".txt", 4) != 0) return false; + } + return true; +} + +int main() { + char filename[256]; + std::cout << " : "; + std::cin.getline(filename, 256); + + if (!isValidFilename(filename)) { + std::cerr << " ." << std::endl; + return 1; + } + + if (!strrchr(filename, '.')) { + strcat(filename, ".txt"); + } + + std::ifstream file(filename, std::ios::binary); + if (!file) { + std::cerr << " ." << std::endl; + return 1; + } + + file.seekg(0, std::ios::end); + std::streamsize fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + char* buffer = new char[fileSize + 1]; + file.read(buffer, fileSize); + buffer[fileSize] = '\0'; + file.close(); + + char searchString[256]; + std::cout << " : "; + std::cin.getline(searchString, 256); + + size_t searchLength = strlen(searchString); + size_t count = 0; + char* position = buffer; + + while ((position = strstr(position, searchString)) != nullptr) { + ++count; + position += searchLength; + } + + std::cout << " : " << count << std::endl; + + delete[] buffer; + return 0; +} diff --git a/Контрольные вопросы.docx b/Контрольные вопросы.docx new file mode 100644 index 0000000..b4a6ed0 Binary files /dev/null and b/Контрольные вопросы.docx differ diff --git a/задание 1.cpp b/задание 1.cpp new file mode 100644 index 0000000..3b0f4d5 --- /dev/null +++ b/задание 1.cpp @@ -0,0 +1,49 @@ +#include +#include + +void print_in_hex(uint8_t byte) { + 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 = static_cast(data); + for (size_t i = 0; i < size; ++i) { + print_in_hex(bytes[i]); + if ((i + 1) % 16 == 0) std::cout << std::endl; + } + std::cout << std::endl; +} + +void print_in_binary(uint8_t byte) { + for (int i = 7; i >= 0; --i) { + std::cout << ((byte >> i) & 1); + } + std::cout << " "; +} + +void print_in_binary(const void* data, size_t size) { + 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) std::cout << std::endl; + } + std::cout << std::endl; +} + +int main() { + uint8_t byte = 0b10101010; + uint8_t data[] = {0b00000001, 0b00000010, 0b00000011, 0b00000100, + 0b00000101, 0b00000110, 0b00000111, 0b00001000, + 0b00001001, 0b00001010, 0b00001011, 0b00001100, + 0b00001101, 0b00001110, 0b00001111, 0b00010000}; + + print_in_hex(byte); + std::cout << std::endl; + print_in_hex(data, sizeof(data)); + + print_in_binary(byte); + std::cout << std::endl; + print_in_binary(data, sizeof(data)); + + return 0; +} diff --git a/задание 2.cpp b/задание 2.cpp new file mode 100644 index 0000000..41c008a --- /dev/null +++ b/задание 2.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +int main() { + uint16_t operand1, operand2; + char operation; + + std::cout << " (0-65535): "; + std::cin >> operand1; + + std::cout << " (&, |, ^): "; + std::cin >> operation; + + std::cout << " (0-65535): "; + std::cin >> operand2; + + uint16_t result; + + switch (operation) { + case '&': + result = operand1 & operand2; + break; + case '|': + result = operand1 | operand2; + break; + case '^': + result = operand1 ^ operand2; + break; + default: + std::cerr << ": !" << std::endl; + return 1; + } + + std::cout << " : 0x" + << std::hex << std::uppercase << result << std::endl; + + std::cout << " : " + << std::bitset<16>(result) << std::endl; + + return 0; +}