usaa24/cv7/program.c

132 lines
2.9 KiB
C
Raw Normal View History

2024-11-15 00:42:02 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-11-15 00:46:23 +00:00
#include <assert.h>
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
#define SIZE 256
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
// Struktúra uzla
typedef struct tree {
char value[SIZE];
struct tree* left;
struct tree* right;
} Tree;
// Funkcia na vytvorenie nového uzla
Tree* create_node(const char* value) {
Tree* node = malloc(sizeof(Tree));
if (node) {
strncpy(node->value, value, SIZE);
node->left = NULL;
node->right = NULL;
2024-11-15 00:42:02 +00:00
}
return node;
}
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
// Funkcia na načítanie stromu
Tree* read_tree() {
char buffer[SIZE];
memset(buffer, 0, SIZE);
// Čítanie riadku
if (!fgets(buffer, SIZE, stdin)) {
return NULL;
2024-11-15 00:42:02 +00:00
}
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
// Odstránenie znaku nového riadku
buffer[strcspn(buffer, "\n")] = 0;
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
if (buffer[0] == '\0') {
return NULL; // Prázdny riadok znamená koniec
2024-11-15 00:42:02 +00:00
}
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
Tree* node = create_node(buffer);
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
// Ak ide o odpoveď, nečítame podstromy
if (buffer[0] == '*') {
return node;
2024-11-15 00:42:02 +00:00
}
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
// Čítanie podstromov (ľavý a pravý uzol)
node->left = read_tree();
node->right = read_tree();
return node;
2024-11-15 00:42:02 +00:00
}
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
// Funkcia na zničenie stromu
void destroy_tree(Tree* tree) {
if (tree) {
destroy_tree(tree->left);
destroy_tree(tree->right);
free(tree);
2024-11-15 00:42:02 +00:00
}
2024-11-15 00:46:23 +00:00
}
2024-11-15 00:40:40 +00:00
2024-11-15 00:46:23 +00:00
// Funkcia na preorder výpis stromu
void print_tree(Tree* tree, int offset) {
if (tree == NULL) return;
for (int i = 0; i < offset; i++) {
printf(" ");
2024-11-15 00:42:02 +00:00
}
2024-11-15 00:46:23 +00:00
printf("%s\n", tree->value);
print_tree(tree->left, offset + 3);
print_tree(tree->right, offset + 3);
2024-11-15 00:47:45 +00:00
}
void ask_question(Tree* tree) {
if (!tree) return;
printf("%s (a/n) ?\n", tree->value);
// Ak ide o odpoveď, ukončujeme
if (tree->left == NULL && tree->right == NULL) {
printf("Koniec\n");
return;
}
char answer;
scanf(" %c", &answer);
// Kontrola platnej odpovede
if (answer == 'a') {
ask_question(tree->left); // Pre odpoveď "a"
} else if (answer == 'n') {
ask_question(tree->right); // Pre odpoveď "n"
} else {
printf("Neplatná odpoveď, koniec.\n");
}
}
int main() {
// Načítanie stromu zo vstupu
Tree* root = read_tree();
// Ak strom nie je prázdny
if (root == NULL) {
printf("Chyba pri načítaní pravidiel.\n");
return 1;
}
// Preverenie a počítanie listových uzlov
int num_items = 0;
Tree* temp = root;
// Rekurzívne prechádzanie a počítanie
void count_items(Tree* tree) {
if (tree == NULL) return;
if (tree->left == NULL && tree->right == NULL) {
num_items++;
}
count_items(tree->left);
count_items(tree->right);
}
count_items(root);
printf("Pozna %d druhov ovocia a zeleniny.\n", num_items);
// Spustenie otázok
ask_question(root);
// Uvoľnenie pamäte
destroy_tree(root);
return 0;
2024-11-15 00:42:02 +00:00
}