baum

view actions.c @ 48:f9fc4c4f9e3d

documented nodes in man page
author meillo@marmaro.de
date Sun, 02 Mar 2008 11:04:52 +0100
parents c31b5bb6d493
children 0870e261bf28
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "baum.h"
6 unsigned char action_print(struct Node* node);
7 unsigned char action_sum(struct Node* node);
8 unsigned char action_number(struct Node* node);
9 unsigned char action_input(struct Node* node);
10 unsigned char action_times(struct Node* node);
14 unsigned char action(struct Node* node) {
15 if (node == NULL) {
16 if (option_verbose) {
17 fprintf(stderr, "warning: action of non existing node\n");
18 }
19 return 0;
20 }
22 if (strcmp(node->name, "print") == 0) {
23 return action_print(node);
25 } else if (strcmp(node->name, "sum") == 0) {
26 return action_sum(node);
28 } else if (strcmp(node->name, "number") == 0) {
29 return action_number(node);
31 } else if (strcmp(node->name, "input") == 0) {
32 return action_input(node);
34 } else if (strcmp(node->name, "times") == 0) {
35 return action_times(node);
37 } else {
38 fprintf(stderr, "unknown kind of node\n");
39 exit(4);
40 }
41 }
45 unsigned char action_print(struct Node* node) {
46 unsigned char result;
47 result = action(node->down);
48 if (node->value == 'c') {
49 printf("%c", result);
50 } else {
51 printf("%d", result);
52 }
53 return result;
54 }
57 unsigned char action_sum(struct Node* node) {
58 struct Node* tp;
59 tp = node->down;
60 while (tp != NULL) {
61 node->value += action(tp);
62 tp = tp->right;
63 }
64 return node->value;
65 }
68 unsigned char action_number(struct Node* node) {
69 if (node->down != NULL) {
70 action(node->down);
71 }
72 return node->value;
73 }
76 unsigned char action_input(struct Node* node) {
77 int input;
78 printf("input: ");
79 scanf("%d", &input);
80 input = input % 256;
81 insertLast(node, newNode("number", (char) input));
82 return 0;
83 }
86 unsigned char action_times(struct Node* node) {
87 unsigned char i;
88 for (i = 0; i < node->value; i++) {
89 insertLast(node, copyTree(node->down));
90 }
91 return 0;
92 }