usaa25/du6/program.c
2025-11-20 20:58:16 +01:00

72 lines
1.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256
struct tree{
char text[SIZE];
struct tree* yes;
struct tree* no;
};
struct tree* read_tree(){
char b[SIZE];
if(!fgets(b,SIZE,stdin)) return NULL;
if(b[0]=='\n') return NULL;
struct tree* t=malloc(sizeof(struct tree));
strcpy(t->text,b);
t->yes=NULL;
t->no=NULL;
if(b[0]=='*') return t;
t->yes=read_tree();
t->no=read_tree();
return t;
}
void destroy_tree(struct tree* t){
if(!t) return;
destroy_tree(t->yes);
destroy_tree(t->no);
free(t);
}
int count_items(struct tree* t){
if(!t) return 0;
if(t->text[0]=='*') return 1;
return count_items(t->yes)+count_items(t->no);
}
void run_chatbot(struct tree* t){
if(t->text[0]=='*'){
printf("%s",t->text);
printf("Koniec\n");
return;
}
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
printf("%s",t->text);
char c;
if(scanf(" %c",&c)!=1){
printf("Chyba vstupu.\n");
return;
}
if(c=='a') run_chatbot(t->yes);
else if(c=='n') run_chatbot(t->no);
else printf("Nespravny vstup. Odpovedajte len 'a' alebo 'n'.\n");
}
int main(){
struct tree* root=read_tree();
if(!root){
printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n");
return 0;
}
int items=count_items(root);
printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n",items);
run_chatbot(root);
destroy_tree(root);
return 0;
}