# HG changeset patch # User meillo@marmaro.de # Date 1202391987 -3600 # Node ID 557fa4df2bcdc3ecabbc89eb7d3dc00d60e92f08 # Parent 3da0ff17c8e7acd299a80b4354ae6f5f33cf6447 added difference between char and number diff -r 3da0ff17c8e7 -r 557fa4df2bcd Makefile --- a/Makefile Thu Feb 07 14:31:02 2008 +0100 +++ b/Makefile Thu Feb 07 14:46:27 2008 +0100 @@ -2,9 +2,9 @@ # program PROGRAM = baum -SRC = baum.c +SRC = baum.c actions.c OBJ = ${SRC:.c=.o} -DEP = +DEP = baum.h actions.h # compile env CC = gcc diff -r 3da0ff17c8e7 -r 557fa4df2bcd actions.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/actions.c Thu Feb 07 14:46:27 2008 +0100 @@ -0,0 +1,55 @@ +#include +#include +#include +#include "baum.h" +#include "actions.h" + + +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, "printchar") == 0) { + logit("printchar-node"); + return action_printchar(node); + } else if (strcmp(node->name, "number") == 0) { + logit("number-node"); + return action_number(node); + } else { + fprintf(stderr, "unknown kind of node"); + exit(1); + } +} + + + +char action_print(struct Node* node) { + printf("%d\n", action(node->down)); + return 0; +} + + +char action_printchar(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; +} + diff -r 3da0ff17c8e7 -r 557fa4df2bcd actions.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/actions.h Thu Feb 07 14:46:27 2008 +0100 @@ -0,0 +1,8 @@ + +char action(struct Node* node); + +char action_print(struct Node* node); +char action_printchar(struct Node* node); +char action_sum(struct Node* node); +char action_number(struct Node* node); + diff -r 3da0ff17c8e7 -r 557fa4df2bcd baum.c --- a/baum.c Thu Feb 07 14:31:02 2008 +0100 +++ b/baum.c Thu Feb 07 14:46:27 2008 +0100 @@ -12,8 +12,8 @@ #include #include "baum.h" +#include "actions.h" -char action(struct Node* node); struct Node* root; @@ -49,7 +49,7 @@ /* print */ void printNode(struct Node* node) { - printf("Node: %20s (%c)\n", node->name, node->value); + printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value); } void printTree(struct Node* root) { @@ -69,42 +69,6 @@ } -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) { @@ -114,7 +78,7 @@ /* init */ void init() { - root = newNode("print"); + root = newNode("printchar"); root->down = newNode("number"); root->down = newNode("sum"); root->down->down = newNode("number"); diff -r 3da0ff17c8e7 -r 557fa4df2bcd baum.h --- a/baum.h Thu Feb 07 14:31:02 2008 +0100 +++ b/baum.h Thu Feb 07 14:46:27 2008 +0100 @@ -1,3 +1,8 @@ + +void logit(char* text); + + + struct Node { char* name; char value;