du6 - 18
This commit is contained in:
parent
55c069087a
commit
c2a3eb71a0
112
du6/program.c
112
du6/program.c
@ -10,14 +10,6 @@ typedef struct tree {
|
|||||||
struct tree *right;
|
struct tree *right;
|
||||||
} Tree;
|
} Tree;
|
||||||
|
|
||||||
int valid_db = 1;
|
|
||||||
|
|
||||||
//odstranit koncovy znak
|
|
||||||
void strip_newline(char *s) {
|
|
||||||
int len = strlen(s);
|
|
||||||
if (len > 0 && s[len-1] == '\n')
|
|
||||||
s[len-1] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
// rekurzivne nacitanie stromu v preorder
|
// rekurzivne nacitanie stromu v preorder
|
||||||
Tree* read_tree() {
|
Tree* read_tree() {
|
||||||
@ -30,110 +22,92 @@ Tree* read_tree() {
|
|||||||
if (strcmp(buffer, "\n") == 0)
|
if (strcmp(buffer, "\n") == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
strip_newline(buffer);
|
|
||||||
|
|
||||||
Tree *node = calloc(1, sizeof(Tree));
|
Tree *node = calloc(1, sizeof(Tree));
|
||||||
if (!node) exit(1);
|
|
||||||
|
|
||||||
strcpy(node->value, buffer);
|
strcpy(node->value, buffer);
|
||||||
|
|
||||||
// odpoved -> list
|
// odpoved -> list
|
||||||
if (buffer[0] == '*') {
|
if (buffer[0] == '*') {
|
||||||
return node;
|
node->left = NULL;
|
||||||
|
node->right = NULL;
|
||||||
|
} else {
|
||||||
|
node->left = read_tree();
|
||||||
|
node->right = read_tree();
|
||||||
}
|
}
|
||||||
|
|
||||||
// inak nacitat oboch potomkov
|
|
||||||
node->left = read_tree();
|
|
||||||
if (!node->left) {
|
|
||||||
valid_db = 0;
|
|
||||||
free(node);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->right = read_tree();
|
|
||||||
if (!node->right) {
|
|
||||||
free(node->left);
|
|
||||||
valid_db = 0;
|
|
||||||
free(node);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// uvolnenie pamate
|
void destroy_tree(Tree *tree) {
|
||||||
void destroy_tree(Tree *t) {
|
if (!tree) return;
|
||||||
if (!t) return;
|
destroy_tree(tree->left);
|
||||||
destroy_tree(t->left);
|
destroy_tree(tree->right);
|
||||||
destroy_tree(t->right);
|
free(tree);
|
||||||
free(t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pocet listov = pocet tovarov
|
int count_leaves(Tree *tree) {
|
||||||
int count_leaves(Tree *t) {
|
if (!tree) return 0;
|
||||||
if (!t) return 0;
|
if (!tree->left && !tree->right)
|
||||||
if (!t->left && !t->right)
|
|
||||||
return 1;
|
return 1;
|
||||||
return count_leaves(t->left) + count_leaves(t->right);
|
return count_leaves(t->left) + count_leaves(t->right);
|
||||||
}
|
}
|
||||||
|
|
||||||
// spustenie znalostneho systemu
|
// spustenie znalostneho systemu
|
||||||
void run_system(Tree *node) {
|
int run_system(Tree *node) {
|
||||||
if (!node) return;
|
if (node == NULL){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
printf("%s\n", node->value);
|
if (node->value[0] == '*'){
|
||||||
|
printf("%s\n", node->value);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// list -> koniec
|
// list -> koniec
|
||||||
if (!node->left && !node->right) {
|
if (!node->left && !node->right) {
|
||||||
printf("Koniec\n");
|
printf("Koniec vstupu\n");
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
do {
|
|
||||||
c = getchar();
|
|
||||||
if (c == EOF) {
|
|
||||||
printf("Koniec vstupu\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} while (c=='\n' || c=='\r' || c==' ' || c=='\t');
|
|
||||||
|
|
||||||
if (c == 'a') {
|
if (c == 'a') {
|
||||||
run_system(node->left);
|
if (node->left != NULL){
|
||||||
|
return run_system(node->left);
|
||||||
|
}
|
||||||
} else if (c == 'n') {
|
} else if (c == 'n') {
|
||||||
run_system(node->right);
|
if (node->right != NULL){
|
||||||
|
return run_system(node->right);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("Nerozumiem\n");
|
printf("Nerozumiem\n");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Tree *root = read_tree();
|
Tree *root = read_tree();
|
||||||
|
printf("Expert z bufetu to vie.\n");
|
||||||
// nepodarilo sa nacitat koren -> chyba
|
|
||||||
if (!root || !valid_db) {
|
if (root == NULL){
|
||||||
printf("Expert z bufetu to vie.\n");
|
printf("Chybna databaza\n");
|
||||||
printf("Chybna databaza\n");
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// skontrolovat prazdny riadok
|
// skontrolovat prazdny riadok
|
||||||
char buffer[SIZE];
|
char buffer[SIZE];
|
||||||
if (!fgets(buffer, SIZE, stdin) || strcmp(buffer, "\n") != 0) {
|
if (!fgets(buffer, SIZE, stdin) || strlen(buffer) > 1) {
|
||||||
printf("Koniec vstupu\n");
|
printf("Chybna databaza\n");
|
||||||
destroy_tree(root);
|
destroy_tree(root);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int leaves = count_leaves(root);
|
int leaves = count_leaves(root);
|
||||||
|
|
||||||
printf("Expert z bufetu to vie.\n");
|
|
||||||
printf("Pozna %d druhov ovocia a zeleniny.\n", leaves);
|
printf("Pozna %d druhov ovocia a zeleniny.\n", leaves);
|
||||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||||
|
int continueworking = run_system(root);
|
||||||
run_system(root);
|
if (continueworking){
|
||||||
|
printf("Koniec\n");
|
||||||
|
}
|
||||||
destroy_tree(root);
|
destroy_tree(root);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user