逆波兰算法解析字符串
参照:https://www.cnblogs.com/lxfqlcz/archive/2011/08/02/2124854.html
针对以上文章做了下稍微的改进,以便支持三角函数计算等逻辑,解决与运算符相冲突的问题
改进后代码如下:
List<string> m_funOperators = new List<string>() { "tan", "sin", "cos", "cot", "sec", "csc", "log", "loge", "ln", "abs" };
//exp=A*1.0#+SIN(B*1.2)-COS(C)+3/2-TAN(45)/(1+F)+LOG(G)-1.5*D+ln8
public bool Parse(string exp)
{
exp = exp.ToUpper();
string pattern = @"\b(" + string.Join<string>("|", m_funOperators).ToUpper() + ")" + @"\b";
exp = Regex.Replace(exp, pattern, new MatchEvaluator((m) =>
{
return m.Value.ToLower();
}));
m_tokens.Clear();
.......
}
参考: