Exemple #1
0
        /// <summary>
        /// 处理表达式
        /// </summary>
        /// <param name="sub"> 是否是子表达式,即,在括号内 </param>
        /// <param name="expression"> 表达式 </param>
        /// <returns> 计算得到的逻辑值 </returns>
        private bool Process(bool sub, string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                throw new ArgumentNullException("expression");
            }
            var c      = expression.ToCharArray();
            var length = c.Length;
            var flag   = false;
            var start  = false;              //是否有过一次 与或计算
            var mark   = false;              //读取括号中的内容
            var logic  = true;               //true是& false是|
            var queue  = new Queue <char>(); //存入数字
            var temp   = false;              //最小表达式的值

            for (var i = 0; i < length; i++)
            {
                if (c[i].Equals('('))
                {
                    mark = true;
                    temp = true;
                }
                else if (c[i].Equals(')'))
                {
                    var b = Process(true, new string(queue.ToArray()));
                    flag = GetLogicValue(start, logic, flag, b);

                    temp = GetLogicValue(false, logic, temp, b);

                    iLogical.ConditionBlock(new string(queue.ToArray()), temp, true);

                    start = true;

                    mark = false;
                    queue.Clear();
                }
                else if (c[i].Equals(or))//逻辑或
                {
                    if (mark)
                    {
                        queue.Enqueue(c[i]);
                        continue;
                    }
                    if (queue.Count != 0)
                    {
                        bool b = iLogical.ProcessItem(new string(queue.ToArray()), sub);
                        flag = GetLogicValue(start, logic, flag, b);
                        temp = GetLogicValue(false, logic, temp, b);

                        if (!sub)
                        {
                            iLogical.ConditionBlock(new string(queue.ToArray()), temp, false);
                        }
                    }
                    logic = false;
                    start = true;

                    queue.Clear();
                }
                else if (c[i].Equals(and))//逻辑与
                {
                    if (mark)
                    {
                        queue.Enqueue(c[i]);
                        continue;
                    }
                    if (queue.Count != 0)
                    {
                        bool b = iLogical.ProcessItem(new string(queue.ToArray()), sub);
                        flag = GetLogicValue(start, logic, flag, b);

                        temp = GetLogicValue(false, logic, temp, b);
                        if (!sub)
                        {
                            iLogical.ConditionBlock(new string(queue.ToArray()), temp, false);
                        }
                    }
                    logic = true;
                    start = true;
                    queue.Clear();
                }
                else //if( char.IsDigit(c[i]) )
                {
                    queue.Enqueue(c[i]);
                }
            }
            if (queue.Count != 0)
            {
                bool b = iLogical.ProcessItem(new string(queue.ToArray()), sub);
                flag = GetLogicValue(start, logic, flag, b);

                temp = GetLogicValue(false, logic, temp, b);

                if (!sub)
                {
                    iLogical.ConditionBlock(new string(queue.ToArray()), temp, false);
                }
            }

            return(flag);
        }