This commit is contained in:
Jakub Frankovič 2026-04-09 22:10:03 +02:00
parent 6a047a3e64
commit 61a5ce7f8b

109
du5/program.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
char text[256];
int is_leaf;
struct Node* left;
struct Node* right;
} Node;
Node* parse_tree(int* count, int* err) {
if (*err) return NULL;
char buffer[256];
if (!fgets(buffer, sizeof(buffer), stdin)) {
*err = 1;
return NULL;
}
buffer[strcspn(buffer, "\r\n")] = '\0';
if (strlen(buffer) == 0) {
*err = 1;
return NULL;
}
Node* node = (Node*)malloc(sizeof(Node));
if (!node) {
*err = 1;
return NULL;
}
strcpy(node->text, buffer);
if (buffer[0] == '*') {
node->is_leaf = 1;
node->left = NULL;
node->right = NULL;
(*count)++;
} else {
node->is_leaf = 0;
node->left = parse_tree(count, err);
node->right = parse_tree(count, err);
}
return node;
}
void free_tree(Node* root) {
if (!root) return;
free_tree(root->left);
free_tree(root->right);
free(root);
}
int main() {
int count = 0;
int err = 0;
Node* root = parse_tree(&count, &err);
if (err || root == NULL) {
printf("Chyba pri nacitani bazy pravidiel.\n");
free_tree(root);
return 0;
}
char buffer[256];
if (!fgets(buffer, sizeof(buffer), stdin)) {
printf("Chyba pri nacitani bazy pravidiel.\n");
free_tree(root);
return 0;
}
buffer[strcspn(buffer, "\r\n")] = '\0';
if (strlen(buffer) != 0) {
printf("Chyba pri nacitani bazy pravidiel.\n");
free_tree(root);
return 0;
}
printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
Node* current = root;
while (current != NULL) {
printf("%s\n", current->text);
if (current->is_leaf) {
printf("Koniec\n");
break;
}
char ans[256];
if (!fgets(ans, sizeof(ans), stdin)) {
printf("Nespravny vstup.\nKoniec\n");
break;
}
ans[strcspn(ans, "\r\n")] = '\0';
if (strcmp(ans, "a") == 0) {
current = current->left;
} else if (strcmp(ans, "n") == 0) {
current = current->right;
} else {
printf("Nespravny vstup.\nKoniec\n");
break;
}
}
free_tree(root);
return 0;
}