繁体   English   中英

为什么我的树结构不分配其变量?

[英]Why will my tree struct not assign its variable?

我正在制作我的第一个解析器,并草拟了一个解析器,但是当我测试解析器是否正常工作时,我发现除了树结构变量没有得到分配之外,其他所有东西都以正确的方式工作。 左右节点正在生成,但未分配节点内的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;
}

buildtree函数中初始化您的节点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM