Загрузил(а) файлы в ''

main
IvanchenkoIS 4 месяцев назад
Родитель 3bb8fdf86f
Сommit 8b447be513

68
1.cpp

@ -0,0 +1,68 @@
#include <iostream>
#include <cassert>
#include <cstdint>
using namespace std;
// Преобразует число от 0 до 15 в символ шестнадцатеричной системы
char to_hex_digit(uint8_t value) {
assert(value < 16);
const char hex_chars[] = "0123456789abcdef";
return hex_chars[value];
}
// Возвращает символ, представляющий бит ('0' или '1')
char get_bit_char(uint8_t byte, uint8_t pos) {
return (byte & (1 << pos)) ? '1' : '0';
}
// Преобразует указатель на void в указатель на uint8_t
const uint8_t* to_byte_array(const void* ptr) {
return static_cast<const uint8_t*>(ptr);
}
// Выводит байт в шестнадцатеричном формате
void display_hex_byte(uint8_t byte) {
cout << to_hex_digit(byte >> 4) << to_hex_digit(byte & 0x0F);
}
// Выводит данные в шестнадцатеричном формате
void display_hex_data(const void* data, size_t length) {
const uint8_t* bytes = to_byte_array(data);
for (size_t idx = 0; idx < length; ++idx) {
display_hex_byte(bytes[idx]);
cout << ((idx + 1) % 16 == 0 ? '\n' : ' ');
}
}
// Выводит байт в двоичном формате
void display_binary_byte(uint8_t byte) {
for (int pos = 7; pos >= 0; --pos) {
cout << get_bit_char(byte, pos);
}
}
// Выводит данные в двоичном формате
void display_binary_data(const void* data, size_t length) {
const uint8_t* bytes = to_byte_array(data);
for (size_t idx = 0; idx < length; ++idx) {
display_binary_byte(bytes[idx]);
cout << ((idx + 1) % 4 == 0 ? '\n' : ' ');
}
}
// Пример использования функций
int main() {
assert(to_hex_digit(0x0) == '0');
assert(to_hex_digit(0xA) == 'a');
assert(to_hex_digit(0xF) == 'f');
uint32_t sample = 0x42;
cout << "Hexadecimal representation:\n";
display_hex_data(&sample, sizeof(sample));
cout << "\n\nBinary representation:\n";
display_binary_data(&sample, sizeof(sample));
cout << '\n';
return 0;
}

72
2.cpp

@ -0,0 +1,72 @@
#include <cstdint>
#include <iostream>
using namespace std;
char hex_digit(uint8_t value) {
static const char hex_chars[] = "0123456789ABCDEF";
return hex_chars[value];
}
void display_hex_byte(uint8_t byte) {
cout << hex_digit(byte >> 4) << hex_digit(byte & 0x0F);
}
void display_hex_value(uint16_t data) {
uint8_t* bytes = reinterpret_cast<uint8_t*>(&data);
display_hex_byte(bytes[0]);
cout << " ";
display_hex_byte(bytes[1]);
}
void display_binary_byte(uint8_t byte) {
for (int pos = 7; pos >= 0; --pos) {
cout << ((byte >> pos) & 1);
}
}
void display_binary_value(uint16_t data) {
uint8_t* bytes = reinterpret_cast<uint8_t*>(&data);
display_binary_byte(bytes[0]);
display_binary_byte(bytes[1]);
}
int main() {
uint16_t num1, num2;
char op;
cin >> num1 >> op >> num2;
uint16_t result;
switch (op) {
case '&':
result = num1 & num2;
break;
case '|':
result = num1 | num2;
break;
case '^':
result = num1 ^ num2;
break;
default:
cout << "Ошибка: доступны только &, | и ^" << endl;
return 1;
}
cout << num1 << " " << op << " " << num2 << endl;
display_hex_value(num1);
cout << " " << op << " ";
display_hex_value(num2);
cout << " = ";
display_hex_value(result);
cout << endl;
display_binary_value(num1);
cout << " " << op << endl;
display_binary_value(num2);
cout << " =" << endl;
display_binary_value(result);
cout << endl;
return 0;
}

73
3.cpp

@ -0,0 +1,73 @@
#include <cstdint>
#include <iostream>
#include <iomanip>
using namespace std;
char to_hex_char(uint8_t value) {
static const char hex_table[] = "0123456789ABCDEF";
return hex_table[value];
}
void print_hex_byte(uint8_t byte) {
cout << to_hex_char(byte >> 4) << to_hex_char(byte & 0x0F);
}
void print_hex_number(uint16_t value) {
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
print_hex_byte(bytes[0]);
cout << " ";
print_hex_byte(bytes[1]);
}
void print_binary_byte(uint8_t byte) {
for (int i = 7; i >= 0; --i) {
cout << ((byte >> i) & 1);
}
}
void print_binary_number(uint16_t value) {
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
print_binary_byte(bytes[0]);
print_binary_byte(bytes[1]);
}
int main() {
uint16_t input1, input2;
char operation;
cin >> input1 >> operation >> input2;
uint16_t outcome;
switch (operation) {
case '&':
outcome = input1 & input2;
break;
case '|':
outcome = input1 | input2;
break;
case '^':
outcome = input1 ^ input2;
break;
default:
cout << "Ошибка: доступны только &, | и ^" << endl;
return 1;
}
cout << input1 << " " << operation << " " << input2 << endl;
print_hex_number(input1);
cout << " " << operation << " ";
print_hex_number(input2);
cout << " = ";
print_hex_number(outcome);
cout << endl;
print_binary_number(input1);
cout << " " << operation << endl;
print_binary_number(input2);
cout << " =" << endl;
print_binary_number(outcome);
cout << endl;
return 0;
}

127
4.cpp

@ -0,0 +1,127 @@
#include <iostream>
#include <cstdio> // Для работы с файлами и C-строками
#include <cstring> // Для обработки строк
#include <cctype> // Для проверки символов
#include <cstdlib> // Для управления памятью
using namespace std;
const size_t MAX_FILENAME_LENGTH = 260; // Максимальная длина имени файла
const size_t MAX_SEARCH_STRING_LENGTH = 256; // Максимальная длина строки поиска
const char* invalid_chars = "*\"<>?|";
bool isValidFilename(const char* filename) {
for (size_t i = 0; i < strlen(invalid_chars); ++i) {
if (strchr(filename, invalid_chars[i]) != nullptr) {
return false;
}
}
const char* colon = strchr(filename, ':');
if (colon != nullptr) {
if (colon - filename != 1 || !isalpha(filename[0]) || *(colon + 1) != '\\') {
return false;
}
}
const char* extension = strrchr(filename, '.');
if (extension != nullptr) {
if (strlen(extension) != 4) return false;
char ext[5];
strncpy(ext, extension, 4);
ext[4] = '\0';
for (int i = 0; i < 4; ++i) ext[i] = tolower(ext[i]);
return strcmp(ext, ".txt") == 0;
}
return true;
}
void ensureTxtExtension(char* filename) {
if (strrchr(filename, '.') == nullptr) {
strcat(filename, ".txt");
}
}
int main() {
char filename[MAX_FILENAME_LENGTH];
cout << "Введите имя файла: ";
cin.getline(filename, MAX_FILENAME_LENGTH);
if (!isValidFilename(filename)) {
cout << "Неверное имя файла." << endl;
return 1;
}
ensureTxtExtension(filename);
if (!isValidFilename(filename)) {
cout << "Ошибка в имени после добавления расширения." << endl;
return 1;
}
FILE* file = fopen(filename, "rb");
if (file == nullptr) {
cout << "Ошибка открытия файла: " << filename << endl;
return 1;
}
if (fseek(file, 0, SEEK_END) != 0) {
cout << "Ошибка перехода в конец файла." << endl;
fclose(file);
return 1;
}
long file_size = ftell(file);
if (file_size == -1L) {
cout << "Не удалось узнать размер файла." << endl;
fclose(file);
return 1;
}
rewind(file);
char* content = (char*)malloc(file_size + 1);
if (content == nullptr) {
cout << "Ошибка выделения памяти." << endl;
fclose(file);
return 1;
}
size_t read_size = fread(content, sizeof(char), file_size, file);
if (read_size != (size_t)file_size) {
cout << "Ошибка чтения файла." << endl;
free(content);
fclose(file);
return 1;
}
content[file_size] = '\0';
fclose(file);
char search_str[MAX_SEARCH_STRING_LENGTH];
cout << "Введите строку для поиска: ";
cin.getline(search_str, MAX_SEARCH_STRING_LENGTH);
int count = 0;
char* pos = content;
size_t search_len = strlen(search_str);
if (search_len == 0) {
cout << "Пустая строка для поиска." << endl;
free(content);
return 1;
}
while ((pos = strstr(pos, search_str)) != nullptr) {
count++;
pos += search_len;
}
cout << "Частота появления строки \"" << search_str << "\": " << count << endl;
free(content);
return 0;
}
Загрузка…
Отмена
Сохранить