Merge branch 'main' of git.kemt.fei.tuke.sk:ak643du/usaa24

This commit is contained in:
Anton 2024-11-18 14:43:49 +01:00
commit 5bc345edef

View File

@ -1,23 +1,25 @@
#include<stdio.h> #include <stdio.h>
#include<string.h> #include <string.h>
#include<assert.h> #include <stdlib.h>
#include<stdlib.h>
#define SIZE 100 #define SIZE 100
struct tree{ struct tree {
char value[SIZE]; char value[SIZE];
struct tree* left; struct tree* left;
struct tree* right; struct tree* right;
}; };
struct tree* read_tree(){ struct tree* read_tree() {
char buffer[SIZE]; char buffer[SIZE];
memset(buffer,0,SIZE); memset(buffer, 0, SIZE);
char* r = fgets(buffer,SIZE,stdin); char* r = fgets(buffer, SIZE, stdin);
assert(r); if (!r || buffer[0] == '\n') {
struct tree* node = calloc(1,sizeof(struct tree)); return NULL;
memcpy(node->value,buffer,SIZE); }
buffer[strcspn(buffer, "\n")] = 0;
struct tree* node = calloc(1, sizeof(struct tree));
strncpy(node->value, buffer, SIZE - 1);
return node; return node;
} }
@ -29,54 +31,78 @@ struct tree* load_tree() {
if (node->value[0] != '*') { if (node->value[0] != '*') {
node->left = load_tree(); node->left = load_tree();
node->right = load_tree(); node->right = load_tree();
} else if (node->left != NULL || node->right != NULL) {
printf("Expert z bufetu to vie.\n");
free(node);
return NULL;
} }
return node; return node;
} }
void print_tree(struct tree* tree, int offset){ void run_tree(struct tree* tree) {
for (int i = 0; i < offset; i++){ if (!tree) {
printf(" "); return;
} }
printf("%s", tree->value);
if (tree->left){ if (tree->value[0] == '*') {
print_tree(tree->left, offset + 3); printf("*%s\nKoniec\n", tree->value + 1);
print_tree(tree->right, offset + 3); return;
}
printf("%s\n", tree->value);
char response;
if (scanf(" %c", &response) != 1) {
printf("Koniec vstupu\n");
return;
} else if (response != 'a' && response != 'n') {
printf("Nerozumiem\n");
return;
}
if (response == 'a') {
run_tree(tree->left);
} else {
run_tree(tree->right);
} }
} }
void destroy_tree(struct tree* tree){ void destroy_tree(struct tree* tree) {
if(tree->left != NULL){ if (tree) {
destroy_tree(tree->left); destroy_tree(tree->left);
}
if(tree->right != NULL){
destroy_tree(tree->right); destroy_tree(tree->right);
}
free(tree); free(tree);
}
} }
void count_items(struct tree* tree, int* count){ void count_items(struct tree* tree, int* count) {
if(tree->left == NULL && tree->right == NULL){ if (tree) {
if (tree->left == NULL && tree->right == NULL) {
(*count)++; (*count)++;
}else{ } else {
count_items(tree->left, count); count_items(tree->left, count);
count_items(tree->right, count); count_items(tree->right, count);
} }
}
} }
int main() { int main() {
printf("Nacitanie baze pravidiel:\n");
struct tree* root = load_tree(); struct tree* root = load_tree();
printf("Expert z bufetu to vie.\n");
if (!root) { if (!root) {
printf("Chyba: Nepodarilo sa nacitat bazu pravidiel.\n"); printf("Chybna databaza\n");
return 1; return 0;
} }
int count = 0; int count = 0;
count_items(root, &count); count_items(root, &count);
printf("Pozna %d druhov ovocia a zeleniny.\n", count); printf("Pozna %d druhov ovocia a zeleniny.\n", count);
printf("Strom pre ladenie:\n"); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
print_tree(root, 0);
run_tree(root);
destroy_tree(root); destroy_tree(root);
return 0; return 0;