Initializacia

This commit is contained in:
Kozar 2024-11-12 17:57:20 +00:00
parent 745926af82
commit dd1da65481

View File

@ -10,31 +10,32 @@ struct tree {
struct tree* right; struct tree* right;
}; };
// Function to read a single node from input
struct tree* read_tree() { struct tree* read_tree() {
char buffer[SIZE]; char buffer[SIZE];
memset(buffer, 0, SIZE); if (fgets(buffer, SIZE, stdin) == NULL || buffer[0] == '\n') {
char* r = fgets(buffer, SIZE, stdin);
if (!r || buffer[0] == '\n') {
return NULL; return NULL;
} }
buffer[strcspn(buffer, "\n")] = 0; buffer[strcspn(buffer, "\n")] = 0; // Remove newline character
struct tree* node = calloc(1, sizeof(struct tree)); struct tree* node = calloc(1, sizeof(struct tree));
strncpy(node->value, buffer, SIZE - 1); strncpy(node->value, buffer, SIZE - 1);
return node; return node;
} }
// Function to load the tree structure from input
struct tree* load_tree() { struct tree* load_tree() {
struct tree* node = read_tree(); struct tree* node = read_tree();
if (!node) { if (!node) {
return NULL; return NULL;
} }
if (node->value[0] != '*') { if (node->value[0] != '*') { // Not an answer, so load children
node->left = load_tree(); node->left = load_tree();
node->right = load_tree(); node->right = load_tree();
} }
return node; return node;
} }
// Function to run the tree and interact with the user
void run_tree(struct tree* tree) { void run_tree(struct tree* tree) {
if (!tree) { if (!tree) {
return; return;
@ -47,6 +48,7 @@ void run_tree(struct tree* tree) {
} }
printf("%s\n", tree->value); printf("%s\n", tree->value);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
char response; char response;
if (scanf(" %c", &response) != 1) { if (scanf(" %c", &response) != 1) {
@ -64,6 +66,7 @@ void run_tree(struct tree* tree) {
} }
} }
// Function to free the memory allocated for the tree
void destroy_tree(struct tree* tree) { void destroy_tree(struct tree* tree) {
if (tree) { if (tree) {
destroy_tree(tree->left); destroy_tree(tree->left);
@ -72,10 +75,11 @@ void destroy_tree(struct tree* tree) {
} }
} }
// Function to count the number of unique items (answers) in the tree
void count_items(struct tree* tree, int* count) { void count_items(struct tree* tree, int* count) {
if (tree) { if (tree) {
if (tree->left == NULL && tree->right == NULL) { if (tree->left == NULL && tree->right == NULL) {
(*count)++; (*count)++; // It's a leaf node (an answer)
} else { } else {
count_items(tree->left, count); count_items(tree->left, count);
count_items(tree->right, count); count_items(tree->right, count);
@ -84,8 +88,9 @@ void count_items(struct tree* tree, int* count) {
} }
int main() { int main() {
struct tree* root = load_tree();
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
struct tree* root = load_tree();
if (!root) { if (!root) {
printf("Chybna databaza\n"); printf("Chybna databaza\n");
return 0; return 0;
@ -94,10 +99,8 @@ int main() {
int count = 0; int count = 0;
count_items(root, &count); count_items(root, &count);
printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Pozna %d druhov ovocia a zeleniny.\n", count);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
run_tree(root); run_tree(root);
destroy_tree(root); destroy_tree(root);
return 0; return 0;
} }