du6
This commit is contained in:
parent
2633e046fc
commit
cf3e69ac7c
@ -9,9 +9,97 @@ typedef struct tree{
|
|||||||
struct tree* right; // nie
|
struct tree* right; // nie
|
||||||
}Tree;
|
}Tree;
|
||||||
|
|
||||||
void trim_newline(char *str){
|
void trim(char *str){
|
||||||
size_t len = strlen(str);
|
size_t len = strlen(str);
|
||||||
if(len > 0 && (str[len -1] == '\n')){
|
if(len > 0 && (str[len -1] == '\n')){
|
||||||
str[len - 1] = '\0';
|
str[len - 1] = '\0';
|
||||||
}
|
}
|
||||||
|
} //odstranenie konca riadku
|
||||||
|
|
||||||
|
Tree* read_tree() {
|
||||||
|
char buffer[256];
|
||||||
|
if(fgets(buffer,256,stdin))return NULL;
|
||||||
|
if(strcmp(buffer, "\n") == 0 ) return NULL;
|
||||||
|
|
||||||
|
trim(buffer);
|
||||||
|
|
||||||
|
Tree* node = calloc(1, sizeof(node));
|
||||||
|
|
||||||
|
strcpy(node->value, buffer);
|
||||||
|
if(buffer[0] == '*'){
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
node->left = read_tree();
|
||||||
|
node->right = read_tree();
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_tree(Tree* t){
|
||||||
|
if(!t) return;
|
||||||
|
destroy_tree(t->left);
|
||||||
|
destry_tree(t->right);
|
||||||
|
free(t);
|
||||||
|
}
|
||||||
|
int count_leaves(Tree* t){
|
||||||
|
if(!t) return 0;
|
||||||
|
if(!t->left && !t->right) return 1;
|
||||||
|
return count_leaves(5->left) + count_leaves(t->right);
|
||||||
|
|
||||||
|
}
|
||||||
|
void run_system(Tree* t) {
|
||||||
|
if (!t) return;
|
||||||
|
|
||||||
|
// Ak ide o odpoveď (list)
|
||||||
|
if (t->value[0] == '*') {
|
||||||
|
printf("%s\n", t->value);
|
||||||
|
printf("Koniec\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otázka
|
||||||
|
printf("%s\n", t->value);
|
||||||
|
|
||||||
|
char ans[10];
|
||||||
|
if (!fgets(ans, sizeof(ans), stdin)) {
|
||||||
|
printf("Chyba vstupu.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
trim_newline(ans);
|
||||||
|
|
||||||
|
if (strcmp(ans, "a") == 0)
|
||||||
|
run_system(t->left);
|
||||||
|
else if (strcmp(ans, "n") == 0)
|
||||||
|
run_system(t->right);
|
||||||
|
else {
|
||||||
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int main() {
|
||||||
|
Tree* root = read_tree();
|
||||||
|
|
||||||
|
if (!root) {
|
||||||
|
printf("Chyba: nepodarilo sa načítať bázu pravidiel.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int count = count_leaves(root);
|
||||||
|
|
||||||
|
printf("Expert z bufetu to vie.\n");
|
||||||
|
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
|
||||||
|
|
||||||
|
// overenie prázdneho riadku medzi databázou a vstupom používateľa
|
||||||
|
char empty[SIZE];
|
||||||
|
if (!fgets(empty, SIZE, stdin) || strcmp(empty, "\n") != 0) {
|
||||||
|
printf("Chyba: chýba prázdny riadok medzi databázou a vstupom.\n");
|
||||||
|
destroy_tree(root);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
run_system(root);
|
||||||
|
|
||||||
|
destroy_tree(root);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user