du6
This commit is contained in:
parent
b00e53a3e4
commit
b1cc764178
128
du6/program.c
Normal file
128
du6/program.c
Normal file
@ -0,0 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SIZE 256
|
||||
|
||||
typedef struct tree {
|
||||
char value[SIZE];
|
||||
struct tree *left;
|
||||
struct tree *right;
|
||||
} Tree;
|
||||
|
||||
/* Odstráni koncový znak \n */
|
||||
void strip_newline(char *s) {
|
||||
int len = strlen(s);
|
||||
if (len > 0 && s[len-1] == '\n')
|
||||
s[len-1] = '\0';
|
||||
}
|
||||
|
||||
/* Rekurzívne načítanie stromu v preorder */
|
||||
Tree* read_tree() {
|
||||
char buffer[SIZE];
|
||||
|
||||
if (!fgets(buffer, SIZE, stdin))
|
||||
return NULL;
|
||||
|
||||
/* prázdny riadok = koniec bázy pravidiel */
|
||||
if (strcmp(buffer, "\n") == 0)
|
||||
return NULL;
|
||||
|
||||
strip_newline(buffer);
|
||||
|
||||
Tree *node = calloc(1, sizeof(Tree));
|
||||
if (!node) exit(1);
|
||||
|
||||
strcpy(node->value, buffer);
|
||||
|
||||
/* Odpoveď = list */
|
||||
if (buffer[0] == '*') {
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Inak načítaj oboch potomkov */
|
||||
node->left = read_tree();
|
||||
if (!node->left) {
|
||||
free(node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->right = read_tree();
|
||||
if (!node->right) {
|
||||
free(node->left);
|
||||
free(node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Uvoľnenie pamäte */
|
||||
void destroy_tree(Tree *t) {
|
||||
if (!t) return;
|
||||
destroy_tree(t->left);
|
||||
destroy_tree(t->right);
|
||||
free(t);
|
||||
}
|
||||
|
||||
/* Počet listov = počet tovarov */
|
||||
int count_leaves(Tree *t) {
|
||||
if (!t) return 0;
|
||||
if (!t->left && !t->right)
|
||||
return 1;
|
||||
return count_leaves(t->left) + count_leaves(t->right);
|
||||
}
|
||||
|
||||
/* Spustenie znalostného systému */
|
||||
void run_system(Tree *node) {
|
||||
printf("%s\n", node->value);
|
||||
|
||||
/* List → koniec */
|
||||
if (!node->left && !node->right) {
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char c;
|
||||
if (scanf(" %c", &c) != 1) {
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (c == 'a') {
|
||||
run_system(node->left);
|
||||
} else if (c == 'n') {
|
||||
run_system(node->right);
|
||||
} else {
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Tree *root = read_tree();
|
||||
|
||||
/* Ak sa nepodarilo načítať koreň, chyba */
|
||||
if (!root) {
|
||||
printf("Chyba\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Skontrolovať prázdny riadok po báze pravidiel */
|
||||
char buffer[SIZE];
|
||||
if (!fgets(buffer, SIZE, stdin) || strcmp(buffer, "\n") != 0) {
|
||||
printf("Chyba\n");
|
||||
destroy_tree(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int leaves = count_leaves(root);
|
||||
|
||||
printf("Expert z bufetu to vie.\n");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", leaves);
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
|
||||
run_system(root);
|
||||
|
||||
destroy_tree(root);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user