baum

annotate 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
rev   line source
meillo@2 1 #include <stdio.h>
meillo@2 2 #include <stdlib.h>
meillo@2 3 #include <string.h>
meillo@2 4 #include "baum.h"
meillo@2 5 #include "actions.h"
meillo@2 6
meillo@26 7 unsigned char action_print(struct Node* node);
meillo@26 8 unsigned char action_sum(struct Node* node);
meillo@26 9 unsigned char action_number(struct Node* node);
meillo@26 10 unsigned char action_input(struct Node* node);
meillo@26 11 unsigned char action_times(struct Node* node);
meillo@26 12
meillo@26 13
meillo@2 14
meillo@3 15 unsigned char action(struct Node* node) {
meillo@5 16 if (node == NULL) {
meillo@38 17 if (option_verbose) {
meillo@38 18 fprintf(stderr, "warning: action of non existing node\n");
meillo@38 19 }
meillo@5 20 return 0;
meillo@5 21 }
meillo@5 22
meillo@2 23 if (strcmp(node->name, "print") == 0) {
meillo@2 24 return action_print(node);
meillo@3 25
meillo@2 26 } else if (strcmp(node->name, "sum") == 0) {
meillo@2 27 return action_sum(node);
meillo@3 28
meillo@2 29 } else if (strcmp(node->name, "number") == 0) {
meillo@2 30 return action_number(node);
meillo@3 31
meillo@3 32 } else if (strcmp(node->name, "input") == 0) {
meillo@3 33 return action_input(node);
meillo@3 34
meillo@5 35 } else if (strcmp(node->name, "times") == 0) {
meillo@5 36 return action_times(node);
meillo@5 37
meillo@2 38 } else {
meillo@31 39 fprintf(stderr, "unknown kind of node\n");
meillo@16 40 exit(4);
meillo@2 41 }
meillo@2 42 }
meillo@2 43
meillo@2 44
meillo@2 45
meillo@3 46 unsigned char action_print(struct Node* node) {
meillo@5 47 unsigned char result;
meillo@5 48 result = action(node->down);
meillo@5 49 if (node->value == 'c') {
meillo@5 50 printf("%c", result);
meillo@5 51 } else {
meillo@5 52 printf("%d", result);
meillo@5 53 }
meillo@5 54 return result;
meillo@2 55 }
meillo@2 56
meillo@2 57
meillo@3 58 unsigned char action_sum(struct Node* node) {
meillo@2 59 struct Node* tp;
meillo@2 60 tp = node->down;
meillo@2 61 while (tp != NULL) {
meillo@2 62 node->value += action(tp);
meillo@2 63 tp = tp->right;
meillo@2 64 }
meillo@2 65 return node->value;
meillo@2 66 }
meillo@2 67
meillo@2 68
meillo@3 69 unsigned char action_number(struct Node* node) {
meillo@38 70 if (node->down != NULL) {
meillo@38 71 action(node->down);
meillo@38 72 }
meillo@2 73 return node->value;
meillo@2 74 }
meillo@2 75
meillo@3 76
meillo@3 77 unsigned char action_input(struct Node* node) {
meillo@5 78 /* reads a number which is treated as ASCII value */
meillo@5 79 int input;
meillo@14 80 printf("input: ");
meillo@5 81 scanf("%d", &input);
meillo@5 82 input = input % 256;
meillo@5 83 insertLast(node, newNode("number", (char) input));
meillo@5 84
meillo@3 85 return 0;
meillo@3 86 }
meillo@3 87
meillo@3 88
meillo@5 89 unsigned char action_times(struct Node* node) {
meillo@5 90 unsigned char i;
meillo@10 91 for (i = 0; i < node->value; i++) {
meillo@46 92 insertLast(node, copyTree(node->down));
meillo@5 93 }
meillo@5 94 return 0;
meillo@5 95 }