# HG changeset patch # User meillo@marmaro.de # Date 1202391062 -3600 # Node ID 3da0ff17c8e7acd299a80b4354ae6f5f33cf6447 # Parent 2f71d692d4f915545099059e5062252ddb448f7f added features (print, sum, number); split in header file diff -r 2f71d692d4f9 -r 3da0ff17c8e7 baum.c --- a/baum.c Thu Feb 07 12:51:54 2008 +0100 +++ b/baum.c Thu Feb 07 14:31:02 2008 +0100 @@ -9,18 +9,21 @@ #include #include +#include +#include "baum.h" -struct Node { - char* name; - char value; - struct Node* down; - struct Node* right; -}; +char action(struct Node* node); struct Node* root; +void logit(char* text) { + fprintf(stderr, "[%s]\n", text); +} + + +/* new */ struct Node* newNode(char* name) { struct Node* node; node = (struct Node*) malloc(sizeof(struct Node)); @@ -31,40 +34,105 @@ return node; } -void init() { - root = newNode("print"); - root->down = newNode("sum"); -} -void cleanup(struct Node* node) { +/* delete */ +void delete(struct Node* node) { if (node->down != NULL) { - cleanup(node->down); + delete(node->down); } if (node->right != NULL) { - cleanup(node->right); + delete(node->right); } free(node); node=0; } + +/* print */ void printNode(struct Node* node) { printf("Node: %20s (%c)\n", node->name, node->value); } void printTree(struct Node* root) { printNode(root); + printf(" down: "); if (root->down != NULL) { printTree(root->down); + } else { + printf("NULL\n"); } + printf(" right: "); if (root->right != NULL) { printTree(root->right); + } else { + printf("NULL\n"); } } +char action_print(struct Node* node) { + printf("%c\n", action(node->down)); + return 0; +} + +char action_sum(struct Node* node) { + struct Node* tp; + tp = node->down; + while (tp != NULL) { + node->value += action(tp); + tp = tp->right; + } + return node->value; +} + +char action_number(struct Node* node) { + return node->value; +} + +char action(struct Node* node) { + if (strcmp(node->name, "print") == 0) { + logit("print-node"); + return action_print(node); + } else if (strcmp(node->name, "sum") == 0) { + logit("sum-node"); + return action_sum(node); + } else if (strcmp(node->name, "number") == 0) { + logit("number-node"); + return action_number(node); + } else { + fprintf(stderr, "unknown kind of node"); + exit(1); + } +} + + + +/* traverse */ +void traverse(struct Node* root) { + /* each node controlls the nodes below itself */ + action(root); +} + +/* init */ +void init() { + root = newNode("print"); + root->down = newNode("number"); + root->down = newNode("sum"); + root->down->down = newNode("number"); + root->down->down->value = 70; /* 'F' */ + root->down->down->right = newNode("number"); + root->down->down->right->value = 50; /* '2' */ + /* result should be 'x' */ +} + + +/* main */ int main(int argc, char* argv[]) { init(); printTree(root); - cleanup(root); + + action(root); + + delete(root); return(0); } diff -r 2f71d692d4f9 -r 3da0ff17c8e7 baum.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/baum.h Thu Feb 07 14:31:02 2008 +0100 @@ -0,0 +1,8 @@ +struct Node { + char* name; + char value; + struct Node* down; + struct Node* right; +}; + +