日期:2014-05-17  浏览次数:20992 次

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));
     &n