From 9acb88877ae927fa47102cb8db80884a3395c8aa Mon Sep 17 00:00:00 2001 From: Yurii Yakovenko Date: Mon, 11 Nov 2024 21:10:54 +0000 Subject: [PATCH] Update cv7/program.c --- cv7/program.c | 63 +++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/cv7/program.c b/cv7/program.c index 950e8ef..a684cc0 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -5,17 +5,14 @@ #include - - - #define SIZE 200 struct tree { - int id; + char value[SIZE]; - char question[SIZE]; - + + struct tree *left, *right; }; @@ -25,12 +22,18 @@ struct tree* read_tree(struct tree* p) memset(buffer,0,SIZE); char* r = fgets(buffer,SIZE,stdin); assert(r); - struct tree* node = calloc(1,sizeof(struct tree)); + struct tree* node = (struct tree*)calloc(1,sizeof(struct tree)); memcpy(node->value,buffer,SIZE); + //printf("\n%s", node->value); + node->left=NULL; + node->right=NULL; + if(node->value[0]!='*') { - read_tree(p->left); - read_tree(p->right); + + read_tree(node->left); + + read_tree(node->right); } return node; } @@ -38,46 +41,26 @@ struct tree* read_tree(struct tree* p) void print_tree(struct tree* tree,int offset){ for (int i = 0; i < offset; i++){ - printf(" "); + printf("."); } - printf("%s",tree->question); + + printf("(%s)",tree->value); if (tree->left){ print_tree(tree->left,offset +3); + } + if (tree->right){ print_tree(tree->right,offset +3); } } -// Výpis stromu vo formáte Graphviz bez hlavičky -void print_dot_node(struct tree* node){ - if (node == NULL){ - return; - } - print_dot_node(node->left); - // Parameter ID je cislo uzla. Cislo uzla urcite pri nacitani pomocou pocitadla. - printf("%d [label=\"%s\"];\n",node->id, node->question); - if (node->left){ - printf("%d -> %d;\n",node->id,node->left->id); - } - if (node->right){ - printf("%d -> %d;\n",node->id,node->right->id); - } - print_dot_node(node->right); -} - -// Výpis stromu vo formáte Graphviz -// obrázok stromu vytvoríte: -// program | dot -T png -o obrazok.png -void print_dot(struct tree* node){ - printf("digraph G {\n"); - print_dot_node(node); - printf("}\n"); -} - - - int main(void) { - + + struct tree *tr; + + read_tree(tr); + printf("-------"); + print_tree(tr,3); return 0; } \ No newline at end of file