#include #include #include #include #include #define SIZE 100 struct tree{ char value[SIZE]; struct tree* left; struct tree* right; }; void destroy_tree(struct tree* tree){ if(tree->left){ destroy_tree(tree->left); } if(tree->right){ destroy_tree(tree->right); } free(tree); } struct tree* read_tree(){ char buffer[SIZE]; memset(buffer,0,SIZE); char* r = fgets(buffer,SIZE,stdin); if(!r) { return NULL; } buffer[strcspn(buffer, "\n")] = '\0'; struct tree* node = calloc(1,sizeof(struct tree)); memcpy(node->value,buffer,SIZE); if(buffer[0] == '*'){ node->left = NULL; node->right = NULL; return node; } node->left = read_tree(); node->right = read_tree(); return node; } bool find_the_incorrect(struct tree* tree) { if (tree == NULL) return false; if ((strcspn(tree->value, "*") != strlen(tree->value) && strcspn(tree->value, "?") != strlen(tree->value)) ) { return true; } return find_the_incorrect(tree->left) || find_the_incorrect(tree->right); } void print_tree(struct tree* tree,int offset){ for (int i = 0; i < offset; i++){ printf(" "); } printf("%s",tree->value); if (tree->left){ print_tree(tree->left,offset +3); print_tree(tree->right,offset +3); } } int count_leaves(struct tree* tree){ int count = 0; if(tree == NULL){ return count; } if(tree->left == NULL && tree->right == NULL && tree){ return count +1; } else{ return count + count_leaves(tree->left) + count_leaves(tree->right); } } int count_non_leaves(struct tree* tree){ if(tree == NULL || (tree->left == NULL && tree->right == NULL)){ return 0; } return 1+ count_non_leaves(tree->left) + count_non_leaves(tree->right); } int main(void){ bool whitespace = false; struct tree* tree = read_tree(); char line[SIZE]; int numOfLeaves = count_leaves(tree); char answer[SIZE]; while (fgets(line, sizeof(line), stdin)) { line[strcspn(line, "\n")] = '\0'; if (line[0] == '\0') { whitespace = true; break; } } int res = count_non_leaves(tree); int res2 = numOfLeaves - res; bool found = find_the_incorrect(tree); printf("Expert z bufetu to vie.\n"); if(!tree || !whitespace || found || res2 < 1){ printf("Chybna databaza\n"); return 0; } printf("Pozna %d druhov ovocia a zeleniny.\n", numOfLeaves); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost."); while(tree){ printf("\n%s", tree->value); fgets(answer, sizeof(answer), stdin); answer[strcspn(answer, "\n")] = '\0'; if(answer[0] == '\0'){ printf("\nKoniec vstupu\n"); return 0; } if(strcmp(answer, "n") == 0){ tree = tree->right; if(tree->value[0] == '*'){ printf("\n%s\n", tree->value); printf("Koniec\n"); return 0; } } else if(strcmp(answer, "a") == 0){ tree = tree->left; if(tree->value[0] == '*'){ printf("\n%s\n", tree->value); printf("Koniec\n"); return 0; } } else{ printf("\nNerozumiem\n"); return 0; } } destroy_tree(tree); return 0;}