From 36c5519813e23b33d2671954f55efadbee484256 Mon Sep 17 00:00:00 2001 From: Yurii Yakovenko Date: Mon, 11 Nov 2024 20:35:40 +0000 Subject: [PATCH] Update cv7/program.c --- cv7/program.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/cv7/program.c b/cv7/program.c index e69de29..950e8ef 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include + + + + + +#define SIZE 200 + +struct tree +{ + int id; + char value[SIZE]; + char question[SIZE]; + + struct tree *left, *right; +}; + +struct tree* read_tree(struct tree* p) +{ + char buffer[SIZE]; + memset(buffer,0,SIZE); + char* r = fgets(buffer,SIZE,stdin); + assert(r); + struct tree* node = calloc(1,sizeof(struct tree)); + memcpy(node->value,buffer,SIZE); + if(node->value[0]!='*') + { + read_tree(p->left); + read_tree(p->right); + } + return node; +} + + +void print_tree(struct tree* tree,int offset){ + for (int i = 0; i < offset; i++){ + printf(" "); + } + printf("%s",tree->question); + if (tree->left){ + print_tree(tree->left,offset +3); + 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) +{ + + return 0; +} \ No newline at end of file