annotate actions.c @ 48:f9fc4c4f9e3d 0.3

documented nodes in man page
author meillo@marmaro.de
date Sun, 02 Mar 2008 11:04:52 +0100
parents c31b5bb6d493
children 0870e261bf28
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
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 16
diff changeset
6 unsigned char action_print(struct Node* node);
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 16
diff changeset
7 unsigned char action_sum(struct Node* node);
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 16
diff changeset
8 unsigned char action_number(struct Node* node);
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 16
diff changeset
9 unsigned char action_input(struct Node* node);
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 16
diff changeset
10 unsigned char action_times(struct Node* node);
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 16
diff changeset
11
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 16
diff changeset
12
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
13
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
14 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
15 if (node == NULL) {
38
ff01f0f076e4 option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents: 36
diff changeset
16 if (option_verbose) {
ff01f0f076e4 option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents: 36
diff changeset
17 fprintf(stderr, "warning: action of non existing node\n");
ff01f0f076e4 option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents: 36
diff changeset
18 }
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
19 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
20 }
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
21
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
22 if (strcmp(node->name, "print") == 0) {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
23 return action_print(node);
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
24
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
25 } else if (strcmp(node->name, "sum") == 0) {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
26 return action_sum(node);
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
27
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
28 } else if (strcmp(node->name, "number") == 0) {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
29 return action_number(node);
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
30
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
31 } else if (strcmp(node->name, "input") == 0) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
32 return action_input(node);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
33
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
34 } 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
35 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
36
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
37 } else {
31
4e60d96265f0 removed -c option completely; updated man page; new error code 5
meillo@marmaro.de
parents: 30
diff changeset
38 fprintf(stderr, "unknown kind of node\n");
16
b62288419c1c added README and LICENSE; changed error code of invalid node
meillo@marmaro.de
parents: 15
diff changeset
39 exit(4);
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
40 }
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
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
45 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
46 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
47 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
48 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
49 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
50 } 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
51 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
52 }
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 return result;
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
54 }
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
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
57 unsigned char action_sum(struct Node* node) {
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
58 struct Node* tp;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
59 tp = node->down;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
60 while (tp != NULL) {
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
61 node->value += action(tp);
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
62 tp = tp->right;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
63 }
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
64 return node->value;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
65 }
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
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
68 unsigned char action_number(struct Node* node) {
38
ff01f0f076e4 option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents: 36
diff changeset
69 if (node->down != NULL) {
ff01f0f076e4 option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents: 36
diff changeset
70 action(node->down);
ff01f0f076e4 option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents: 36
diff changeset
71 }
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
72 return node->value;
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
73 }
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents:
diff changeset
74
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
75
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
76 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
77 int input;
14
15e11eea1c66 input says now that it wants an input
meillo@marmaro.de
parents: 11
diff changeset
78 printf("input: ");
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
79 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
80 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
81 insertLast(node, newNode("number", (char) input));
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
82 return 0;
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
83 }
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
84
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
85
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
86 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
87 unsigned char i;
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 5
diff changeset
88 for (i = 0; i < node->value; i++) {
46
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
89 insertLast(node, copyTree(node->down));
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
90 }
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 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
92 }