This commit is contained in:
Ivan Leichenko 2024-11-15 06:53:43 +01:00
parent 8d356d6106
commit ab7d4d65fd

View File

@ -90,38 +90,52 @@ void interact(struct tree *node)
//while (scanf(" %c", &answer) == 1) //while (scanf(" %c", &answer) == 1)
char answer[SIZE]; char answer[SIZE];
while (fgets(answer, SIZE, stdin)) while (1)
{ {
if (answer[0] == '\n')
if (fgets(answer, sizeof(answer), stdin) == NULL)
{ {
printf("Koniec vstupu\n"); printf("Koniec vstupu\n");
return; break;
} }
answer[strcspn(answer, "\n")] = 0; size_t len = strlen(answer);
if (strcmp(answer, "a") == 0) if (answer[len - 1] == '\n') {
answer[len - 1] = '\0';
}
if (strlen(answer) != 1 || (answer[0] != 'a' && answer[0] != 'n'))
{
printf("Nerozumiem\n");
break;
}
if (answer[0] == 'a')
{ {
interact(node->left); interact(node->left);
return; return;
} else if (strcmp(answer, "n") == 0) }
else if (answer[0] == 'n')
{ {
interact(node->right); interact(node->right);
return; return;
} else
{
printf("Nerozumiem\n");
getchar();
} }
} }
} }
int main() int main()
{ {
struct tree *root = read_tree(); struct tree *root = read_tree();
getchar(); int leafs = count_leaf_nodes(root);
if(leafs == 0)
{
printf("Chybna databaza\n");
return 0;
}
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", count_leaf_nodes(root)); printf("Pozna %d druhov ovocia a zeleniny.\n", leafs);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
if (root == NULL) if (root == NULL)
{ {