1
This commit is contained in:
parent
8bac8b96a3
commit
36a36be66e
70
du6/program.c
Normal file
70
du6/program.c
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#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 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;
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user