首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#截取多个括号里面的数据,该如何处理

2013-12-13 
C#截取多个括号里面的数据请帮我看看这么一个问题我实在不会写(((空调1 30)且(空调2 30))或((房间温度

C#截取多个括号里面的数据
请帮我看看这么一个问题我实在不会写
(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)
我的需求是我要截取依次从扩号:首先截取空调1 > 30再截取空调2 < 30,再截取房间温度 < 32,在截取门 = 1,最后截取((空调1 > 30)且(空调2 < 30))和((房间温度 < 32)且(门 = 1)),最后截取(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))和(门 = 0)当然条件也要且或非也要截取到,条件可能不是这个顺序,可能最后一个且,这个代码怎么写,有高手帮我写个,我写不出来。高分,有好的代码调试通过立马结贴,也可以加我QQ519552243,实在搞不出来了才求助CSDN的高手了。注意这个条件且或非也要截取到我要截取到一个数据我要插入到数据库的。

[解决办法]


class Program
    {
        static void Main(string[] args)
        {
            string sInput = "(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)";
            string[] arr = sInput.Select(x => x.ToString()).ToArray();

            //记录括号的位置
            List<Brackets> listBrackets = new List<Brackets>();
            //左括号个数
            int iLeftNum = 0;
            //右括号个数
            int iRightNum = 0;
            //括号最大层数
            int iMaxDeep = 0;
            bool bIsErrFormat = false;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == "(")
                {
                    iLeftNum++;
                    listBrackets.Add(new Brackets(true, i));
                }
                else if (arr[i] == ")")
                {
                    if (iLeftNum - iRightNum > iMaxDeep)
                    {
                        iMaxDeep = iLeftNum - iRightNum;
                    }
                    iRightNum++;
                    listBrackets.Add(new Brackets(false, i));
                    
                    if (iRightNum > iLeftNum)
                    {
                        bIsErrFormat = true;
                        break;
                    }


                    

                }
            }
            //简单验证
            if (bIsErrFormat 
[解决办法]
 (iLeftNum != iRightNum))
            {
                Console.WriteLine("括号不匹配");
                return;
            }

            //分析括号
            List<Condition> lstCondition = new List<Condition>();

            string sCondition1 = "";
            string sCondition2 = "";
            string sRelation = "";
            int iLastRight = 0;

            //逐层取出条件
            for (int iDeep = iMaxDeep; iDeep > 0; iDeep--)
            {
                sCondition1 = "";
                sCondition2 = "";
                sRelation = "";
                iLastRight = 0;

                //左括号个数
                iLeftNum = 0;
                //右括号个数
                iRightNum = 0;

                for (int i = 0; i < listBrackets.Count; i++)
                {
                    Brackets obj = listBrackets[i];
                    if (obj.bIsLeft)
                    {
                        iLeftNum++;
                    }
                    else
                    {
                        if (iLeftNum - iRightNum == iDeep)
                        {
                            int iLeft = listBrackets[i - 1].iIndexOfStr;
                            int iRight = obj.iIndexOfStr;

                            string sCondition = sInput.Substring(iLeft + 1, iRight - iLeft - 1);
                            if (string.IsNullOrEmpty(sCondition1))
                            {
                                sCondition1 = sCondition;
                                iLastRight = iRight;


                            }
                            else
                            {
                                sCondition2 = sCondition;
                                sRelation = sInput.Substring(iLastRight + 1, iLeft - iLastRight - 1);
                                lstCondition.Add(new Condition(sCondition1, sCondition2, sRelation));
                                //移除这对条件
                                listBrackets.RemoveRange(i - 3, 4);
                                i -= 4;

                                sCondition1 = "";
                            }
                        }

                        iRightNum++;
                    }
                }
            }

            //显示结果
            foreach (Condition item in lstCondition)
            {
                Console.WriteLine("[{0}]{1}[{2}]", item.sCondition1, item.sRelation, item.sCondition2);
            }
            Console.ReadLine();
        }
    }

    /// <summary>
    /// 括号
    /// </summary>
    public class Brackets
    {
        /// <summary>
        /// 是否是左括号
        /// </summary>
        public bool bIsLeft;
        /// <summary>
        /// 在字符串中的位置
        /// </summary>
        public int iIndexOfStr;

        public Brackets(bool bIsLeft, int iIndexOfStr)
        {
            this.bIsLeft = bIsLeft;
            this.iIndexOfStr = iIndexOfStr;
        }
    }

    /// <summary>
    /// 条件
    /// </summary>
    public class Condition
    {
        public string sCondition1;
        public string sCondition2;
        public string sRelation;

        public Condition(string sCondition1, string sCondition2, string sRelation)
        {
            this.sCondition1 = sCondition1;
            this.sCondition2 = sCondition2;


            this.sRelation = sRelation;
        }
    }


[解决办法]
private void DoDebug0001()
        {
            List<string> oGet = new List<string>();
            string sPut = "(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)";
            Stack<StringBuilder> stack = new Stack<StringBuilder>();
            stack.Push(new StringBuilder(""));
            foreach (char cPut in sPut)
            {
                switch (cPut)
                {
                    case '(':
                        stack.Peek().Append(cPut);
                        stack.Push(new StringBuilder(""));
                        break;
                    case ')':
                        StringBuilder oPop = stack.Pop();
                        oGet.Add(oPop.ToString());
                        stack.Peek().Append(oPop.ToString());
                        stack.Peek().Append(cPut);
                        break;
                    default:
                        stack.Peek().Append(cPut);
                        break;
                }
            }
            while (stack.Count > 0)
            {
                StringBuilder oPop = stack.Pop();
                oGet.Add(oPop.ToString());
            }
            foreach (string sGet in oGet)
            {
                Console.WriteLine(sGet);
            }
        }



输出:


空调1 > 30
空调2 < 30
(空调1 > 30)且(空调2 < 30)
房间温度 < 32
门 = 1
(房间温度 < 32)且(门 = 1)
((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1))
门 = 0
(((空调1 > 30)且(空调2 < 30))或((房间温度 < 32)且(门 = 1)))非(门 = 0)

热点排行