102 lines
1.8 KiB
C
102 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define SIZE 256
|
|
|
|
struct tree {
|
|
char text[SIZE];
|
|
struct tree* yes;
|
|
struct tree* no;
|
|
};
|
|
|
|
int count_items = 0;
|
|
|
|
struct tree* read_tree() {
|
|
char buffer[SIZE];
|
|
if (!fgets(buffer, SIZE, stdin))
|
|
return NULL;
|
|
if (buffer[0] == '\n')
|
|
return NULL;
|
|
|
|
struct tree* node = malloc(sizeof(struct tree));
|
|
strcpy(node->text, buffer);
|
|
node->yes = NULL;
|
|
node->no = NULL;
|
|
|
|
if (buffer[0] == '*') {
|
|
count_items++;
|
|
return node;
|
|
}
|
|
|
|
node->yes = read_tree();
|
|
if (!node->yes) {
|
|
printf("Chyba\n");
|
|
exit(0);
|
|
}
|
|
|
|
node->no = read_tree();
|
|
if (!node->no) {
|
|
printf("Chyba\n");
|
|
exit(0);
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
void destroy_tree(struct tree* t) {
|
|
if (!t) return;
|
|
destroy_tree(t->yes);
|
|
destroy_tree(t->no);
|
|
free(t);
|
|
}
|
|
|
|
void run(struct tree* t) {
|
|
printf("%s", t->text);
|
|
|
|
if (t->text[0] == '*') {
|
|
printf("Koniec\n");
|
|
return;
|
|
}
|
|
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
|
|
char c;
|
|
if (scanf(" %c", &c) != 1) {
|
|
printf("Chyba\n");
|
|
return;
|
|
}
|
|
|
|
if (c == 'a')
|
|
run(t->yes);
|
|
else if (c == 'n')
|
|
run(t->no);
|
|
else
|
|
printf("Chyba\n");
|
|
}
|
|
|
|
int main() {
|
|
struct tree* root = read_tree();
|
|
|
|
if (!root) {
|
|
printf("Chyba\n");
|
|
return 0;
|
|
}
|
|
|
|
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("Pozna %d druhov ovocia a zeleniny.\n", count_items);
|
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
|
|
|
run(root);
|
|
destroy_tree(root);
|
|
return 0;
|
|
}
|
|
|