110,545
社区成员
发帖
与我相关
我的任务
分享
bool result;
result = (90 > 100 || 200 < 300) ? true : false;
protected bool getNumResult(int n1,int n2,string operation) //调用前请检查输入参数
{
bool result=true;
switch(operation)
{
case ">":
result = n1 > n2;
break;
case "<":
result = n1 < n2;
break;
case ">=":
result = n1 >= n2;
break;
case "<=":
result = n1 <= n2;
break;
case "!=":
result = n1 != n2;
break;
}
return result;
}
protected bool getBoolResult(bool b1,bool b2,string operation)
{
bool result = true;
switch(operation)
{
case "||":
result = b1 || b2;
break;
case "&&":
result = b1 && b2;
break;
}
return result;
}
//执行调用,web下测试通过
protected void Page_Load(object sender, EventArgs e)
{
string input = "90>100 || 200<300";
MatchCollection mColl = Regex.Matches(input, @"\s(\|\||&&|^)\s(\d+)(>|<|=|>=|<=)(\d+)");
bool str2bool=false;
foreach(Match m in mColl)
{
if(m.Groups[1].Value.ToString()=="")
{
str2bool = getNumResult(int.Parse(m.Groups[2].Value.ToString()), int.Parse(m.Groups[4].Value.ToString()), m.Groups[3].Value.ToString());
}
else
{
str2bool=getBoolResult(str2bool,getNumResult(int.Parse(m.Groups[2].Value.ToString()), int.Parse(m.Groups[4].Value.ToString()), m.Groups[3].Value.ToString()),m.Groups[1].Value.ToString());
}
}
Response.Write(str2bool.ToString());
}
如果含有||,&&,括号等复杂表达式,还需要改进算法,我这里仅仅是举例