Sunday, 15 July 2012

c - Why will my tree struct not assign its variable? -



c - Why will my tree struct not assign its variable? -

i making first parser , have drawn parser should work when tested whether parser working correctly discovered working right way except tree struct's variables not getting assignment. right , left nodes getting generated sym value within node not beingness assigned. here code:

#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)); homecoming name; }; struct node *buildtree(){ struct node* name=(struct node*)malloc(sizeof(struct node)); homecoming name; } void settable(struct look_up_table *table){ int i; (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': (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); homecoming 0; }

initialize node in buildtree function

c xcode struct tree variable-assignment

No comments:

Post a Comment