baum

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