Example #1
0
        /// <summary>
        /// 字符串表达式计算
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public double ExpressonCalculate(string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                throw new Exception("表达式不能为空或null");
            }
            expression = expression.Replace(" ", string.Empty);

            if (expression[0] == Operators.Subtraction)
            {
                expression = "0" + expression;
            }

            if (!ExpressionValidate(expression))
            {
                throw new Exception(string.Format("表达式{0}不是有效的表达式,表达式中不能含有非数字或是四则运算符及小括号运算符", expression));
            }
            if (BaseCalate.IsOperator(expression[expression.Length - 1]))
            {
                throw new Exception(string.Format("表达式{0}不是有效的表达式,表达式结尾不能为+-*/四种运算符", expression));
            }
            //return this.Calculator(this.BaseExpressionParse(expression));//测试该函数用
            //return this.Calculator(this.BlendExpressionParse(expression));//测试该函数用

            return(this.Calculator(this.ExpressionParse(expression)));
        }
Example #2
0
        /// <summary>
        /// 树值计算
        /// </summary>
        /// <param name="root">要计算的节点</param>
        /// <returns>节点计算结果值</returns>
        private double Calculator(ExpressionNode root)
        {
            double result     = default(double);
            double leftValue  = default(double);
            double rightValue = default(double);

            if (root == null)
            {
                return(result);
            }

            if (root.Left != null)
            {
                if (!root.Left.IsLeaf)
                {
                    leftValue = Calculator(root.Left);
                }
                else
                {
                    leftValue = root.Left.Value;
                }
            }
            if (root.Right != null)
            {
                if (!root.Right.IsLeaf)
                {
                    rightValue = Calculator(root.Right);
                }
                else
                {
                    rightValue = root.Right.Value;
                }
            }

            if (!BaseCalate.IsOperator(root.Operator))
            {
                result = root.Value;
            }
            else
            {
                result = BaseCalate.BaseCal(leftValue, rightValue, root.Operator.ToString());
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// 仅加法和减法或仅乘法和除法(不能有加减和乘除混合)表达式解析生成树
        /// </summary>
        /// <param name="expression">要计算的表达式字符串</param>
        /// <returns>表达式节点树</returns>
        private ExpressionNode BaseExpressionParse(string expression)
        {
            ExpressionNode root             = new ExpressionNode();
            int            valueStaratIndex = 0;
            string         value            = string.Empty;
            char           chDefaultValue   = default(char);
            char           currentOperator  = chDefaultValue;

            for (int i = 0; i < expression.Length; i++)
            {
                if (BaseCalate.IsOperator(expression[i]))
                {
                    value            = expression.Substring(valueStaratIndex, i - valueStaratIndex);
                    valueStaratIndex = i + 1;

                    currentOperator = expression[i];
                    continue;
                }
                else
                {
                    if (!string.IsNullOrEmpty(value) && currentOperator != chDefaultValue)
                    {
                        if (root.Left == null)
                        {
                            var lefttLeafNode = new ExpressionNode();
                            lefttLeafNode.Value  = double.Parse(value.Trim());
                            lefttLeafNode.Parent = root;

                            root.Operator = currentOperator;
                            root.Left     = lefttLeafNode;
                        }
                        else if (root.Left != null && root.Right == null)
                        {
                            var rightLeafNode = new ExpressionNode();
                            rightLeafNode.Value  = double.Parse(value.Trim());
                            rightLeafNode.Parent = root;

                            root.Right = rightLeafNode;

                            var parentNode = new ExpressionNode();
                            parentNode.Operator = currentOperator;
                            parentNode.Left     = root;
                            root.Parent         = parentNode;
                            root = parentNode;
                        }

                        value = string.Empty;
                    }
                }
            }

            value = expression.Substring(valueStaratIndex, expression.Length - valueStaratIndex);
            if (root.Left != null)
            {
                var endLeaf = new ExpressionNode();
                endLeaf.Value  = double.Parse(value.Trim());
                endLeaf.Parent = root;
                root.Right     = endLeaf;
            }
            else
            {
                root.Value = double.Parse(value.Trim());
            }
            return(root);
        }
Example #4
0
        /// <summary>
        /// 无括号的加减乘除混合表达式运算
        /// </summary>
        /// <param name="expression">要计算的表达式字符串</param>
        /// <returns>表达式节点树</returns>
        private ExpressionNode BlendExpressionParse(string expression)
        {
            int    startSubExpressionIndex = 0;
            int    endSubExpressionIndex   = -1;
            string subExpression           = string.Empty;
            double subExpressionValue      = 0.0d;

            if (expression[0] == Operators.Subtraction)
            {
                expression = "0" + expression;
            }

            while (true)
            {
                for (int i = 0; i < expression.Length; i++)
                {
                    if (expression[i] == Operators.Multiply || expression[i] == Operators.Division)
                    {
                        if (i + 1 == expression.Length)
                        {
                            endSubExpressionIndex = i + 1;
                        }
                        else
                        {
                            for (int j = i + 1; j < expression.Length; j++)
                            {
                                if (BaseCalate.IsOperator(expression[j]))
                                {
                                    endSubExpressionIndex = j;
                                    break;;
                                }
                            }
                            if (endSubExpressionIndex == -1)
                            {
                                endSubExpressionIndex = expression.Length;
                            }
                        }

                        subExpression      = expression.Substring(startSubExpressionIndex, endSubExpressionIndex - startSubExpressionIndex);
                        subExpressionValue = BaseCalate.BaseCal(subExpression);
                        expression         = expression.Remove(startSubExpressionIndex, endSubExpressionIndex - startSubExpressionIndex);
                        expression         = expression.Insert(startSubExpressionIndex, subExpressionValue.ToString());

                        endSubExpressionIndex = -1;
                        break;
                    }
                    if (expression[i] == Operators.Add || expression[i] == Operators.Subtraction)
                    {
                        startSubExpressionIndex = i + 1;
                    }
                }

                if (!expression.Contains(Operators.Multiply) && !expression.Contains(Operators.Division))
                {
                    break;
                }
            }

            ExpressionNode root = BaseExpressionParse(expression);

            return(root);
        }