This commit is contained in:
Denis Landa 2025-11-20 21:06:31 +01:00
parent 0bcc777c74
commit 0f5f86fa92

View File

@ -2,93 +2,87 @@
#include <stdlib.h>
#include <string.h>
#define VELKOST 256
#define MAX 256
struct Uzol{
char text[VELKOST];
struct Uzol* ano;
struct Uzol* nie;
};
typedef struct Node {
char text[MAX];
struct Node* left;
struct Node* right;
} Node;
struct Uzol* nacitaj_uzol(){
char riadok[VELKOST];
Node* read_tree() {
char line[MAX];
if (!fgets(line, MAX, stdin)) return NULL;
if (line[0] == '\n') return NULL;
if(!fgets(riadok, VELKOST, stdin))
return NULL;
Node* n = malloc(sizeof(Node));
strcpy(n->text, line);
n->left = NULL;
n->right = NULL;
if(riadok[0] == '\n')
return NULL;
struct Uzol* u = malloc(sizeof(struct Uzol));
strcpy(u->text, riadok);
u->ano = NULL;
u->nie = NULL;
if(riadok[0] == '*'){
return u;
if (line[0] == '*') {
return n;
}
u->ano = nacitaj_uzol();
u->nie = nacitaj_uzol();
return u;
n->left = read_tree();
n->right = read_tree();
return n;
}
void zrus_strom(struct Uzol* u){
if(!u) return;
zrus_strom(u->ano);
zrus_strom(u->nie);
free(u);
int count_leafs(Node* n) {
if (!n) return 0;
if (n->text[0] == '*') return 1;
return count_leafs(n->left) + count_leafs(n->right);
}
int pocet_tovarov(struct Uzol* u){
if(!u) return 0;
if(u->text[0] == '*') return 1;
return pocet_tovarov(u->ano) + pocet_tovarov(u->nie);
void destroy_tree(Node* n) {
if (!n) return;
destroy_tree(n->left);
destroy_tree(n->right);
free(n);
}
void spusti_system(struct Uzol* u){
if(u->text[0] == '*'){
printf("%s", u->text);
printf("Koniec\n");
return;
}
void run(Node* n) {
printf("Odpovedajte 'a' alebo 'n'\n");
printf("Nerozumiem\n");
printf("%s", u->text);
while (1) {
printf("%s", n->text);
char c;
if(scanf(" %c", &c) != 1){
printf("Chyba vstupu.\n");
return;
}
if (n->text[0] == '*') {
printf("Koniec\n");
return;
}
if(c == 'a'){
spusti_system(u->ano);
}
else if(c == 'n'){
spusti_system(u->nie);
}
else{
printf("Nerozumiem\n");
char c;
if (scanf(" %c", &c) != 1) {
printf("Nespravny vstup.\n");
return;
}
if (c == 'a') n = n->left;
else if (c == 'n') n = n->right;
else {
printf("Nespravny vstup.\n");
return;
}
}
}
int main(){
struct Uzol* koren = nacitaj_uzol();
if(!koren){
int main() {
Node* root = read_tree();
if (!root) {
printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n");
return 0;
}
int pocet = pocet_tovarov(koren);
int items = count_leafs(root);
printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", pocet);
printf("MUDrC to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", items);
spusti_system(koren);
zrus_strom(koren);
run(root);
destroy_tree(root);
return 0;
}