From 53d1e2e806ffccc2696ab2c9d44e1690cfc7137e Mon Sep 17 00:00:00 2001 From: Denis Landa Date: Thu, 20 Nov 2025 21:11:58 +0100 Subject: [PATCH] 1sssaaaaaaadsdsddsd --- du6/program.c | 94 +++++++++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 51 deletions(-) diff --git a/du6/program.c b/du6/program.c index 09e40e9..52d373c 100644 --- a/du6/program.c +++ b/du6/program.c @@ -4,86 +4,78 @@ #define MAX 256 -typedef struct Node { +typedef struct Uzol { char text[MAX]; - struct Node* left; - struct Node* right; -} Node; + struct Uzol* left; + struct Uzol* right; +} Uzol; -Node* read_tree() { - char line[MAX]; - if (!fgets(line, MAX, stdin)) return NULL; - if (line[0] == '\n') return NULL; - - Node* n = malloc(sizeof(Node)); - strcpy(n->text, line); - n->left = NULL; - n->right = NULL; - - if (line[0] == '*') - return n; - - n->left = read_tree(); - n->right = read_tree(); - return n; +Uzol* nacitaj_strom() { + char riadok[MAX]; + if (!fgets(riadok, MAX, stdin)) return NULL; + if (riadok[0] == '\n') return NULL; + Uzol* u = malloc(sizeof(Uzol)); + if (!u) return NULL; + strcpy(u->text, riadok); + u->left = NULL; + u->right = NULL; + if (riadok[0] == '*') return u; + u->left = nacitaj_strom(); + u->right = nacitaj_strom(); + return u; } -int count_leafs(Node* n) { - if (!n) return 0; - if (n->text[0] == '*') return 1; - return count_leafs(n->left) + count_leafs(n->right); +int pocet_tovarov(Uzol* u) { + if (!u) return 0; + if (u->text[0] == '*') return 1; + return pocet_tovarov(u->left) + pocet_tovarov(u->right); } -void destroy_tree(Node* n) { - if (!n) return; - destroy_tree(n->left); - destroy_tree(n->right); - free(n); +void zrus_strom(Uzol* u) { + if (!u) return; + zrus_strom(u->left); + zrus_strom(u->right); + free(u); } -void run(Node* n) { +void spusti_system(Uzol* root) { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - + Uzol* akt = root; while (1) { - printf("%s", n->text); - - if (n->text[0] == '*') { + printf("%s", akt->text); + if (akt->text[0] == '*') { printf("Koniec\n"); return; } - char c; if (scanf(" %c", &c) != 1) { printf("Nespravny vstup.\n"); return; } - - if (c == 'a') n = n->left; - else if (c == 'n') n = n->right; + if (c == 'a') akt = akt->left; + else if (c == 'n') akt = akt->right; else { printf("Nespravny vstup.\n"); return; } + if (!akt) { /* ak strom nie je dobre zadaný */ + printf("Chyba: neplatna struktura databazy.\n"); + return; + } } } -int main() { - - Node* root = read_tree(); - - if (!root) { +int main(void) { + Uzol* koren = nacitaj_strom(); + if (!koren) { printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n"); return 0; } - - int items = count_leafs(root); - + int n = pocet_tovarov(koren); printf("Expert z bufetu to vie.\n"); - printf("Pozna %d druhov ovocia a zeleniny.\n", items); - - run(root); - - destroy_tree(root); + printf("Pozna %d druhov ovocia a zeleniny.\n", n); + spusti_system(koren); + zrus_strom(koren); return 0; }