Initializacia

This commit is contained in:
Kozar 2024-11-10 15:36:06 +00:00
parent 7a96dc2c17
commit ea6d529fad

View File

@ -15,9 +15,12 @@ struct tree* read_tree(){
char buffer[SIZE]; char buffer[SIZE];
memset(buffer, 0, SIZE); memset(buffer, 0, SIZE);
char* r = fgets(buffer, SIZE, stdin); char* r = fgets(buffer, SIZE, stdin);
assert(r); if (!r || buffer[0] == '\n') {
return NULL;
}
buffer[strcspn(buffer, "\n")] = 0;
struct tree* node = calloc(1, sizeof(struct tree)); struct tree* node = calloc(1, sizeof(struct tree));
memcpy(node->value,buffer,SIZE); strncpy(node->value, buffer, SIZE - 1);
return node; return node;
} }
@ -34,12 +37,26 @@ struct tree* load_tree() {
} }
void print_tree(struct tree* tree, int offset) { void print_tree(struct tree* tree, int offset) {
for (int i = 0; i < offset; i++){ if (!tree) {
printf(" "); return;
} }
printf("%s", tree->value);
if (tree->left){ if (tree->value[0] == '*') {
printf("Expert z bufetu to vie.\n%s\nKoniec\n", tree->value + 1);
return;
}
printf("%s\n", tree->value);
char response;
if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) {
printf("Nespravny vstup. Koniec.\n");
return;
}
if (response == 'a') {
print_tree(tree->left, offset + 3); print_tree(tree->left, offset + 3);
} else {
print_tree(tree->right, offset + 3); print_tree(tree->right, offset + 3);
} }
} }
@ -75,7 +92,7 @@ int main() {
printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Pozna %d druhov ovocia a zeleniny.\n", count);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
print_tree(root); print_tree(root, 0);
destroy_tree(root); destroy_tree(root);
return 0; return 0;