baum

changeset 3:15d7d6b9766f

added input; added nextNode, lastNode, insertLast
author meillo@marmaro.de
date Thu, 07 Feb 2008 16:15:07 +0100
parents 557fa4df2bcd
children 24a697f37e7c
files actions.c actions.h baum.c baum.h
diffstat 4 files changed, 82 insertions(+), 21 deletions(-) [+]
line diff
     1.1 --- a/actions.c	Thu Feb 07 14:46:27 2008 +0100
     1.2 +++ b/actions.c	Thu Feb 07 16:15:07 2008 +0100
     1.3 @@ -5,19 +5,27 @@
     1.4  #include "actions.h"
     1.5  
     1.6  
     1.7 -char action(struct Node* node) {
     1.8 +unsigned char action(struct Node* node) {
     1.9  	if (strcmp(node->name, "print") == 0) {
    1.10  		logit("print-node");
    1.11  		return action_print(node);
    1.12 +
    1.13  	} else if (strcmp(node->name, "sum") == 0) {
    1.14  		logit("sum-node");
    1.15  		return action_sum(node);
    1.16 +
    1.17  	} else if (strcmp(node->name, "printchar") == 0) {
    1.18  		logit("printchar-node");
    1.19  		return action_printchar(node);
    1.20 +
    1.21  	} else if (strcmp(node->name, "number") == 0) {
    1.22  		logit("number-node");
    1.23  		return action_number(node);
    1.24 +
    1.25 +	} else if (strcmp(node->name, "input") == 0) {
    1.26 +		logit("input-node");
    1.27 +		return action_input(node);
    1.28 +
    1.29  	} else {
    1.30  		fprintf(stderr, "unknown kind of node");
    1.31  		exit(1);
    1.32 @@ -26,19 +34,19 @@
    1.33  
    1.34  
    1.35  
    1.36 -char action_print(struct Node* node) {
    1.37 +unsigned char action_print(struct Node* node) {
    1.38  	printf("%d\n", action(node->down));
    1.39  	return 0;
    1.40  }
    1.41  
    1.42  
    1.43 -char action_printchar(struct Node* node) {
    1.44 +unsigned char action_printchar(struct Node* node) {
    1.45  	printf("%c\n", action(node->down));
    1.46  	return 0;
    1.47  }
    1.48  
    1.49  
    1.50 -char action_sum(struct Node* node) {
    1.51 +unsigned char action_sum(struct Node* node) {
    1.52  	struct Node* tp;
    1.53  	tp = node->down;
    1.54  	while (tp != NULL) {
    1.55 @@ -49,7 +57,16 @@
    1.56  }
    1.57  
    1.58  
    1.59 -char action_number(struct Node* node) {
    1.60 +unsigned char action_number(struct Node* node) {
    1.61  	return node->value;
    1.62  }
    1.63  
    1.64 +
    1.65 +unsigned char action_input(struct Node* node) {
    1.66 +	unsigned char input = (unsigned char) getchar();
    1.67 +	getchar();  /* catches the newline */
    1.68 +	insertLast(node, newNode("number", input));
    1.69 +	return 0;
    1.70 +}
    1.71 +
    1.72 +
     2.1 --- a/actions.h	Thu Feb 07 14:46:27 2008 +0100
     2.2 +++ b/actions.h	Thu Feb 07 16:15:07 2008 +0100
     2.3 @@ -1,8 +1,9 @@
     2.4  
     2.5 -char action(struct Node* node);
     2.6 +unsigned char action(struct Node* node);
     2.7  
     2.8 -char action_print(struct Node* node);
     2.9 -char action_printchar(struct Node* node);
    2.10 -char action_sum(struct Node* node);
    2.11 -char action_number(struct Node* node);
    2.12 +unsigned char action_print(struct Node* node);
    2.13 +unsigned char action_printchar(struct Node* node);
    2.14 +unsigned char action_sum(struct Node* node);
    2.15 +unsigned char action_number(struct Node* node);
    2.16 +unsigned char action_input(struct Node* node);
    2.17  
     3.1 --- a/baum.c	Thu Feb 07 14:46:27 2008 +0100
     3.2 +++ b/baum.c	Thu Feb 07 16:15:07 2008 +0100
     3.3 @@ -24,17 +24,38 @@
     3.4  
     3.5  
     3.6  /* new */
     3.7 -struct Node* newNode(char* name) {
     3.8 +struct Node* newNode(char* name, unsigned char value) {
     3.9  	struct Node* node;
    3.10  	node = (struct Node*) malloc(sizeof(struct Node));
    3.11  	node->name = name;
    3.12 -	node->value = 0;
    3.13 +	node->value = value;
    3.14  	node->right = 0;
    3.15  	node->down = 0;
    3.16  	return node;
    3.17  }
    3.18  
    3.19  
    3.20 +void setValue(struct Node* node, unsigned char value) {
    3.21 +	node->value = value;
    3.22 +}
    3.23 +
    3.24 +
    3.25 +struct Node* nextNode(struct Node* node) {
    3.26 +	return node->right;
    3.27 +}
    3.28 +
    3.29 +struct Node* lastNode(struct Node* node) {
    3.30 +	while (node->right != NULL) {
    3.31 +		node = node->right;
    3.32 +	}
    3.33 +	return node;
    3.34 +}
    3.35 +
    3.36 +void insertLast(struct Node* node, struct Node* insert) {
    3.37 +	node = lastNode(node);
    3.38 +	node->right = insert;
    3.39 +}
    3.40 +
    3.41  /* delete */
    3.42  void delete(struct Node* node) {
    3.43  	if (node->down != NULL) {
    3.44 @@ -78,14 +99,25 @@
    3.45  
    3.46  /* init */
    3.47  void init() {
    3.48 -	root = newNode("printchar");
    3.49 -	root->down = newNode("number");
    3.50 -	root->down = newNode("sum");
    3.51 -	root->down->down = newNode("number");
    3.52 -	root->down->down->value = 70;  /* 'F' */
    3.53 -	root->down->down->right = newNode("number");
    3.54 -	root->down->down->right->value = 50;  /* '2' */
    3.55 -	/* result should be 'x' */
    3.56 +	/* add some numbers
    3.57 +	root = newNode("print", 0);
    3.58 +	root->down = newNode("sum", 0);
    3.59 +
    3.60 +	root->down->down = newNode("number", 60);
    3.61 +
    3.62 +	root->down->down->right = newNode("number", 50);
    3.63 +
    3.64 +	root->down->down->right->right = newNode("sum", 0);
    3.65 +	root->down->down->right->right->down = newNode("number", 1);
    3.66 +	root->down->down->right->right->down->right = newNode("number", 5);
    3.67 +	*/
    3.68 +
    3.69 +	/* input numbers and add them */
    3.70 +	root = newNode("print", 0);
    3.71 +	root->down = newNode("sum", 0);
    3.72 +	root->down->down = newNode("input", 0);
    3.73 +	root->down->down->right = newNode("input", 0);
    3.74 +	root->down->down->right->right = newNode("input", 0);
    3.75  }
    3.76  
    3.77  
    3.78 @@ -96,6 +128,7 @@
    3.79  
    3.80  	action(root);
    3.81  
    3.82 +	printTree(root);
    3.83  	delete(root);
    3.84  	
    3.85  	return(0);
     4.1 --- a/baum.h	Thu Feb 07 14:46:27 2008 +0100
     4.2 +++ b/baum.h	Thu Feb 07 16:15:07 2008 +0100
     4.3 @@ -1,11 +1,21 @@
     4.4  
     4.5  void logit(char* text);
     4.6  
     4.7 +struct Node* newNode(char* name, unsigned char value);
     4.8 +void setValue(struct Node* node, unsigned char value);
     4.9 +void delete(struct Node* node);
    4.10 +void printNode(struct Node* node);
    4.11 +void printTree(struct Node* root);
    4.12 +void traverse(struct Node* root);
    4.13 +
    4.14 +struct Node* nextNode(struct Node* node);
    4.15 +struct Node* lastNode(struct Node* node);
    4.16 +void insertLast(struct Node* node, struct Node* insert);
    4.17  
    4.18  
    4.19  struct Node {
    4.20  	char* name;
    4.21 -	char value;
    4.22 +	unsigned char value;
    4.23  	struct Node* down;
    4.24  	struct Node* right;
    4.25  };