hey guys i wrote this code for expression tree evaluation and traversal in C . I've come across a few errors , couldn't resolve them please help me out!
Code:
#include<string.h>#include<malloc.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
union info
{
int x;
char ch;
};
struct tree
{
char data;
union info data2;
struct tree *left,*right;
}h;
char exp[50];
int n,lb,ub,l,u;
struct tree *q;
int check_precedency(char ch)
{
switch(ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 3;
}
}
int detect(char ch)
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/':
return 1;
default:
return 0;
}
}
int convert(char ch)
{
switch(ch)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return 0;
}
}
void convertCharToInt(exp[],l,u) // line 81
{
char s[10];
int i;
int k=0;
char **str;
for(i=l;i<u;i++,k++)
{
s[k]=exp[i];
}
s[k]='\0';
float x=strtod(s,str);
}
void Exp_tree(struct tree *q,lb,ub) //line 95
{
int i;
if(lb>ub)return;
if(lb>=ub)
{
q->data=exp[lb];
q->data2.x=convert(exp[lb]);
q->right=NULL;
q->left=NULL;
return;
}
int previous=4,precedency,position;
for(i=ub;i>=lb;i--)
{
precedency=check_precedency(exp[i]);
if(previous>precedency)
{position=i;previous=precedency;}
}
i=position;
q->data2.ch=exp[i];
q->data=exp[i];
q->left=(struct tree*)malloc(sizeof(struct tree));
Exp_tree(q->left,lb,i-1);
q->right=(struct tree*)malloc(sizeof(struct tree));
Exp_tree(q->right,i+1,ub);
}
int evaluate(struct tree* q)
{
switch(q->data2.ch)
{
case '+':
return(q->left->data2.x+q->right->data2.x);
case '-':
return(q->left->data2.x-q->right->data2.x);
case '*':
return(q->left->data2.x*q->right->data2.x);
case '/':
return(q->left->data2.x/q->right->data2.x);
}
}
void find(struct tree *q)
{
if(q->left==NULL||q->right==NULL)return;
find(q->left);
find(q->right);
if(q->left->right==NULL&&q->left->left==NULL&&q->right->right==NULL&&q->right->left==NULL)
{
printf("\nLeaf Node.: %d %d ",q->left->data2.x,q->right->data2.x);
q->data2.x=evaluate(q);
printf("\tOperator.: %d Result.: %d",q->data,q->data2.x);
q->left=NULL;
q->right=NULL;
}
}
void print_inorder(struct tree *p)
{
if(p==NULL)return;
print_inorder(p->left);
printf("%d",p->data);
print_inorder(p->right);
}
void print_preorder(struct tree *p)
{
if(p==NULL)return;
printf("%d",p->data);
print_preorder(p->left);
print_preorder(p->right);
}
void print_postorder(struct tree *p)
{
if(p==NULL)return;
print_postorder(p->left);
print_postorder(p->right);
printf("%d",p->data);
}
int main()
{
clrscr();
printf("\nEnter Infix Expression\n");
scanf("%d",&exp);
n=strlen(exp);
struct tree *q;
q=(struct tree*)malloc(sizeof(struct tree));
Exp_tree(q,0,n-1);
printf("\nInorder.: ");
print_inorder(q);
printf("\nPreorder.: ");
print_preorder(q);
printf("\nPostorder.: ");
print_postorder(q);
find(q);
printf("\nFinal Solution.: %d",q->data2.x);
getch();
return(0);
}
The errors are following from DEV C++
line 19-[Warning]built-in function 'exp' declared as non-function.
line 81 - syntax error before '[' token.
line 95 - syntax error before "lb".