1ss
This commit is contained in:
parent
d7cecb13a7
commit
e9d6f73da8
101
du6/program.c
101
du6/program.c
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_tree(struct tree* t){
|
u->ano = nacitaj_uzol();
|
||||||
if(!t) return;
|
u->nie = nacitaj_uzol();
|
||||||
destroy_tree(t->yes);
|
return u;
|
||||||
destroy_tree(t->no);
|
|
||||||
free(t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int count_items(struct tree* t){
|
void zrus_strom(struct Uzol* u){
|
||||||
if(!t) return 0;
|
if(!u) return;
|
||||||
if(t->text[0]=='*') return 1;
|
zrus_strom(u->ano);
|
||||||
return count_items(t->yes)+count_items(t->no);
|
zrus_strom(u->nie);
|
||||||
|
free(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
void run_chatbot(struct tree* t){
|
int pocet_tovarov(struct Uzol* u){
|
||||||
if(t->text[0]=='*'){
|
if(!u) return 0;
|
||||||
printf("%s",t->text);
|
if(u->text[0] == '*') return 1;
|
||||||
|
return pocet_tovarov(u->ano) + pocet_tovarov(u->nie);
|
||||||
|
}
|
||||||
|
|
||||||
|
void spusti_system(struct Uzol* u){
|
||||||
|
if(u->text[0] == '*'){
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user