#include #include #include #define MAX 256 typedef struct Uzol { char text[MAX]; struct Uzol* left; struct Uzol* right; } Uzol; 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 pocet_tovarov(Uzol* u) { if (!u) return 0; if (u->text[0] == '*') return 1; return pocet_tovarov(u->left) + pocet_tovarov(u->right); } void zrus_strom(Uzol* u) { if (!u) return; zrus_strom(u->left); zrus_strom(u->right); free(u); } void spusti_system(Uzol* root) { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); Uzol* akt = root; while (1) { printf("%s", akt->text); if (akt->text[0] == '*') { printf("Koniec\n"); return; } char c; if (scanf(" %c", &c) != 1) { printf("Koniec vstupu\n"); return; } if (c == 'a') akt = akt->left; else if (c == 'n') akt = akt->right; else { printf("Nerozumiem\n"); return; } if (!akt) { /* ak strom nie je dobre zadaný */ printf("Chyba: neplatna struktura databazy.\n"); return; } } } int main(void) { Uzol* koren = nacitaj_strom(); if (!koren) { printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n"); return 0; } int n = pocet_tovarov(koren); printf("Expert z bufetu to vie.\n"); printf("Pozna %d druhov ovocia a zeleniny.\n", n); spusti_system(koren); zrus_strom(koren); return 0; }