Mercurial > baum
comparison baum.c @ 1:3da0ff17c8e7
added features (print, sum, number); split in header file
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 14:31:02 +0100 |
parents | 2f71d692d4f9 |
children | 557fa4df2bcd |
comparison
equal
deleted
inserted
replaced
0:2f71d692d4f9 | 1:3da0ff17c8e7 |
---|---|
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 #include <string.h> | |
12 | 13 |
14 #include "baum.h" | |
13 | 15 |
14 struct Node { | 16 char action(struct Node* node); |
15 char* name; | |
16 char value; | |
17 struct Node* down; | |
18 struct Node* right; | |
19 }; | |
20 | 17 |
21 struct Node* root; | 18 struct Node* root; |
22 | 19 |
23 | 20 |
21 void logit(char* text) { | |
22 fprintf(stderr, "[%s]\n", text); | |
23 } | |
24 | |
25 | |
26 /* new */ | |
24 struct Node* newNode(char* name) { | 27 struct Node* newNode(char* name) { |
25 struct Node* node; | 28 struct Node* node; |
26 node = (struct Node*) malloc(sizeof(struct Node)); | 29 node = (struct Node*) malloc(sizeof(struct Node)); |
27 node->name = name; | 30 node->name = name; |
28 node->value = 0; | 31 node->value = 0; |
29 node->right = 0; | 32 node->right = 0; |
30 node->down = 0; | 33 node->down = 0; |
31 return node; | 34 return node; |
32 } | 35 } |
33 | 36 |
34 void init() { | |
35 root = newNode("print"); | |
36 root->down = newNode("sum"); | |
37 } | |
38 | 37 |
39 void cleanup(struct Node* node) { | 38 /* delete */ |
39 void delete(struct Node* node) { | |
40 if (node->down != NULL) { | 40 if (node->down != NULL) { |
41 cleanup(node->down); | 41 delete(node->down); |
42 } | 42 } |
43 if (node->right != NULL) { | 43 if (node->right != NULL) { |
44 cleanup(node->right); | 44 delete(node->right); |
45 } | 45 } |
46 free(node); node=0; | 46 free(node); node=0; |
47 } | 47 } |
48 | 48 |
49 | |
50 /* print */ | |
49 void printNode(struct Node* node) { | 51 void printNode(struct Node* node) { |
50 printf("Node: %20s (%c)\n", node->name, node->value); | 52 printf("Node: %20s (%c)\n", node->name, node->value); |
51 } | 53 } |
52 | 54 |
53 void printTree(struct Node* root) { | 55 void printTree(struct Node* root) { |
54 printNode(root); | 56 printNode(root); |
57 printf(" down: "); | |
55 if (root->down != NULL) { | 58 if (root->down != NULL) { |
56 printTree(root->down); | 59 printTree(root->down); |
60 } else { | |
61 printf("NULL\n"); | |
57 } | 62 } |
63 printf(" right: "); | |
58 if (root->right != NULL) { | 64 if (root->right != NULL) { |
59 printTree(root->right); | 65 printTree(root->right); |
66 } else { | |
67 printf("NULL\n"); | |
60 } | 68 } |
61 } | 69 } |
62 | 70 |
63 | 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 | |
109 /* traverse */ | |
110 void traverse(struct Node* root) { | |
111 /* each node controlls the nodes below itself */ | |
112 action(root); | |
113 } | |
114 | |
115 /* init */ | |
116 void init() { | |
117 root = newNode("print"); | |
118 root->down = newNode("number"); | |
119 root->down = newNode("sum"); | |
120 root->down->down = newNode("number"); | |
121 root->down->down->value = 70; /* 'F' */ | |
122 root->down->down->right = newNode("number"); | |
123 root->down->down->right->value = 50; /* '2' */ | |
124 /* result should be 'x' */ | |
125 } | |
126 | |
127 | |
128 /* main */ | |
64 int main(int argc, char* argv[]) { | 129 int main(int argc, char* argv[]) { |
65 init(); | 130 init(); |
66 printTree(root); | 131 printTree(root); |
67 cleanup(root); | 132 |
133 action(root); | |
134 | |
135 delete(root); | |
68 | 136 |
69 return(0); | 137 return(0); |
70 } | 138 } |