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

求解计算公式替换
计算公式:(mc380a*100.2+mc380b*2)/3600/mc380b12+mc380ac*4.9
运算符:+ - * /
替换字符串:
mc380a替换为a1.mc380a
mc380ac替换为a2.mc380ac
mc380b替换为b.mc380b
mc380b12替换为c.mc380b12
替换结果为:(a1.mc380a*100.2+b.mc380b*2)/3600/c.mc380b12+a2.mc380ac*4.9

解出来了,有赏。。
计算公式?替换?拆分

------解决方案--------------------
 string str = "(mc380a*100.2+mc380b*2)/3600/mc380b12+mc380ac*4.9";
            Dictionary<string, string> dic = new Dictionary<string, string>
            {
                {"mc380b12","c.mc380b12"},{"mc380b","b.mc380b"},{"mc380ac","a2.mc380ac"},{"mc380a","a1.mc380a"}
            };
            var chars = new char[] { '*', '/', '+', '-', '(', ')' };
            List<string> list = new List<string>();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.Length; i++)
            {
                if (chars.Contains(str[i]))
                {
                    if (sb.Length > 0)
                    {
                        list.Add(sb.ToString());
                        sb = new StringBuilder ();
                    }
                    list.Add(str[i].ToString());
                }
                else
                {
                    sb.Append(str[i].ToString());
                }
            }
            if (sb.Length > 0)
            {
           &nb