Ejemplo n.º 1
0
Archivo: Node.cs Proyecto: Wowo51/Sym
        public Node Clone()
        {
            Node newNode = null;

            if (this is OperatorNode)
            {
                OperatorNode oldOperatorNode = (OperatorNode)this;
                newNode = new OperatorNode();
                OperatorNode newOperatorNode = (OperatorNode)newNode;
                newOperatorNode.Operator = oldOperatorNode.Operator;
                foreach (Node child in oldOperatorNode.Children)
                {
                    Node newChild = child.Clone();
                    newChild.Parent = newOperatorNode;
                    newOperatorNode.Children.Add(newChild);
                }
            }
            else if (this is NumericNode)
            {
                NumericNode oldNumericNode = (NumericNode)this;
                newNode = new NumericNode();
                NumericNode newNumericNode = (NumericNode)newNode;
                newNumericNode.Number = oldNumericNode.Number;
            }
            else if (this is VariableNode)
            {
                VariableNode oldVariableNode = (VariableNode)this;
                newNode = new VariableNode();
                VariableNode newVariableNode = (VariableNode)newNode;
                newVariableNode.Variable         = oldVariableNode.Variable;
                newVariableNode.VariableNodeType = oldVariableNode.VariableNodeType;
                newVariableNode.Number           = oldVariableNode.Number;
            }
            return(newNode);
        }
Ejemplo n.º 2
0
Archivo: Node.cs Proyecto: Wowo51/Sym
        public static Node RemoveDoubleNegatives(Node inNode)
        {
            Node workNode = inNode;

            if (workNode is OperatorNode)
            {
                OperatorNode operatorNode = (OperatorNode)workNode;
                if (operatorNode.Operator.OperatorString == "-" && operatorNode.Operator.OperatorType == OperatorType.Unary)
                {
                    Node child = operatorNode.Children[0];
                    if (child is OperatorNode)
                    {
                        OperatorNode childOperatorNode = (OperatorNode)child;
                        if (childOperatorNode.Operator.OperatorString == "-" && childOperatorNode.Operator.OperatorType == OperatorType.Unary)
                        {
                            workNode = childOperatorNode.Children[0];
                        }
                    }
                    else if (child is NumericNode)
                    {
                        NumericNode childNumericNode = (NumericNode)child;
                        if (childNumericNode.Number < 0)
                        {
                            childNumericNode.Number *= -1;
                            workNode = childNumericNode;
                        }
                    }
                }
            }
            List <Node> newChildren = new List <Node>();

            foreach (Node child in workNode.Children)
            {
                Node newChild = RemoveDoubleNegatives(child);
                newChild.Parent = workNode;
                newChildren.Add(newChild);
            }
            workNode.Children = newChildren;
            return(workNode);
        }
Ejemplo n.º 3
0
Archivo: Node.cs Proyecto: Wowo51/Sym
        public static Node ParseNewEnclosing(string inExpression, out bool foundOperator, List <Operator> operators)
        {
            foundOperator = false;
            int openingBracketLocation = inExpression.IndexOf('(');
            int closingBracketLocation = inExpression.Length - 1;

            if (inExpression.Substring(closingBracketLocation, 1) != ")")
            {
                return(null);
            }
            if (openingBracketLocation >= 0)
            {
                foundOperator = true;
                string        keyword        = inExpression.Substring(0, openingBracketLocation);
                string        wholeOperands  = inExpression.Substring(openingBracketLocation + 1, inExpression.Length - openingBracketLocation - 2);
                Operator      commaOperator  = new Operator(",", 0, OperatorType.Delimiting, null);
                List <int>    bracketDepths  = Node.BracketDepths(wholeOperands);
                bool          foundOperator2 = false;
                List <string> operands       = Node.ParseDelimiting(wholeOperands, commaOperator, bracketDepths, out foundOperator2);
                OperatorNode  newNode        = new OperatorNode();
                Operator      o = operators.FirstOrDefault(x => x.OperatorString == keyword);
                if (o == null)
                {
                    o = new Operator(keyword, operators.Count, OperatorType.Enclosing, null);
                    operators.Add(o);
                }
                newNode.Operator = o;
                foreach (string operand in operands)
                {
                    Node childNode = Parse(operand, operators);
                    childNode.Parent = newNode;
                    newNode.Children.Add(childNode);
                }
                return(newNode);
            }
            return(null);
        }
Ejemplo n.º 4
0
Archivo: Node.cs Proyecto: Wowo51/Sym
 public static string JoinNest(Node inNode, string useNumbers = "")
 {
     if (inNode is OperatorNode)
     {
         List <string> operandStrings = new List <string>();
         OperatorNode  operatorNode   = (OperatorNode)inNode;
         string        operatorString = operatorNode.Operator.OperatorString;
         foreach (Node child in inNode.Children)
         {
             string operandString = JoinNest(child, useNumbers);
             if (child is OperatorNode)
             {
                 OperatorNode childOperatorNode = (OperatorNode)child;
                 if (childOperatorNode.Operator.OperatorType == OperatorType.Delimiting)
                 {
                     string childOperatorString = childOperatorNode.Operator.OperatorString;
                     if (operatorString == "-")
                     {
                         if (operatorNode.Children[0] != childOperatorNode && operatorNode.Operator.OperatorType == OperatorType.Delimiting)
                         {
                             if (childOperatorString == "+" || childOperatorString == "-")
                             {
                                 operandString = "(" + operandString + ")";
                             }
                         }
                         else if (operatorNode.Operator.OperatorType == OperatorType.Unary)
                         {
                             if (childOperatorString == "+" || childOperatorString == "-" || childOperatorString == "," || childOperatorString == "=")
                             {
                                 operandString = "(" + operandString + ")";
                             }
                         }
                     }
                     else if (operatorString == "/")
                     {
                         if (childOperatorString == "+" || childOperatorString == "-" || childOperatorString == "," || childOperatorString == "=")
                         {
                             operandString = "(" + operandString + ")";
                         }
                         else if (operatorNode.Children[0] != childOperatorNode)
                         {
                             if (childOperatorString == "*" || childOperatorString == "/")
                             {
                                 operandString = "(" + operandString + ")";
                             }
                         }
                     }
                     else if (operatorString == "*")
                     {
                         if (childOperatorString == "+" || childOperatorString == "-" || childOperatorString == "," || childOperatorString == "=")
                         {
                             operandString = "(" + operandString + ")";
                         }
                     }
                     else if (operatorString == "+")
                     {
                         if (childOperatorString == "," || childOperatorString == "=")
                         {
                             operandString = "(" + operandString + ")";
                         }
                     }
                 }
             }
             operandStrings.Add(operandString);
         }
         if (operatorNode.Operator.OperatorType == OperatorType.Delimiting)
         {
             string outString = string.Join(operatorNode.Operator.OperatorString, operandStrings.ToArray());
             return(outString);
         }
         else if (operatorNode.Operator.OperatorType == OperatorType.Unary)
         {
             string outString = operatorNode.Operator.OperatorString + operandStrings[0];
             return(outString);
         }
         else if (operatorNode.Operator.OperatorType == OperatorType.Enclosing)
         {
             string outString = operatorNode.Operator.OperatorString + "(" + string.Join(",", operandStrings.ToArray()) + ")";
             return(outString);
         }
     }
     else if (inNode is VariableNode)
     {
         VariableNode variableNode = (VariableNode)inNode;
         if (useNumbers != "" && variableNode.Variable.Substring(0, useNumbers.Length) == useNumbers)
         {
             return(variableNode.Number.ToString().Trim());
         }
         return(variableNode.Variable);
     }
     else if (inNode is NumericNode)
     {
         NumericNode numericNode = (NumericNode)inNode;
         return(numericNode.Number.ToString().Trim());
     }
     return(null);
 }
Ejemplo n.º 5
0
Archivo: Node.cs Proyecto: Wowo51/Sym
        public static Node Parse(string inExpression, List <Operator> operators)
        {
            //inExpression = RemoveEnclosingBrackets(inExpression);
            double number;

            if (double.TryParse(inExpression, out number))
            {
                NumericNode numericNode = new NumericNode();
                numericNode.Number = number;
                return(numericNode);
            }
            List <int> bracketDepths = BracketDepths(inExpression);
            bool       foundOperator = false;

            foreach (Operator o in operators)
            {
                List <string> operands = null;
                if (o.OperatorType == OperatorType.Delimiting)
                {
                    operands = ParseDelimiting(inExpression, o, bracketDepths, out foundOperator);
                }
                else if (o.OperatorType == OperatorType.Unary)
                {
                    operands = ParseUnary(inExpression, o, out foundOperator);
                }
                else if (o.OperatorType == OperatorType.Enclosing)
                {
                    operands = ParseEnclosing(inExpression, o, out foundOperator);
                }
                if (foundOperator)
                {
                    OperatorNode operatorNode = new OperatorNode();
                    foreach (string operand in operands)
                    {
                        Node childNode = Parse(operand, operators);
                        childNode.Parent = operatorNode;
                        operatorNode.Children.Add(childNode);
                    }
                    operatorNode.Operator = o;
                    return(operatorNode);
                }
            }
            Node newNode = ParseNewEnclosing(inExpression, out foundOperator, operators);

            if (foundOperator)
            {
                return(newNode);
            }
            VariableNode variableNode = new VariableNode();

            variableNode.Variable = inExpression;
            if (inExpression.First() == 'C')
            {
                variableNode.VariableNodeType = VariableNodeType.RequireNumber;
            }
            else if (inExpression.First() == 'V')
            {
                variableNode.VariableNodeType = VariableNodeType.RequireVariable;
            }
            else
            {
                variableNode.VariableNodeType = VariableNodeType.NoRequirement;
            }
            return(variableNode);
        }