comparison baum.c @ 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
comparison
equal deleted inserted replaced
1:3da0ff17c8e7 2:557fa4df2bcd
10 #include <stdio.h> 10 #include <stdio.h>
11 #include <stdlib.h> 11 #include <stdlib.h>
12 #include <string.h> 12 #include <string.h>
13 13
14 #include "baum.h" 14 #include "baum.h"
15 #include "actions.h"
15 16
16 char action(struct Node* node);
17 17
18 struct Node* root; 18 struct Node* root;
19 19
20 20
21 void logit(char* text) { 21 void logit(char* text) {
47 } 47 }
48 48
49 49
50 /* print */ 50 /* print */
51 void printNode(struct Node* node) { 51 void printNode(struct Node* node) {
52 printf("Node: %20s (%c)\n", node->name, node->value); 52 printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value);
53 } 53 }
54 54
55 void printTree(struct Node* root) { 55 void printTree(struct Node* root) {
56 printNode(root); 56 printNode(root);
57 printf(" down: "); 57 printf(" down: ");
67 printf("NULL\n"); 67 printf("NULL\n");
68 } 68 }
69 } 69 }
70 70
71 71
72 char action_print(struct Node* node) {
73 printf("%c\n", action(node->down));
74 return 0;
75 }
76
77 char action_sum(struct Node* node) {
78 struct Node* tp;
79 tp = node->down;
80 while (tp != NULL) {
81 node->value += action(tp);
82 tp = tp->right;
83 }
84 return node->value;
85 }
86
87 char action_number(struct Node* node) {
88 return node->value;
89 }
90
91 char action(struct Node* node) {
92 if (strcmp(node->name, "print") == 0) {
93 logit("print-node");
94 return action_print(node);
95 } else if (strcmp(node->name, "sum") == 0) {
96 logit("sum-node");
97 return action_sum(node);
98 } else if (strcmp(node->name, "number") == 0) {
99 logit("number-node");
100 return action_number(node);
101 } else {
102 fprintf(stderr, "unknown kind of node");
103 exit(1);
104 }
105 }
106
107
108 72
109 /* traverse */ 73 /* traverse */
110 void traverse(struct Node* root) { 74 void traverse(struct Node* root) {
111 /* each node controlls the nodes below itself */ 75 /* each node controlls the nodes below itself */
112 action(root); 76 action(root);
113 } 77 }
114 78
115 /* init */ 79 /* init */
116 void init() { 80 void init() {
117 root = newNode("print"); 81 root = newNode("printchar");
118 root->down = newNode("number"); 82 root->down = newNode("number");
119 root->down = newNode("sum"); 83 root->down = newNode("sum");
120 root->down->down = newNode("number"); 84 root->down->down = newNode("number");
121 root->down->down->value = 70; /* 'F' */ 85 root->down->down->value = 70; /* 'F' */
122 root->down->down->right = newNode("number"); 86 root->down->down->right = newNode("number");