2024-11-15 04:36:39 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define SIZE 256
|
|
|
|
|
|
|
|
struct tree
|
|
|
|
{
|
|
|
|
char value[SIZE];
|
|
|
|
struct tree *left;
|
|
|
|
struct tree *right;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tree* read_tree()
|
|
|
|
{
|
|
|
|
char buffer[SIZE];
|
2024-11-15 06:11:55 +00:00
|
|
|
if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n' || buffer[0] == '\0')
|
2024-11-15 04:36:39 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[strcspn(buffer, "\n")] = 0;
|
|
|
|
|
|
|
|
struct tree *node = (struct tree*)malloc(sizeof(struct tree));
|
|
|
|
assert(node != NULL);
|
|
|
|
strcpy(node->value, buffer);
|
|
|
|
|
|
|
|
if (buffer[0] != '*')
|
|
|
|
{
|
|
|
|
node->left = read_tree();
|
|
|
|
node->right = read_tree();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->left = NULL;
|
|
|
|
node->right = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
|
|
|
|
void free_tree(struct tree *tree)
|
|
|
|
{
|
|
|
|
if (tree == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free_tree(tree->left);
|
|
|
|
free_tree(tree->right);
|
|
|
|
free(tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
int count_leaf_nodes(struct tree *node)
|
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (node->left == NULL && node->right == NULL)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return count_leaf_nodes(node->left) + count_leaf_nodes(node->right);
|
|
|
|
}
|
|
|
|
|
|
|
|
int count_non_leaf_nodes(struct tree *node)
|
|
|
|
{
|
|
|
|
if (node == NULL || (node->left == NULL && node->right == NULL))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1 + count_non_leaf_nodes(node->left) + count_non_leaf_nodes(node->right);
|
|
|
|
}
|
|
|
|
|
2024-11-15 06:07:10 +00:00
|
|
|
void interact(struct tree *node, int fstenter)
|
2024-11-15 04:36:39 +00:00
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-15 04:51:11 +00:00
|
|
|
printf("%s\n", node->value);
|
2024-11-15 04:36:39 +00:00
|
|
|
|
|
|
|
if (node->left == NULL && node->right == NULL)
|
|
|
|
{
|
|
|
|
printf("Koniec\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-15 05:23:33 +00:00
|
|
|
//while (scanf(" %c", &answer) == 1)
|
|
|
|
char answer[SIZE];
|
2024-11-15 05:53:43 +00:00
|
|
|
while (1)
|
2024-11-15 04:36:39 +00:00
|
|
|
{
|
2024-11-15 05:53:43 +00:00
|
|
|
if (fgets(answer, sizeof(answer), stdin) == NULL)
|
2024-11-15 04:36:39 +00:00
|
|
|
{
|
2024-11-15 05:23:33 +00:00
|
|
|
printf("Koniec vstupu\n");
|
2024-11-15 05:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t len = strlen(answer);
|
|
|
|
|
2024-11-15 06:07:10 +00:00
|
|
|
if (answer[len - 1] == '\n')
|
|
|
|
{
|
2024-11-15 05:53:43 +00:00
|
|
|
answer[len - 1] = '\0';
|
2024-11-15 04:36:39 +00:00
|
|
|
}
|
2024-11-15 05:23:33 +00:00
|
|
|
|
2024-11-15 05:53:43 +00:00
|
|
|
if (strlen(answer) != 1 || (answer[0] != 'a' && answer[0] != 'n'))
|
|
|
|
{
|
2024-11-15 06:07:10 +00:00
|
|
|
if(fstenter == 0)
|
|
|
|
{
|
|
|
|
fstenter = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Nerozumiem\n");
|
|
|
|
return;
|
|
|
|
}
|
2024-11-15 05:53:43 +00:00
|
|
|
}
|
2024-11-15 05:28:57 +00:00
|
|
|
|
2024-11-15 05:53:43 +00:00
|
|
|
if (answer[0] == 'a')
|
2024-11-15 05:23:33 +00:00
|
|
|
{
|
2024-11-15 06:07:10 +00:00
|
|
|
interact(node->left, fstenter);
|
2024-11-15 05:23:33 +00:00
|
|
|
return;
|
2024-11-15 05:53:43 +00:00
|
|
|
}
|
|
|
|
else if (answer[0] == 'n')
|
2024-11-15 04:36:39 +00:00
|
|
|
{
|
2024-11-15 06:07:10 +00:00
|
|
|
interact(node->right, fstenter);
|
2024-11-15 04:36:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-11-15 05:53:43 +00:00
|
|
|
|
2024-11-15 04:36:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
struct tree *root = read_tree();
|
2024-11-15 06:13:30 +00:00
|
|
|
printf("Expert z bufetu to vie.\n");
|
2024-11-15 06:11:55 +00:00
|
|
|
if(root == NULL)
|
|
|
|
{
|
|
|
|
printf("Chybna databaza\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-11-15 06:07:10 +00:00
|
|
|
//getchar();
|
2024-11-15 05:53:43 +00:00
|
|
|
int leafs = count_leaf_nodes(root);
|
|
|
|
if(leafs == 0)
|
|
|
|
{
|
|
|
|
printf("Chybna databaza\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", leafs);
|
2024-11-15 04:51:11 +00:00
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
2024-11-15 04:36:39 +00:00
|
|
|
if (root == NULL)
|
|
|
|
{
|
|
|
|
printf("Chyba pri nacitani databazy pravidiel.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-11-15 06:07:10 +00:00
|
|
|
int fstenter = 0;
|
|
|
|
interact(root, fstenter);
|
2024-11-15 04:36:39 +00:00
|
|
|
free_tree(root);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|