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