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