This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlaba6.c
101 lines (90 loc) · 2.08 KB
/
laba6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
void checkPtr(void* ptr) {
if (ptr == NULL) {
printf("Не удалось выделить память. Программа перестала работать. \n");
exit(-1);
}
}
long int filesize(FILE* fp) {
long int save_pos, size_of_file;
save_pos = ftell(fp);
fseek(fp, 0L, SEEK_END);
size_of_file = ftell(fp);
fseek(fp, save_pos, SEEK_SET);
return(size_of_file);
}
struct tree {
int info;
struct tree* left;
struct tree* right;
};
struct tree* root;
void print_tree(struct tree* r, int l, int n) {
int i;
if (l > n - 1) return;
print_tree(r->right, l + 1, n);
for (i = 0; i < l; ++i) printf(" ");
printf("%d\n", r->info);
print_tree(r->left, l + 1, n);
}
void treeremove(struct tree* root) {
if (root != NULL) {
treeremove(root->left);
treeremove(root->right);
free(root);
}
}
struct tree* NodeBuilder(int n) {
struct tree* r = (struct tree*)malloc(sizeof(struct tree));
checkPtr(r);
if (n > 0) {
int a = -1;
r->info = a;
r->right = NodeBuilder(n - 1);
r->left = NodeBuilder(n - 1);
}
else {
r->right = NULL;
r->left = NULL;
}
return r;
}
int main() {
system("chcp 1251");
system("cls");
FILE* fp;
char file[100];
char* a;
int countNum = 1;
fp = fopen("input.txt", "r");
if (!fp) {
printf("Входной файл недействителен ");
exit(-1);
}
a = fgets(file, sizeof(file), fp);
for (int i = 0; i < filesize(fp); i++) {
if (a[i] == ' ') countNum++;
}
countNum++;
root = NodeBuilder(countNum);
for (int i = 0, j = 0; i < countNum - 1; i++, j++) {
struct tree* temp = root;
for (;; j++) {
if (a[j] == '0') temp = temp->left;
else if (a[j] == '1') temp = temp->right;
if (temp->right->right == NULL) {
temp->info = 1;
for (int k = j, l = 0; k != 0 && a[k] != ' '; k--, l++) {
if (a[k] == '1') temp->info += (2 <<(l - 1));
}
break;
}
}
}
fclose(fp);
print_tree(root, 0, countNum);
treeremove(root);
return 0;
}