baum

view actions.c @ 46:22305a6e128d

added deep copy and fixed so node times
author meillo@marmaro.de
date Sun, 02 Mar 2008 10:29:55 +0100
parents 0b82169d4129
children c31b5bb6d493
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "baum.h"
5 #include "actions.h"
7 unsigned char action_print(struct Node* node);
8 unsigned char action_sum(struct Node* node);
9 unsigned char action_number(struct Node* node);
10 unsigned char action_input(struct Node* node);
11 unsigned char action_times(struct Node* node);
15 unsigned char action(struct Node* node) {
16 if (node == NULL) {
17 if (option_verbose) {
18 fprintf(stderr, "warning: action of non existing node\n");
19 }
20 return 0;
21 }
23 if (strcmp(node->name, "print") == 0) {
24 return action_print(node);
26 } else if (strcmp(node->name, "sum") == 0) {
27 return action_sum(node);
29 } else if (strcmp(node->name, "number") == 0) {
30 return action_number(node);
32 } else if (strcmp(node->name, "input") == 0) {
33 return action_input(node);
35 } else if (strcmp(node->name, "times") == 0) {
36 return action_times(node);
38 } else {
39 fprintf(stderr, "unknown kind of node\n");
40 exit(4);
41 }
42 }
46 unsigned char action_print(struct Node* node) {
47 unsigned char result;
48 result = action(node->down);
49 if (node->value == 'c') {
50 printf("%c", result);
51 } else {
52 printf("%d", result);
53 }
54 return result;
55 }
58 unsigned char action_sum(struct Node* node) {
59 struct Node* tp;
60 tp = node->down;
61 while (tp != NULL) {
62 node->value += action(tp);
63 tp = tp->right;
64 }
65 return node->value;
66 }
69 unsigned char action_number(struct Node* node) {
70 if (node->down != NULL) {
71 action(node->down);
72 }
73 return node->value;
74 }
77 unsigned char action_input(struct Node* node) {
78 /* reads a number which is treated as ASCII value */
79 int input;
80 printf("input: ");
81 scanf("%d", &input);
82 input = input % 256;
83 insertLast(node, newNode("number", (char) input));
85 return 0;
86 }
89 unsigned char action_times(struct Node* node) {
90 unsigned char i;
91 for (i = 0; i < node->value; i++) {
92 insertLast(node, copyTree(node->down));
93 }
94 return 0;
95 }