12dsdddddddd
This commit is contained in:
parent
3d9f4bc057
commit
405a2511af
129
du6/program.c
129
du6/program.c
@ -2,98 +2,81 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define SIZE 256
|
#define MAX 256
|
||||||
|
|
||||||
struct tree {
|
typedef struct Uzol {
|
||||||
char text[SIZE];
|
char text[MAX];
|
||||||
struct tree* yes;
|
struct Uzol* left;
|
||||||
struct tree* no;
|
struct Uzol* right;
|
||||||
};
|
} Uzol;
|
||||||
|
|
||||||
int count_items = 0;
|
Uzol* nacitaj_strom() {
|
||||||
|
char riadok[MAX];
|
||||||
struct tree* read_tree() {
|
if (!fgets(riadok, MAX, stdin)) return NULL;
|
||||||
char buffer[SIZE];
|
if (riadok[0] == '\n') return NULL;
|
||||||
if (!fgets(buffer, SIZE, stdin))
|
Uzol* u = malloc(sizeof(Uzol));
|
||||||
return NULL;
|
if (!u) return NULL;
|
||||||
if (buffer[0] == '\n')
|
strcpy(u->text, riadok);
|
||||||
return NULL;
|
u->left = NULL;
|
||||||
|
u->right = NULL;
|
||||||
struct tree* node = malloc(sizeof(struct tree));
|
if (riadok[0] == '*') return u;
|
||||||
strcpy(node->text, buffer);
|
u->left = nacitaj_strom();
|
||||||
node->yes = NULL;
|
u->right = nacitaj_strom();
|
||||||
node->no = NULL;
|
return u;
|
||||||
|
|
||||||
if (buffer[0] == '*') {
|
|
||||||
count_items++;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->yes = read_tree();
|
|
||||||
if (!node->yes) {
|
|
||||||
printf("Koniec vstupu\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
node->no = read_tree();
|
|
||||||
if (!node->no) {
|
|
||||||
printf("Nerozumiem\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_tree(struct tree* t) {
|
int pocet_tovarov(Uzol* u) {
|
||||||
if (!t) return;
|
if (!u) return 0;
|
||||||
destroy_tree(t->yes);
|
if (u->text[0] == '*') return 1;
|
||||||
destroy_tree(t->no);
|
return pocet_tovarov(u->left) + pocet_tovarov(u->right);
|
||||||
free(t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void run(struct tree* t) {
|
void zrus_strom(Uzol* u) {
|
||||||
printf("%s", t->text);
|
if (!u) return;
|
||||||
|
zrus_strom(u->left);
|
||||||
|
zrus_strom(u->right);
|
||||||
|
free(u);
|
||||||
|
}
|
||||||
|
|
||||||
if (t->text[0] == '*') {
|
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");
|
printf("Koniec\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char c;
|
char c;
|
||||||
if (scanf(" %c", &c) != 1) {
|
if (scanf(" %c", &c) != 1) {
|
||||||
printf("Chyba\n");
|
printf("Koniec vstupu\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (c == 'a') akt = akt->left;
|
||||||
if (c == 'a')
|
else if (c == 'n') akt = akt->right;
|
||||||
run(t->yes);
|
else {
|
||||||
else if (c == 'n')
|
printf("Nerozumiem\n");
|
||||||
run(t->no);
|
return;
|
||||||
else
|
}
|
||||||
printf("Chyba\n");
|
if (!akt){
|
||||||
|
printf("Chyba: neplatna struktura databazy.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main(void) {
|
||||||
struct tree* root = read_tree();
|
Uzol* koren = nacitaj_strom();
|
||||||
|
if (!koren) {
|
||||||
if (!root) {
|
printf("Chybna databaza\n");
|
||||||
printf("Chyba\n");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
int n = pocet_tovarov(koren);
|
||||||
char line[SIZE];
|
|
||||||
if (!fgets(line, SIZE, stdin) || line[0] != '\n') {
|
|
||||||
printf("Chyba\n");
|
|
||||||
destroy_tree(root);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Expert z bufetu to vie.\n");
|
printf("Expert z bufetu to vie.\n");
|
||||||
printf("Pozna %d druhov ovocia a zeleniny.\n", count_items);
|
printf("Pozna %d druhov ovocia a zeleniny.\n", n);
|
||||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
spusti_system(koren);
|
||||||
|
zrus_strom(koren);
|
||||||
run(root);
|
|
||||||
destroy_tree(root);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user