From f9d493774132568ff842eea11a305f43df7d0d57 Mon Sep 17 00:00:00 2001 From: TereshchenkoIY Date: Thu, 14 Dec 2023 02:42:40 +0300 Subject: [PATCH] first commit --- Exercise1.cpp | 73 ++++++++++++++++++++++++++++++ Exercise2.cpp | 68 ++++++++++++++++++++++++++++ Exercise3.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ Exercise4.cpp | 65 +++++++++++++++++++++++++++ README.md | 1 + 5 files changed, 328 insertions(+) create mode 100644 Exercise1.cpp create mode 100644 Exercise2.cpp create mode 100644 Exercise3.cpp create mode 100644 Exercise4.cpp create mode 100644 README.md diff --git a/Exercise1.cpp b/Exercise1.cpp new file mode 100644 index 0000000..dc0e906 --- /dev/null +++ b/Exercise1.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +void print_in_hex(uint8_t byte) +{ + printf("%04X ", byte); +} + +void print_in_hex(const void* data, size_t size) +{ + const uint8_t* bytes = (const uint8_t*)data; + for (size_t i = 0; i < size; i++) + { + print_in_hex(bytes[i]); + if ((i + 1) % 16 == 0) + printf("\n"); + } + printf("\n"); +} + +void print_in_binary(uint8_t byte) +{ + for (int i = 7; i >= 0; i--) + { + printf("%d", (byte >> i) & 1); + if (i % 4 == 0) + printf(" "); + } +} + +void print_in_binary(const void* data, size_t size) +{ + const uint8_t* bytes = (const uint8_t*)data; + for (size_t i = 0; i < size; ++i) + { + print_in_binary(bytes[i]); + if ((i + 1) % 4 == 0) + printf("\n"); + } + printf("\n"); +} + +int main() +{ + uint8_t byte = 127; + + printf("Source number: %hhu\n", byte); + printf("Number in hexadecimal: "); + print_in_hex(byte); + printf("\n"); + printf("Binary number: "); + print_in_binary(byte); + printf("\n"); + printf("\n"); + const int SIZE = 10; + uint8_t data[] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x00, 0xA1 }; + size_t size = sizeof(data); + + printf("Source data block in decimal form\n"); + for (int i = 0; i < SIZE; ++i) + printf("%hhu ", data[i]); + printf("\n"); + + printf("Data block in hexadecimal\n"); + print_in_hex(data, size); + printf("Binary data block\n"); + print_in_binary(data, size); + + system("pause"); + return 0; +} diff --git a/Exercise2.cpp b/Exercise2.cpp new file mode 100644 index 0000000..bb6bc56 --- /dev/null +++ b/Exercise2.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +uint16_t reverse(uint16_t x) +{ + x = (x & 0xFF) << 8 | (x & 0xFF00) >> 8; + return x; +} + +void print_in_hex(uint16_t num) +{ + printf("%04X ", num); +} + +void print_in_binary(uint16_t num) +{ + for (int i = 15; i >= 0; i--) + { + printf("%d", (num >> i) & 1); + if (i % 8 == 0) + printf(" "); + } +} + +int main() +{ + uint16_t operand1, operand2; + char symbol; + printf("Enter the first operand, then the operator(&, | or ^), then the second operand.\n"); + scanf("%hu %c %hu", &operand1, &symbol, &operand2); + + operand1 = reverse(operand1); + operand2 = reverse(operand2); + + uint16_t result; + switch (symbol) + { + case '&': + result = operand1 & operand2; + break; + case '|': + result = operand1 | operand2; + break; + case '^': + result = operand1 ^ operand2; + break; + default: + printf("Invalid operator\n"); + return 1; + } + + printf("\nResult: "); + print_in_hex(operand1); + printf("%c ", symbol); + print_in_hex(operand2); + printf("= "); + print_in_binary(operand1); + printf("%c ", symbol); + print_in_binary(operand2); + printf("= "); + print_in_binary(result); + printf("\n"); + + system("pause"); + return 0; +} diff --git a/Exercise3.cpp b/Exercise3.cpp new file mode 100644 index 0000000..337f2de --- /dev/null +++ b/Exercise3.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include + +using namespace std; + +void print_in_hex(uint8_t byte) +{ + printf("%02X ", byte); +} + +void print_in_hex(const void* data, size_t size) +{ + const uint8_t* bytes = (const uint8_t*)data; + for (size_t i = 0; i < size; i++) + { + print_in_hex(bytes[i]); + if ((i + 1) % 8 == 0) + printf("\n"); + } + printf("\n"); +} + +void print_in_binary(uint8_t byte) +{ + for (int i = 7; i >= 0; i--) + { + printf("%d", (byte >> i) & 1); + if (i % 8 == 0) + printf(" "); + } +} + +void print_in_binary(const void* data, size_t size) +{ + const uint8_t* bytes = (const uint8_t*)data; + for (size_t i = 0; i < size; ++i) + { + print_in_binary(bytes[i]); + if ((i + 1) % 8 == 0) + printf("\n"); + } +} + +#pragma pack(1) + +struct Student +{ + char _name[17]; + uint16_t _year; + float _averageGrade; + uint8_t _gender : 1; + // 0 - female, 1 - male + uint8_t _numCourses : 7; + Student* _leader; +}; +#pragma pack() + +int main() +{ + Student students[3] = {0}; + + strcpy(students[0]._name, "Alex"); + students[0]._year = 2021; + students[0]._averageGrade = 3.7f; + students[0]._gender = 1; + students[0]._numCourses = 6; + students[0]._leader = nullptr; + + strcpy(students[1]._name, "Masha"); + students[1]._year = 2020; + students[1]._averageGrade = 4.6f; + students[1]._gender = 0; + students[1]._numCourses = 4; + students[1]._leader = &students[0]; + + strcpy(students[2]._name, "John"); + students[2]._year = 2021; + students[2]._averageGrade = 4.1f; + students[2]._gender = 1; + students[2]._numCourses = 5; + students[2]._leader = &students[0]; + + cout << "Array address: " << &students << endl; + cout << "Array size: " << sizeof(students) << " byte" << endl; + + for (int i = 0; i < 3; i++) + { + cout << "Element address " << i << ": " << &students[i] << endl; + cout << "Element Size " << i << ": " << sizeof(students[i]) << " byte" << endl; + } + + int index = 1; + Student& student = students[index]; + cout << "Student information " << index << ":" << endl; + cout << "Field value _name: " << student._name << endl; + cout << "Field address _name: " << &(student._name) << endl; + cout << "Field offset _name from the beginning of the structure: " << offsetof(Student, _name) << " byte" << endl; + cout << "Field size _name: " << sizeof(student._name) << " byte" << endl; + cout << "Hexadecimal representation of the field _name:" << endl; + print_in_hex(student._name, sizeof(student._name)); + cout << "Binary field representation _name:" << endl; + print_in_binary(student._name, sizeof(student._name)); + cout << endl; + + cout << "Array elements in hexadecimal:" << endl; + cout << "(First " << sizeof(student._name) << " byte - field _name):" << endl; + cout << "(Next " << sizeof(student._year) << " bytes - field _year):" << endl; + cout << "(Next " << sizeof(student._averageGrade) << " bytes - field _averageGrade):" << endl; + cout << "(Next " << sizeof(uint8_t) << " bytes - 1 bit for _gender and 7 bit for _numCourses):" << endl; + cout << "(Next " << sizeof(student._leader) << " bytes - field _leader):" << endl; + for (int i = 0; i < 3; i++) + { + cout << "Student with index " << i << endl; + print_in_hex(&students[i], sizeof(students[i])); + } + + system("pause"); + return 0; +} diff --git a/Exercise4.cpp b/Exercise4.cpp new file mode 100644 index 0000000..bbf2311 --- /dev/null +++ b/Exercise4.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +using namespace std; + +int main() +{ + + char filename[256]; + cout << "Enter file name: "; + cin.getline(filename, sizeof(filename)); + + bool isValid = true; + + const char* forbiddenChars = "*\"<>?|"; + if (strpbrk(filename, forbiddenChars) != 0) + isValid = false; + + if (strchr(filename, ':') != nullptr && + (filename[1] != '\\' || !isalpha(filename[0]) || + strchr(filename, '\\') != strrchr(filename, '\\'))) + isValid = false; + + if (strchr(filename, '.') == NULL) { + strcat(filename, ".txt"); + } + + if (isValid) + { + ifstream file(filename, ios::binary | ios::ate); + if (file.is_open()) + { + streampos fileSize = file.tellg(); + char* buffer = new char[fileSize]; + file.seekg(0, ios::beg); + file.read(buffer, fileSize); + file.close(); + + char searchString[256]; + cout << "Enter a search string: "; + cin.getline(searchString, sizeof(searchString)); + + int count = 0; + char* p = buffer; + while ((p = strstr(p, searchString)) != nullptr) + { + count++; + p += strlen(searchString); + } + + cout << "Number of occurrences of a string \"" << searchString << "\" in a text file: " << count << endl; + + delete[] buffer; + } + else + cout << "Error opening file." << endl; + } + else + cout << "Invalid file name." << endl; + + system("pause"); + return 0; +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..ce01362 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +hello