2024-11-14 16:46:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
typedef struct Uzol {
|
|
|
|
char *text;
|
|
|
|
struct Uzol *ano;
|
|
|
|
struct Uzol *nie;
|
|
|
|
} Uzol;
|
|
|
|
|
|
|
|
int pocet_tovarov = 0;
|
|
|
|
|
|
|
|
Uzol* vytvor_uzol(const char *text) {
|
|
|
|
Uzol *uzol = (Uzol *)malloc(sizeof(Uzol));
|
|
|
|
uzol->text = strdup(text);
|
|
|
|
uzol->ano = NULL;
|
|
|
|
uzol->nie = NULL;
|
|
|
|
return uzol;
|
|
|
|
}
|
|
|
|
|
2024-11-14 16:48:31 +00:00
|
|
|
Uzol* nacitaj_strom() {
|
2024-11-14 16:46:13 +00:00
|
|
|
char riadok[256];
|
2024-11-14 16:48:31 +00:00
|
|
|
if (fgets(riadok, sizeof(riadok), stdin) == NULL || riadok[0] == '\n') {
|
2024-11-14 16:46:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
riadok[strcspn(riadok, "\n")] = '\0';
|
|
|
|
if (riadok[0] == '*') {
|
|
|
|
pocet_tovarov++;
|
|
|
|
return vytvor_uzol(riadok + 1);
|
|
|
|
}
|
|
|
|
Uzol *uzol = vytvor_uzol(riadok);
|
2024-11-14 16:48:31 +00:00
|
|
|
uzol->ano = nacitaj_strom();
|
|
|
|
uzol->nie = nacitaj_strom();
|
2024-11-14 16:46:13 +00:00
|
|
|
return uzol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spusti_system(Uzol *uzol) {
|
|
|
|
while (uzol != NULL) {
|
|
|
|
if (uzol->ano == NULL && uzol->nie == NULL) {
|
2024-11-14 16:57:42 +00:00
|
|
|
printf("*%s\nKoniec\n", uzol->text);
|
2024-11-14 16:46:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-14 16:52:52 +00:00
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
2024-11-14 16:57:42 +00:00
|
|
|
printf("%s\n", uzol->text);
|
|
|
|
|
2024-11-14 16:46:13 +00:00
|
|
|
char odpoved;
|
|
|
|
if (scanf(" %c", &odpoved) != 1 || (odpoved != 'a' && odpoved != 'n')) {
|
2024-11-14 16:59:06 +00:00
|
|
|
printf("Nerozumiem.\n");
|
2024-11-14 16:46:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uzol = (odpoved == 'a') ? uzol->ano : uzol->nie;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uvolni_strom(Uzol *uzol) {
|
|
|
|
if (uzol == NULL) return;
|
|
|
|
uvolni_strom(uzol->ano);
|
|
|
|
uvolni_strom(uzol->nie);
|
|
|
|
free(uzol->text);
|
|
|
|
free(uzol);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2024-11-14 16:48:31 +00:00
|
|
|
Uzol *koren = nacitaj_strom();
|
2024-11-14 16:46:13 +00:00
|
|
|
|
|
|
|
if (!koren) {
|
|
|
|
printf("Chyba: Nepodarilo sa načítať bázu pravidiel.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("Expert z bufetu to vie.\nPozna %d druhov ovocia a zeleniny.\n", pocet_tovarov);
|
|
|
|
spusti_system(koren);
|
|
|
|
uvolni_strom(koren);
|
|
|
|
return 0;
|
|
|
|
}
|