This commit is contained in:
Your Name 2025-11-20 15:34:05 +01:00
parent 8a8d995f0c
commit 9bb436a453

View File

@ -6,41 +6,49 @@
#define SIZE 256 #define SIZE 256
struct tree { struct tree
{
char value[SIZE]; char value[SIZE];
struct tree* left; struct tree* left;
struct tree* right; struct tree* right;
}; };
void trim_whitespace(char* str) { void trim_whitespace(char* str)
int i = strlen(str) - 1; {
while (i >= 0 && isspace(str[i])) { int i=strlen(str)-1;
str[i] = '\0'; while (i>=0 && isspace(str[i]))
{
str[i]='\0';
i--; i--;
} }
} }
struct tree* read_tree() { struct tree* read_tree()
{
char buffer[SIZE]; char buffer[SIZE];
if (fgets(buffer, SIZE, stdin) == NULL) { if (fgets(buffer, SIZE, stdin)==NULL)
{
return NULL; return NULL;
} }
if (buffer[0] == '\n') { if (buffer[0]=='\n')
{
return NULL; return NULL;
} }
trim_whitespace(buffer); trim_whitespace(buffer);
struct tree* node = (struct tree*)calloc(1, sizeof(struct tree)); struct tree* node=(struct tree*)calloc(1, sizeof(struct tree));
strcpy(node->value, buffer); strcpy(node->value, buffer);
if (buffer[0] != '*') { if (buffer[0]!='*')
node->left = read_tree(); {
node->right = read_tree(); node->left=read_tree();
node->right=read_tree();
} }
return node; return node;
} }
void destroy_tree(struct tree* tree) { void destroy_tree(struct tree* tree)
if (tree == NULL) { {
if (tree==NULL)
{
return; return;
} }
destroy_tree(tree->left); destroy_tree(tree->left);
@ -48,36 +56,49 @@ void destroy_tree(struct tree* tree) {
free(tree); free(tree);
} }
int count_answers(struct tree* tree) { int count_answers(struct tree* tree)
if (tree == NULL) { {
if (tree==NULL)
{
return 0; return 0;
} }
if (tree->value[0] == '*') { if (tree->value[0]=='*')
{
return 1; return 1;
} }
return count_answers(tree->left) + count_answers(tree->right); return count_answers(tree->left) + count_answers(tree->right);
} }
void run_knowledge_system(struct tree* node) { void run_knowledge_system(struct tree* node)
if (node == NULL) { {
if (node==NULL)
{
return; return;
} }
printf("%s\n", node->value); printf("%s\n", node->value);
if (node->value[0] == '*') { if (node->value[0]=='*')
{
return; return;
} }
if (node->left != NULL || node->right != NULL) { if (node->left!=NULL || node->right!=NULL)
{
char answer[10]; char answer[10];
if (fgets(answer, sizeof(answer), stdin) == NULL) { if (fgets(answer, sizeof(answer), stdin)==NULL)
{
printf("Koniec vstupu\n"); printf("Koniec vstupu\n");
exit(0); exit(0);
return; return;
} }
if (answer[0] == 'a') { if (answer[0]=='a')
{
run_knowledge_system(node->left); run_knowledge_system(node->left);
} else if (answer[0] == 'n') { }
else if (answer[0]=='n')
{
run_knowledge_system(node->right); run_knowledge_system(node->right);
} else { }
else
{
printf("Nerozumiem\n"); printf("Nerozumiem\n");
exit(0); exit(0);
return; return;
@ -86,21 +107,25 @@ void run_knowledge_system(struct tree* node) {
return; return;
} }
int main() { int main()
struct tree* knowledge_base = read_tree(); {
struct tree* knowledge_base=read_tree();
char empty_line[SIZE]; char empty_line[SIZE];
if (fgets(empty_line, SIZE, stdin) == NULL || empty_line[0] != '\n') { if (fgets(empty_line, SIZE, stdin)==NULL || empty_line[0]!='\n')
{
printf("Nespravna baza pravidiel.\n"); printf("Nespravna baza pravidiel.\n");
if (knowledge_base != NULL) { if (knowledge_base!=NULL)
{
destroy_tree(knowledge_base); destroy_tree(knowledge_base);
} }
return 1; return 1;
} }
if (knowledge_base == NULL) { if (knowledge_base==NULL)
{
printf("Nespravna baza pravidiel.\n"); printf("Nespravna baza pravidiel.\n");
return 1; return 1;
} }
int answer_count = count_answers(knowledge_base); int answer_count=count_answers(knowledge_base);
printf("Expert z bufetu to vie.\n"); printf("Expert z bufetu to vie.\n");
printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count); printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");