Parse tree doesn't work
<p>I want to build simple parse tree/but it's not working. I think that strlen is returning the wrong result.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;conio.h&gt;

int min(int a ,int b){ return (a&gt;b)?b:a;}

class Node
{
    Node* left;
    Node* right;
    char operation;

public:
    float res;

    Node(char* str)
    {
        char* ch = NULL;
        ch = strrchr2(str);
        if (ch==NULL)
        {
            if(str[0]=='(')
            {
                str = &amp;str[1]; 
                int k = strlen(str);
                char *tmp = new char[k-1];

                strncpy(tmp,str,k-1);
                ch = strrchr2(tmp);

                div(tmp,ch);
            }
            else
                res = atof(str);                
        }
        else
        {
            div(str,ch);
        }
    }

    void div(char* str,char* ch)
    {
            //getting opertion
            strncpy(&amp;operation,ch,1);

            //getting position of operation
            int pos = strlen(str)-strlen(ch);

            //right node creation
            ch = &amp;ch[1];
            right = new Node(ch);

            //left node creation
            char* tmp = new char[pos];
            strncpy(tmp,str,pos);
            left = new Node(tmp);

            //applying opertion to to node's results
            switch( operation )
            {
                case '/':this-&gt;res = left-&gt;res/right-&gt;res;break;
                case '*':this-&gt;res = left-&gt;res*right-&gt;res;break;
                case '+':this-&gt;res = left-&gt;res+right-&gt;res;break;
                case '-':this-&gt;res = left-&gt;res-right-&gt;res;break;
                default:break;
            };
    }

    char* strrchr2(char* s)
    {
        char* str = new char[strlen(s)];
        strcpy(str,s);
        int i = search(str);
        if(i==-1) return NULL;
            else
        if(str[i]=='+' || str[i]=='-') return &amp;str[i];
            else if (str[i] == '*' || str[i]=='/')
            {
                char c = str[i+1];
                if(i+1&lt;strlen(str)-1 &amp;&amp; search(&amp;c)!=-1) return strrchr2(&amp;c);
                else return &amp;str[i];
            }
    }

    int search(char* str)
    {
        int cnt1 = 0;
        int cnt2 = 0;
        for(int i=0;i&lt;strlen(str);i++)
        {
            if (str[i]=='(') cnt1++;
            else if (str[i]==')') cnt2++;
            if (cnt1!=0)
            {
               if(cnt1==cnt2)
               {
                  int m = strlen(str);
                  return ((i+1)!=m?i+1:-1);
               }
            }
            else
            {
                if (str[i]=='-' || str[i]=='+' || str[i]=='*' || str[i]=='/') return i;
            }
        }
        char* ch = NULL;
        ch = strchr(str,'-');
        if(ch==NULL) ch = strchr(str,'+');
        if(ch==NULL) ch = strchr(str,'*');
        if(ch==NULL) ch = strchr(str,'/');
        if(ch==NULL)return -1;
        else return strlen(str)-strlen(ch);
    }
};

int main()
{
    char* expr = "(1/2)-(1/2)";
    Node* n = new Node(expr);
    printf("%s = %.3f",expr,n-&gt;res);



    _getch();
}
</code></pre>
