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

main
FokinSA 4 месяцев назад
Родитель f87c0efce8
Сommit 6aac203481

@ -0,0 +1,59 @@
#include <iostream>
#include <iomanip>
#include <cstddef>
#include <cstring>
struct Student {
char name[17];
uint16_t year;
float gpa;
unsigned gender : 1;
uint8_t courses;
Student* leader;
};
void printStudentInfo(const Student &student) {
std::cout << "Èìÿ: " << student.name << std::endl;
std::cout << "Ãîä ïîñòóïëåíèÿ: " << student.year << std::endl;
std::cout << "Ñðåäíèé áàëë: " << student.gpa << std::endl;
std::cout << "Ïîë: " << (student.gender ? "Ìóæñêîé" : "Æåíñêèé") << std::endl;
std::cout << "Êîëè÷åñòâî ïðîéäåííûõ êóðñîâ: " << static_cast<int>(student.courses) << std::endl;
std::cout << "Óêàçàòåëü íà ñòàðîñòó: " << student.leader << std::endl;
}
void printMemoryLayout(const Student &student) {
std::cout << "Àäðåñ ñòðóêòóðû: " << &student << std::endl;
std::cout << "Ñìåùåíèå èìåíè: " << offsetof(Student, name) << ", ðàçìåð: " << sizeof(student.name) << std::endl;
std::cout << "Ñìåùåíèå ãîäà ïîñòóïëåíèÿ: " << offsetof(Student, year) << ", ðàçìåð: " << sizeof(student.year) << std::endl;
std::cout << "Ñìåùåíèå ñðåäíåãî áàëëà: " << offsetof(Student, gpa) << ", ðàçìåð: " << sizeof(student.gpa) << std::endl;
// Óáèðàåì ïîïûòêó èñïîëüçîâàíèÿ offsetof äëÿ gender
// std::cout << "Ñìåùåíèå ïîëà: " << offsetof(Student, gender) << ", ðàçìåð: 1 áèò" << std::endl;
std::cout << "Ïîë: çíà÷åíèÿ 1 áèò (âíóòðåííèé ïîðÿäîê íå îïðåäåëåí äëÿ offset-of)" << std::endl;
std::cout << "Ñìåùåíèå êóðñîâ: " << offsetof(Student, courses) << ", ðàçìåð: " << sizeof(student.courses) << std::endl;
std::cout << "Ñìåùåíèå óêàçàòåëÿ íà ñòàðîñòó: " << offsetof(Student, leader) << ", ðàçìåð: " << sizeof(student.leader) << std::endl;
std::cout << "Øåñòíàäöàòåðè÷íîå ïðåäñòàâëåíèå ñòóäåíòñêîé ñòðóêòóðû: ";
const unsigned char* bytes = reinterpret_cast<const unsigned char*>(&student);
for (size_t i = 0; i < sizeof(Student); ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]) << " ";
}
std::cout << std::dec << std::endl;
}
int main() {
Student student1 = {"Alice", 2021, 3.8, 0, 5, nullptr};
Student student2 = {"Bob", 2021, 3.5, 1, 6, &student1};
Student starosta = {"Charlie", 2020, 4.0, 1, 8, nullptr};
Student students[] = {student1, student2, starosta};
std::cout << "Àäðåñ ìàññèâà: " << &students << ", ðàçìåð: " << sizeof(students) << std::endl;
for (size_t i = 0; i < 3; ++i) {
std::cout << "\n--- Ñòóäåíò " << i + 1 << " ---" << std::endl;
printStudentInfo(students[i]);
printMemoryLayout(students[i]);
}
return 0;
}

@ -0,0 +1,72 @@
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
bool isValidFilename(const char* filename) {
const char* invalidChars = "*\"<>?|";
if (strpbrk(filename, invalidChars)) return false;
char* colon = strchr(filename, ':');
if (colon) {
if (colon != filename + 1 || !isalpha(filename[0]) || colon[1] != '\\') return false;
}
const char* extension = strrchr(filename, '.');
if (extension) {
char ext[5];
strncpy(ext, extension, 4);
ext[4] = '\0';
for (char& c : ext) c = tolower(c);
if (strncmp(ext, ".txt", 4) != 0) return false;
}
return true;
}
int main() {
char filename[256];
std::cout << "Ââåäèòå èìÿ ôàéëà: ";
std::cin.getline(filename, 256);
if (!isValidFilename(filename)) {
std::cerr << "Íåêîððåêòíîå èìÿ ôàéëà." << std::endl;
return 1;
}
if (!strrchr(filename, '.')) {
strcat(filename, ".txt");
}
std::ifstream file(filename, std::ios::binary);
if (!file) {
std::cerr << "Îøèáêà îòêðûòèÿ ôàéëà." << std::endl;
return 1;
}
file.seekg(0, std::ios::end);
std::streamsize fileSize = file.tellg();
file.seekg(0, std::ios::beg);
char* buffer = new char[fileSize + 1];
file.read(buffer, fileSize);
buffer[fileSize] = '\0';
file.close();
char searchString[256];
std::cout << "Ââåäèòå ñòðîêó äëÿ ïîèñêà: ";
std::cin.getline(searchString, 256);
size_t searchLength = strlen(searchString);
size_t count = 0;
char* position = buffer;
while ((position = strstr(position, searchString)) != nullptr) {
++count;
position += searchLength;
}
std::cout << "×èñëî âõîæäåíèé ñòðîêè: " << count << std::endl;
delete[] buffer;
return 0;
}

Двоичные данные
Контрольные вопросы.docx

Двоичный файл не отображается.

@ -0,0 +1,49 @@
#include <iostream>
#include <iomanip>
void print_in_hex(uint8_t byte) {
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 = static_cast<const uint8_t*>(data);
for (size_t i = 0; i < size; ++i) {
print_in_hex(bytes[i]);
if ((i + 1) % 16 == 0) std::cout << std::endl;
}
std::cout << std::endl;
}
void print_in_binary(uint8_t byte) {
for (int i = 7; i >= 0; --i) {
std::cout << ((byte >> i) & 1);
}
std::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) std::cout << std::endl;
}
std::cout << std::endl;
}
int main() {
uint8_t byte = 0b10101010;
uint8_t data[] = {0b00000001, 0b00000010, 0b00000011, 0b00000100,
0b00000101, 0b00000110, 0b00000111, 0b00001000,
0b00001001, 0b00001010, 0b00001011, 0b00001100,
0b00001101, 0b00001110, 0b00001111, 0b00010000};
print_in_hex(byte);
std::cout << std::endl;
print_in_hex(data, sizeof(data));
print_in_binary(byte);
std::cout << std::endl;
print_in_binary(data, sizeof(data));
return 0;
}

@ -0,0 +1,43 @@
#include <iostream>
#include <bitset>
#include <cstdint>
#include <iomanip>
int main() {
uint16_t operand1, operand2;
char operation;
std::cout << "Ââåäèòå ïåðâûé îïåðàíä (0-65535): ";
std::cin >> operand1;
std::cout << "Ââåäèòå îïåðàöèþ (&, |, ^): ";
std::cin >> operation;
std::cout << "Ââåäèòå âòîðîé îïåðàíä (0-65535): ";
std::cin >> operand2;
uint16_t result;
switch (operation) {
case '&':
result = operand1 & operand2;
break;
case '|':
result = operand1 | operand2;
break;
case '^':
result = operand1 ^ operand2;
break;
default:
std::cerr << "Îøèáêà: íåâåðíûé îïåðàòîð!" << std::endl;
return 1;
}
std::cout << "Ðåçóëüòàò â øåñòíàäöàòåðè÷íîì âèäå: 0x"
<< std::hex << std::uppercase << result << std::endl;
std::cout << "Ðåçóëüòàò â äâîè÷íîì âèäå: "
<< std::bitset<16>(result) << std::endl;
return 0;
}
Загрузка…
Отмена
Сохранить