Initializacia

This commit is contained in:
Kozar 2024-11-10 16:12:30 +00:00
parent bdb10349f2
commit f256260aca

View File

@ -21,10 +21,6 @@ struct tree* read_tree() {
} }
buffer[strcspn(buffer, "\n")] = 0; buffer[strcspn(buffer, "\n")] = 0;
if (buffer[0] == '*' && buffer[1] == ' ') {
error_flag = 1; // Invalid format if there's a space after '*'
}
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;
@ -35,7 +31,7 @@ struct tree* load_tree() {
if (!node) { if (!node) {
return NULL; return NULL;
} }
if (error_flag || node->value[0] != '*') { if (node->value[0] != '*') {
node->left = load_tree(); node->left = load_tree();
node->right = load_tree(); node->right = load_tree();
} }
@ -55,11 +51,8 @@ void run_tree(struct tree* tree) {
printf("%s\n", tree->value); printf("%s\n", tree->value);
char response; char response;
if (scanf(" %c", &response) != 1) { if (scanf(" %c", &response) != 1 || (response != 'a' && response != 'n')) {
printf("Koniec vstupu\n"); error_flag = 1;
return;
} else if (response != 'a' && response != 'n') {
printf("Nerozumiem\n");
return; return;
} }
@ -94,9 +87,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;
} }
@ -107,6 +99,10 @@ int main() {
run_tree(root); run_tree(root);
if (error_flag) {
printf("Chybna databaza\n");
}
destroy_tree(root); destroy_tree(root);
return 0; return 0;
} }