Beispiel #1
0
 public override Function SimplifyFunction()
 {
     Left  = Left.SimplifyFunction();
     Right = Right.SimplifyFunction();
     if (Left.IsConstant() && Right.IsConstant())
     {
         return(new RealNumber(CalculateValue(0)));
     }
     if (Right.ToString() == "0")
     {
         return(Left);
     }
     if (Left is Substract && Right.IsConstant())
     {
         if (Left.Left.IsConstant())
         {
             Function function = new Substract();
             function.Left  = new RealNumber(Left.Left.CalculateValue(0) - Right.CalculateValue(0));
             function.Right = Left.Right;
             return(function.SimplifyFunction());
         }
         if (Left.Right.IsConstant())
         {
             Function function = new Substract();
             function.Left  = Left.Left;
             function.Right = new RealNumber(Left.Right.CalculateValue(0) + Right.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     if (Right is Substract && Left.IsConstant())
     {
         if (Right.Left.IsConstant())
         {
             Function function = new Plus();
             function.Left  = new RealNumber(Left.CalculateValue(0) - Right.Left.CalculateValue(0));
             function.Right = Right.Right;
             return(function.SimplifyFunction());
         }
         if (Right.Right.IsConstant())
         {
             Function function = new Substract();
             function.Left  = new RealNumber(Left.CalculateValue(0) + Right.Right.CalculateValue(0));
             function.Right = Right.Left;
             return(function.SimplifyFunction());
         }
     }
     if (Left is Plus && Right.IsConstant())
     {
         if (Left.Left.IsConstant())
         {
             Function function = new Plus();
             function.Left  = new RealNumber(Left.Left.CalculateValue(0) - Right.CalculateValue(0));
             function.Right = Left.Right;
             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 Substract();
             function.Left  = new RealNumber(Left.CalculateValue(0) - Right.Left.CalculateValue(0));
             function.Right = Right.Right;
             return(function.SimplifyFunction());
         }
         if (Right.Right.IsConstant())
         {
             Function function = new Substract();
             function.Left  = new RealNumber(Left.CalculateValue(0) - Right.Right.CalculateValue(0));
             function.Right = Right.Left;
             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);
 }
Beispiel #2
0
        private Function CreateTree()
        {
            Function currentNode = null;
            string   token       = listNotations.First();

            switch (token)
            {
            case "c":
                currentNode = new Cosine();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "/":
                currentNode = new Divide();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            case "e":
                currentNode = new Exp();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "!":
                currentNode = new Factorial();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "*":
                currentNode = new Multiply();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            case "l":
                currentNode = new NaturalLogarithm();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "n":
                listNotations.Remove(token);
                string valueN = listNotations.First();
                currentNode = new NaturalNumber(Convert.ToInt32(valueN));
                listNotations.Remove(valueN);
                break;

            case "x":
                currentNode = new ParameterX();
                listNotations.Remove(token);
                break;

            case "p":
                currentNode = new Pi();
                listNotations.Remove(token);
                break;

            case "+":
                currentNode = new Plus();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            case "^":
                currentNode = new Power();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            case "r":
                listNotations.Remove(token);
                string valueR = listNotations.First();
                currentNode = new RealNumber(Convert.ToDouble(valueR));
                listNotations.Remove(listNotations.First());
                break;

            case "s":
                currentNode = new Sine();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "-":
                currentNode = new Substract();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            default:
                currentNode = new NaturalNumber(Convert.ToInt32(token));
                listNotations.Remove(token);
                break;
            }
            return(currentNode);
        }