Initializacia

This commit is contained in:
Kozar 2024-11-12 16:05:03 +00:00
parent 8a8974112a
commit 5001be183c

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define SIZE 100
@ -12,41 +13,56 @@ struct tree {
struct tree* read_tree() {
char buffer[SIZE];
memset(buffer, 0, SIZE);
char* r = fgets(buffer, SIZE, stdin);
if (!r || buffer[0] == '\n') {
return NULL;
while (fgets(buffer, SIZE, stdin)) {
buffer[strcspn(buffer, "\n")] = 0;
if (buffer[0] == '\0') {
continue;
}
struct tree* node = calloc(1, sizeof(struct tree));
strncpy(node->value, buffer, SIZE - 1);
return node;
}
buffer[strcspn(buffer, "\n")] = 0;
struct tree* node = calloc(1, sizeof(struct tree));
strncpy(node->value, buffer, SIZE - 1);
return node;
return NULL;
}
struct tree* load_tree() {
struct tree* node = read_tree();
if (!node) {
return NULL;
}
if (node->value[0] != '*') {
if (node->value[0] != '*') {
node->left = load_tree();
node->right = load_tree();
} else {
node->left = NULL;
node->right = NULL;
}
return node;
return node;
}
void run_tree(struct tree* tree) {
if (!tree) {
return;
}
if (tree->value[0] == '*') {
printf("%s\nKoniec\n", tree->value + 1);
printf("*%s\nKoniec\n", tree->value + 1);
return;
}
printf("%s\n", tree->value);
printf("%s\n", tree->value);
char response;
if (scanf(" %c", &response) != 1) {
@ -75,13 +91,11 @@ void destroy_tree(struct tree* tree) {
}
void count_items(struct tree* tree, int* count) {
if (!tree) return;
if (tree->value[0] == '*') {
if (tree->left == NULL && tree->right == NULL) {
(*count)++;
} else {
if (tree->left) count_items(tree->left, count);
if (tree->right) count_items(tree->right, count);
count_items(tree->left, count);
count_items(tree->right, count);
}
}
@ -89,7 +103,7 @@ int main() {
struct tree* root = load_tree();
printf("Expert z bufetu to vie.\n");
if (!root) {
if (!root) {
printf("Chybna databaza\n");
return 0;
}
@ -100,7 +114,8 @@ int main() {
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
run_tree(root);
destroy_tree(root);
return 0;
}