diff --git a/cv8/program.c b/cv8/program.c index d0e492a..a2a7df6 100644 --- a/cv8/program.c +++ b/cv8/program.c @@ -4,16 +4,16 @@ #include struct tree { - char value[30]; + char value[50]; struct tree* left; struct tree* right; }; struct tree* read_tree(){ - char buffer[30]; - memset(buffer,0,30); - char* r = fgets(buffer,30,stdin); + char buffer[50]; + memset(buffer,0,50); + char* r = fgets(buffer,50,stdin); int x = strlen(buffer); buffer[x-1]='\0'; @@ -23,7 +23,7 @@ struct tree* read_tree(){ if(buffer[0] == '\0'){ return node; } - memcpy(node->value, buffer,30); + memcpy(node->value, buffer,50); if(buffer[0] != '*'){ if(node->left == NULL){ node->left = read_tree(); @@ -36,43 +36,38 @@ struct tree* read_tree(){ return node; // } } - -void print_tree(struct tree* node, int offset){ - int i; - for (i = 0; i < offset; i++){ - printf(" "); - } - printf("%s",node->value); - if (node->left){ - print_tree(node->left,offset + 3); - print_tree(node->right,offset + 3); - } -} - struct tree* search(struct tree* this){ - char buffer[5]; - memset(buffer,0,5); - char* r = fgets(buffer,5,stdin); - int x = strlen(buffer); - buffer[x-1]='\0'; - /*if(count ==){ - printf("%s\n", this->value); - }*/ - /*if(buffer[0] != 'a' || buffer[0] != 'n'){ - return NULL; - }*/ - if(buffer[0] == 'a'){ - printf("%s\n", this->value); - this = search(this->left); + char question[30] = "Je to ovocie alebo zelenina"; + if(strcmp(this->value,question) == 0){ + printf("%s\n", this->value); + this = search(this->left); + } - else if(buffer[0] == 'n'){ + char c = getchar(); + char buffer[50]; + buffer[strlen(buffer)-1]='\0'; + + memcpy(buffer, this->value, 50); + //printf("%s\n", buffer); + if(buffer[0] != '*'){ + if(c == 'a'){ + printf("%s\n", this->value); + this->left = search(this->left); + } + else if(c == 'n'){ + printf("%s\n", this->value); + this->right = search(this->right); + } + + } + else if(buffer[0] == '*'){ printf("%s\n", this->value); - this = search(this->right); - } - printf("Koniec\n"); - return this; -} + printf("Koniec\n"); + exit(0); + } +} + void destroy_tree(struct tree* root){ if(root == NULL) return; @@ -121,14 +116,9 @@ int main(){ printf("Expert z bufetu to vie\n"); printf("Pozna %d druhov ovocia a zeleniny\n", count); printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); - //printf("%s\n", tree->value); - //int all = count_all(tree); - + tree = search(tree); - //print_tree(tree, 3); - //display(tree); - //destroy_tree(tree); return 0; }