예제 #1
0
        private void PushUp(ExpressionOperateNode preNode, ExpressionOperateNode curNode)
        {
            var tmpNode = GetRoot(preNode);

            curNode.Node0  = tmpNode;
            tmpNode.Parent = curNode;
        }
예제 #2
0
 private ExpressionOperateNode GetRoot(ExpressionOperateNode node)
 {
     while (node != null && node.HasParent())
     {
         node = node.Parent;
     }
     return(node);
 }
예제 #3
0
 public ExpressionTree(string expr)
 {
     try
     {
         exp   = expr;
         _root = GenerateExpressionTree(expr);
         Calculate();
     }
     catch (Exception e)
     {
         throw new BasicCalculatorException("输入的表达式有误!");
     }
 }
예제 #4
0
 private void PushDown(ExpressionOperateNode preNode, ExpressionOperateNode curNode)
 {
     curNode.Node0  = preNode.Node1;
     preNode.Node1  = curNode;
     curNode.Parent = preNode;
 }
예제 #5
0
        private ExpressionOperateNode GenerateExpressionTree(string expr)
        {
            StringBuilder         tmp             = new StringBuilder();
            ExpressionNode        curLeafNode     = null;
            ExpressionOperateNode curOperatorNode = null;
            bool ForceUp = false;

            expr = expr + ".";
            for (int i = 0; i < expr.Length; i++)
            {
                if (IsNum(expr[i]))
                {
                    tmp.Append(expr[i]);
                }
                else
                {
                    if (tmp.Length != 0)
                    {
                        float num = float.Parse(tmp.ToString());
                        tmp.Clear();
                        curLeafNode = new ExpressionNumNode(num);
                        _numNodes.Add(curLeafNode);
                        if (curOperatorNode != null)
                        {
                            curOperatorNode.Node1 = curLeafNode;
                        }
                    }

                    if (ForceUp && curOperatorNode != null)
                    {
                        curOperatorNode.Node1 = curLeafNode;
                    }

                    if (IsOperator(expr[i]))
                    {
                        var tmpOperateNode = new ExpressionOperateNode(expr[i], curLeafNode, null);
                        if (curLeafNode != null && !curLeafNode.IsNumber)
                        {
                            ((ExpressionOperateNode)curLeafNode).Parent = tmpOperateNode;
                        }
                        _operatorNodes.Add(tmpOperateNode);
                        if (IsHighOperator(expr[i]) && curOperatorNode != null)
                        {
                            PushDown(curOperatorNode, tmpOperateNode);
                        }
                        else if ((IsLowOperator(expr[i]) && curOperatorNode != null) ||
                                 (ForceUp && curOperatorNode != null))
                        {
                            PushUp(curOperatorNode, tmpOperateNode);
                            if (ForceUp)
                            {
                                ForceUp = false;
                            }
                        }

                        curOperatorNode = tmpOperateNode;
                    }
                    else
                    {
                        if (!IsBracket(expr[i]) || expr[i] != '(')
                        {
                            continue;
                        }
                        curLeafNode =
                            GenerateExpressionTree(expr.Substring(i + 1, SearchMatchBracket(expr, i) - i - 1));
                        i = SearchMatchBracket(expr, i);
                        if (curOperatorNode != null)
                        {
                            curOperatorNode.Node1 = curLeafNode;
                            if (curLeafNode != null && !curLeafNode.IsNumber)
                            {
                                ((ExpressionOperateNode)curLeafNode).Parent = curOperatorNode;
                            }
                        }

                        ForceUp = true;
                    }
                }
            }

            return(GetRoot(curOperatorNode));
        }