Этот коммит содержится в:
ruddos
2024-12-01 13:55:30 +03:00
Коммит b616275308
12 изменённых файлов: 284 добавлений и 0 удалений

45
lab4/task_1.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,45 @@
#include <iostream>
#include <iomanip>
#include <cstdint>
using namespace std;
void print_in_hex(uint8_t byte) {
cout << hex << setw(2) << setfill('0') << static_cast<int>(byte) << " ";
}
void print_in_hex(const void* data, size_t size) {
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 << endl;
}
cout << endl;
}
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 = 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 << endl;
}
cout << endl;
}
int main() {
uint8_t data[] = {0xDE, 0xAD, 0xBE, 0xEF, 0x12, 0x34, 0x56, 0x78};
size_t size = sizeof(data);
cout << "Hexadecimal representation:" << endl;
print_in_hex(data, size);
cout << "Binary representation:" << endl;
print_in_binary(data, size);
return 0;
}

31
lab4/task_2.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,31 @@
#include <iostream>
#include <iomanip>
#include <bitset>
using namespace std;
int main() {
uint16_t operand1, operand2;
char operation;
cout << "Enter first operand: ";
cin >> operand1;
cout << "Enter operation (&, |, ^): ";
cin >> operation;
cout << "Enter second operand: ";
cin >> operand2;
uint16_t result = 0;
if (operation == '&') result = operand1 & operand2;
else if (operation == '|') result = operand1 | operand2;
else if (operation == '^') result = operand1 ^ operand2;
else {
cout << "Invalid operation" << endl;
return 1;
}
cout << "Hexadecimal: " << hex << setw(4) << setfill('0') << result << endl;
cout << "Binary: " << bitset<16>(result) << endl;
return 0;
}

40
lab4/task_3.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,40 @@
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdint>
using namespace std;
struct Student {
char name[17];
uint16_t enrollment_year;
float gpa;
uint8_t gender : 1; // 0 = female, 1 = male
uint8_t courses_completed;
Student* group_leader;
};
void print_memory_info(const Student& student) {
cout << "Name address: " << &student.name << ", size: " << sizeof(student.name) << endl;
cout << "Year address: " << &student.enrollment_year << ", size: " << sizeof(student.enrollment_year) << endl;
cout << "GPA address: " << &student.gpa << ", size: " << sizeof(student.gpa) << endl;
cout << "Gender address: " << &student.gender << ", size: 1 bit" << endl;
cout << "Courses address: " << &student.courses_completed << ", size: " << sizeof(student.courses_completed) << endl;
cout << "Leader address: " << &student.group_leader << ", size: " << sizeof(student.group_leader) << endl;
}
int main() {
Student students[3] = {
{"Alice", 2021, 3.8, 0, 5, nullptr},
{"Bob", 2021, 3.5, 1, 4, nullptr},
{"Charlie", 2020, 3.9, 1, 6, &students[0]}
};
for (size_t i = 0; i < 3; ++i) {
cout << "Student " << i + 1 << " memory info:" << endl;
print_memory_info(students[i]);
cout << endl;
}
return 0;
}

63
lab4/task_4.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,63 @@
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
bool is_valid_filename(const char* filename) {
const char* invalid_chars = "*<>?|";
if (strpbrk(filename, invalid_chars)) return false;
const char* colon = strchr(filename, ':');
if (colon && (colon != filename + 1 || !isalpha(filename[0]) || colon[1] != '\n')) return false;
const char* ext = strrchr(filename, '.');
if (ext && strcasecmp(ext, ".txt") != 0) return false;
return true;
}
void add_txt_extension(char* filename) {
strcat(filename, ".txt");
}
int main() {
char filename[256];
cout << "Enter filename: ";
cin >> filename;
if (!is_valid_filename(filename)) {
cout << "Invalid filename." << endl;
return 1;
}
if (!strrchr(filename, '.')) add_txt_extension(filename);
ifstream file(filename, ios::binary | ios::ate);
if (!file.is_open()) {
cout << "Failed to open file." << endl;
return 1;
}
streamsize size = file.tellg();
file.seekg(0, ios::beg);
char* buffer = new char[size];
file.read(buffer, size);
char search[256];
cout << "Enter string to search: ";
cin >> search;
size_t count = 0;
char* pos = buffer;
while ((pos = strstr(pos, search))) {
++count;
pos += strlen(search);
}
cout << "Occurrences: " << count << endl;
delete[] buffer;
return 0;
}