示例#1
0
        private float SolveTree(float x, float y, BinTreeNode <string> tree)
        {
            switch (tree.GetInfo())
            {
            case "x":
                return(x);

            case "y":
                return(y);

            case "+":
                return(SolveTree(x, y, tree.GetLeft()) + SolveTree(x, y, tree.GetRight()));

            case "-":
                return(SolveTree(x, y, tree.GetLeft()) - SolveTree(x, y, tree.GetRight()));

            case "*":
                return(SolveTree(x, y, tree.GetLeft()) * SolveTree(x, y, tree.GetRight()));

            case "/":
                return(SolveTree(x, y, tree.GetLeft()) / SolveTree(x, y, tree.GetRight()));

            case "^":
                return((float)Math.Pow(SolveTree(x, y, tree.GetLeft()), SolveTree(x, y, tree.GetRight())));

            case "sin":
                return((float)Math.Sin(SolveTree(x, y, tree.GetLeft())));

            case "cos":
                return((float)Math.Cos(SolveTree(x, y, tree.GetLeft())));

            case "tan":
                return((float)Math.Tan(SolveTree(x, y, tree.GetLeft())));

            case "abs":
                return((float)Math.Abs(SolveTree(x, y, tree.GetLeft())));

            case "pow":
                return((float)Math.Pow(SolveTree(x, y, tree.GetLeft()), SolveTree(x, y, tree.GetRight())));

            case "max":
                return((float)Math.Max(SolveTree(x, y, tree.GetLeft()), SolveTree(x, y, tree.GetRight())));

            case "min":
                return((float)Math.Min(SolveTree(x, y, tree.GetLeft()), SolveTree(x, y, tree.GetRight())));

            case "sqrt":
                return((float)Math.Sqrt(SolveTree(x, y, tree.GetLeft())));



            default:
                return(float.Parse(tree.GetInfo()));
            }
        }
示例#2
0
        private void MakeTree(BinTreeNode <string> tree)
        {
            short  bracketsCounter = 0;
            string s;
            string tempS = tree.GetInfo();

            if (IsWrappedWithBrackets(tempS))
            {
                tempS = tempS.Substring(1, tempS.Length - 2);
                tree.SetInfo(tempS);
            }

            // +, -
            for (int i = 0; i < tempS.Length; i++)
            {
                if (tempS[i] == '(')
                {
                    bracketsCounter++;
                }
                else if (tempS[i] == ')')
                {
                    bracketsCounter--;
                }

                else if (bracketsCounter == 0)
                {
                    s  = "";
                    s += tempS[i];

                    if (s == "+" || s == "-")
                    {
                        if (i == 0)
                        {
                            tempS = '0' + tempS;
                            i++;
                        }

                        tree.SetLeft(new BinTreeNode <string>(tempS.Substring(0, i)));
                        MakeTree(tree.GetLeft());
                        tree.SetRight(new BinTreeNode <string>(tempS.Substring(i + 1)));
                        MakeTree(tree.GetRight());

                        tree.SetInfo(s);
                        i     = tempS.Length;
                        tempS = s;
                    }
                }
            }

            // *, /, ^
            for (int i = 0; i < tempS.Length; i++)
            {
                if (tempS[i] == '(')
                {
                    bracketsCounter++;
                }
                else if (tempS[i] == ')')
                {
                    bracketsCounter--;
                }

                else if (bracketsCounter == 0)
                {
                    s  = "";
                    s += tempS[i];

                    if (s == "*" || s == "/" || s == "^")
                    {
                        if (i == 0)
                        {
                            tempS = '0' + tempS;
                            i++;
                        }

                        tree.SetLeft(new BinTreeNode <string>(tempS.Substring(0, i)));
                        MakeTree(tree.GetLeft());
                        tree.SetRight(new BinTreeNode <string>(tempS.Substring(i + 1)));
                        MakeTree(tree.GetRight());

                        tree.SetInfo(s);
                        i     = tempS.Length;
                        tempS = s;
                    }
                }
            }

            //
            for (int i = 0; i < tempS.Length; i++)
            {
                if (tempS[i] == '(')
                {
                    bracketsCounter++;
                }
                else if (tempS[i] == ')')
                {
                    bracketsCounter--;
                }

                else if (bracketsCounter == 0)
                {
                    s  = "";
                    s += tempS[i];

                    //if (s == "*" || s == "/" || s == "^")
                    //{
                    //    tree.SetLeft(new BinTreeNode<string>(tempS.Substring(0, i)));
                    //    MakeTree(tree.GetLeft());
                    //    tree.SetRight(new BinTreeNode<string>(tempS.Substring(i + 1)));
                    //    MakeTree(tree.GetRight());

                    //    tree.SetInfo(s);
                    //    i = tempS.Length;
                    //}


                    //else
                    if (tempS.Length > i + 3)
                    {
                        s = tempS.Substring(i, 3);

                        if (s == "sin" || s == "cos" || s == "tan" || s == "abs" || s == "log")
                        {
                            tree.SetLeft(new BinTreeNode <string>(tempS.Substring(i + 3)));
                            MakeTree(tree.GetLeft());

                            tree.SetInfo(s);
                            i = tempS.Length;
                        }
                        else if (s == "max" || s == "min" || s == "pow")
                        {
                            int g = i + 3;
                            for (; g < tempS.Length && tempS[g] != ','; g++)
                            {
                                ;
                            }

                            tree.SetLeft(new BinTreeNode <string>(tempS.Substring(i + 4, g - i - 4)));
                            MakeTree(tree.GetLeft());
                            tree.SetRight(new BinTreeNode <string>(tempS.Substring(g + 1)));
                            MakeTree(tree.GetRight());

                            tree.SetInfo(s);
                            i = tempS.Length;
                        }


                        else if (tempS.Length > i + 4)
                        {
                            s = tempS.Substring(i, 4);

                            if (s == "sqrt")
                            {
                                tree.SetLeft(new BinTreeNode <string>(tempS.Substring(i + 4)));
                                MakeTree(tree.GetLeft());

                                tree.SetInfo(s);
                                i = tempS.Length;
                            }
                        }
                    }
                }
            }
        }