Mercurial > baum
view actions.c @ 9:c020b0d1cfca
read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
author | meillo@marmaro.de |
---|---|
date | Sat, 09 Feb 2008 16:41:41 +0100 |
parents | c202ccccedb5 |
children | 0e15841ae111 |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "baum.h" #include "actions.h" unsigned char action(struct Node* node) { if (node == NULL) { fprintf(stderr, "action of non existing node\n"); return 0; } if (strcmp(node->name, "print") == 0) { logit("print-node"); return action_print(node); } else if (strcmp(node->name, "sum") == 0) { logit("sum-node"); return action_sum(node); } else if (strcmp(node->name, "number") == 0) { logit("number-node"); return action_number(node); } else if (strcmp(node->name, "input") == 0) { logit("input-node"); return action_input(node); } else if (strcmp(node->name, "times") == 0) { logit("times-node"); return action_times(node); } else if (strcmp(node->name, "blackhole") == 0) { logit("blackhole-node"); return action_blackhole(node); } else { fprintf(stderr, "unknown kind of node"); exit(1); } } unsigned char action_print(struct Node* node) { unsigned char result; result = action(node->down); if (node->value == 'c') { printf("%c", result); } else { printf("%d", result); } return result; } unsigned char action_sum(struct Node* node) { struct Node* tp; tp = node->down; while (tp != NULL) { node->value += action(tp); tp = tp->right; } return node->value; } unsigned char action_number(struct Node* node) { return node->value; } unsigned char action_input(struct Node* node) { /* unsigned char input = (unsigned char) getchar(); getchar(); /* catches the newline */ /* reads a number which is treated as ASCII value */ int input; scanf("%d", &input); input = input % 256; insertLast(node, newNode("number", (char) input)); return 0; } unsigned char action_times(struct Node* node) { struct Node* tp; unsigned char i; tp = node->down; for (i; i < node->value; i++) { /* deep copy */ } return 0; } unsigned char action_blackhole(struct Node* node) { action(node->down); return 0; }