Initializacia

This commit is contained in:
Kozar 2024-11-12 12:23:34 +00:00
parent 2b3c70feac
commit 2da0b7a3e5

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#define SIZE 100 #define SIZE 100
@ -10,8 +11,6 @@ struct tree {
struct tree* right; struct tree* right;
}; };
int error_flag = 0;
struct tree* read_tree() { struct tree* read_tree() {
char buffer[SIZE]; char buffer[SIZE];
memset(buffer, 0, SIZE); memset(buffer, 0, SIZE);
@ -20,7 +19,6 @@ struct tree* read_tree() {
return NULL; return NULL;
} }
buffer[strcspn(buffer, "\n")] = 0; buffer[strcspn(buffer, "\n")] = 0;
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;
@ -29,18 +27,15 @@ struct tree* read_tree() {
struct tree* load_tree() { struct tree* load_tree() {
struct tree* node = read_tree(); struct tree* node = read_tree();
if (!node) { if (!node) {
error_flag = 1;
return NULL; return NULL;
} }
if (node->value[0] != '*') { if (node->value[0] != '*') {
node->left = load_tree(); node->left = load_tree();
if (error_flag) return node;
node->right = load_tree(); node->right = load_tree();
} }
return node; return node;
} }
void run_tree(struct tree* tree) { void run_tree(struct tree* tree) {
if (!tree) { if (!tree) {
return; return;
@ -57,12 +52,11 @@ void run_tree(struct tree* tree) {
if (scanf(" %c", &response) != 1) { if (scanf(" %c", &response) != 1) {
printf("Koniec vstupu\n"); printf("Koniec vstupu\n");
return; return;
}else if (response != 'a' && response != 'n') { } else if (response != 'a' && response != 'n') {
printf("Nerozumiem\n"); printf("Nerozumiem\n");
return; return;
} }
if (response == 'a') { if (response == 'a') {
run_tree(tree->left); run_tree(tree->left);
} else { } else {
@ -71,18 +65,16 @@ void run_tree(struct tree* tree) {
} }
void destroy_tree(struct tree* tree) { void destroy_tree(struct tree* tree) {
if (!tree) { if (tree->left != NULL) {
return; destroy_tree(tree->left);
}
if (tree->right != NULL) {
destroy_tree(tree->right);
} }
destroy_tree(tree->left);
destroy_tree(tree->right);
free(tree); free(tree);
} }
void count_items(struct tree* tree, int* count) { void count_items(struct tree* tree, int* count) {
if (!tree) {
return;
}
if (tree->left == NULL && tree->right == NULL) { if (tree->left == NULL && tree->right == NULL) {
(*count)++; (*count)++;
} else { } else {
@ -94,9 +86,8 @@ void count_items(struct tree* tree, int* count) {
int main() { int main() {
struct tree* root = load_tree(); struct tree* root = load_tree();
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
if (!root || error_flag) { if (!root) {
printf("Chybna databaza\n"); printf("Chybna databaza\n");
destroy_tree(root);
return 0; return 0;
} }
@ -106,7 +97,7 @@ int main() {
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); 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;
} }