This commit is contained in:
Denis Landa 2025-11-20 21:00:29 +01:00
parent d7cecb13a7
commit e9d6f73da8

View File

@ -2,70 +2,93 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define SIZE 256 #define VELKOST 256
struct tree{ struct Uzol{
char text[SIZE]; char text[VELKOST];
struct tree* yes; struct Uzol* ano;
struct tree* no; struct Uzol* nie;
}; };
struct tree* read_tree(){ struct Uzol* nacitaj_uzol(){
char b[SIZE]; char riadok[VELKOST];
if(!fgets(b,SIZE,stdin)) return NULL;
if(b[0]=='\n') return NULL; if(!fgets(riadok, VELKOST, stdin))
struct tree* t=malloc(sizeof(struct tree)); return NULL;
strcpy(t->text,b);
t->yes=NULL; if(riadok[0] == '\n')
t->no=NULL; return NULL;
if(b[0]=='*') return t;
t->yes=read_tree(); struct Uzol* u = malloc(sizeof(struct Uzol));
t->no=read_tree(); strcpy(u->text, riadok);
return t; u->ano = NULL;
u->nie = NULL;
if(riadok[0] == '*'){
return u;
}
u->ano = nacitaj_uzol();
u->nie = nacitaj_uzol();
return u;
} }
void destroy_tree(struct tree* t){ void zrus_strom(struct Uzol* u){
if(!t) return; if(!u) return;
destroy_tree(t->yes); zrus_strom(u->ano);
destroy_tree(t->no); zrus_strom(u->nie);
free(t); free(u);
} }
int count_items(struct tree* t){ int pocet_tovarov(struct Uzol* u){
if(!t) return 0; if(!u) return 0;
if(t->text[0]=='*') return 1; if(u->text[0] == '*') return 1;
return count_items(t->yes)+count_items(t->no); return pocet_tovarov(u->ano) + pocet_tovarov(u->nie);
} }
void run_chatbot(struct tree* t){ void spusti_system(struct Uzol* u){
if(t->text[0]=='*'){ if(u->text[0] == '*'){
printf("%s",t->text); printf("%s", u->text);
printf("Koniec\n"); printf("Koniec\n");
return; return;
} }
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
printf("%s",t->text); printf("%s", u->text);
char c; char c;
if(scanf(" %c",&c)!=1){ if(scanf(" %c", &c) != 1){
printf("Chyba vstupu.\n"); printf("Chyba vstupu.\n");
return; return;
} }
if(c=='a') run_chatbot(t->yes);
else if(c=='n') run_chatbot(t->no); if(c == 'a'){
else printf("Nespravny vstup. Odpovedajte len 'a' alebo 'n'.\n"); spusti_system(u->ano);
}
else if(c == 'n'){
spusti_system(u->nie);
}
else{
printf("Nespravny vstup. Odpovedajte len 'a' alebo 'n'.\n");
}
} }
int main(){ int main(){
struct tree* root=read_tree(); struct Uzol* koren = nacitaj_uzol();
if(!root){
if(!koren){
printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n"); printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n");
return 0; return 0;
} }
int items=count_items(root);
int pocet = pocet_tovarov(koren);
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", pocet);
run_chatbot(root);
destroy_tree(root); spusti_system(koren);
zrus_strom(koren);
return 0; return 0;
} }