init commit

This commit is contained in:
2025-04-13 21:48:15 +03:00
commit 23255e9121
192 changed files with 12200 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <stdlib.h>
enum
{
PEN_CHAR_BIT = 12
};
int
safe_write(int fd, void *buf, size_t count)
{
errno = 0;
size_t bytes_written = 0;
while (bytes_written < count) {
ssize_t res = write(fd, buf + bytes_written, count - bytes_written);
if (res < 0) {
if (errno == EINTR) {
errno = 0;
continue;
}
fprintf(stderr, "Error writing to file descriptor %d\n", fd);
close(fd);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
bytes_written += res;
}
return bytes_written;
}
int
main(int argc, char **argv)
{
int fdout = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fdout == -1) {
return 1;
}
unsigned char res[4];
unsigned tmp;
while (scanf("%u", &tmp) == 1) {
res[0] = (tmp & (0x0F << (CHAR_BIT + PEN_CHAR_BIT))) >> (CHAR_BIT + PEN_CHAR_BIT);
res[1] = (tmp & (0xFF << PEN_CHAR_BIT)) >> PEN_CHAR_BIT;
res[2] = (tmp & (0x0F << CHAR_BIT)) >> CHAR_BIT;
res[3] = tmp & 0xFF;
safe_write(fdout, res, sizeof(res));
}
close(fdout);
return 0;
}
+91
View File
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
void
str_to_ll(char *str, long long *num)
{
errno = 0;
char *endptr;
*num = strtoll(str, &endptr, 10);
if (*endptr || errno || endptr == str) {
fprintf(stderr, "Invalid number: %s\n", str);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
return;
}
int
safe_write(int fd, void *buf, size_t count)
{
errno = 0;
size_t bytes_written = 0;
while (bytes_written < count) {
ssize_t res = write(fd, buf + bytes_written, count - bytes_written);
if (res < 0) {
if (errno == EINTR) {
errno = 0;
continue;
}
fprintf(stderr, "Error writing to file descriptor %d\n", fd);
close(fd);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
bytes_written += res;
}
return bytes_written;
}
int
main(int argc, char **argv)
{
if (argc < 3) {
return 1;
}
long long n;
str_to_ll(argv[2], &n);
int fd = open(argv[1], O_RDWR);
if (fd == -1) {
return 1;
}
int is_first = 1;
double prev;
for (int i = 0; i < n; ++i) {
double tmp;
int r = read(fd, &tmp, sizeof(tmp));
if (r == -1 || (r && r != sizeof(tmp))) {
return 1;
}
if (r < 1) {
break;
}
if (!is_first) {
tmp -= prev;
} else {
is_first = 0;
}
if (lseek(fd, -sizeof(tmp), SEEK_CUR) == -1) {
return 1;
}
safe_write(fd, &tmp, sizeof(tmp));
prev = tmp;
}
close(fd);
return 0;
}
+93
View File
@@ -0,0 +1,93 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
ssize_t
my_write(int fd, void *buf, size_t count)
{
size_t count_1 = 0;
ssize_t count_2;
while (count_1 != count) {
count_2 = write(fd, buf, count - count_1);
if (count_2 == -1) {
return -1;
} else if (count_2 == 0) {
return (ssize_t) count_1;
} else {
count_1 += (size_t) count_2;
}
}
return (ssize_t) count_1;
}
ssize_t
my_read(int fd, void *buf, size_t count)
{
size_t count_1 = 0;
ssize_t count_2;
while (count_1 != count) {
count_2 = read(fd, buf, count - count_1);
if (count_2 == -1) {
return -1;
} else if (count_2 == 0) {
return (ssize_t) count_1;
} else {
count_1 += (size_t) count_2;
}
}
return (ssize_t) count_1;
}
int
main(int argc, char **argv)
{
if (argc < 2) {
return 1;
}
int fd;
if ((fd = open(argv[1], O_RDWR)) == -1) {
return 1;
}
long long num, min, shift = 0;
if (my_read(fd, &min, sizeof(min)) < sizeof(min)) {
if (errno) {
close(fd);
return 1;
}
close(fd);
return 0;
}
for (int i = (int) sizeof(num); my_read(fd, &num, sizeof(min)) == sizeof(min); i += (int) sizeof(num)) {
if (num < min) {
min = num;
shift = i;
}
}
lseek(fd, shift, 0);
if (errno) {
close(fd);
return 1;
}
if (min != LLONG_MIN) {
min = -min;
}
if (my_write(fd, &min, sizeof(min)) != sizeof(min)) {
close(fd);
return 1;
}
close(fd);
if (errno) {
return 1;
}
}
+118
View File
@@ -0,0 +1,118 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <errno.h>
enum
{
ARGS_NUM = 2,
};
int
safe_read(int fd, void *buf, size_t count)
{
errno = 0;
size_t bytes_read = 0;
while (bytes_read < count) {
ssize_t res = read(fd, buf + bytes_read, count - bytes_read);
if (res < 0) {
if (errno == EINTR) {
errno = 0;
continue;
}
fprintf(stderr, "Error reading from file descriptor %d\n", fd);
close(fd);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
if (res == 0) {
break;
}
bytes_read += res;
}
return bytes_read;
}
int
safe_write(int fd, void *buf, size_t count)
{
errno = 0;
size_t bytes_written = 0;
while (bytes_written < count) {
ssize_t res = write(fd, buf + bytes_written, count - bytes_written);
if (res < 0) {
if (errno == EINTR) {
errno = 0;
continue;
}
fprintf(stderr, "Error writing to file descriptor %d\n", fd);
close(fd);
exit(1); // TODO: по-хорошему заменить на внешний обработчик ошибок
}
bytes_written += res;
}
return bytes_written;
}
int
main(int argc, char *argv[])
{
if (argc != ARGS_NUM) {
fprintf(stderr, "Wrong number of arguments\n");
return 1;
}
int fd = open(argv[1], O_RDWR);
if (fd == -1) {
fprintf(stderr, "Error opening file %s\n", argv[1]);
return 1;
}
long long num, min_num = LLONG_MAX;
off_t pt_min = -1;
while (safe_read(fd, &num, sizeof(num)) == sizeof(num)) {
if (num < min_num || pt_min == -1) {
min_num = num;
if ((pt_min = lseek(fd, 0, SEEK_CUR) - sizeof(num)) == -1) {
fprintf(stderr, "Error in navigate file %s\n", argv[1]);
return 1;
}
}
}
if (min_num != LLONG_MIN && pt_min != -1) {
min_num = -min_num;
if (lseek(fd, pt_min, SEEK_SET) == -1) {
fprintf(stderr, "Error in navigate file %s\n", argv[1]);
return 1;
}
if (safe_write(fd, &min_num, sizeof(min_num)) != sizeof(num)) {
printf("Error while writing");
return 1;
}
}
close(fd);
return 0;
}
+98
View File
@@ -0,0 +1,98 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdint.h>
struct Node
{
int32_t key;
int32_t left_idx;
int32_t right_idx;
};
void
read_struct(int fd, struct Node *node, int is_le)
{
unsigned char buf[sizeof(*node)];
int n = 0;
while (n != sizeof(*node)) {
ssize_t off = read(fd, buf + n, sizeof(*node) - n);
if (off == -1) {
close(fd);
exit(1);
}
n += off;
}
// int r = read(fd, buf, sizeof(*node));
// if (r == -1) {
// close(fd);
// exit(1);
// }
int n_byte = sizeof(int32_t);
node->key = 0;
node->left_idx = 0;
node->right_idx = 0;
for (int j = 0; j < n_byte; ++j) {
int off = (is_le) ? (CHAR_BIT * (sizeof(uint32_t) - (j + 1))) : (CHAR_BIT * j);
node->key |= (uint32_t) buf[j] << off;
node->left_idx |= (uint32_t) buf[j + n_byte * 1] << off;
node->right_idx |= (uint32_t) buf[j + n_byte * 2] << off;
}
}
void
inorder_traversal(int fd, int32_t idx, int is_le)
{
if (idx == 0) {
return;
}
if (idx == -1) {
idx = 0;
}
struct Node tmp;
if (lseek(fd, idx * sizeof(struct Node), SEEK_SET) == -1) {
close(fd);
exit(1);
}
read_struct(fd, &tmp, is_le);
inorder_traversal(fd, tmp.right_idx, is_le);
printf("%d\n", tmp.key);
inorder_traversal(fd, tmp.left_idx, is_le);
return;
}
int
main(int argc, char **argv)
{
if (argc != 2) {
return 1;
}
int test = 1;
int is_le = 0;
if (*(char *) &test == 1) {
is_le = 1;
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1) {
return 1;
}
inorder_traversal(fd, -1, is_le);
putchar('\n');
close(fd);
return 0;
}
+102
View File
@@ -0,0 +1,102 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
typedef struct Node
{
int32_t key;
int32_t left_idx;
int32_t right_idx;
} Node;
int
convert_num_to_sys(char num[])
{
unsigned res = 0;
for (int i = 0; i < sizeof(res); ++i) {
unsigned cur = (unsigned char) num[sizeof(res) - i - 1];
res ^= (cur << (CHAR_BIT * i));
}
return (int) res;
}
int
read_int(int fd)
{
int res = 0;
char buf[sizeof(res)];
int pos = 0;
while (pos != sizeof(res)) {
ssize_t off = read(fd, buf + pos, sizeof(res) - pos);
if (off == -1) {
printf("Error, can't read from file\n");
exit(1);
}
pos += off;
}
res = convert_num_to_sys(buf);
return res;
}
Node
read_node(int fd)
{
Node new_node;
new_node.key = read_int(fd);
new_node.right_idx = read_int(fd);
new_node.left_idx = read_int(fd);
return new_node;
}
void
dfs(Node head, int fd)
{
if (head.left_idx != 0) {
if (lseek(fd, head.left_idx * sizeof(Node), SEEK_SET) == -1) {
printf("Error? can't seek throw file\n");
exit(1);
}
Node left_node = read_node(fd);
dfs(left_node, fd);
}
printf("%d\n", head.key);
if (head.right_idx != 0) {
if (lseek(fd, head.right_idx * sizeof(Node), SEEK_SET) == -1) {
printf("Error? can't seek throw file\n");
exit(1);
}
Node right_node = read_node(fd);
dfs(right_node, fd);
}
}
int
main(int argc, char *argv[])
{
if (argc < 2) {
printf("Error: Not enough argumens\n");
exit(1);
}
int fd = open(argv[1], O_RDONLY, 0600);
if (fd < 0) {
printf("Error: Can't open file\n");
exit(1);
}
struct stat st;
if (stat(argv[1], &st) == -1) {
printf("Error, can't get info about file\n");
exit(1);
}
if (st.st_size == 0) {
return 0;
}
Node head = read_node(fd);
dfs(head, fd);
close(fd);
}
+104
View File
@@ -0,0 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
struct Node
{
int32_t key;
int32_t left_idx;
int32_t right_idx;
};
void
inorder_traversal(struct Node *mas, int32_t idx, int32_t *keys, int32_t *pos)
{
if (idx == 0) {
return;
}
if (idx == -1) {
idx = 0;
}
inorder_traversal(mas, mas[idx].left_idx, keys, pos);
keys[(*pos)++] = mas[idx].key;
inorder_traversal(mas, mas[idx].right_idx, keys, pos);
return;
}
int
main(int argc, char **argv)
{
if (argc < 2) {
return 1;
}
int test = 1;
int is_le = 0;
if (*(char *) &test == 1) {
is_le = 1;
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1) {
return 1;
}
int n = lseek(fd, 0, SEEK_END) / sizeof(struct Node);
lseek(fd, 0, SEEK_SET);
struct Node *mas = calloc(n, sizeof(struct Node));
for (int i = 0; i < n; ++i) {
struct Node tmp;
unsigned char buf[sizeof(tmp)];
int r = read(fd, buf, sizeof(tmp));
if (r == -1) {
free(mas);
close(fd);
return 1;
}
if (r < 12) {
break;
}
int n_byte = sizeof(int32_t);
tmp.key = 0;
tmp.left_idx = 0;
tmp.right_idx = 0;
if (is_le) {
for (int j = 0; j < n_byte; ++j) {
tmp.key |= (u_int32_t) buf[j] << (CHAR_BIT * j);
tmp.left_idx |= (u_int32_t) buf[j + n_byte * 1] << (CHAR_BIT * j);
tmp.right_idx |= (u_int32_t) buf[j + n_byte * 2] << (CHAR_BIT * j);
}
} else {
for (int j = 0; j < n_byte; ++j) {
tmp.key |= (u_int32_t) buf[j] << (CHAR_BIT * (sizeof(u_int32_t) - (j + 1)));
tmp.left_idx |= (u_int32_t) buf[j + n_byte * 1] << (CHAR_BIT * (sizeof(u_int32_t) - (j + 1)));
tmp.right_idx |= (u_int32_t) buf[j + n_byte * 2] << (CHAR_BIT * (sizeof(u_int32_t) - (j + 1)));
}
}
mas[i] = tmp;
}
int32_t *keys = calloc(n, sizeof(int32_t));
int32_t pos = 0;
inorder_traversal(mas, -1, keys, &pos);
for (int32_t i = 0; i < pos; ++i) {
printf("%d\n", keys[i]);
}
free(keys);
free(mas);
close(fd);
return 0;
}
+78
View File
@@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
struct Node
{
int32_t key, left_idx, right_idx;
};
void
inorder_traversal(struct Node *mas, int32_t idx, int32_t *keys, int32_t *pos)
{
if (idx == 0) {
return;
}
if (idx == -1) {
idx = 0;
}
inorder_traversal(mas, mas[idx].left_idx, keys, pos), keys[(*pos)++] = mas[idx].key,
inorder_traversal(mas, mas[idx].right_idx, keys, pos);
return;
}
int
main(int argc, char **argv)
{
if (argc < 2) {
return 1;
}
int test = 1, is_le = 0;
if (*(char *) &test == 1) {
is_le = 1;
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1) {
return 1;
}
int n = lseek(fd, 0, SEEK_END) / sizeof(struct Node);
lseek(fd, 0, SEEK_SET);
struct Node *mas = calloc(n, sizeof(struct Node));
for (int i = 0; i < n; ++i) {
struct Node tmp;
unsigned char buf[sizeof(tmp)];
int r = read(fd, buf, sizeof(tmp));
if (r == -1) {
free(mas), close(fd);
return 1;
}
if (r < 12) {
break;
}
int n_byte = sizeof(int32_t);
tmp.key = 0, tmp.left_idx = 0, tmp.right_idx = 0;
if (is_le) {
for (int j = 0; j < n_byte; ++j) {
tmp.key |= (u_int32_t) buf[j] << (CHAR_BIT * j),
tmp.left_idx |= (u_int32_t) buf[j + n_byte * 1] << (CHAR_BIT * j),
tmp.right_idx |= (u_int32_t) buf[j + n_byte * 2] << (CHAR_BIT * j);
}
} else {
for (int j = 0; j < n_byte; ++j) {
tmp.key |= (u_int32_t) buf[j] << (CHAR_BIT * (sizeof(u_int32_t) - (j + 1))),
tmp.left_idx |= (u_int32_t) buf[j + n_byte * 1] << (CHAR_BIT * (sizeof(u_int32_t) - (j + 1))),
tmp.right_idx |= (u_int32_t) buf[j + n_byte * 2] << (CHAR_BIT * (sizeof(u_int32_t) - (j + 1)));
}
}
mas[i] = tmp;
}
int32_t *keys = calloc(n, sizeof(int32_t)), pos = 0;
inorder_traversal(mas, -1, keys, &pos);
for (int32_t i = 0; i < pos; ++i) {
printf("%d\n", keys[i]);
}
free(keys), free(mas), close(fd);
return 0;
}
+68
View File
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <endian.h>
int
main(int argc, char *argv[])
{
if (argc != 4) {
return 1;
}
int fdin = open(argv[1], O_RDONLY);
int fdout = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fdin == -1 || fdout == -1) {
return 1;
}
char *p;
errno = 0;
long long mod = strtoll(argv[3], &p, 10);
if (errno || *p || p == argv[3] || !mod || (int32_t) mod != mod) {
return 1;
}
unsigned long long res = 0;
unsigned long long num = 1;
unsigned char buf;
int r = read(fdin, &buf, sizeof(buf));
if (r && r != sizeof(buf)) {
return 1;
}
while (r > 0) {
for (int i = 0; i < CHAR_BIT; i++) {
num %= mod;
res += (num * num) % mod;
res %= mod;
if (buf & (1 << i)) {
int32_t res32 = res;
if (write(fdout, &res32, sizeof(res32)) == -1) {
return 1;
}
}
num++;
}
r = read(fdin, &buf, sizeof(buf));
if (r && r != sizeof(buf)) {
return 1;
}
}
if (r == -1) {
return 1;
}
close(fdin);
close(fdout);
return 0;
}
+72
View File
@@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <assert.h>
struct Node
{
int32_t key;
int32_t left_idx;
int32_t right_idx;
};
void
create_binary_file(const char *filename, struct Node *nodes, int n)
{
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
for (int i = 0; i < n; ++i) {
struct Node node = nodes[i];
node.key = htonl(node.key);
node.left_idx = htonl(node.left_idx);
node.right_idx = htonl(node.right_idx);
write(fd, &node, sizeof(struct Node));
}
close(fd);
}
char *
execute_program(const char *filename)
{
char command[256];
snprintf(command, sizeof(command), "./04 %s", filename);
FILE *fp = popen(command, "r");
if (fp == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
char *output = malloc(1024);
fread(output, 1, 1024, fp);
pclose(fp);
return output;
}
void
test_case(struct Node *nodes, int n, const char *expected_output)
{
const char *filename = "test.bin";
create_binary_file(filename, nodes, n);
char *output = execute_program(filename);
assert(strcmp(output, expected_output) == 0);
free(output);
}
int
main()
{
struct Node nodes1[] = {{10, 1, 2}, {5, 0, 0}, {15, 0, 0}};
test_case(nodes1, 3, "15 10 5 \n");
struct Node nodes2[] = {{20, 1, 2}, {10, 0, 0}, {30, 0, 0}};
test_case(nodes2, 3, "30 20 10 \n");
printf("All tests passed.\n");
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
import struct
# Define the Node structure
class Node:
def __init__(self, key, left_idx, right_idx):
self.key = key
self.left_idx = left_idx
self.right_idx = right_idx
# Create a binary search tree
nodes = [
Node(20, 1, 2), # Root node
Node(-10, 3, 4), # Left child of root
Node(30, 5, 6), # Right child of root
Node(-5, 0, 0), # Left child of node with key 10
Node(15, 0, 0), # Right child of node with key 10
Node(25, 0, 0), # Left child of node with key 30
Node(35, 0, 0) # Right child of node with key 30
]
# Write the nodes to a binary file in Big-Endian format
with open('test_tree.bin', 'wb') as f:
for node in nodes:
# Pack the data in Big-Endian format
f.write(struct.pack('>i', node.key))
f.write(struct.pack('>i', node.left_idx))
f.write(struct.pack('>i', node.right_idx))
# Verify the content of the file
with open('test_tree.bin', 'rb') as f:
content = f.read()
print("Binary content:", bytes(content).hex())