From 4e584924fc5d19bf3dfc6955414bda712e2c0111 Mon Sep 17 00:00:00 2001 From: vj586da Date: Fri, 26 Nov 2021 02:37:17 +0100 Subject: [PATCH] third commit --- cv8/program.c | 55 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/cv8/program.c b/cv8/program.c index 6a3dbcd..b34a192 100644 --- a/cv8/program.c +++ b/cv8/program.c @@ -4,6 +4,7 @@ #include #define SIZE 100 + struct node { // Otázka aleo odpoveď char data[SIZE]; @@ -27,7 +28,9 @@ struct node* insert(struct node* root, char data[SIZE]){ root = getNewNode(data); return root; } - + if(strncmp(data, "*", 1)){ + return root; + } if(root->left == NULL){ root->left = insert(root->left,data); }else{ @@ -41,7 +44,6 @@ void remove_tree(struct node* node){ remove_tree(node->left); remove_tree(node->right); free(node); - } } @@ -55,40 +57,57 @@ void printt_tree(struct node* tree,int offset){ printt_tree(tree->right,offset+3); } } +void printTabs(int numtabs){ + for(int i=0; idata); + printTabs(level); + printf("vlavo \n"); + printTree(root->left, level+1); + printTabs(level); + printf("vpravo \n"); + printTree(root->right, level+1); + printTabs(level); + printf("hotovo \n"); +} +void printtree(struct node* root){ + printTree(root, 0); +} int main(){ struct node* tree = NULL; + char buff[SIZE]; char* r; int counter=0; int lists=0; while(1){ r = fgets(buff, SIZE, stdin); - if(r == NULL){ + if(buff[0] == '\n' || r == NULL){ break; } - if(strncmp(buff, "*", 1)) lists++; + tree = insert(tree, buff); counter++; } puts("Expert z bufetu to vie."); printf("Pozna %d druhov ovocia a zeleniny.\n", lists); puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost."); - for(int i=0; i< counter; i++){ - printf("%s", tree->data); - r = fgets(buff, SIZE, stdin); - if(strncmp(buff, "a", 1) && tree->right != NULL){ - tree = tree->right; - continue; - }else if(strncmp(buff, "n", 1) && tree->left != NULL){ - tree = tree->left; - continue; - }else { - puts("Koniec"); - break; - } - } + + printf("counter %d, lists %d\n", counter, lists); + + //printt_tree(tree, 1); + printtree(tree); remove_tree(tree); return 0; }