示例#1
0
文件: Node.cs 项目: 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);
 }
示例#2
0
文件: Node.cs 项目: 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);
        }