From 7a365e73c02833429d84a8d5176469f4805daae8 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 20 Nov 2025 15:13:32 +0100 Subject: [PATCH] funguje --- du6/program.c | 44 ++------------------------------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/du6/program.c b/du6/program.c index 4802166..0969a60 100644 --- a/du6/program.c +++ b/du6/program.c @@ -12,7 +12,6 @@ struct tree { struct tree* right; }; -// Funkcia na odstránenie bieleho miesta z reťazca void trim_whitespace(char* str) { int i = strlen(str) - 1; while (i >= 0 && isspace(str[i])) { @@ -21,28 +20,17 @@ void trim_whitespace(char* str) { } } -// Funkcia na načítanie stromu z pravidiel struct tree* read_tree() { char buffer[SIZE]; - - // Načítanie riadku if (fgets(buffer, SIZE, stdin) == NULL) { return NULL; } - - // Ak je prázdny riadok, vráť NULL if (buffer[0] == '\n') { return NULL; } - - // Odstráň biele miesto trim_whitespace(buffer); - - // Vytvorenie nového uzla struct tree* node = (struct tree*)calloc(1, sizeof(struct tree)); strcpy(node->value, buffer); - - // Ak nie je listový uzol (nezačína sa *), rekurzívne načítaj ľavého a pravého syna if (buffer[0] != '*') { node->left = read_tree(); node->right = read_tree(); @@ -51,7 +39,6 @@ struct tree* read_tree() { return node; } -// Funkcia na uvoľnenie pamäte stromu void destroy_tree(struct tree* tree) { if (tree == NULL) { return; @@ -61,46 +48,30 @@ void destroy_tree(struct tree* tree) { free(tree); } -// Funkcia na spočítanie listových uzlov (odpovedí) int count_answers(struct tree* tree) { if (tree == NULL) { return 0; } - - // Ak je listový uzol (odpoveď) if (tree->value[0] == '*') { return 1; } - - // Inak rekurzívne spočítaj odpovede z podstromov return count_answers(tree->left) + count_answers(tree->right); } -// Funkcia na spustenie znalostného systému void run_knowledge_system(struct tree* node) { if (node == NULL) { return; } - - // Vypíš otázku/odpoveď printf("%s\n", node->value); - - // Ak je to listový uzol (odpoveď), skonči if (node->value[0] == '*') { return; } - - // Ak má potomkov, pokračuj podľa odpovede používateľa if (node->left != NULL || node->right != NULL) { char answer[10]; - - // Načítaj odpoveď používateľa if (fgets(answer, sizeof(answer), stdin) == NULL) { - printf("Koniec\n"); + printf("Koniec_vstupu\n"); return; } - - // Spracuj odpoveď if (answer[0] == 'a') { run_knowledge_system(node->left); } else if (answer[0] == 'n') { @@ -114,10 +85,7 @@ void run_knowledge_system(struct tree* node) { } int main() { - // Načítanie bázy pravidiel struct tree* knowledge_base = read_tree(); - - // Overenie, či nasleduje prázdny riadok char empty_line[SIZE]; if (fgets(empty_line, SIZE, stdin) == NULL || empty_line[0] != '\n') { printf("Nespravna baza pravidiel.\n"); @@ -126,23 +94,15 @@ int main() { } return 1; } - - // Overenie platnosti bázy pravidiel if (knowledge_base == NULL) { printf("Nespravna baza pravidiel.\n"); return 1; } - - // Spočítanie a výpis počtu tovarov int answer_count = count_answers(knowledge_base); - printf("Expert z bufetu to vie.\n"); // Medzery namiesto podtržníkov + printf("Expert z bufetu to vie.\n"); printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - - // Spustenie znalostného systému run_knowledge_system(knowledge_base); - - // Vyčistenie pamäte destroy_tree(knowledge_base); return 0;