# HG changeset patch # User meillo@marmaro.de # Date 1202397307 -3600 # Node ID 15d7d6b9766fcb2ea86eb140b626bd0e2d8b0fb4 # Parent 557fa4df2bcdc3ecabbc89eb7d3dc00d60e92f08 added input; added nextNode, lastNode, insertLast diff -r 557fa4df2bcd -r 15d7d6b9766f actions.c --- a/actions.c Thu Feb 07 14:46:27 2008 +0100 +++ b/actions.c Thu Feb 07 16:15:07 2008 +0100 @@ -5,19 +5,27 @@ #include "actions.h" -char action(struct Node* node) { +unsigned char action(struct Node* node) { 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, "printchar") == 0) { logit("printchar-node"); return action_printchar(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 { fprintf(stderr, "unknown kind of node"); exit(1); @@ -26,19 +34,19 @@ -char action_print(struct Node* node) { +unsigned char action_print(struct Node* node) { printf("%d\n", action(node->down)); return 0; } -char action_printchar(struct Node* node) { +unsigned char action_printchar(struct Node* node) { printf("%c\n", action(node->down)); return 0; } -char action_sum(struct Node* node) { +unsigned char action_sum(struct Node* node) { struct Node* tp; tp = node->down; while (tp != NULL) { @@ -49,7 +57,16 @@ } -char action_number(struct Node* node) { +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 */ + insertLast(node, newNode("number", input)); + return 0; +} + + diff -r 557fa4df2bcd -r 15d7d6b9766f actions.h --- a/actions.h Thu Feb 07 14:46:27 2008 +0100 +++ b/actions.h Thu Feb 07 16:15:07 2008 +0100 @@ -1,8 +1,9 @@ -char action(struct Node* node); +unsigned char action(struct Node* node); -char action_print(struct Node* node); -char action_printchar(struct Node* node); -char action_sum(struct Node* node); -char action_number(struct Node* node); +unsigned char action_print(struct Node* node); +unsigned char action_printchar(struct Node* node); +unsigned char action_sum(struct Node* node); +unsigned char action_number(struct Node* node); +unsigned char action_input(struct Node* node); diff -r 557fa4df2bcd -r 15d7d6b9766f baum.c --- a/baum.c Thu Feb 07 14:46:27 2008 +0100 +++ b/baum.c Thu Feb 07 16:15:07 2008 +0100 @@ -24,17 +24,38 @@ /* new */ -struct Node* newNode(char* name) { +struct Node* newNode(char* name, unsigned char value) { struct Node* node; node = (struct Node*) malloc(sizeof(struct Node)); node->name = name; - node->value = 0; + node->value = value; node->right = 0; node->down = 0; return node; } +void setValue(struct Node* node, unsigned char value) { + node->value = value; +} + + +struct Node* nextNode(struct Node* node) { + return node->right; +} + +struct Node* lastNode(struct Node* node) { + while (node->right != NULL) { + node = node->right; + } + return node; +} + +void insertLast(struct Node* node, struct Node* insert) { + node = lastNode(node); + node->right = insert; +} + /* delete */ void delete(struct Node* node) { if (node->down != NULL) { @@ -78,14 +99,25 @@ /* init */ void init() { - root = newNode("printchar"); - root->down = newNode("number"); - root->down = newNode("sum"); - root->down->down = newNode("number"); - root->down->down->value = 70; /* 'F' */ - root->down->down->right = newNode("number"); - root->down->down->right->value = 50; /* '2' */ - /* result should be 'x' */ + /* add some numbers + root = newNode("print", 0); + root->down = newNode("sum", 0); + + root->down->down = newNode("number", 60); + + root->down->down->right = newNode("number", 50); + + root->down->down->right->right = newNode("sum", 0); + root->down->down->right->right->down = newNode("number", 1); + root->down->down->right->right->down->right = newNode("number", 5); + */ + + /* input numbers and add them */ + root = newNode("print", 0); + root->down = newNode("sum", 0); + root->down->down = newNode("input", 0); + root->down->down->right = newNode("input", 0); + root->down->down->right->right = newNode("input", 0); } @@ -96,6 +128,7 @@ action(root); + printTree(root); delete(root); return(0); diff -r 557fa4df2bcd -r 15d7d6b9766f baum.h --- a/baum.h Thu Feb 07 14:46:27 2008 +0100 +++ b/baum.h Thu Feb 07 16:15:07 2008 +0100 @@ -1,11 +1,21 @@ void logit(char* text); +struct Node* newNode(char* name, unsigned char value); +void setValue(struct Node* node, unsigned char value); +void delete(struct Node* node); +void printNode(struct Node* node); +void printTree(struct Node* root); +void traverse(struct Node* root); + +struct Node* nextNode(struct Node* node); +struct Node* lastNode(struct Node* node); +void insertLast(struct Node* node, struct Node* insert); struct Node { char* name; - char value; + unsigned char value; struct Node* down; struct Node* right; };