Сommit
f9d4937741
@ -0,0 +1,73 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <locale.h>
|
||||
#include <cstdlib>
|
||||
|
||||
void print_in_hex(uint8_t byte)
|
||||
{
|
||||
printf("%04X ", byte);
|
||||
}
|
||||
|
||||
void print_in_hex(const void* data, size_t size)
|
||||
{
|
||||
const uint8_t* bytes = (const uint8_t*)data;
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
print_in_hex(bytes[i]);
|
||||
if ((i + 1) % 16 == 0)
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void print_in_binary(uint8_t byte)
|
||||
{
|
||||
for (int i = 7; i >= 0; i--)
|
||||
{
|
||||
printf("%d", (byte >> i) & 1);
|
||||
if (i % 4 == 0)
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
void print_in_binary(const void* data, size_t size)
|
||||
{
|
||||
const uint8_t* bytes = (const uint8_t*)data;
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
{
|
||||
print_in_binary(bytes[i]);
|
||||
if ((i + 1) % 4 == 0)
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
uint8_t byte = 127;
|
||||
|
||||
printf("Source number: %hhu\n", byte);
|
||||
printf("Number in hexadecimal: ");
|
||||
print_in_hex(byte);
|
||||
printf("\n");
|
||||
printf("Binary number: ");
|
||||
print_in_binary(byte);
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
const int SIZE = 10;
|
||||
uint8_t data[] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x00, 0xA1 };
|
||||
size_t size = sizeof(data);
|
||||
|
||||
printf("Source data block in decimal form\n");
|
||||
for (int i = 0; i < SIZE; ++i)
|
||||
printf("%hhu ", data[i]);
|
||||
printf("\n");
|
||||
|
||||
printf("Data block in hexadecimal\n");
|
||||
print_in_hex(data, size);
|
||||
printf("Binary data block\n");
|
||||
print_in_binary(data, size);
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <locale.h>
|
||||
#include <cstdlib>
|
||||
|
||||
uint16_t reverse(uint16_t x)
|
||||
{
|
||||
x = (x & 0xFF) << 8 | (x & 0xFF00) >> 8;
|
||||
return x;
|
||||
}
|
||||
|
||||
void print_in_hex(uint16_t num)
|
||||
{
|
||||
printf("%04X ", num);
|
||||
}
|
||||
|
||||
void print_in_binary(uint16_t num)
|
||||
{
|
||||
for (int i = 15; i >= 0; i--)
|
||||
{
|
||||
printf("%d", (num >> i) & 1);
|
||||
if (i % 8 == 0)
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
uint16_t operand1, operand2;
|
||||
char symbol;
|
||||
printf("Enter the first operand, then the operator(&, | or ^), then the second operand.\n");
|
||||
scanf("%hu %c %hu", &operand1, &symbol, &operand2);
|
||||
|
||||
operand1 = reverse(operand1);
|
||||
operand2 = reverse(operand2);
|
||||
|
||||
uint16_t result;
|
||||
switch (symbol)
|
||||
{
|
||||
case '&':
|
||||
result = operand1 & operand2;
|
||||
break;
|
||||
case '|':
|
||||
result = operand1 | operand2;
|
||||
break;
|
||||
case '^':
|
||||
result = operand1 ^ operand2;
|
||||
break;
|
||||
default:
|
||||
printf("Invalid operator\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("\nResult: ");
|
||||
print_in_hex(operand1);
|
||||
printf("%c ", symbol);
|
||||
print_in_hex(operand2);
|
||||
printf("= ");
|
||||
print_in_binary(operand1);
|
||||
printf("%c ", symbol);
|
||||
print_in_binary(operand2);
|
||||
printf("= ");
|
||||
print_in_binary(result);
|
||||
printf("\n");
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
#include <iostream>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void print_in_hex(uint8_t byte)
|
||||
{
|
||||
printf("%02X ", byte);
|
||||
}
|
||||
|
||||
void print_in_hex(const void* data, size_t size)
|
||||
{
|
||||
const uint8_t* bytes = (const uint8_t*)data;
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
print_in_hex(bytes[i]);
|
||||
if ((i + 1) % 8 == 0)
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void print_in_binary(uint8_t byte)
|
||||
{
|
||||
for (int i = 7; i >= 0; i--)
|
||||
{
|
||||
printf("%d", (byte >> i) & 1);
|
||||
if (i % 8 == 0)
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
void print_in_binary(const void* data, size_t size)
|
||||
{
|
||||
const uint8_t* bytes = (const uint8_t*)data;
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
{
|
||||
print_in_binary(bytes[i]);
|
||||
if ((i + 1) % 8 == 0)
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
struct Student
|
||||
{
|
||||
char _name[17];
|
||||
uint16_t _year;
|
||||
float _averageGrade;
|
||||
uint8_t _gender : 1;
|
||||
// 0 - female, 1 - male
|
||||
uint8_t _numCourses : 7;
|
||||
Student* _leader;
|
||||
};
|
||||
#pragma pack()
|
||||
|
||||
int main()
|
||||
{
|
||||
Student students[3] = {0};
|
||||
|
||||
strcpy(students[0]._name, "Alex");
|
||||
students[0]._year = 2021;
|
||||
students[0]._averageGrade = 3.7f;
|
||||
students[0]._gender = 1;
|
||||
students[0]._numCourses = 6;
|
||||
students[0]._leader = nullptr;
|
||||
|
||||
strcpy(students[1]._name, "Masha");
|
||||
students[1]._year = 2020;
|
||||
students[1]._averageGrade = 4.6f;
|
||||
students[1]._gender = 0;
|
||||
students[1]._numCourses = 4;
|
||||
students[1]._leader = &students[0];
|
||||
|
||||
strcpy(students[2]._name, "John");
|
||||
students[2]._year = 2021;
|
||||
students[2]._averageGrade = 4.1f;
|
||||
students[2]._gender = 1;
|
||||
students[2]._numCourses = 5;
|
||||
students[2]._leader = &students[0];
|
||||
|
||||
cout << "Array address: " << &students << endl;
|
||||
cout << "Array size: " << sizeof(students) << " byte" << endl;
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
cout << "Element address " << i << ": " << &students[i] << endl;
|
||||
cout << "Element Size " << i << ": " << sizeof(students[i]) << " byte" << endl;
|
||||
}
|
||||
|
||||
int index = 1;
|
||||
Student& student = students[index];
|
||||
cout << "Student information " << index << ":" << endl;
|
||||
cout << "Field value _name: " << student._name << endl;
|
||||
cout << "Field address _name: " << &(student._name) << endl;
|
||||
cout << "Field offset _name from the beginning of the structure: " << offsetof(Student, _name) << " byte" << endl;
|
||||
cout << "Field size _name: " << sizeof(student._name) << " byte" << endl;
|
||||
cout << "Hexadecimal representation of the field _name:" << endl;
|
||||
print_in_hex(student._name, sizeof(student._name));
|
||||
cout << "Binary field representation _name:" << endl;
|
||||
print_in_binary(student._name, sizeof(student._name));
|
||||
cout << endl;
|
||||
|
||||
cout << "Array elements in hexadecimal:" << endl;
|
||||
cout << "(First " << sizeof(student._name) << " byte - field _name):" << endl;
|
||||
cout << "(Next " << sizeof(student._year) << " bytes - field _year):" << endl;
|
||||
cout << "(Next " << sizeof(student._averageGrade) << " bytes - field _averageGrade):" << endl;
|
||||
cout << "(Next " << sizeof(uint8_t) << " bytes - 1 bit for _gender and 7 bit for _numCourses):" << endl;
|
||||
cout << "(Next " << sizeof(student._leader) << " bytes - field _leader):" << endl;
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
cout << "Student with index " << i << endl;
|
||||
print_in_hex(&students[i], sizeof(students[i]));
|
||||
}
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
char filename[256];
|
||||
cout << "Enter file name: ";
|
||||
cin.getline(filename, sizeof(filename));
|
||||
|
||||
bool isValid = true;
|
||||
|
||||
const char* forbiddenChars = "*\"<>?|";
|
||||
if (strpbrk(filename, forbiddenChars) != 0)
|
||||
isValid = false;
|
||||
|
||||
if (strchr(filename, ':') != nullptr &&
|
||||
(filename[1] != '\\' || !isalpha(filename[0]) ||
|
||||
strchr(filename, '\\') != strrchr(filename, '\\')))
|
||||
isValid = false;
|
||||
|
||||
if (strchr(filename, '.') == NULL) {
|
||||
strcat(filename, ".txt");
|
||||
}
|
||||
|
||||
if (isValid)
|
||||
{
|
||||
ifstream file(filename, ios::binary | ios::ate);
|
||||
if (file.is_open())
|
||||
{
|
||||
streampos fileSize = file.tellg();
|
||||
char* buffer = new char[fileSize];
|
||||
file.seekg(0, ios::beg);
|
||||
file.read(buffer, fileSize);
|
||||
file.close();
|
||||
|
||||
char searchString[256];
|
||||
cout << "Enter a search string: ";
|
||||
cin.getline(searchString, sizeof(searchString));
|
||||
|
||||
int count = 0;
|
||||
char* p = buffer;
|
||||
while ((p = strstr(p, searchString)) != nullptr)
|
||||
{
|
||||
count++;
|
||||
p += strlen(searchString);
|
||||
}
|
||||
|
||||
cout << "Number of occurrences of a string \"" << searchString << "\" in a text file: " << count << endl;
|
||||
|
||||
delete[] buffer;
|
||||
}
|
||||
else
|
||||
cout << "Error opening file." << endl;
|
||||
}
|
||||
else
|
||||
cout << "Invalid file name." << endl;
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
@ -0,0 +1 @@
|
||||
hello
|
Загрузка…
Ссылка в новой задаче