我需要将Node对象转换为C中的字符串

时间:2014-04-17 21:49:48

标签: c string struct

我在完成家庭作业时遇到了麻烦。我完成了大部分工作,但我仍然坚持这一部分。如果查看头文件,您会看到一条评论:

  

将Node对象转换为字符串。我们需要一个指向Node对象的指针,以便我们可以显示对象的地址以供进一步参考

我尽我所能,但我不知道从哪里开始。

The output from this test program looks like: 

<0x0022ff40>[value: 2, left: 0x00000000, right: 0x00000000] 
<0x0022ff30>[value: 3, left: 0x00000000, right: 0x00000000] 
<0x0022ff20>[value: 4, left: 0x00000000, right: 0x00000000] 

<0x0022ff10>[value: *, left: 0x0022ff30, right: 0x0022ff20] 
<0x0022ff00>[value: +, left: 0x0022ff40, right: 0x0022ff10] 
 Tree evaluation: 14 

<0x0022ff00>[value: +, left: 0x0022ff40, right: 0x0022ff30] 
<0x0022ff10>[value: *, left: 0x0022ff00, right: 0x0022ff20] 
 Tree evaluation: 20 

<0x0022ff00>[value: ., left: 0x0022ff40, right: 0x0022ff30] 
<0x0022ff10>[value: *, left: 0x0022ff00, right: 0x0022ff20] 
invalid operator: . 

这是我的main()

#include <stdio.h>
#include "TreeNode.h"

int main(void) 
{ 
 Node n2, n3, n4, times, plus; 

 setNode(&n2, 2, NULL, NULL, true); 
 setNode(&n3, 3, NULL, NULL, true); 
 setNode(&n4, 4, NULL, NULL, true); 
 printf("\n"); 

 setNode(&times, '*', &n3, &n4, true); 
 setNode(&plus, '+', &n2, &times, true); 

 printf(" Tree evaluation: %d\n\n", eval(&plus)); 

 setNode(&plus, '+', &n2, &n3, true); 
 setNode(&times, '*', &plus, &n4, true); 

 printf(" Tree evaluation: %d\n\n", eval(&times)); 

 setNode(&plus, '.', &n2, &n3, true); 
 setNode(&times, '*', &plus, &n4, true); 

 printf(" Tree evaluation: %d\n\n", eval(&times)); 

 return 0; 
}

这是我的标题:

#include <stdio.h>
#ifndef TREE_NODE
#define TREE_NODE

typedef struct Node_t {
    int value;
    struct Node_t   *left;
    struct Node_t   *right;
} Node, *Node_p;

typedef char* String;

typedef enum {false, true} bool;

/*
 *  Convert a Node object to a string.
 *
 *  We require a pointer to the Node object so that we can display the
 *  address of the object for further reference.
 */
String nodeToString(Node_p np) {
    static char output[0];
    sprintf(output, "<0x%d>\n", np, np->value, np->left, np->right);
    return output;
}

void setNode(Node_p np,
     int    value,
     Node_p    left,
     Node_p    right,
     bool    display) {
        np->value = value; 
        np->left = left;   
        np->right = right;
     if (display) {
          printf("%s\n", nodeToString(np));  
     }

}

int eval(Node_p np){

    switch(np->value) {
        case '+':
               np->value = eval(np->left) + eval(np->right);
               return np->value;
        case '-':
               np->value = eval(np->left) - eval(np->right);
               return np->value;
        case '*':
               np->value = eval(np->left) * eval(np->right);
               return np->value;
        case '/':
               np->value = eval(np->left) / eval(np->right);
               return np->value;
        default:
                exit(1);
    }       
    return 0;
}

#endif

1 个答案:

答案 0 :(得分:1)

您在结构Node_t中使用相同的字段作为运算符和值。像这样扩展你的结构:

typedef struct Node_t {
    int value;
    char op; /*operator*/
    struct Node_t   *left;
    struct Node_t   *right;
} Node, *Node_p;

为运算符的函数setNode添加一个新参数(当Node是数字时将其设置为'\ 0';您必须在此运算符上切换eval),然后添加

case '\0':
        return np->value;
        break;

转换为eval。