funguje
This commit is contained in:
parent
8a8d995f0c
commit
9bb436a453
@ -6,41 +6,49 @@
|
||||
|
||||
#define SIZE 256
|
||||
|
||||
struct tree {
|
||||
struct tree
|
||||
{
|
||||
char value[SIZE];
|
||||
struct tree* left;
|
||||
struct tree* right;
|
||||
};
|
||||
|
||||
void trim_whitespace(char* str) {
|
||||
int i = strlen(str) - 1;
|
||||
while (i >= 0 && isspace(str[i])) {
|
||||
str[i] = '\0';
|
||||
void trim_whitespace(char* str)
|
||||
{
|
||||
int i=strlen(str)-1;
|
||||
while (i>=0 && isspace(str[i]))
|
||||
{
|
||||
str[i]='\0';
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
struct tree* read_tree() {
|
||||
struct tree* read_tree()
|
||||
{
|
||||
char buffer[SIZE];
|
||||
if (fgets(buffer, SIZE, stdin) == NULL) {
|
||||
if (fgets(buffer, SIZE, stdin)==NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (buffer[0] == '\n') {
|
||||
if (buffer[0]=='\n')
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
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);
|
||||
if (buffer[0] != '*') {
|
||||
node->left = read_tree();
|
||||
node->right = read_tree();
|
||||
if (buffer[0]!='*')
|
||||
{
|
||||
node->left=read_tree();
|
||||
node->right=read_tree();
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void destroy_tree(struct tree* tree) {
|
||||
if (tree == NULL) {
|
||||
void destroy_tree(struct tree* tree)
|
||||
{
|
||||
if (tree==NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
destroy_tree(tree->left);
|
||||
@ -48,36 +56,49 @@ void destroy_tree(struct tree* tree) {
|
||||
free(tree);
|
||||
}
|
||||
|
||||
int count_answers(struct tree* tree) {
|
||||
if (tree == NULL) {
|
||||
int count_answers(struct tree* tree)
|
||||
{
|
||||
if (tree==NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (tree->value[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) {
|
||||
void run_knowledge_system(struct tree* node)
|
||||
{
|
||||
if (node==NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
printf("%s\n", node->value);
|
||||
if (node->value[0] == '*') {
|
||||
if (node->value[0]=='*')
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (node->left != NULL || node->right != NULL) {
|
||||
if (node->left!=NULL || node->right!=NULL)
|
||||
{
|
||||
char answer[10];
|
||||
if (fgets(answer, sizeof(answer), stdin) == NULL) {
|
||||
if (fgets(answer, sizeof(answer), stdin)==NULL)
|
||||
{
|
||||
printf("Koniec vstupu\n");
|
||||
exit(0);
|
||||
return;
|
||||
}
|
||||
if (answer[0] == 'a') {
|
||||
if (answer[0]=='a')
|
||||
{
|
||||
run_knowledge_system(node->left);
|
||||
} else if (answer[0] == 'n') {
|
||||
}
|
||||
else if (answer[0]=='n')
|
||||
{
|
||||
run_knowledge_system(node->right);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Nerozumiem\n");
|
||||
exit(0);
|
||||
return;
|
||||
@ -86,21 +107,25 @@ void run_knowledge_system(struct tree* node) {
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct tree* knowledge_base = read_tree();
|
||||
int main()
|
||||
{
|
||||
struct tree* knowledge_base=read_tree();
|
||||
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");
|
||||
if (knowledge_base != NULL) {
|
||||
if (knowledge_base!=NULL)
|
||||
{
|
||||
destroy_tree(knowledge_base);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (knowledge_base == NULL) {
|
||||
if (knowledge_base==NULL)
|
||||
{
|
||||
printf("Nespravna baza pravidiel.\n");
|
||||
return 1;
|
||||
}
|
||||
int answer_count = count_answers(knowledge_base);
|
||||
int answer_count=count_answers(knowledge_base);
|
||||
printf("Expert z bufetu to vie.\n");
|
||||
printf("Pozna %d druhov ovocia a zeleniny.\n", answer_count);
|
||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user