12dsdddddddd

This commit is contained in:
Denis Landa 2025-11-21 12:24:37 +01:00
parent 3d9f4bc057
commit 405a2511af

View File

@ -2,98 +2,81 @@
#include <stdlib.h>
#include <string.h>
#define SIZE 256
#define MAX 256
struct tree {
char text[SIZE];
struct tree* yes;
struct tree* no;
};
typedef struct Uzol {
char text[MAX];
struct Uzol* left;
struct Uzol* right;
} Uzol;
int count_items = 0;
struct tree* read_tree() {
char buffer[SIZE];
if (!fgets(buffer, SIZE, stdin))
return NULL;
if (buffer[0] == '\n')
return NULL;
struct tree* node = malloc(sizeof(struct tree));
strcpy(node->text, buffer);
node->yes = NULL;
node->no = NULL;
if (buffer[0] == '*') {
count_items++;
return node;
}
node->yes = read_tree();
if (!node->yes) {
printf("Koniec vstupu\n");
exit(0);
}
node->no = read_tree();
if (!node->no) {
printf("Nerozumiem\n");
exit(0);
}
return node;
Uzol* nacitaj_strom() {
char riadok[MAX];
if (!fgets(riadok, MAX, stdin)) return NULL;
if (riadok[0] == '\n') return NULL;
Uzol* u = malloc(sizeof(Uzol));
if (!u) return NULL;
strcpy(u->text, riadok);
u->left = NULL;
u->right = NULL;
if (riadok[0] == '*') return u;
u->left = nacitaj_strom();
u->right = nacitaj_strom();
return u;
}
void destroy_tree(struct tree* t) {
if (!t) return;
destroy_tree(t->yes);
destroy_tree(t->no);
free(t);
int pocet_tovarov(Uzol* u) {
if (!u) return 0;
if (u->text[0] == '*') return 1;
return pocet_tovarov(u->left) + pocet_tovarov(u->right);
}
void run(struct tree* t) {
printf("%s", t->text);
if (t->text[0] == '*') {
printf("Koniec\n");
return;
}
char c;
if (scanf(" %c", &c) != 1) {
printf("Chyba\n");
return;
}
if (c == 'a')
run(t->yes);
else if (c == 'n')
run(t->no);
else
printf("Chyba\n");
void zrus_strom(Uzol* u) {
if (!u) return;
zrus_strom(u->left);
zrus_strom(u->right);
free(u);
}
int main() {
struct tree* root = read_tree();
if (!root) {
printf("Chyba\n");
return 0;
}
char line[SIZE];
if (!fgets(line, SIZE, stdin) || line[0] != '\n') {
printf("Chyba\n");
destroy_tree(root);
return 0;
}
printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", count_items);
void spusti_system(Uzol* root) {
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
Uzol* akt = root;
while (1) {
printf("%s", akt->text);
if (akt->text[0] == '*') {
printf("Koniec\n");
return;
}
char c;
if (scanf(" %c", &c) != 1) {
printf("Koniec vstupu\n");
return;
}
if (c == 'a') akt = akt->left;
else if (c == 'n') akt = akt->right;
else {
printf("Nerozumiem\n");
return;
}
if (!akt){
printf("Chyba: neplatna struktura databazy.\n");
return;
}
}
}
run(root);
destroy_tree(root);
int main(void) {
Uzol* koren = nacitaj_strom();
if (!koren) {
printf("Chybna databaza\n");
return 0;
}
int n = pocet_tovarov(koren);
printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", n);
spusti_system(koren);
zrus_strom(koren);
return 0;
}