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 diff
1.1 --- a/Makefile Thu Feb 07 14:31:02 2008 +0100 1.2 +++ b/Makefile Thu Feb 07 14:46:27 2008 +0100 1.3 @@ -2,9 +2,9 @@ 1.4 1.5 # program 1.6 PROGRAM = baum 1.7 -SRC = baum.c 1.8 +SRC = baum.c actions.c 1.9 OBJ = ${SRC:.c=.o} 1.10 -DEP = 1.11 +DEP = baum.h actions.h 1.12 1.13 # compile env 1.14 CC = gcc
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/actions.c Thu Feb 07 14:46:27 2008 +0100 2.3 @@ -0,0 +1,55 @@ 2.4 +#include <stdio.h> 2.5 +#include <stdlib.h> 2.6 +#include <string.h> 2.7 +#include "baum.h" 2.8 +#include "actions.h" 2.9 + 2.10 + 2.11 +char action(struct Node* node) { 2.12 + if (strcmp(node->name, "print") == 0) { 2.13 + logit("print-node"); 2.14 + return action_print(node); 2.15 + } else if (strcmp(node->name, "sum") == 0) { 2.16 + logit("sum-node"); 2.17 + return action_sum(node); 2.18 + } else if (strcmp(node->name, "printchar") == 0) { 2.19 + logit("printchar-node"); 2.20 + return action_printchar(node); 2.21 + } else if (strcmp(node->name, "number") == 0) { 2.22 + logit("number-node"); 2.23 + return action_number(node); 2.24 + } else { 2.25 + fprintf(stderr, "unknown kind of node"); 2.26 + exit(1); 2.27 + } 2.28 +} 2.29 + 2.30 + 2.31 + 2.32 +char action_print(struct Node* node) { 2.33 + printf("%d\n", action(node->down)); 2.34 + return 0; 2.35 +} 2.36 + 2.37 + 2.38 +char action_printchar(struct Node* node) { 2.39 + printf("%c\n", action(node->down)); 2.40 + return 0; 2.41 +} 2.42 + 2.43 + 2.44 +char action_sum(struct Node* node) { 2.45 + struct Node* tp; 2.46 + tp = node->down; 2.47 + while (tp != NULL) { 2.48 + node->value += action(tp); 2.49 + tp = tp->right; 2.50 + } 2.51 + return node->value; 2.52 +} 2.53 + 2.54 + 2.55 +char action_number(struct Node* node) { 2.56 + return node->value; 2.57 +} 2.58 +
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/actions.h Thu Feb 07 14:46:27 2008 +0100 3.3 @@ -0,0 +1,8 @@ 3.4 + 3.5 +char action(struct Node* node); 3.6 + 3.7 +char action_print(struct Node* node); 3.8 +char action_printchar(struct Node* node); 3.9 +char action_sum(struct Node* node); 3.10 +char action_number(struct Node* node); 3.11 +
4.1 --- a/baum.c Thu Feb 07 14:31:02 2008 +0100 4.2 +++ b/baum.c Thu Feb 07 14:46:27 2008 +0100 4.3 @@ -12,8 +12,8 @@ 4.4 #include <string.h> 4.5 4.6 #include "baum.h" 4.7 +#include "actions.h" 4.8 4.9 -char action(struct Node* node); 4.10 4.11 struct Node* root; 4.12 4.13 @@ -49,7 +49,7 @@ 4.14 4.15 /* print */ 4.16 void printNode(struct Node* node) { 4.17 - printf("Node: %20s (%c)\n", node->name, node->value); 4.18 + printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value); 4.19 } 4.20 4.21 void printTree(struct Node* root) { 4.22 @@ -69,42 +69,6 @@ 4.23 } 4.24 4.25 4.26 -char action_print(struct Node* node) { 4.27 - printf("%c\n", action(node->down)); 4.28 - return 0; 4.29 -} 4.30 - 4.31 -char action_sum(struct Node* node) { 4.32 - struct Node* tp; 4.33 - tp = node->down; 4.34 - while (tp != NULL) { 4.35 - node->value += action(tp); 4.36 - tp = tp->right; 4.37 - } 4.38 - return node->value; 4.39 -} 4.40 - 4.41 -char action_number(struct Node* node) { 4.42 - return node->value; 4.43 -} 4.44 - 4.45 -char action(struct Node* node) { 4.46 - if (strcmp(node->name, "print") == 0) { 4.47 - logit("print-node"); 4.48 - return action_print(node); 4.49 - } else if (strcmp(node->name, "sum") == 0) { 4.50 - logit("sum-node"); 4.51 - return action_sum(node); 4.52 - } else if (strcmp(node->name, "number") == 0) { 4.53 - logit("number-node"); 4.54 - return action_number(node); 4.55 - } else { 4.56 - fprintf(stderr, "unknown kind of node"); 4.57 - exit(1); 4.58 - } 4.59 -} 4.60 - 4.61 - 4.62 4.63 /* traverse */ 4.64 void traverse(struct Node* root) { 4.65 @@ -114,7 +78,7 @@ 4.66 4.67 /* init */ 4.68 void init() { 4.69 - root = newNode("print"); 4.70 + root = newNode("printchar"); 4.71 root->down = newNode("number"); 4.72 root->down = newNode("sum"); 4.73 root->down->down = newNode("number");