示例#1
0
 public void SetRight(BinTreeNode <T> tree)
 {
     this.right = tree;
 }
示例#2
0
 public void SetLeft(BinTreeNode <T> tree)
 {
     this.left = tree;
 }
示例#3
0
 public BinTreeNode(BinTreeNode <T> left, T x, BinTreeNode <T> right)
 {
     this.left  = left;
     this.info  = x;
     this.right = right;
 }
示例#4
0
 public BinTreeNode(T x)
 {
     this.left  = null;
     this.info  = x;
     this.right = null;
 }
示例#5
0
        static BinTreeNode <string> BuildTreeOfParser(String str)
        {
            BinTreeNode <string> tr = null;
            int Brackets            = 0;

            for (int i = str.Length - 1; i >= 0; i--)
            {
                if (str[i] == ')')
                {
                    Brackets++;
                }
                if (str[i] == '(')
                {
                    Brackets--;
                }
                if (str[i] == '+' && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str[i].ToString());
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 1, str.Length - i - 1)));
                    tr.SetLeft(BuildTreeOfParser(str.Substring(0, i)));
                }
                else
                if (str[i] == '-' && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str[i].ToString());
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 1, str.Length - i - 1)));
                    if (i != 0)
                    {
                        tr.SetLeft(BuildTreeOfParser(str.Substring(0, i)));
                    }
                    else
                    {
                        tr.SetLeft(BuildTreeOfParser("0"));
                    }
                }
            }
            for (int i = str.Length - 1; i >= 0; i--)
            {
                if (str[i] == ')')
                {
                    Brackets++;
                }
                if (str[i] == '(')
                {
                    Brackets--;
                }
                if (str[i] == '*' && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str[i].ToString());
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 1, str.Length - i - 1)));
                    tr.SetLeft(BuildTreeOfParser(str.Substring(0, i)));
                }
                else
                if (str[i] == '/' && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str[i].ToString());
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 1, str.Length - i - 1)));
                    tr.SetLeft(BuildTreeOfParser(str.Substring(0, i)));
                }
            }
            for (int i = str.Length - 1; i >= 0; i--)
            {
                if (str[i] == ')')
                {
                    Brackets++;
                }
                if (str[i] == '(')
                {
                    Brackets--;
                }
                if (str[i] == '^' && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str[i].ToString());
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 1, str.Length - i - 1)));
                    tr.SetLeft(BuildTreeOfParser(str.Substring(0, i)));
                }
            }
            for (int i = str.Length - 1; i >= 0; i--)
            {
                if (str[i] == ')')
                {
                    Brackets++;
                }
                if (str[i] == '(')
                {
                    Brackets--;
                }
                if (i < str.Length - 3 && str.Substring(i, 3) == "cos" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 3 && str.Substring(i, 3) == "sin" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 3 && str.Substring(i, 3) == "tan" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 3 && str.Substring(i, 3) == "sec" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 3 && str.Substring(i, 3) == "csc" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 3 && str.Substring(i, 3) == "cot" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 6 && str.Substring(i, 3) == "arcsin" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 6 && str.Substring(i, 3) == "arccos" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 3 && str.Substring(i, 3) == "abs" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                }
                else if (i < str.Length - 3 && str.Substring(i, 3) == "log" && tr == null && Brackets == 0)
                {
                    tr = new BinTreeNode <string>(str.Substring(i, 3));
                    tr.SetRight(BuildTreeOfParser(str.Substring(i + 3)));
                    tr.SetLeft(BuildTreeOfParser(str.Substring(0, i)));
                }
            }

            if (tr == null && str[0] != '(')
            {
                tr = new BinTreeNode <string>(str);
            }
            if (str[0] == '(' && tr == null)
            {
                return(BuildTreeOfParser(str.Substring(1, str.Length - 2)));
            }

            return(tr);
        }
示例#6
0
 static double CalculateTreeOfParser(BinTreeNode <string> t, double x, double y)
 {
     if (t != null)
     {
         if (t.GetInfo() == "+")
         {
             return(CalculateTreeOfParser(t.GetLeft(), x, y) + CalculateTreeOfParser(t.GetRight(), x, y));
         }
         else if (t.GetInfo() == "-")
         {
             return(CalculateTreeOfParser(t.GetLeft(), x, y) - CalculateTreeOfParser(t.GetRight(), x, y));
         }
         else if (t.GetInfo() == "*")
         {
             return(CalculateTreeOfParser(t.GetLeft(), x, y) * CalculateTreeOfParser(t.GetRight(), x, y));
         }
         else if (t.GetInfo() == "/")
         {
             return(CalculateTreeOfParser(t.GetLeft(), x, y) / CalculateTreeOfParser(t.GetRight(), x, y));
         }
         else if (t.GetInfo() == "^")
         {
             return(Math.Pow(CalculateTreeOfParser(t.GetLeft(), x, y), CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "cos")
         {
             return(Math.Cos(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "sin")
         {
             return(Math.Sin(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "tan")
         {
             return(Math.Tan(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "sec")
         {
             return(1 / Math.Cos(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "csc")
         {
             return(1 / Math.Sin(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "cot")
         {
             return(1 / Math.Tan(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "arcsin")
         {
             return(Math.Asin(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "arccos")
         {
             return(Math.Acos(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "arctan")
         {
             return(Math.Atan(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "log")
         {
             return(Math.Log(CalculateTreeOfParser(t.GetRight(), x, y), CalculateTreeOfParser(t.GetLeft(), x, y)));
         }
         else if (t.GetInfo() == "abs")
         {
             return(Math.Abs(CalculateTreeOfParser(t.GetRight(), x, y)));
         }
         else if (t.GetInfo() == "PI")
         {
             return(Math.PI);
         }
         else if (t.GetInfo() == "e")
         {
             return(Math.E);
         }
         else
         if (t.GetInfo() == "x")
         {
             return(x);
         }
         else
         if (t.GetInfo() == "y")
         {
             return(y);
         }
     }
     return(double.Parse(t.GetInfo()));
 }