Thread: Help debugging 3 errors

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Help debugging 3 errors

    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".

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > #include<malloc.h>
    > #include<conio.h>
    There are no standard header files with these names.

    > line 19-[Warning]built-in function 'exp' declared as non-function.
    There is a standard function called exp.
    Pick a different name for your variable - say expression.

    > void convertCharToInt(exp[],l,u) // line 81
    > void Exp_tree(struct tree *q,lb,ub) //line 95
    Where are the types for these parameters?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Help me convert it to C

    Hmmm
    Last edited by dexter_a; 09-02-2013 at 11:10 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Hey I've this working code in C++ working , I can't find it to work the same converting to C. Need help in converting to C .
    So try writing it yourself in C to begin with.

    As opposed to just finding code on the web, then trying to palm it off on us as your own work (which it isn't), hoping to get it converted to C (which won't be your work either).
    https://sites.google.com/site/vikran...xpression-tree
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging help :(
    By Zul56 in forum C Programming
    Replies: 51
    Last Post: 01-12-2013, 10:21 PM
  2. No errors when debugging?
    By yaya in forum C++ Programming
    Replies: 18
    Last Post: 03-07-2010, 01:07 PM
  3. debugging with gdb
    By -EquinoX- in forum C Programming
    Replies: 2
    Last Post: 11-22-2008, 03:00 PM
  4. Need help w/ debugging errors
    By Blak_Lightnin in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2003, 09:14 PM
  5. Debugging Parse errors
    By niudago in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2003, 07:14 PM