gpt4 book ai didi

c - 为什么我的树结构不分配它的变量?

转载 作者:行者123 更新时间:2023-11-30 17:27:03 25 4
gpt4 key购买 nike

我正在制作我的第一个解析器,并制定了一个应该可以工作的解析器,但是当我测试解析器是否正常工作时,我发现一切都在正常工作,除了树结构的变量没有得到它们的分配。左右节点正在生成,但节点内的 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 函数中初始化您的节点

关于c - 为什么我的树结构不分配它的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26539426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com