1sssaaaaaaadsdsddsd

This commit is contained in:
Denis Landa 2025-11-20 21:11:58 +01:00
parent 5377b221f9
commit 53d1e2e806

View File

@ -4,86 +4,78 @@
#define MAX 256 #define MAX 256
typedef struct Node { typedef struct Uzol {
char text[MAX]; char text[MAX];
struct Node* left; struct Uzol* left;
struct Node* right; struct Uzol* right;
} Node; } Uzol;
Node* read_tree() { Uzol* nacitaj_strom() {
char line[MAX]; char riadok[MAX];
if (!fgets(line, MAX, stdin)) return NULL; if (!fgets(riadok, MAX, stdin)) return NULL;
if (line[0] == '\n') return NULL; if (riadok[0] == '\n') return NULL;
Uzol* u = malloc(sizeof(Uzol));
Node* n = malloc(sizeof(Node)); if (!u) return NULL;
strcpy(n->text, line); strcpy(u->text, riadok);
n->left = NULL; u->left = NULL;
n->right = NULL; u->right = NULL;
if (riadok[0] == '*') return u;
if (line[0] == '*') u->left = nacitaj_strom();
return n; u->right = nacitaj_strom();
return u;
n->left = read_tree();
n->right = read_tree();
return n;
} }
int count_leafs(Node* n) { int pocet_tovarov(Uzol* u) {
if (!n) return 0; if (!u) return 0;
if (n->text[0] == '*') return 1; if (u->text[0] == '*') return 1;
return count_leafs(n->left) + count_leafs(n->right); return pocet_tovarov(u->left) + pocet_tovarov(u->right);
} }
void destroy_tree(Node* n) { void zrus_strom(Uzol* u) {
if (!n) return; if (!u) return;
destroy_tree(n->left); zrus_strom(u->left);
destroy_tree(n->right); zrus_strom(u->right);
free(n); free(u);
} }
void run(Node* n) { void spusti_system(Uzol* root) {
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
Uzol* akt = root;
while (1) { while (1) {
printf("%s", n->text); printf("%s", akt->text);
if (akt->text[0] == '*') {
if (n->text[0] == '*') {
printf("Koniec\n"); printf("Koniec\n");
return; return;
} }
char c; char c;
if (scanf(" %c", &c) != 1) { if (scanf(" %c", &c) != 1) {
printf("Nespravny vstup.\n"); printf("Nespravny vstup.\n");
return; return;
} }
if (c == 'a') akt = akt->left;
if (c == 'a') n = n->left; else if (c == 'n') akt = akt->right;
else if (c == 'n') n = n->right;
else { else {
printf("Nespravny vstup.\n"); printf("Nespravny vstup.\n");
return; return;
} }
if (!akt) { /* ak strom nie je dobre zadaný */
printf("Chyba: neplatna struktura databazy.\n");
return;
}
} }
} }
int main() { int main(void) {
Uzol* koren = nacitaj_strom();
Node* root = read_tree(); if (!koren) {
if (!root) {
printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n"); printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n");
return 0; return 0;
} }
int n = pocet_tovarov(koren);
int items = count_leafs(root);
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", items); printf("Pozna %d druhov ovocia a zeleniny.\n", n);
spusti_system(koren);
run(root); zrus_strom(koren);
destroy_tree(root);
return 0; return 0;
} }