Родитель
a478b08794
Сommit
0927d1b42a
@ -1,107 +1,177 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cassert>
|
#include <fstream>
|
||||||
#include <cstdint> // Äëÿ uint8_t, uint16_t
|
#include <iomanip>
|
||||||
#include <cstring> // Äëÿ ðàáîòû ñ C-style ñòðîêàìè
|
#include <cstring>
|
||||||
#include <iomanip> // Äëÿ âûâîäà â ôîðìàòå hex
|
#include <cstdint>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
// 1. Ôóíêöèè äëÿ ïå÷àòè äàííûõ
|
||||||
|
|
||||||
// Ïå÷àòü áàéòà â øåñòíàäöàòåðè÷íîì âèäå
|
|
||||||
void print_in_hex(uint8_t byte) {
|
void print_in_hex(uint8_t byte) {
|
||||||
cout << hex << uppercase << (byte >> 4) << (byte & 0x0F) << ' ';
|
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ïå÷àòü ìàññèâà â øåñòíàäöàòåðè÷íîì âèäå
|
|
||||||
void print_in_hex(const void* data, size_t size) {
|
void print_in_hex(const void* data, size_t size) {
|
||||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
|
const uint8_t* bytes = static_cast<const uint8_t*>(data);
|
||||||
for (size_t i = 0; i < size; i++) {
|
for (size_t i = 0; i < size; ++i) {
|
||||||
print_in_hex(bytes[i]);
|
print_in_hex(bytes[i]);
|
||||||
if ((i + 1) % 16 == 0) cout << '\n';
|
if ((i + 1) % 16 == 0) std::cout << '\n';
|
||||||
}
|
}
|
||||||
cout << dec << '\n'; // Âîçâðàùàåì äåñÿòè÷íûé ôîðìàò
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ïå÷àòü áàéòà â äâîè÷íîì âèäå
|
|
||||||
void print_in_binary(uint8_t byte) {
|
void print_in_binary(uint8_t byte) {
|
||||||
for (int i = 7; i >= 0; --i) {
|
for (int i = 7; i >= 0; --i) {
|
||||||
cout << ((byte >> i) & 1);
|
std::cout << ((byte >> i) & 1);
|
||||||
}
|
}
|
||||||
cout << ' ';
|
std::cout << ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ïå÷àòü ìàññèâà â äâîè÷íîì âèäå
|
|
||||||
void print_in_binary(const void* data, size_t size) {
|
void print_in_binary(const void* data, size_t size) {
|
||||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
|
const uint8_t* bytes = static_cast<const uint8_t*>(data);
|
||||||
for (size_t i = 0; i < size; i++) {
|
for (size_t i = 0; i < size; ++i) {
|
||||||
print_in_binary(bytes[i]);
|
print_in_binary(bytes[i]);
|
||||||
if ((i + 1) % 4 == 0) cout << '\n';
|
if ((i + 1) % 4 == 0) std::cout << '\n';
|
||||||
}
|
}
|
||||||
cout << '\n';
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. Êàëüêóëÿòîð äëÿ ïîáèòîâûõ îïåðàöèé
|
||||||
void bitwise_calculator() {
|
void bitwise_calculator() {
|
||||||
uint16_t operand1, operand2;
|
uint16_t operand1, operand2;
|
||||||
char op;
|
char operation;
|
||||||
cout << "Enter first operand (uint16_t): ";
|
|
||||||
cin >> operand1;
|
std::cout << "Enter the first operand: ";
|
||||||
cout << "Enter operator (&, |, ^): ";
|
std::cin >> operand1;
|
||||||
cin >> op;
|
std::cout << "Enter the operator (&, |, ^): ";
|
||||||
cout << "Enter second operand (uint16_t): ";
|
std::cin >> operation;
|
||||||
cin >> operand2;
|
std::cout << "Enter the second operand: ";
|
||||||
|
std::cin >> operand2;
|
||||||
uint16_t result = 0;
|
|
||||||
switch (op) {
|
uint16_t result;
|
||||||
|
switch (operation) {
|
||||||
case '&': result = operand1 & operand2; break;
|
case '&': result = operand1 & operand2; break;
|
||||||
case '|': result = operand1 | operand2; break;
|
case '|': result = operand1 | operand2; break;
|
||||||
case '^': result = operand1 ^ operand2; break;
|
case '^': result = operand1 ^ operand2; break;
|
||||||
default:
|
default:
|
||||||
cerr << "Invalid operator!\n";
|
std::cerr << "Invalid operator!\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "Result in hex: ";
|
std::cout << "Result in hexadecimal format: ";
|
||||||
print_in_hex(&result, sizeof(result));
|
print_in_hex(&result, sizeof(result));
|
||||||
|
|
||||||
cout << "Result in binary: ";
|
std::cout << "Result in binary format: ";
|
||||||
print_in_binary(&result, sizeof(result));
|
print_in_binary(&result, sizeof(result));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3. Ðàáîòà ñî ñòðóêòóðàìè
|
||||||
struct Student {
|
struct Student {
|
||||||
char name[17]; // Èìÿ ñòóäåíòà
|
char name[17];
|
||||||
uint16_t year; // Ãîä ïîñòóïëåíèÿ
|
uint16_t year_of_admission;
|
||||||
float average_score; // Ñðåäíèé áàëë
|
float gpa;
|
||||||
uint8_t gender : 1; // Ïîë: 0 - æåíñêèé, 1 - ìóæñêîé
|
unsigned gender : 1;
|
||||||
uint8_t completed_courses; // Êîëè÷åñòâî êóðñîâ
|
uint8_t completed_courses;
|
||||||
Student* group_leader; // Óêàçàòåëü íà ñòàðîñòó
|
Student* group_leader;
|
||||||
};
|
};
|
||||||
|
|
||||||
void test_students() {
|
void analyze_students() {
|
||||||
Student students[3] = {
|
Student students[3] = {
|
||||||
{"Alice", 2020, static_cast<float>(4.5), 0, 3, nullptr},
|
{"Alice", 2020, 3.8, 0, 5, nullptr},
|
||||||
{"Bob", 2021, static_cast<float>(4.0), 1, 2, nullptr},
|
{"Bob", 2020, 3.5, 1, 5, nullptr},
|
||||||
{"Charlie", 2020, static_cast<float>(4.2), 1, 5, nullptr}
|
{"Charlie", 2019, 4.0, 1, 6, nullptr}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Ïå÷àòü àäðåñîâ è ðàçìåðîâ
|
students[0].group_leader = &students[2];
|
||||||
cout << "Array address: " << &students << ", size: " << sizeof(students) << " bytes\n";
|
students[1].group_leader = &students[2];
|
||||||
|
|
||||||
|
std::cout << "Array address: " << &students << "\n";
|
||||||
|
std::cout << "Array size: " << sizeof(students) << " bytes\n\n";
|
||||||
|
|
||||||
for (size_t i = 0; i < 3; ++i) {
|
for (size_t i = 0; i < 3; ++i) {
|
||||||
cout << "Student " << i + 1 << " address: " << &students[i] << ", size: " << sizeof(students[i]) << " bytes\n";
|
std::cout << "Student " << i + 1 << ":\n";
|
||||||
|
std::cout << " Address: " << &students[i] << "\n";
|
||||||
|
std::cout << " Size: " << sizeof(students[i]) << " bytes\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ïå÷àòü âñåõ ïîëåé
|
Student& student = students[0];
|
||||||
for (const auto& student : students) {
|
std::cout << "\nAnalysis of Alice's fields:\n";
|
||||||
cout << "Name: " << student.name << ", Address: " << (void*)student.name << '\n';
|
std::cout << " Name: Address=" << static_cast<void*>(student.name)
|
||||||
cout << "Year: " << student.year << ", Address: " << &student.year << '\n';
|
<< ", Offset=" << offsetof(Student, name)
|
||||||
cout << "Average Score: " << student.average_score << ", Address: " << &student.average_score << '\n';
|
<< ", Size=" << sizeof(student.name) << '\n';
|
||||||
cout << "Gender: " << static_cast<int>(student.gender) << '\n';
|
std::cout << " Year of admission: Address=" << &student.year_of_admission
|
||||||
cout << "Completed Courses: " << static_cast<int>(student.completed_courses) << '\n';
|
<< ", Offset=" << offsetof(Student, year_of_admission)
|
||||||
cout << "Group Leader Address: " << student.group_leader << "\n\n";
|
<< ", Size=" << sizeof(student.year_of_admission) << '\n';
|
||||||
|
std::cout << " GPA: Address=" << &student.gpa
|
||||||
|
<< ", Offset=" << offsetof(Student, gpa)
|
||||||
|
<< ", Size=" << sizeof(student.gpa) << '\n';
|
||||||
|
std::cout << " Number of courses: Address=" << static_cast<void*>(&student.completed_courses)
|
||||||
|
<< ", Offset=" << offsetof(Student, completed_courses)
|
||||||
|
<< ", Size=" << sizeof(student.completed_courses) << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Îáðàáîòêà òåêñòîâîãî ôàéëà
|
||||||
|
void process_text_file() {
|
||||||
|
const char* filename = "text.txt";
|
||||||
|
|
||||||
|
std::ifstream file(filename, std::ios::binary);
|
||||||
|
if (!file) {
|
||||||
|
std::cerr << "File open error!\n";
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file.seekg(0, std::ios::end);
|
||||||
|
size_t filesize = file.tellg();
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
char* buffer = new char[filesize];
|
||||||
|
file.read(buffer, filesize);
|
||||||
|
|
||||||
|
std::cout << "Enter the string to search for: ";
|
||||||
|
char search_str[256];
|
||||||
|
std::cin.ignore();
|
||||||
|
std::cin.getline(search_str, 256);
|
||||||
|
|
||||||
|
size_t count = 0;
|
||||||
|
char* pos = buffer;
|
||||||
|
while ((pos = strstr(pos, search_str)) != nullptr) {
|
||||||
|
++count;
|
||||||
|
pos += strlen(search_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Number of occurrences: " << count << "\n";
|
||||||
|
|
||||||
|
delete[] buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Âûçîâ ôóíêöèé
|
std::cout << "Choose a task: 1 - Print, 2 - Calculator, 3 - Students, 4 - File\n";
|
||||||
|
int choice;
|
||||||
|
std::cin >> choice;
|
||||||
|
|
||||||
|
switch (choice) {
|
||||||
|
case 1:
|
||||||
|
// Äåìîíñòðàöèÿ ôóíêöèé ïå÷àòè
|
||||||
|
{
|
||||||
|
uint16_t data = 0xABCD;
|
||||||
|
std::cout << "Hexadecimal output:\n";
|
||||||
|
print_in_hex(&data, sizeof(data));
|
||||||
|
std::cout << "Binary output:\n";
|
||||||
|
print_in_binary(&data, sizeof(data));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
bitwise_calculator();
|
bitwise_calculator();
|
||||||
test_students();
|
break;
|
||||||
|
case 3:
|
||||||
|
analyze_students();
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
process_text_file();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cerr << "Invalid choice!\n";
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
Hello, world!
|
||||||
|
This is a test file.
|
||||||
|
Hello again!
|
Загрузка…
Ссылка в новой задаче