From 36a36be66ec1ae78a4d61478420440d5018e3351 Mon Sep 17 00:00:00 2001 From: Denis Landa Date: Thu, 20 Nov 2025 20:56:33 +0100 Subject: [PATCH] 1 --- du6/program.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 du6/program.c diff --git a/du6/program.c b/du6/program.c new file mode 100644 index 0000000..a315956 --- /dev/null +++ b/du6/program.c @@ -0,0 +1,70 @@ +#include +#include +#include + +#define SIZE 256 + +struct tree{ + char text[SIZE]; + struct tree* yes; + struct tree* no; +}; + +struct tree* read_tree(){ + char buffer[SIZE]; + if(!fgets(buffer,SIZE,stdin)) return NULL; + if(buffer[0]=='\n') return NULL; + struct tree* t=malloc(sizeof(struct tree)); + strcpy(t->text,buffer); + t->yes=NULL; + t->no=NULL; + if(buffer[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){ + printf("%s",t->text); + if(t->text[0]=='*'){ + printf("Koniec\n"); + return; + } + printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); + 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; +} +