From e6391fbe837ed4239ecdc7615317b6abbc51e1bc Mon Sep 17 00:00:00 2001 From: Viktor Daniv Date: Fri, 15 Nov 2024 00:47:45 +0000 Subject: [PATCH] Update cv7/program.c --- cv7/program.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/cv7/program.c b/cv7/program.c index ab37498..918b3f4 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -71,4 +71,62 @@ void print_tree(Tree* tree, int offset) { printf("%s\n", tree->value); print_tree(tree->left, offset + 3); print_tree(tree->right, offset + 3); +} + +void ask_question(Tree* tree) { + if (!tree) return; + + printf("%s (a/n) ?\n", tree->value); + + // Ak ide o odpoveď, ukončujeme + if (tree->left == NULL && tree->right == NULL) { + printf("Koniec\n"); + return; + } + + char answer; + scanf(" %c", &answer); + + // Kontrola platnej odpovede + if (answer == 'a') { + ask_question(tree->left); // Pre odpoveď "a" + } else if (answer == 'n') { + ask_question(tree->right); // Pre odpoveď "n" + } else { + printf("Neplatná odpoveď, koniec.\n"); + } +} + +int main() { + // Načítanie stromu zo vstupu + Tree* root = read_tree(); + + // Ak strom nie je prázdny + if (root == NULL) { + printf("Chyba pri načítaní pravidiel.\n"); + return 1; + } + + // Preverenie a počítanie listových uzlov + int num_items = 0; + Tree* temp = root; + // Rekurzívne prechádzanie a počítanie + void count_items(Tree* tree) { + if (tree == NULL) return; + if (tree->left == NULL && tree->right == NULL) { + num_items++; + } + count_items(tree->left); + count_items(tree->right); + } + + count_items(root); + printf("Pozna %d druhov ovocia a zeleniny.\n", num_items); + + // Spustenie otázok + ask_question(root); + + // Uvoľnenie pamäte + destroy_tree(root); + return 0; } \ No newline at end of file