This commit is contained in:
Maryna Kravtsova 2020-11-22 12:54:57 +01:00
parent 2469d21681
commit 86f592b851

View File

@ -4,16 +4,16 @@
#include <assert.h> #include <assert.h>
struct tree { struct tree {
char value[30]; char value[50];
struct tree* left; struct tree* left;
struct tree* right; struct tree* right;
}; };
struct tree* read_tree(){ struct tree* read_tree(){
char buffer[30]; char buffer[50];
memset(buffer,0,30); memset(buffer,0,50);
char* r = fgets(buffer,30,stdin); char* r = fgets(buffer,50,stdin);
int x = strlen(buffer); int x = strlen(buffer);
buffer[x-1]='\0'; buffer[x-1]='\0';
@ -23,7 +23,7 @@ struct tree* read_tree(){
if(buffer[0] == '\0'){ if(buffer[0] == '\0'){
return node; return node;
} }
memcpy(node->value, buffer,30); memcpy(node->value, buffer,50);
if(buffer[0] != '*'){ if(buffer[0] != '*'){
if(node->left == NULL){ if(node->left == NULL){
node->left = read_tree(); node->left = read_tree();
@ -36,43 +36,38 @@ struct tree* read_tree(){
return node; 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){ struct tree* search(struct tree* this){
char buffer[5]; char question[30] = "Je to ovocie alebo zelenina";
memset(buffer,0,5); if(strcmp(this->value,question) == 0){
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); printf("%s\n", this->value);
this = search(this->left); 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); printf("%s\n", this->value);
this = search(this->right); 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);
printf("Koniec\n"); printf("Koniec\n");
return this; exit(0);
} }
}
void destroy_tree(struct tree* root){ void destroy_tree(struct tree* root){
if(root == NULL) return; if(root == NULL) return;
@ -121,14 +116,9 @@ int main(){
printf("Expert z bufetu to vie\n"); printf("Expert z bufetu to vie\n");
printf("Pozna %d druhov ovocia a zeleniny\n", count); printf("Pozna %d druhov ovocia a zeleniny\n", 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");
//printf("%s\n", tree->value);
//int all = count_all(tree);
tree = search(tree); tree = search(tree);
//print_tree(tree, 3);
//display(tree);
//destroy_tree(tree);
return 0; return 0;
} }