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 <string.h>
|
||||
|
||||
#define SIZE 256
|
||||
#define VELKOST 256
|
||||
|
||||
struct tree{
|
||||
char text[SIZE];
|
||||
struct tree* yes;
|
||||
struct tree* no;
|
||||
struct Uzol{
|
||||
char text[VELKOST];
|
||||
struct Uzol* ano;
|
||||
struct Uzol* nie;
|
||||
};
|
||||
|
||||
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;
|
||||
struct Uzol* nacitaj_uzol(){
|
||||
char riadok[VELKOST];
|
||||
|
||||
if(!fgets(riadok, VELKOST, stdin))
|
||||
return NULL;
|
||||
|
||||
if(riadok[0] == '\n')
|
||||
return NULL;
|
||||
|
||||
struct Uzol* u = malloc(sizeof(struct Uzol));
|
||||
strcpy(u->text, riadok);
|
||||
u->ano = NULL;
|
||||
u->nie = NULL;
|
||||
|
||||
if(riadok[0] == '*'){
|
||||
return u;
|
||||
}
|
||||
|
||||
void destroy_tree(struct tree* t){
|
||||
if(!t) return;
|
||||
destroy_tree(t->yes);
|
||||
destroy_tree(t->no);
|
||||
free(t);
|
||||
u->ano = nacitaj_uzol();
|
||||
u->nie = nacitaj_uzol();
|
||||
return u;
|
||||
}
|
||||
|
||||
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 zrus_strom(struct Uzol* u){
|
||||
if(!u) return;
|
||||
zrus_strom(u->ano);
|
||||
zrus_strom(u->nie);
|
||||
free(u);
|
||||
}
|
||||
|
||||
void run_chatbot(struct tree* t){
|
||||
if(t->text[0]=='*'){
|
||||
printf("%s",t->text);
|
||||
int pocet_tovarov(struct Uzol* u){
|
||||
if(!u) return 0;
|
||||
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");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
printf("%s",t->text);
|
||||
printf("%s", u->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");
|
||||
|
||||
if(c == 'a'){
|
||||
spusti_system(u->ano);
|
||||
}
|
||||
else if(c == 'n'){
|
||||
spusti_system(u->nie);
|
||||
}
|
||||
else{
|
||||
printf("Nespravny vstup. Odpovedajte len 'a' alebo 'n'.\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(){
|
||||
struct tree* root=read_tree();
|
||||
if(!root){
|
||||
struct Uzol* koren = nacitaj_uzol();
|
||||
|
||||
if(!koren){
|
||||
printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n");
|
||||
return 0;
|
||||
}
|
||||
int items=count_items(root);
|
||||
|
||||
int pocet = pocet_tovarov(koren);
|
||||
|
||||
printf("Expert z bufetu to vie.\n");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n",items);
|
||||
run_chatbot(root);
|
||||
destroy_tree(root);
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", pocet);
|
||||
|
||||
spusti_system(koren);
|
||||
zrus_strom(koren);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user