求解计算公式替换
计算公式:(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)
{
list.Add(sb.ToString());
sb = new StringBuilder() ;
}
for (int j = 0; j < list.Count; j++)
{
if (dic.ContainsKey(list[j]))
{
list[j] = dic[list[j]];
}
sb.Append(list[j]);
}
Console.WriteLine(sb.ToString());
Console.ReadKey();