95 lines
1.7 KiB
C
95 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define VELKOST 256
|
|
|
|
struct Uzol{
|
|
char text[VELKOST];
|
|
struct Uzol* ano;
|
|
struct Uzol* nie;
|
|
};
|
|
|
|
struct Uzol* nacitaj_uzol(){
|
|
char riadok[VELKOST];
|
|
|
|
if(!fgets(riadok, VELKOST, stdin))
|
|
return 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;
|
|
}
|
|
|
|
u->ano = nacitaj_uzol();
|
|
u->nie = nacitaj_uzol();
|
|
return u;
|
|
}
|
|
|
|
void zrus_strom(struct Uzol* u){
|
|
if(!u) return;
|
|
zrus_strom(u->ano);
|
|
zrus_strom(u->nie);
|
|
free(u);
|
|
}
|
|
|
|
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 spusti_system(struct Uzol* u){
|
|
if(u->text[0] == '*'){
|
|
printf("%s", u->text);
|
|
printf("Koniec\n");
|
|
return;
|
|
}
|
|
|
|
printf("Chybny vystup\n");
|
|
printf("%s", u->text);
|
|
|
|
char c;
|
|
if(scanf(" %c", &c) != 1){
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
return;
|
|
}
|
|
|
|
if(c == 'a'){
|
|
spusti_system(u->ano);
|
|
}
|
|
else if(c == 'n'){
|
|
spusti_system(u->nie);
|
|
}
|
|
else{
|
|
printf("Nerozumiem\n");
|
|
}
|
|
}
|
|
|
|
int main(){
|
|
struct Uzol* koren = nacitaj_uzol();
|
|
|
|
if(!koren){
|
|
printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n");
|
|
return 0;
|
|
}
|
|
|
|
int pocet = pocet_tovarov(koren);
|
|
|
|
printf("Expert z bufetu to vie.\n");
|
|
printf("Pozna %d druhov ovocia a zeleniny.\n", pocet);
|
|
|
|
spusti_system(koren);
|
|
zrus_strom(koren);
|
|
|
|
return 0;
|
|
}
|
|
|