예제 #1
0
        //Method

        //Insert function to the binary tree
        public Function Insert(ref string s)
        {
            Function root = null;

            switch (s[0])
            {
            case '+':
            {
                root = new Plus();
                s    = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case '-':
            {
                root = new Substract();
                if (s[1] != '(')
                {
                    break;
                }
                s = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case '*':
            {
                root = new Multiply();
                s    = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case '/':
            {
                root = new Divide();
                s    = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case '^':
            {
                root = new Power();
                s    = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case 'n':
            {
                root = new n();
                s    = s.Substring(2);
                string[] parts = s.Split(')');
                (root as n).Data = Convert.ToInt32(parts[0].ToString());
                while (s[0] != ')')
                {
                    s = s.Substring(1);
                }
                break;
            }

            case 'r':
            {
                root = new r();
                s    = s.Substring(2);
                string[] parts = s.Split(')');
                (root as r).Data = Convert.ToDouble(parts[0].ToString());
                while (s[0] != ')')
                {
                    s = s.Substring(1);
                }
                break;
            }

            case 's':
            {
                root = new S();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case 'c':
            {
                root = new c();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case 'e':
            {
                root = new e();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case 'l':
            {
                root = new l();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case '!':
            {
                root = new Factorial();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case 'p':
            {
                root = new pi();
                break;
            }

            case 'x':
            {
                root = new x();
                break;
            }

            default:
            {
                root = new Digit();
                (root as Digit).Data = Convert.ToInt32(s[0].ToString());
                break;
            }
            }
            return(root);
        }