funguje
This commit is contained in:
parent
037734be17
commit
ef8b2a3dd2
120
du6/program.c
Normal file
120
du6/program.c
Normal file
@ -0,0 +1,120 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define SIZE 256
|
||||
|
||||
struct tree
|
||||
{
|
||||
char value[SIZE];
|
||||
struct tree* left;
|
||||
struct tree* right;
|
||||
};
|
||||
|
||||
struct tree* read_tree()
|
||||
{
|
||||
char buffer[SIZE];
|
||||
if (fgets(buffer, SIZE, stdin)==NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (buffer[0]=='\n')
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
struct tree* node=(struct tree*)calloc(1, sizeof(struct tree));
|
||||
strcpy(node->value, buffer);
|
||||
if (buffer[0]!='*')
|
||||
{
|
||||
node->left=read_tree();
|
||||
node->right=read_tree();
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
void destroy_tree(struct tree* tree)
|
||||
{
|
||||
if (tree==NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
destroy_tree(tree->left);
|
||||
destroy_tree(tree->right);
|
||||
free(tree);
|
||||
}
|
||||
|
||||
int count_answers(struct tree* tree)
|
||||
{
|
||||
if (tree==NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (tree->value[0]=='*')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return count_answers(tree->left)+count_answers(tree->right);
|
||||
}
|
||||
|
||||
void run_knowledge_system(struct tree* node)
|
||||
{
|
||||
if (node==NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
printf("%s", node->value);
|
||||
if (node->value[0]=='*')
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (node->left!=NULL || node->right!=NULL)
|
||||
{
|
||||
char answer[10];
|
||||
if (fgets(answer, sizeof(answer), stdin)==NULL)
|
||||
{
|
||||
printf("Koniec\n");
|
||||
return;
|
||||
}
|
||||
if (answer[0]=='a')
|
||||
{
|
||||
run_knowledge_system(node->left);
|
||||
}
|
||||
else if (answer[0]=='n')
|
||||
{
|
||||
run_knowledge_system(node->right);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Nespravny vstup.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
struct tree* knowledge_base=read_tree();
|
||||
char empty_line[SIZE];
|
||||
if (fgets(empty_line, SIZE, stdin)==NULL || empty_line[0]!='\n')
|
||||
{
|
||||
printf("Nespravna baza pravidiel.\n");
|
||||
if (knowledge_base!=NULL)
|
||||
{
|
||||
destroy_tree(knowledge_base);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (knowledge_base==NULL)
|
||||
{
|
||||
printf("Nespravna baza pravidiel.\n");
|
||||
return 1;
|
||||
}
|
||||
int answer_count=count_answers(knowledge_base);
|
||||
printf("MUDrC to vie.\n");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n",answer_count);
|
||||
printf("Odpovedajte 'a' alebo 'n'\n");
|
||||
run_knowledge_system(knowledge_base);
|
||||
destroy_tree(knowledge_base);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user