This commit is contained in:
Bohdan Kapliuk 2024-11-11 14:32:47 +02:00
parent ae9d9a9a08
commit 0b7175626f

View File

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#define SIZE 100 #define SIZE 100
struct tree { struct tree {
@ -20,7 +21,7 @@ struct tree* read_tree(int* counter){
assert(r); assert(r);
struct tree* tree = calloc(1, sizeof(struct tree)); struct tree* tree = calloc(1, sizeof(struct tree));
tree->value = malloc(strlen(buffer) + 1); tree->value = malloc(strlen(buffer) + 1);
strcpy(tree->value,r); strcpy(tree->value, buffer);
assert(tree->value); assert(tree->value);
if (buffer[0] == '*') { if (buffer[0] == '*') {
(*counter)++; (*counter)++;
@ -40,17 +41,15 @@ void destroy_tree(struct tree* tree) {
} }
void print_tree(struct tree* tree, int offset) { void print_tree(struct tree* tree, int offset) {
char buffer[SIZE]; if (tree == NULL) return;
memset(buffer, 0, SIZE);
for (int i = 0; i < offset && tree->value[0] != '*'; i++) { for (int i = 0; i < offset && tree->value[0] != '*'; i++) {
printf(" "); printf(" ");
} }
printf("%s", tree->value); printf("%s", tree->value);
if (tree->value[0] == '*') { if (tree->value[0] == '*') {
printf("Koniec\n"); printf("Koniec vstupu\n");
exit(0); return;
} }
getchar();
char r = getchar(); char r = getchar();
if (r == 'a') { if (r == 'a') {
print_tree(tree->left, offset + 3); print_tree(tree->left, offset + 3);