Mercurial > baum
changeset 2:557fa4df2bcd
added difference between char and number
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 14:46:27 +0100 |
parents | 3da0ff17c8e7 |
children | 15d7d6b9766f |
files | Makefile actions.c actions.h baum.c baum.h |
diffstat | 5 files changed, 73 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- /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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#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; +} +
--- /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); +
--- 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 <string.h> #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");