1sssaaaaaaadsdsddsdsddsddsddds
This commit is contained in:
parent
6efef8d978
commit
b1445d41d9
113
du6/program.c
113
du6/program.c
@ -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] == '*') {
|
|
||||||
|
void spusti_system(struct Uzol* u){
|
||||||
|
if(u->text[0] == '*'){
|
||||||
|
printf("%s", u->text);
|
||||||
printf("Koniec\n");
|
printf("Koniec\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("Chybny vystup\n");
|
||||||
|
printf("%s", u->text);
|
||||||
|
|
||||||
char c;
|
char c;
|
||||||
if (scanf(" %c", &c) != 1) {
|
if(scanf(" %c", &c) != 1){
|
||||||
printf("Koniec vstupu\n");
|
printf("Chyba vstupu.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (c == 'a') akt = akt->left;
|
|
||||||
else if (c == 'n') akt = akt->right;
|
if(c == 'a'){
|
||||||
else {
|
spusti_system(u->ano);
|
||||||
|
}
|
||||||
|
else if(c == 'n'){
|
||||||
|
spusti_system(u->nie);
|
||||||
|
}
|
||||||
|
else{
|
||||||
printf("Nerozumiem\n");
|
printf("Nerozumiem\n");
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!akt) { /* ak strom nie je dobre zadaný */
|
|
||||||
printf("Chyba: neplatna struktura databazy.\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user