为什么我的树结构不会赋值给它?

时间:2014-10-23 23:54:40

标签: c xcode struct tree variable-assignment

我正在制作我的第一个解析器并且已经编写了一个应该可以工作的解析器但是当我测试解析器是否正常工作时我发现一切都正常工作,除了树形结构的变量没有得到他们的任务。正在生成右侧和左侧节点,但未分配节点内的sym值。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h> // isalpha, etc.

struct node{
    int val;
    char sym;
    struct node *left;
    struct node *right;
};

struct look_up_table{
    char equation[20];
    char literal[20];
    char symbols[20];
    int number[20];
    int symbolsx[20];
    int symbolsy[20];
    char abstractsf[20];
    int abstractx[20];
    int abstracty[20];
};

struct look_up_table *buildtable(){
    struct look_up_table* name =(struct look_up_table*)malloc(sizeof(struct look_up_table));
    return name;
};

struct node *buildtree(){
    struct node* name=(struct node*)malloc(sizeof(struct node));
    return name;
}

void settable(struct look_up_table *table){
    int i;
    for (i=0;i<20;i++){
        table->equation[i]='\0';
        table->literal[i]='\0';
        table->symbols[i]='0';
        table->number[i]='\0';
        table->symbolsx[i]='\0';
        table->symbolsy[i]='\0';
        table->abstractsf[i]='\0';
    }
}
void handle(struct look_up_table * table){
    _Bool pattern = 1;
    int i;
    for(i=0;table->equation[i]!='\0';i++){
        if (pattern){
            if (isalpha(table->equation[i]))
                table->literal[i]='l';
            else if (isdigit(table->equation[i]))
                table->literal[i]='i';
            else
                table->literal[i]=table->equation[i];
        }
    }
}

void parse1(struct look_up_table *table,int i, int p){
    char dec[20];
    if (table->literal[i]!='\0'){
        switch(table->literal[i]){
        case 'i':
            for (table->symbolsx[i]=i; table->literal[i]=='i'; i++)
                dec[p]=table->equation[i];
            --i;
            table->symbolsy[i]=p;
            table->symbols[p]='i';
            table->number[p++]=atoi(dec);
            break;
        case '*': table->symbols[p++]='*';  break;
        case '/': table->symbols[p++]='/';  break;
        case '+': table->symbols[p++]='+';  break;
        case '-': table->symbols[p++]='-';  break;
        case '(': table->symbols[p++]='(';  break;
        case ')': table->symbols[p++]=')';  break;
        case '^': table->symbols[p++]='^';  break;
        case 'l': table->symbols[p]='l';
                  table->symbolsx[p++]=i;
                  break;
        default:  // TODO: error handling
                  break;
        }
        if (table->literal[i]!='\0')
            parse1(table, ++i, p);
    }
}

void treebuild(struct look_up_table table, struct node root,int i, int b, int e){
    while (table.symbols[i]!='\0'){
        if(table.symbols[i]=='('){
            int count=1;
            while(table.symbols[++i]!=')'&& count>0){
                if (table.symbols[i]==')')
                    count--;
                if(table.symbols[i]=='(')
                    count++;
            }
        }
        if (table.symbols[i]=='+'||table.symbols[i]=='-'){
            switch (table.literal[i]) {
            case '+':
                root.sym='+';
                treebuild(table, *root.left, b,b,i-1);
                treebuild(table, *root.right, i+1, i+1,e);
                break;
            case '-':
                root.sym='-';
                treebuild(table, *root.left, b,b,i-1);
                treebuild(table, *root.right, i+1, i+1,e);
            default:
                break;
            }
        }
        i=b;
        while (table.symbols[i]!='\0'){
            switch (table.symbols[i]){
            case 'i':
                if (table.symbols[++i]=='\0'){
                    root.sym='i';
                    root.val=table.number[--i];
                    return;
                }
                switch(table.symbols[i]){
                case '(':
                    root.sym='*';
                    root.left->val=table.number[table.symbolsx[i-1]];
                    treebuild(table, *root.right,i , i, e);
                    break;
                case 'l':
                    root.sym='*';
                    root.left=buildtree();
                    root.left->val=table.number[table.symbolsx[i-1]];
                    root.right=buildtree();
                    treebuild(table, *root.right,i , i, e);
                    break;
                case '*':
                    root.sym='*';
                    root.left->val=table.number[table.symbolsx[i-1]];
                    treebuild(table, *root.right, i+1, i+1, e);
                    break;
                case'/':
                    root.sym='/';
                    root.left->val=table.number[table.symbolsx[i-1]];
                    treebuild(table, *root.right, i+1, i+1, e);
                    break;
                case'^':
                    root.sym='^';
                    root.left->val=table.number[table.symbolsx[i-1]];
                    treebuild(table, *root.right, i+1, i+1, e);
                    break;
                }
            }
        }
    }
}

int main(int argc, const char * argv[]) {
    struct look_up_table* table= buildtable();
    settable(table);
    gets(table->equation);
    handle(table);
    int i=0;
    int p=0;
    parse1(table, i, p);
    i=0;
    int t=0;
    struct node* root= buildtree();
    int m;
    for(m=0;table->symbols[m]!='0';m++){

    }
    treebuild(*table, *root, i, i, m);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

buildtree函数

中初始化您的节点