Ejemplo n.º 1
0
Archivo: Plus.cs Proyecto: Hainv201/CPP
 public override Function SimplifyFunction()
 {
     Left  = Left.SimplifyFunction();
     Right = Right.SimplifyFunction();
     if (Left.IsConstant() && Right.IsConstant())
     {
         return(new RealNumber(CalculateValue(0)));
     }
     if (Left.ToString() == "0")
     {
         return(Right);
     }
     if (Right.ToString() == "0")
     {
         return(Left);
     }
     if (Left is Plus && Right.IsConstant())
     {
         if (Left.Left.IsConstant())
         {
             Function function = new Plus();
             function.Left  = Left.Right;
             function.Right = new RealNumber(Right.CalculateValue(0) + Left.Left.CalculateValue(0));
             return(function.SimplifyFunction());
         }
         if (Left.Right.IsConstant())
         {
             Function function = new Plus();
             function.Left  = Left.Left;
             function.Right = new RealNumber(Left.Right.CalculateValue(0) + Right.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     if (Right is Plus && Left.IsConstant())
     {
         if (Right.Left.IsConstant())
         {
             Function function = new Plus();
             function.Left  = Right.Right;
             function.Right = new RealNumber(Right.Left.CalculateValue(0) + Left.CalculateValue(0));
             return(function.SimplifyFunction());
         }
         if (Right.Right.IsConstant())
         {
             Function function = new Plus();
             function.Left  = Right.Left;
             function.Right = new RealNumber(Right.Right.CalculateValue(0) + Left.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     if (Left is Substract && Right.IsConstant())
     {
         if (Left.Left.IsConstant())
         {
             Function function = new Substract();
             function.Right = Left.Right;
             function.Left  = new RealNumber(Left.Left.CalculateValue(0) + Right.CalculateValue(0));
             return(function.SimplifyFunction());
         }
         if (Left.Right.IsConstant())
         {
             Function function = new Plus();
             function.Left  = Left.Left;
             function.Right = new RealNumber(Right.CalculateValue(0) - Left.Right.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     if (Right is Substract && Left.IsConstant())
     {
         if (Right.Left.IsConstant())
         {
             Function function = new Substract();
             function.Left  = new RealNumber(Right.Left.CalculateValue(0) + Left.CalculateValue(0));
             function.Right = Right.Right;
             return(function.SimplifyFunction());
         }
         if (Right.Right.IsConstant())
         {
             Function function = new Plus();
             function.Left  = Right.Left;
             function.Right = new RealNumber(Left.CalculateValue(0) - Right.Right.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     if (Left is NaturalLogarithm && Right is NaturalLogarithm)
     {
         if (Left.Left.IsConstant() && Right.Left.IsConstant())
         {
             Function function = new NaturalLogarithm();
             function.Left = new RealNumber(Left.Left.CalculateValue(0) * Right.Left.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     return(this);
 }
Ejemplo n.º 2
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);
        }