public static class SplitExp
{
static string Pattern = @"^[0-9.]*$";
static char[] Tokens = { '+', '-', '*', '/', '(', ')', '>', '<', '=', '!', '|', '&', '^', '?', ':', 'Δ', '%' };
static HashSet<string> KeyWordsSet = new HashSet<string>() { "true", "false", "TRUE", "FALSE", "int", "double", "bool", "abs" };
public static List<string> GetParas(string Expression)
{
List<string> RT = new List<string>();
if (!string.IsNullOrEmpty(Expression))
{
foreach (string Item in Expression.Split(Tokens))
{
if ((!Regex.IsMatch(Item, Pattern)) && (!KeyWordsSet.Contains(Item)))
{
RT.Add(Item);
}
}
}
return RT;
}
public static bool IsNumeric(string value)
{
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
}
public static bool IsScope(string value)
{
var result = Regex.IsMatch(value, @"^(\[|\()| $(\)|\])");
return result;
}
public static List<string> GetParasAll(string Expression)
{
List<string> RT = new List<string>();
try
{
//取出数组
List<string> ExpList = new List<string>();
if (!string.IsNullOrEmpty(Expression))
{
foreach (string Item in Expression.Split(Tokens))
{
ExpList.Add(Item);
}
}
if (ExpList.Count > 0)
{
int startIndex = 0;
int length = 0;
string lastString = Expression;
if (!string.IsNullOrEmpty(lastString))
{
for (int i = 0; i < ExpList.Count; i++)
{
if (!string.IsNullOrWhiteSpace(ExpList[i]))
{
var stringIndex = lastString.IndexOf(ExpList[i]);
//先取第一块数据
length = stringIndex - startIndex;
string string1 = lastString.Substring(startIndex, length);
if (!string.IsNullOrWhiteSpace(string1))
{
var ls = MatchFunText(string1);
if (ls.Count > 0)
{
RT.AddRange(ls);
}
}
lastString = lastString.Substring(length, lastString.Length - length);
//取第二块数据
RT.Add(ExpList[i]);
lastString = lastString.Substring(ExpList[i].Length, lastString.Length - ExpList[i].Length);
}
//最后一个处理
if (i == ExpList.Count - 1)
{
if (lastString.Length > startIndex)
{
RT.Add(lastString.Substring(startIndex, lastString.Length - startIndex));
}
}
length = 0;
}
}
}
}
catch (Exception ex) {
return RT;
}
return RT;
}
public static List<string> FuncText = new List<string> { "+", "-", "*", "/", "(", ")", ">", "<", "=", "!", "||", "&&", "^", "?", ":", "Δ","%",
"tan", "aver", "sum", "min", "max", "abs","rhs" };
private static List<string> MatchFunText(string text)
{
var chars = text.ToCharArray();
var returnInfo = new List<string>();
for (int i = 0; i < chars.Length; i++)
{
if (Regex.IsMatch(chars[i].ToString(), Pattern))
{
returnInfo.Add(chars[i].ToString());
}
else
if (FuncText.Contains(chars[i].ToString()))
{
returnInfo.Add(chars[i].ToString());
}
else
if (FuncText.Contains(chars[i].ToString() + chars[i + 1].ToString()))
{
returnInfo.Add(chars[i].ToString() + chars[i + 1].ToString());
i = i + 1;
}
else
if (FuncText.Contains(chars[i].ToString() + chars[i + 1].ToString() + chars[i + 2].ToString()))
{
returnInfo.Add(chars[i].ToString() + chars[i + 1].ToString() + chars[i + 2].ToString());
i = i + 2;
}
else
if (FuncText.Contains(chars[i].ToString() + chars[i + 1].ToString() + chars[i + 2].ToString() + chars[i + 3].ToString()))
{
returnInfo.Add(chars[i].ToString() + chars[i + 1].ToString() + chars[i + 2].ToString() + chars[i + 3].ToString());
i = i + 3;
}
else
{
returnInfo.Add(chars[i].ToString());
}
}
return returnInfo;
}
}
c#数学公式拆分成list集合
最新推荐文章于 2024-08-03 02:23:02 发布