c# Reverse Polish algorithm to parse strings

Reverse Polish algorithm to parse strings

Reference: https://www.cnblogs.com/lxfqlcz/archive/2011/08/02/2124854.html

A slight improvement has been made to the above article to support logic such as trigonometric function calculations and solve problems that conflict with operators

The improved code is as follows:

 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();
   .......
}

reference:

https://www.codeproject.com/Articles/34626/C-RPN-Expression

https://mp.weixin.qq.com/s/Zdof-uGtQ0CylF544ZYbUA

 

 

Guess you like

Origin blog.csdn.net/fuweiping/article/details/114844210