1sssaaaaaaadsdsddsdsddsddsddds
This commit is contained in:
parent
6efef8d978
commit
b1445d41d9
127
du6/program.c
127
du6/program.c
@ -2,80 +2,93 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX 256
|
||||
#define VELKOST 256
|
||||
|
||||
typedef struct Uzol {
|
||||
char text[MAX];
|
||||
struct Uzol* left;
|
||||
struct Uzol* right;
|
||||
} Uzol;
|
||||
struct Uzol{
|
||||
char text[VELKOST];
|
||||
struct Uzol* ano;
|
||||
struct Uzol* nie;
|
||||
};
|
||||
|
||||
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;
|
||||
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->left = NULL;
|
||||
u->right = NULL;
|
||||
if (riadok[0] == '*') return u;
|
||||
u->left = nacitaj_strom();
|
||||
u->right = nacitaj_strom();
|
||||
u->ano = NULL;
|
||||
u->nie = NULL;
|
||||
|
||||
if(riadok[0] == '*'){
|
||||
return u;
|
||||
}
|
||||
|
||||
u->ano = nacitaj_uzol();
|
||||
u->nie = nacitaj_uzol();
|
||||
return u;
|
||||
}
|
||||
|
||||
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 zrus_strom(Uzol* u) {
|
||||
if (!u) return;
|
||||
zrus_strom(u->left);
|
||||
zrus_strom(u->right);
|
||||
void zrus_strom(struct Uzol* u){
|
||||
if(!u) return;
|
||||
zrus_strom(u->ano);
|
||||
zrus_strom(u->nie);
|
||||
free(u);
|
||||
}
|
||||
|
||||
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) { /* ak strom nie je dobre zadaný */
|
||||
printf("Chyba: neplatna struktura databazy.\n");
|
||||
return;
|
||||
}
|
||||
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("Chyba vstupu.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if(c == 'a'){
|
||||
spusti_system(u->ano);
|
||||
}
|
||||
else if(c == 'n'){
|
||||
spusti_system(u->nie);
|
||||
}
|
||||
else{
|
||||
printf("Nerozumiem\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
Uzol* koren = nacitaj_strom();
|
||||
if (!koren) {
|
||||
printf("Chybna databaza\n");
|
||||
int main(){
|
||||
struct Uzol* koren = nacitaj_uzol();
|
||||
|
||||
if(!koren){
|
||||
printf("Chyba: nepodarilo sa nacitat bazu pravidiel.\n");
|
||||
return 0;
|
||||
}
|
||||
int n = pocet_tovarov(koren);
|
||||
|
||||
int pocet = pocet_tovarov(koren);
|
||||
|
||||
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);
|
||||
zrus_strom(koren);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user