예제 #1
0
        /// <summary>
        /// 二元计算
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="op"></param>
        /// <returns></returns>
        public object DyadicOperation(ExpNode left, ExpNode right, string symbol)
        {
            IOperator iOp = OperatorFactory.GetOperator(symbol);

            iOp.LeftOperand  = OperandOperation(left).ToString();
            iOp.RightOperand = OperandOperation(right).ToString();
            return(iOp.Eval());
            //            return iOp.Eval(;
        }
예제 #2
0
        private int ReadOperator(string readStr, List <ExpNode> infixList, string operand)
        {
            ExpNode node = OperatorFactory.CheckOperatorSing(readStr);

            if (node != null)
            {
                operand = operand.Trim();
                if (!string.IsNullOrEmpty(operand))
                {
                    infixList.Add(new ExpNode(operand, ExpNodeType.Operand, 0));
                }

                infixList.Add(node);
                return(node.Text.Length);
            }
            return(0);
        }
예제 #3
0
        /// <summary>
        /// 计算后缀表达式的值
        /// </summary>
        /// <param name="postfixList"></param>
        /// <returns></returns>
        public object Eval(List <ExpNode> postfixList)
        {
            ExpNode         num1, num2;
            Stack <ExpNode> num    = new Stack <ExpNode>();
            object          result = null;

            foreach (ExpNode node in postfixList)
            {
                if (node.NodeType == ExpNodeType.Operator)
                {
                    num2 = num.Pop();
                    num1 = num.Pop();
                    try
                    {
                        result = DyadicOperation(num1, num2, node.Text);
                        ExpNode resultNode = new ExpNode(result.ToString(), ExpNodeType.Operand, 0);
                        num.Push(resultNode);
                    }
                    catch (Exception e)
                    {
                        throw new EExpressException("在计算" + num1.Text + node.Text + num2.Text + "时发生错误," + e.Message);
                    }
                }
                else
                {
                    num.Push(node);
                }
            }

            if (result == null)
            {
                ExpNode node = num.Pop();

                if (node == null)
                {
                    return(null);
                }

                result = OperandOperation(node);
            }
            return(result);
        }
예제 #4
0
        /// <summary>
        /// 将操作数节点处理为操作数
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private object OperandOperation(ExpNode node)
        {
            object result = null;

            switch (node.NodeType)
            {
            case ExpNodeType.Operand:
                result = node.Text.ToString();
                break;

            case ExpNodeType.Method:
                result = FunctionOperation(node.Text);
                break;

            default:
                result = 0;
                break;
            }
            return(result);
        }
예제 #5
0
        private bool IsPop(ExpNode op, Stack <ExpNode> operators)
        {
            if (operators.Count == 0)
            {
                return(false);
            }

            ExpNode stackTop = operators.Peek();

            if (stackTop.NodeType == ExpNodeType.LeftBracket)
            {
                return(false);
            }

            if (stackTop.Level >= op.Level)
            {
                return(true);
            }

            return(false);
        }
예제 #6
0
        /// <summary>
        /// 检查表达式首个字符开始的位置是否是运算符,是则返回这个运算符,否则返回空
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public static ExpNode CheckOperatorSing(string exp)
        {
            ExpNode expNode = null;

            foreach (OperatorSchema opReg in _operatorRegList.Operators)
            {
                string sign = opReg.Id;
                if (exp.StartsWith(sign))
                {
                    if (expNode == null)
                    {
                        expNode = new ExpNode(sign, ExpNodeType.Operator, opReg.Level);
                    }
                    else if (sign.Length > expNode.Text.Length)
                    {
                        expNode = new ExpNode(sign, ExpNodeType.Operator, opReg.Level);
                    }
                }
            }
            return(expNode);
        }