Исправлены коментарии, задания выполнены до конца и добавлен тестовый файл

main
GorokhovDE 4 месяцев назад
Родитель a478b08794
Сommit 0927d1b42a

@ -1,107 +1,177 @@
#include <iostream>
#include <cassert>
#include <cstdint> // Äëÿ uint8_t, uint16_t
#include <cstring> // Äëÿ ðàáîòû ñ C-style ñòðîêàìè
#include <iomanip> // Äëÿ âûâîäà â ôîðìàòå hex
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cstdint>
#include <cstddef>
#include <string>
using namespace std;
// Ïå÷àòü áàéòà â øåñòíàäöàòåðè÷íîì âèäå
// 1. Ôóíêöèè äëÿ ïå÷àòè äàííûõ
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) {
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
for (size_t i = 0; i < size; i++) {
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 << '\n';
if ((i + 1) % 16 == 0) std::cout << '\n';
}
cout << dec << '\n'; // Âîçâðàùàåì äåñÿòè÷íûé ôîðìàò
std::cout << '\n';
}
// Ïå÷àòü áàéòà â äâîè÷íîì âèäå
void print_in_binary(uint8_t byte) {
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) {
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
for (size_t i = 0; i < size; i++) {
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 << '\n';
if ((i + 1) % 4 == 0) std::cout << '\n';
}
cout << '\n';
std::cout << '\n';
}
// 2. Êàëüêóëÿòîð äëÿ ïîáèòîâûõ îïåðàöèé
void bitwise_calculator() {
uint16_t operand1, operand2;
char op;
cout << "Enter first operand (uint16_t): ";
cin >> operand1;
cout << "Enter operator (&, |, ^): ";
cin >> op;
cout << "Enter second operand (uint16_t): ";
cin >> operand2;
uint16_t result = 0;
switch (op) {
char operation;
std::cout << "Enter the first operand: ";
std::cin >> operand1;
std::cout << "Enter the operator (&, |, ^): ";
std::cin >> operation;
std::cout << "Enter the second operand: ";
std::cin >> operand2;
uint16_t result;
switch (operation) {
case '&': result = operand1 & operand2; break;
case '|': result = operand1 | operand2; break;
case '^': result = operand1 ^ operand2; break;
default:
cerr << "Invalid operator!\n";
std::cerr << "Invalid operator!\n";
return;
}
cout << "Result in hex: ";
std::cout << "Result in hexadecimal format: ";
print_in_hex(&result, sizeof(result));
cout << "Result in binary: ";
std::cout << "Result in binary format: ";
print_in_binary(&result, sizeof(result));
}
// 3. Ðàáîòà ñî ñòðóêòóðàìè
struct Student {
char name[17]; // Èìÿ ñòóäåíòà
uint16_t year; // Ãîä ïîñòóïëåíèÿ
float average_score; // Ñðåäíèé áàëë
uint8_t gender : 1; // Ïîë: 0 - æåíñêèé, 1 - ìóæñêîé
uint8_t completed_courses; // Êîëè÷åñòâî êóðñîâ
Student* group_leader; // Óêàçàòåëü íà ñòàðîñòó
char name[17];
uint16_t year_of_admission;
float gpa;
unsigned gender : 1;
uint8_t completed_courses;
Student* group_leader;
};
void test_students() {
void analyze_students() {
Student students[3] = {
{"Alice", 2020, static_cast<float>(4.5), 0, 3, nullptr},
{"Bob", 2021, static_cast<float>(4.0), 1, 2, nullptr},
{"Charlie", 2020, static_cast<float>(4.2), 1, 5, nullptr}
{"Alice", 2020, 3.8, 0, 5, nullptr},
{"Bob", 2020, 3.5, 1, 5, nullptr},
{"Charlie", 2019, 4.0, 1, 6, nullptr}
};
// Ïå÷àòü àäðåñîâ è ðàçìåðîâ
cout << "Array address: " << &students << ", size: " << sizeof(students) << " bytes\n";
students[0].group_leader = &students[2];
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) {
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";
}
// Ïå÷àòü âñåõ ïîëåé
for (const auto& student : students) {
cout << "Name: " << student.name << ", Address: " << (void*)student.name << '\n';
cout << "Year: " << student.year << ", Address: " << &student.year << '\n';
cout << "Average Score: " << student.average_score << ", Address: " << &student.average_score << '\n';
cout << "Gender: " << static_cast<int>(student.gender) << '\n';
cout << "Completed Courses: " << static_cast<int>(student.completed_courses) << '\n';
cout << "Group Leader Address: " << student.group_leader << "\n\n";
Student& student = students[0];
std::cout << "\nAnalysis of Alice's fields:\n";
std::cout << " Name: Address=" << static_cast<void*>(student.name)
<< ", Offset=" << offsetof(Student, name)
<< ", Size=" << sizeof(student.name) << '\n';
std::cout << " Year of admission: Address=" << &student.year_of_admission
<< ", Offset=" << offsetof(Student, year_of_admission)
<< ", 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() {
// Âûçîâ ôóíêöèé
bitwise_calculator();
test_students();
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();
break;
case 3:
analyze_students();
break;
case 4:
process_text_file();
break;
default:
std::cerr << "Invalid choice!\n";
}
return 0;
}

@ -0,0 +1,3 @@
Hello, world!
This is a test file.
Hello again!
Загрузка…
Отмена
Сохранить