annotate actions.c @ 9:c020b0d1cfca

read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
author meillo@marmaro.de
date Sat, 09 Feb 2008 16:41:41 +0100
parents c202ccccedb5
children 0e15841ae111
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
1 #include <stdio.h>
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
2 #include <stdlib.h>
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
3 #include <string.h>
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
4 #include "baum.h"
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
5 #include "actions.h"
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
6
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
7
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
8 unsigned char action(struct Node* node) {
5
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
9 if (node == NULL) {
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
10 fprintf(stderr, "action of non existing node\n");
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
11 return 0;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
12 }
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
13
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
14 if (strcmp(node->name, "print") == 0) {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
15 logit("print-node");
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
16 return action_print(node);
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
17
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
18 } else if (strcmp(node->name, "sum") == 0) {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
19 logit("sum-node");
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
20 return action_sum(node);
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
21
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
22 } else if (strcmp(node->name, "number") == 0) {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
23 logit("number-node");
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
24 return action_number(node);
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
25
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
26 } else if (strcmp(node->name, "input") == 0) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
27 logit("input-node");
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
28 return action_input(node);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
29
5
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
30 } else if (strcmp(node->name, "times") == 0) {
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
31 logit("times-node");
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
32 return action_times(node);
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
33
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
34 } else if (strcmp(node->name, "blackhole") == 0) {
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
35 logit("blackhole-node");
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
36 return action_blackhole(node);
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
37
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
38 } else {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
39 fprintf(stderr, "unknown kind of node");
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
40 exit(1);
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
41 }
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
42 }
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
43
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
44
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
45
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
46 unsigned char action_print(struct Node* node) {
5
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
47 unsigned char result;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
48 result = action(node->down);
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
49 if (node->value == 'c') {
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
50 printf("%c", result);
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
51 } else {
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
52 printf("%d", result);
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
53 }
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
54 return result;
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
55 }
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
56
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
57
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
58 unsigned char action_sum(struct Node* node) {
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
59 struct Node* tp;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
60 tp = node->down;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
61 while (tp != NULL) {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
62 node->value += action(tp);
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
63 tp = tp->right;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
64 }
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
65 return node->value;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
66 }
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
67
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
68
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
69 unsigned char action_number(struct Node* node) {
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
70 return node->value;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
71 }
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
72
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
73
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
74 unsigned char action_input(struct Node* node) {
5
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
75 /*
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
76 unsigned char input = (unsigned char) getchar();
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
77 getchar(); /* catches the newline */
5
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
78
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
79 /* reads a number which is treated as ASCII value */
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
80 int input;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
81 scanf("%d", &input);
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
82 input = input % 256;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
83 insertLast(node, newNode("number", (char) input));
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
84
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
85 return 0;
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
86 }
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
87
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
88
5
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
89 unsigned char action_times(struct Node* node) {
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
90 struct Node* tp;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
91 unsigned char i;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
92 tp = node->down;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
93 for (i; i < node->value; i++) {
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
94 /* deep copy */
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
95 }
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
96 return 0;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
97 }
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
98
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
99
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
100 unsigned char action_blackhole(struct Node* node) {
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
101 action(node->down);
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
102 return 0;
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
103 }
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
104
c202ccccedb5 added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
meillo@marmaro.de
parents: 3
diff changeset
105