1sssaaaaaaadsdsddsdsddsddsddds

This commit is contained in:
Denis Landa 2025-11-20 21:23:16 +01:00
parent 6efef8d978
commit b1445d41d9

View File

@ -2,80 +2,93 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX 256 #define VELKOST 256
typedef struct Uzol { struct Uzol{
char text[MAX]; char text[VELKOST];
struct Uzol* left; struct Uzol* ano;
struct Uzol* right; struct Uzol* nie;
} Uzol; };
Uzol* nacitaj_strom() { struct Uzol* nacitaj_uzol(){
char riadok[MAX]; char riadok[VELKOST];
if (!fgets(riadok, MAX, stdin)) return NULL;
if (riadok[0] == '\n') return NULL; if(!fgets(riadok, VELKOST, stdin))
Uzol* u = malloc(sizeof(Uzol)); return NULL;
if (!u) return NULL;
if(riadok[0] == '\n')
return NULL;
struct Uzol* u = malloc(sizeof(struct Uzol));
strcpy(u->text, riadok); strcpy(u->text, riadok);
u->left = NULL; u->ano = NULL;
u->right = NULL; u->nie = NULL;
if (riadok[0] == '*') return u;
u->left = nacitaj_strom(); if(riadok[0] == '*'){
u->right = nacitaj_strom(); return u;
}
u->ano = nacitaj_uzol();
u->nie = nacitaj_uzol();
return u; return u;
} }
int pocet_tovarov(Uzol* u) { void zrus_strom(struct Uzol* u){
if (!u) return 0; if(!u) return;
if (u->text[0] == '*') return 1; zrus_strom(u->ano);
return pocet_tovarov(u->left) + pocet_tovarov(u->right); zrus_strom(u->nie);
}
void zrus_strom(Uzol* u) {
if (!u) return;
zrus_strom(u->left);
zrus_strom(u->right);
free(u); free(u);
} }
void spusti_system(Uzol* root) { int pocet_tovarov(struct Uzol* u){
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); if(!u) return 0;
Uzol* akt = root; if(u->text[0] == '*') return 1;
while (1) { return pocet_tovarov(u->ano) + pocet_tovarov(u->nie);
printf("%s", akt->text); }
if (akt->text[0] == '*') {
printf("Koniec\n"); void spusti_system(struct Uzol* u){
return; if(u->text[0] == '*'){
} printf("%s", u->text);
char c; printf("Koniec\n");
if (scanf(" %c", &c) != 1) { return;
printf("Koniec vstupu\n"); }
return;
} printf("Chybny vystup\n");
if (c == 'a') akt = akt->left; printf("%s", u->text);
else if (c == 'n') akt = akt->right;
else { char c;
printf("Nerozumiem\n"); if(scanf(" %c", &c) != 1){
return; printf("Chyba vstupu.\n");
} return;
if (!akt) { /* ak strom nie je dobre zadaný */ }
printf("Chyba: neplatna struktura databazy.\n");
return; if(c == 'a'){
} spusti_system(u->ano);
}
else if(c == 'n'){
spusti_system(u->nie);
}
else{
printf("Nerozumiem\n");
} }
} }
int main(void) { int main(){
Uzol* koren = nacitaj_strom(); struct Uzol* koren = nacitaj_uzol();
if (!koren) {
printf("Chybna databaza\n"); if(!koren){
printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n");
return 0; return 0;
} }
int n = pocet_tovarov(koren);
int pocet = pocet_tovarov(koren);
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", n); printf("Pozna %d druhov ovocia a zeleniny.\n", pocet);
spusti_system(koren); spusti_system(koren);
zrus_strom(koren); zrus_strom(koren);
return 0; return 0;
} }