Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

301 строка
7.2 KiB
C++

#include <iostream>
#include <cassert>
#include <iomanip>
#include <bitset>
#include <cstddef>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace std;
//char nibble_to_hex(uint8_t i)
char nibble_to_hex(uint8_t i) //Conversion to hex format
{
assert(0x0 <= i && i <= 0xf);
return "0123456789abcdef"[i];
}
void print_in_hex(uint8_t byte)
{
cout << nibble_to_hex(byte >> 4)
<< nibble_to_hex(byte & 0xf);
}
const uint8_t* as_bytes(const void* data);
void print_in_hex(const void* data, size_t size)
{
const uint8_t* bytes = as_bytes(data);
for (size_t i = 0; i < size; i++)
{
print_in_hex(bytes[i]);
if ((i + 1) % 16 == 0)
{
cout << '\n';
}
else
{
cout << ' ';
}
}
}
const uint8_t* as_bytes(const void* data)
{
return reinterpret_cast<const uint8_t*>(data);
}
char
bit_digit(uint8_t byte, uint8_t bit) {
if (byte & (0x1 << bit)) {
return '1';
}
return '0';
}
void print_in_binary(uint8_t byte) {
for (int bit = 7; bit >= 0; bit--) {
cout << bit_digit(byte, bit);
}
}
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++) {
// Äîáàâëÿåì ïðîáåë ïåðåä áàéòîì, êðîìå ïåðâîãî â ñòðîêå
if (i % 4 == 0 && i != 0) {
cout << "\n";
}
else if (i != 0) {
cout << " ";
}
print_in_binary(bytes[i]);
}
cout << "\n"; // Ïåðåíîñ ñòðîêè â êîíöå
}
struct Example {
uint8_t a;
uint16_t b;
uint32_t c;
char d[3];
};
void print_in_hex(uint16_t value) {
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
for (int i = 1; i >= 0; i--) {
cout << nibble_to_hex(bytes[i] >> 4) << nibble_to_hex(bytes[i] & 0xf) << " ";
}
}
void print_in_binary(uint16_t value) {
bitset<16> bits(value);
cout << bits;
}
struct Student {
char name[17]; // 17 áàéò
uint16_t year; // 2 áàéòà
float gpa; // 4 áàéòà
unsigned gender : 1; // 1 áèò (áèòîâîå ïîëå)
uint8_t courses; // 1 áàéò
Student* monitor; // 8 áàéò (äëÿ 64-áèòíîé ñèñòåìû)
};
bool is_valid_filename(const char* name) {
const char* forbidden = "*\"<>?|";
for (size_t i = 0; i < strlen(name); i++) {
if (strchr(forbidden, name[i])) return false;
}
// Ïðîâåðêà äâîåòî÷èÿ
const char* colon = strchr(name, ':');
if (colon) {
if (colon - name != 1 || !isalpha(name[0]) || *(colon + 1) != '\\')
return false;
}
// Ïðîâåðêà ðàñøèðåíèÿ
const char* dot = strrchr(name, '.');
if (dot) {
if (strncasecmp(dot, ".txt", 4) != 0) return false;
}
return true;
}
int main() {
// Òåñòèðîâàíèå ÷èñåë èç ëåêöèîííîãî ñëàéäà
uint16_t a = 0x4d2; // 1234 â äåñÿòè÷íîé
uint16_t b = 0x1234; // 4660 â äåñÿòè÷íîé
cout << "0x4d2 (1234) in binary:\n";
print_in_binary(&a, sizeof(a));
cout << "\n\n";
cout << "0x1234 (4660) in binary:\n";
print_in_binary(&b, sizeof(b));
cout << "\n\n";
// Ïðèìåð ñ àðèôìåòè÷åñêèìè îïåðàöèÿìè
uint16_t sum = a + b;
uint16_t diff = b - a;
uint16_t xorr = a ^ b;
cout << "Sum (0x4d2 + 0x1234):\n";
print_in_binary(&sum, sizeof(sum));
cout << "\n\n";
cout << "Difference (0x1234 - 0x4d2):\n";
print_in_binary(&diff, sizeof(diff));
cout << "\n\n";
cout << "XOR (0x4d2 ^ 0x1234):\n";
print_in_binary(&xorr, sizeof(xorr));
cout << "\n\n";
Example arr[2] = {
{0x01, 0x0203, 0x04050607, {'a', 'b', 'c'}},
{0x08, 0x090a, 0x0b0c0d0e, {'d', 'e', 'f'}}
};
cout << "The location of the structure in memory (hex):\n";
print_in_hex(arr, sizeof(arr));
cout << "\n\nThe location of the structure in memory (binary):\n";
print_in_binary(arr, sizeof(arr));
Student students[3] = {
{"Ivanov Ivan", 2023, 4.5f, 1, 3, &students[1]}, // 'f' suffix for float
{"Petrova Anna", 2022, 4.8f, 0, 4, nullptr},
{"Sidorov Alex", 2023, 4.2f, 1, 2, &students[1]}
};
// Structure analysis
std::cout << "Array address: " << &students << std::endl;
std::cout << "Array size: " << sizeof(students) << " bytes\n\n";
for (size_t i = 0; i < 3; i++) {
std::cout << "Element " << i << ":\n";
std::cout << "Address: " << &students[i] << std::endl;
std::cout << "Size: " << sizeof(students[i]) << " bytes\n";
}
Student& s = students[0];
std::cout << "\nFields of the first student:\n";
std::cout << "name: Address=" << static_cast<void*>(s.name)
<< " Offset=" << offsetof(Student, name)
<< " Size=17 bytes\n";
std::cout << "year: Address=" << &s.year
<< " Offset=" << offsetof(Student, year)
<< " Size=2 bytes\n";
std::cout << "gpa: Address=" << &s.gpa
<< " Offset=" << offsetof(Student, gpa)
<< " Size=4 bytes\n";
std::cout << "courses:Address=" << static_cast<void*>(&s.courses)
<< " Offset=" << offsetof(Student, courses)
<< " Size=1 byte\n";
std::cout << "monitor:Address=" << &s.monitor
<< " Offset=" << offsetof(Student, monitor)
<< " Size=8 bytes\n";
cout << endl;
char filename[256];
cout << "Enter file name: ";
cin.getline(filename, 256);
if (!is_valid_filename(filename)) {
cerr << "Incorrect file name!";
return 1;
}
// Äîáàâëåíèå .txt ïðè îòñóòñòâèè ðàñøèðåíèÿ
if (!strrchr(filename, '.')) {
strcat(filename, ".txt");
}
// Çàãðóçêà ôàéëà
ifstream file(filename, ios::binary | ios::ate);
if (!file) {
cerr << "Error open file!";
return 1;
}
streamsize size = file.tellg();
file.seekg(0, ios::beg);
char* buffer = new char[size + 1];
file.read(buffer, size);
buffer[size] = '\0';
// Ïîèñê ïîäñòðîêè
char query[256];
cout << "Enter line for find: ";
cin.getline(query, 256);
int count = 0;
const char* ptr = buffer;
while ((ptr = strstr(ptr, query)) != nullptr) {
count++;
ptr += strlen(query);
}
cout << "number of occurrences: " << count << endl;
delete[] buffer;
cout << endl;
char op;
cout << "Enter first operand (hex): ";
cin >> hex >> a;
cout << "Enter operator (&, |, ^): ";
cin >> op;
cout << "Enter second operator (hex): ";
cin >> hex >> b;
uint16_t result;
switch (op) {
case '&': result = a & b; break;
case '|': result = a | b; break;
case '^': result = a ^ b; break;
default:
cerr << "Operator incorrect!";
return 1;
}
// Âûâîä â hex
cout << "\nResult (hex): ";
print_in_hex(a);
cout << " " << op << " ";
print_in_hex(b);
cout << " = ";
print_in_hex(result);
// Âûâîä â binary
cout << "\nResult (binary):\n";
print_in_binary(a);
cout << " " << op << "\n";
print_in_binary(b);
cout << " =\n";
print_in_binary(result);
cout << endl;
system("pause");
return 0;
}